Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit 3e1b651

Browse files
author
Dr. Safi
committed
Addition of decorators & Code refactoring.
1 parent f94de8b commit 3e1b651

19 files changed

+503
-435
lines changed

module/prototypeChainDebug/entrypoint.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Adds prototype information on static classes, prototypes, and instances.
33
* Returns a proxy with traps to add meta information.
44
*/
5-
export default Class => {
5+
function prototypeChainDebug(Class) {
66
// Static class
77
Class.meta = {
88
Class: `${Class.name}`,
@@ -38,4 +38,5 @@ export default Class => {
3838
return Class
3939
}
4040

41-
41+
export { prototypeChainDebug as classDecorator}
42+
export default prototypeChainDebug

module/reusableNestedUnit/Controller.class.js

Lines changed: 109 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -4,137 +4,143 @@ const EventEmitter = require('events')
44
import createInstance from 'appscript/module/createInstance.staticMethod'
55
import { usingGenericInstance as populateInstancePropertyFromJson, usingThis as populateInstancePropertyFromJson_this } from 'appscript/module/populateInstancePropertyFromJson.method'
66
import addStaticSubclassToClassArray from 'appscript/module/addStaticSubclassToClassArray.staticMethod'
7-
import prototypeChainDebug from 'appscript/module/prototypeChainDebug'
7+
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
88
import { MultiplePrototypeChain } from 'appscript/module/multiplePrototypeChain'
9-
import assert from 'assert'
9+
import { add, execute } from 'appscript/utilityFunction/decoratorUtility.js'
1010

11-
let debugExecuted = false
12-
let debugEx2 = false
1311
/**
1412
* @class
1513
* @usage new instance is created for each check.
1614
*/
17-
module.exports = ({
15+
export default ({
1816
methodInstanceName,
19-
superclass,
17+
Superclass,
2018
mixin
2119
}) => {
2220
let mixinArray = [/*commonMethod*/]
23-
let self = class ReusableController extends mix(superclass).with(...mixinArray) {
24-
static meta = {
25-
description: 'Static Reusable Controller'
26-
}
27-
28-
static eventEmitter = new EventEmitter() // i.e. new EventEmitter()
29-
static extendedSubclass = {
30-
static: {}
31-
}
32-
33-
/**
34-
* Properties on instnace object (not on the prototype)
35-
*/
36-
AppInstance; // calling instance that contains the context
37-
instance = {
38-
nestedUnit: [],
39-
unit: [],
40-
} // conditionTreeKey -> { Json data, properties }
41-
42-
constructor(skipConstructor = false, {portAppInstance}) {
43-
super(true)
44-
if(skipConstructor) return;
45-
if(portAppInstance) this.AppInstance = portAppInstance
46-
}
21+
let self =
22+
@prototypeChainDebug
23+
@add({ to: 'static'}, {
24+
createInstance,
25+
populateInstancePropertyFromJson,
26+
addStaticSubclassToClassArray
27+
})
28+
@add({ to: 'prototype'}, {
29+
populateInstancePropertyFromJson_this
30+
})
31+
@execute({ staticMethod: 'initializeStaticClass' })
32+
class ReusableController extends mix(Superclass).with(...mixinArray) {
4733

48-
static createContext(...args) {
49-
let self = this // specific Controller that is a subclass of 'self' (ReusableController)
50-
let contextInstance = new self(false, ...args)
34+
static eventEmitter = new EventEmitter() // i.e. new EventEmitter()
35+
static extendedSubclass = {
36+
static: {}
37+
}
5138

52-
// create a new list object for proxied refrence of subclasses
53-
contextInstance.instanceExtendedSubclass = Object.keys(self.extendedSubclass.static)
54-
.reduce((object, key) => {
55-
// add proxied subclass to the list
56-
object[key] = MultiplePrototypeChain.newChainOnInstanceCreation({
57-
Class: self.extendedSubclass.static[key],
58-
contextInstance
39+
static initializeStaticClass() {
40+
// Mutation observer on array for debugging purposes.
41+
// self.extendedSubclass.static = new Proxy(self.extendedSubclass.static, {
42+
// set: function(target, property, value, receiver) {
43+
// target[property] = value;
44+
// console.log(self.extendedSubclass.static)
45+
// return true;
46+
// }
47+
// })
48+
49+
if(methodInstanceName && Superclass && Superclass.eventEmitter) {
50+
super.eventEmitter.on('initializationEnd', () => {
51+
let ClassObject = {}
52+
ClassObject[`${methodInstanceName}`] = self
53+
super.addStaticSubclassToClassArray(ClassObject)
5954
})
60-
return object
61-
}, {})
62-
return contextInstance
63-
}
55+
}
56+
}
6457

65-
static initializeStaticClass() {
66-
if(methodInstanceName && superclass && superclass.eventEmitter) {
67-
superclass.eventEmitter.on('initializationEnd', () => {
58+
static initializeStaticClassControllerLevel() {
59+
let Class = this
60+
Class.eventEmitter.on('initializationEnd', () => {
6861
let ClassObject = {}
69-
ClassObject[`${methodInstanceName}`] = self
70-
superclass.addStaticSubclassToClassArray(ClassObject)
62+
ClassObject[`${Class.name}`] = Class
63+
Class.addStaticSubclassToClassArray(ClassObject)
7164
})
7265
}
73-
}
7466

75-
static callSubclass(name, args) {
76-
return Reflect.construct(self.extendedSubclass.static[name], args)
77-
}
78-
callSubclass(name, args) {
79-
let contextInstance = this
80-
return Reflect.construct(contextInstance.instanceExtendedSubclass[name], args)
81-
}
67+
static createContext(...args) {
68+
let self = this // specific Controller that is a subclass of 'self' (ReusableController)
69+
let contextInstance = new self(false, ...args)
8270

83-
async getNestedUnit({ nestedUnitKey, additionalChildNestedUnit = [], pathPointerKey = null}) {
84-
let nestedUnitInstance;
85-
if(!(nestedUnitKey in this.instance.nestedUnit)) {
86-
nestedUnitInstance = await this.callSubclass('NestedUnit', [nestedUnitKey])
87-
await nestedUnitInstance.initializeInstance()
88-
// add children trees:
89-
nestedUnitInstance.additionalChildNestedUnit = additionalChildNestedUnit
90-
// add pathPointerKey to allow applying additional corresponding additional children.
91-
nestedUnitInstance.pathPointerKey = pathPointerKey
92-
// add to class cache
93-
this.instance.nestedUnit[nestedUnitKey] = nestedUnitInstance
94-
} else {
95-
nestedUnitInstance = this.instance.nestedUnit[nestedUnitKey]
71+
// create a new list object for proxied refrence of subclasses
72+
contextInstance.instanceExtendedSubclass = Object.keys(self.extendedSubclass.static)
73+
.reduce((object, key) => {
74+
// add proxied subclass to the list
75+
object[key] = MultiplePrototypeChain.newChainOnInstanceCreation({
76+
Class: self.extendedSubclass.static[key],
77+
contextInstance
78+
})
79+
return object
80+
}, {})
81+
return contextInstance
9682
}
97-
return nestedUnitInstance
98-
}
9983

100-
async getUnit({unitKey}) {
101-
let unitInstance;
102-
if(!(unitKey in this.instance.unit)) {
103-
unitInstance = await this.callSubclass('Unit', [unitKey])
104-
await unitInstance.initializeInstance()
105-
this.instance.unit[unitKey] = unitInstance
106-
} else {
107-
unitInstance = this.instance.unit[unitKey]
84+
static callSubclass(name, args) {
85+
return Reflect.construct(self.extendedSubclass.static[name], args)
10886
}
109-
return unitInstance
110-
}
11187

112-
}
88+
/**
89+
* Properties on instnace object (not on the prototype)
90+
*/
91+
AppInstance; // calling instance that contains the context
92+
instance = {
93+
nestedUnit: [],
94+
unit: [],
95+
}
96+
// conditionTreeKey -> { Json data, properties }
11397

114-
self.addStaticSubclassToClassArray = function(...args) {
115-
// this - is a subclass of the class which the method resides.
116-
addStaticSubclassToClassArray.call(this, ...args)
117-
}
118-
self.createInstance = createInstance
119-
self.populateInstancePropertyFromJson = populateInstancePropertyFromJson
120-
self.prototype.populateInstancePropertyFromJson_this = populateInstancePropertyFromJson_this
98+
constructor(skipConstructor = false, {portAppInstance}) {
99+
super(true)
100+
if(skipConstructor) return;
101+
if(portAppInstance) this.AppInstance = portAppInstance
102+
}
103+
104+
callSubclass(name, args) {
105+
let contextInstance = this
106+
return Reflect.construct(contextInstance.instanceExtendedSubclass[name], args)
107+
}
108+
109+
async getNestedUnit({ nestedUnitKey, additionalChildNestedUnit = [], pathPointerKey = null}) {
110+
let nestedUnitInstance;
111+
if(!(nestedUnitKey in this.instance.nestedUnit)) {
112+
nestedUnitInstance = await this.callSubclass('NestedUnit', [nestedUnitKey])
113+
await nestedUnitInstance.initializeInstance()
114+
// add children trees:
115+
nestedUnitInstance.additionalChildNestedUnit = additionalChildNestedUnit
116+
// add pathPointerKey to allow applying additional corresponding additional children.
117+
nestedUnitInstance.pathPointerKey = pathPointerKey
118+
// add to class cache
119+
this.instance.nestedUnit[nestedUnitKey] = nestedUnitInstance
120+
} else {
121+
nestedUnitInstance = this.instance.nestedUnit[nestedUnitKey]
122+
}
123+
return nestedUnitInstance
124+
}
121125

122-
self.initializeStaticClass()
126+
async getUnit({unitKey}) {
127+
let unitInstance;
128+
if(!(unitKey in this.instance.unit)) {
129+
unitInstance = await this.callSubclass('Unit', [unitKey])
130+
await unitInstance.initializeInstance()
131+
this.instance.unit[unitKey] = unitInstance
132+
} else {
133+
unitInstance = this.instance.unit[unitKey]
134+
}
135+
return unitInstance
136+
}
137+
138+
}
123139

124-
// Mutation observer on array for debugging purposes.
125-
// self.extendedSubclass.static = new Proxy(self.extendedSubclass.static, {
126-
// set: function(target, property, value, receiver) {
127-
// target[property] = value;
128-
// console.log(self.extendedSubclass.static)
129-
// return true;
130-
// }
131-
// })
132-
self = prototypeChainDebug(self)
133-
134140
// add controller methods for the specific module that uses them.
135141
let Controller
136-
if(mixin) {
137-
Controller = mixin({ superclass: self}) // return Specific implementation Controller
142+
if(mixin) {
143+
Controller = mixin({ Superclass: self}) // return Specific implementation Controller
138144
} else {
139145
Controller = self; // return Reusable nested unit
140146
}

module/reusableNestedUnit/NestedUnit.class.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import prototypeChainDebug from 'appscript/module/prototypeChainDebug'
1+
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
2+
3+
export default ({ Superclass }) => {
4+
let self = @prototypeChainDebug
5+
class RNestedUnit extends Superclass {
6+
7+
static getDocumentQuery;
8+
9+
static initializeStaticClass(getTableDocument) {
10+
super.initializeStaticClassControllerLevel()
11+
self.getDocumentQuery = getTableDocument
12+
}
213

3-
module.exports = ({ superclass }) => {
4-
let self = class RNestedUnit extends superclass {
514
constructor(databaseDocumentKey, AppInstance) {
615
super(false, {portAppInstance: AppInstance})
716
this.key = databaseDocumentKey
817
return this
918
}
10-
static getDocumentQuery;
11-
static initializeStaticClassControllerLevel(getTableDocument) {
12-
let Class = this
13-
Class.eventEmitter.on('initializationEnd', () => {
14-
let ClassObject = {}
15-
ClassObject[`${Class.name}`] = Class
16-
Class.addStaticSubclassToClassArray(ClassObject)
17-
})
18-
self.getDocumentQuery = getTableDocument
19-
}
2019

2120
/**
2221
* @description gets document from database using documentKey and populates the data to the instance.
@@ -47,13 +46,15 @@ module.exports = ({ superclass }) => {
4746
let additionalFilteredChildren = await this.filterAndModifyChildrenArray(this.additionalChildNestedUnit, insertionPointKey, this.pathPointerKey)
4847
return await this.mergeAndOrderChildren(ownFilteredChildren, additionalFilteredChildren);
4948
}
49+
5050
async filterAndModifyChildrenArray(childrenArray, insertionPointKey, pathPointerKey) {
5151
return childrenArray.filter((child, index) => { // filter children that correspont to the current insertionpoint.
5252
let result = (child.insertionPosition.insertionPoint == insertionPointKey && child.insertionPosition.insertionPathPointer == pathPointerKey)
5353
// if (result) childrenArray.splice(index, 1); // was ment to increase the performance of the program, preventing rechecking of already checked array items. But it causes some issues.
5454
return result
5555
})
5656
}
57+
5758
// order additional children that will be mixed into ownChildren. According to a setting that needs to be added into each child object.
5859
async mergeAndOrderChildren(ownFilteredChildren, additionalFilteredChildren) {
5960
// metrge 2 arrays., appending one to the other.
@@ -110,7 +111,6 @@ module.exports = ({ superclass }) => {
110111
}
111112

112113
}
113-
self = prototypeChainDebug(self)
114114

115115
return self
116116
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import prototypeChainDebug from 'appscript/module/prototypeChainDebug'
1+
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
2+
3+
export default ({ Superclass }) => {
4+
let self = @prototypeChainDebug
5+
class RUnit extends Superclass {
6+
7+
static getDocumentQuery;
8+
9+
static initializeStaticClass(getTableDocument) {
10+
self.getDocumentQuery = getTableDocument
11+
super.initializeStaticClassControllerLevel()
12+
}
213

3-
module.exports = ({ superclass }) => {
4-
let self = class RUnit extends superclass {
514
constructor(databaseDocumentKey, AppInstance) {
615
super(false, {portAppInstance: AppInstance})
716
this.key = databaseDocumentKey
817
return this
918
}
10-
static getDocumentQuery;
11-
static initializeStaticClassControllerLevel(getTableDocument) {
12-
let Class = this
13-
Class.eventEmitter.on('initializationEnd', () => {
14-
let ClassObject = {}
15-
ClassObject[`${Class.name}`] = Class
16-
Class.addStaticSubclassToClassArray(ClassObject)
17-
})
18-
self.getDocumentQuery = getTableDocument
19-
}
19+
2020
/**
2121
* @description gets document from database using documentKey and populates the data to the instance.
2222
*
@@ -29,8 +29,6 @@ module.exports = ({ superclass }) => {
2929
}
3030
}
3131
}
32-
self = prototypeChainDebug(self)
3332

3433
return self
35-
3634
}

module/reusableNestedUnit/commonMethod.mixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Mixin } from 'mixwith'
33
/**
44
* @description Extends a class by super class and adds some common functionality.
55
*/
6-
export default Mixin(({superclass}) => {
7-
const self = class RNUMixin extends superclass {
6+
export default Mixin(({Superclass}) => {
7+
const self = class RNUMixin extends Superclass {
88
constructor(...args) {
99
// mixins should either 1) not define a constructor, 2) require a specific
1010
// constructor signature, or 3) pass along all arguments.

0 commit comments

Comments
 (0)