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

Commit 197298b

Browse files
author
Dr. Safi
committed
Refactor Nested Unit implementations.
merged common methods used by each implementation into shared methods accepting different returned types.
1 parent fee02ca commit 197298b

File tree

4 files changed

+77
-133
lines changed

4 files changed

+77
-133
lines changed

module/reusableNestedUnit/NestedUnit.class.js

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
22
import { add, execute, applyMixin, conditional } from 'appscript/utilityFunction/decoratorUtility.js'
3+
import promiseProperRace from 'appscript/utilityFunction/promiseProperRace.js'
34

45
export 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

module/reusableNestedUnit/implementation/condition/NestedUnit.class.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,7 @@ export default ({ Superclass }) => {
4444
}
4545
return returnedValue;
4646
}
47-
48-
async initializeInsertionPoint({ insertionPoint, children }) {
49-
let callback;
50-
// [2] check type of subtrees execution: race first, all ... .
51-
let executionTypeCallbackName;
52-
switch(insertionPoint.executionType) {
53-
case 'raceFirstPromise':
54-
executionTypeCallbackName = 'initializeConditionTreeInRaceExecutionType'
55-
break;
56-
default:
57-
console.log('executionType doesn\'t match any kind.')
58-
}
59-
// [3] call handler on them.
60-
return await this[executionTypeCallbackName](children)
61-
}
6247

63-
async initializeConditionTreeInRaceExecutionType(conditionTreeChildren) {
64-
let promiseArray = []
65-
promiseArray = conditionTreeChildren.map(conditionTreeChild => {
66-
return new Promise(async (resolve, reject) => {
67-
// Add the rest of the immediate children to the next tree as additional children. propagate children to the next tree.
68-
69-
if(this.children.length != 0) {
70-
await Array.prototype.push.apply(this.children, this.additionalChildNestedUnit)
71-
} else {
72-
this.children = await this.additionalChildNestedUnit.slice()
73-
}
74-
75-
let callback = await this.initializeNestedUnit({
76-
nestedUnitKey: conditionTreeChild.nestedUnit,
77-
additionalChildNestedUnit: this.children,
78-
pathPointerKey: conditionTreeChild.pathPointerKey
79-
})
80-
if(!callback) reject('SZN - No callback choosen from this childTree.')
81-
resolve(callback)
82-
})
83-
})
84-
85-
let callback;
86-
await promiseProperRace(promiseArray).then((promiseReturnValueArray) => {
87-
callback = promiseReturnValueArray[0] // as only one promise is return in the array.
88-
}).catch(reason => { if(process.env.SZN_DEBUG == 'true' && this.portAppInstance.context.headers.debug == 'true') console.log(`🔀⚠️ promiseProperRace rejected because: ${reason}`) })
89-
return callback ? callback : false;
90-
}
91-
9248
}
9349

9450
return self

module/reusableNestedUnit/implementation/middleware/NestedUnit.class.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,50 +50,6 @@ export default ({ Superclass }) => {
5050
return array;
5151
}
5252

53-
async initializeInsertionPoint({ insertionPoint, children }) {
54-
// [2] check type of subtrees execution: race first, all ... .
55-
let callback;
56-
switch(insertionPoint.executionType) { // execution type callback name
57-
case 'chronological':
58-
callback = 'initializeTreeInChronologicalSequence'
59-
break;
60-
case 'middlewareArray':
61-
callback = 'returnMiddlewareArray'
62-
break;
63-
default:
64-
console.log(`"${insertionPoint.executionType}" executionType doesn\'t match any kind.`)
65-
}
66-
// [3] call handler on them.
67-
return await this[callback](children)
68-
}
69-
70-
async initializeTreeInChronologicalSequence(treeChildren) {
71-
}
72-
73-
async returnMiddlewareArray(treeChildren) {
74-
let middlewareArray = []
75-
for (var index = 0; index < treeChildren.length; index++) {
76-
let treeChild = treeChildren[index]
77-
// Add the rest of the immediate children to the next tree as additional children. propagate children to the next tree.
78-
if(this.children.length != 0) {
79-
await Array.prototype.push.apply(this.children, this.additionalChildNestedUnit)
80-
} else {
81-
this.children = await this.additionalChildNestedUnit.slice()
82-
}
83-
let subsequentArray = await this.initializeNestedUnit({
84-
nestedUnitKey: treeChild.nestedUnit,
85-
additionalChildNestedUnit: this.children,
86-
pathPointerKey: treeChild.pathPointerKey
87-
})
88-
if(middlewareArray.length != 0) {
89-
await Array.prototype.push.apply(middlewareArray, subsequentArray)
90-
} else {
91-
middlewareArray = await subsequentArray.slice()
92-
}
93-
}
94-
95-
return middlewareArray
96-
}
9753
}
9854

9955
return self

module/reusableNestedUnit/implementation/template/NestedUnit.class.js

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import filesystem from 'fs'
55
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
66
import { add, execute, applyMixin, conditional } from 'appscript/utilityFunction/decoratorUtility.js'
77
import { extendedSubclassPattern } from 'appscript/utilityFunction/extendedSubclassPattern.js'
8-
import promiseProperRace from 'appscript/utilityFunction/promiseProperRace.js'
98
import { curried as getTableDocumentCurried } from "appscript/utilityFunction/database/query/getTableDocument.query.js";
109

1110
let getDocument = {
@@ -51,48 +50,6 @@ export default ({ Superclass }) => {
5150
return view;
5251
}
5352

54-
async initializeInsertionPoint({ insertionPoint, children }) {
55-
56-
// [2] check type of subtrees execution: race first, all ... .
57-
let executionTypeCallbackName;
58-
switch(insertionPoint.executionType) {
59-
case 'chronological':
60-
executionTypeCallbackName = 'initializeTreeInChronologicalSequence'
61-
break;
62-
default:
63-
console.log(`"${insertionPoint.executionType}" executionType doesn\'t match any kind (in function called initializeInsertionPoint of NestedUnit.class.js file).`)
64-
}
65-
// [3] call handler on them & and return rendered template array corresponding to the specifc insertionPoint.
66-
return await this[executionTypeCallbackName](children)
67-
}
68-
69-
async initializeTreeInChronologicalSequence(treeChildren) {
70-
let renderedStringArray = []
71-
for (var index = 0; index < treeChildren.length; index++) {
72-
let treeChild = treeChildren[index]
73-
// Add the rest of the immediate children to the next tree as additional children. propagate children to the next tree.
74-
if(this.children.length != 0) {
75-
await Array.prototype.push.apply(this.children, this.additionalChildNestedUnit)
76-
} else {
77-
this.children = await this.additionalChildNestedUnit.slice()
78-
}
79-
let subsequentRenderedTemplate = [
80-
await this.initializeNestedUnit({
81-
nestedUnitKey: treeChild.nestedUnit,
82-
additionalChildNestedUnit: this.children,
83-
pathPointerKey: treeChild.pathPointerKey
84-
})
85-
]
86-
if(renderedStringArray.length != 0) {
87-
await Array.prototype.push.apply(renderedStringArray, subsequentRenderedTemplate)
88-
} else {
89-
renderedStringArray = await subsequentRenderedTemplate.slice()
90-
}
91-
}
92-
93-
return renderedStringArray
94-
}
95-
9653
}
9754

9855
return self

0 commit comments

Comments
 (0)