@@ -4,137 +4,143 @@ const EventEmitter = require('events')
44import createInstance from 'appscript/module/createInstance.staticMethod'
55import { usingGenericInstance as populateInstancePropertyFromJson , usingThis as populateInstancePropertyFromJson_this } from 'appscript/module/populateInstancePropertyFromJson.method'
66import addStaticSubclassToClassArray from 'appscript/module/addStaticSubclassToClassArray.staticMethod'
7- import prototypeChainDebug from 'appscript/module/prototypeChainDebug'
7+ import { classDecorator as prototypeChainDebug } from 'appscript/module/prototypeChainDebug'
88import { 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 }
0 commit comments