11import { classDecorator as prototypeChainDebug } from 'appscript/module/prototypeChainDebug'
22import { add , execute , applyMixin , conditional } from 'appscript/utilityFunction/decoratorUtility.js'
3+ import promiseProperRace from 'appscript/utilityFunction/promiseProperRace.js'
34
45export default ( { Superclass } ) => {
56 let self =
@@ -44,7 +45,6 @@ export default ({ Superclass }) => {
4445 let additionalFilteredChildren = await this . filterAndModifyChildrenArray ( this . additionalChildNestedUnit , insertionPointKey , this . pathPointerKey )
4546 return await this . mergeAndOrderChildren ( ownFilteredChildren , additionalFilteredChildren ) ;
4647 }
47-
4848 /**
4949 * Get children corresponding to the current insertion point.
5050 * // Take into consideration the indirect children added from previous (inhereted) trees.
@@ -61,7 +61,6 @@ export default ({ Superclass }) => {
6161 return result
6262 } )
6363 }
64-
6564 // order additional children that will be mixed into ownChildren. According to a setting that needs to be added into each child object.
6665 async mergeAndOrderChildren ( ownFilteredChildren , additionalFilteredChildren ) {
6766 // metrge 2 arrays., appending one to the other.
@@ -117,6 +116,82 @@ export default ({ Superclass }) => {
117116 return Array . prototype . concat ( firstChildren , orderedChildren , lastChildren )
118117 }
119118
119+ /**
120+ * Call correct execution type method of the current insertionpoint settings.
121+ */
122+ async initializeInsertionPoint ( { insertionPoint, children } ) {
123+ // [2] check type of subtrees execution: race first, all ... .
124+ let callback ;
125+ switch ( insertionPoint . executionType ) { // execution type callback name
126+ case 'raceFirstPromise' :
127+ callback = 'initializeNestedUnitInRaceExecutionType'
128+ break ;
129+ case 'middlewareArray' : // TODO: Deprected name - Change middlewareArray in database to chronological
130+ case 'chronological' :
131+ callback = 'initializeTreeInChronologicalSequence'
132+ break ;
133+ default :
134+ console . log ( `"${ insertionPoint . executionType } " executionType doesn\'t match any kind.` )
135+ }
136+ // [3] call handler on them.
137+ return await this [ callback ] ( children )
138+ }
139+
140+ async initializeTreeInChronologicalSequence ( children /* nestedUnitChildren / TreeChildren */ ) {
141+ let array = [ ] // nested Unit Array or rendered nested unit initalization results.
142+ for ( var index = 0 ; index < children . length ; index ++ ) {
143+ let child = children [ index ]
144+ // Add the rest of the immediate children to the next tree as additional children. propagate children to the next tree.
145+ if ( this . children . length != 0 ) {
146+ await Array . prototype . push . apply ( this . children , this . additionalChildNestedUnit )
147+ } else {
148+ this . children = await this . additionalChildNestedUnit . slice ( )
149+ }
150+ let initialized = await this . initializeNestedUnit ( {
151+ nestedUnitKey : child . nestedUnit ,
152+ additionalChildNestedUnit : this . children ,
153+ pathPointerKey : child . pathPointerKey
154+ } )
155+ let subsequentArray = Array . isArray ( initialized ) ? initialized : [ initialized ] ; // Convert to array
156+ if ( array . length != 0 ) {
157+ await Array . prototype . push . apply ( array , subsequentArray )
158+ } else {
159+ array = await subsequentArray . slice ( )
160+ }
161+ }
162+
163+ return array
164+ }
165+
166+ async initializeNestedUnitInRaceExecutionType ( children ) {
167+ let promiseArray = [ ]
168+ promiseArray = children . map ( conditionTreeChild => {
169+ return new Promise ( async ( resolve , reject ) => {
170+ // Add the rest of the immediate children to the next tree as additional children. propagate children to the next tree.
171+
172+ if ( this . children . length != 0 ) {
173+ await Array . prototype . push . apply ( this . children , this . additionalChildNestedUnit )
174+ } else {
175+ this . children = await this . additionalChildNestedUnit . slice ( )
176+ }
177+
178+ let callback = await this . initializeNestedUnit ( {
179+ nestedUnitKey : conditionTreeChild . nestedUnit ,
180+ additionalChildNestedUnit : this . children ,
181+ pathPointerKey : conditionTreeChild . pathPointerKey
182+ } )
183+ if ( ! callback ) reject ( 'SZN - No callback choosen from this childTree.' )
184+ resolve ( callback )
185+ } )
186+ } )
187+
188+ let callback ;
189+ await promiseProperRace ( promiseArray ) . then ( ( promiseReturnValueArray ) => {
190+ callback = promiseReturnValueArray [ 0 ] // as only one promise is return in the array.
191+ } ) . catch ( reason => { if ( process . env . SZN_DEBUG == 'true' && this . portAppInstance . context . headers . debug == 'true' ) console . log ( `🔀⚠️ promiseProperRace rejected because: ${ reason } ` ) } )
192+ return callback ? callback : false ;
193+ }
194+
120195 }
121196
122197 return self
0 commit comments