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

Commit fee02ca

Browse files
author
Dr. Safi
committed
Refactor - using same filterAndOrderChildren method.
1 parent dd698f4 commit fee02ca

File tree

4 files changed

+32
-46
lines changed

4 files changed

+32
-46
lines changed

module/reusableNestedUnit/NestedUnit.class.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ export default ({ Superclass }) => {
1919
return this
2020
}
2121

22-
/**
23-
* Get children corresponding to the current insertion point.
24-
*/
25-
async filterChildrenOfCurrentInsertionPoint({
26-
insertionPointKey,
27-
children = this.children
28-
} = {}) {
29-
// [1] get children immediate & relating to this insertion position.
30-
return await children.filter(child => { // filter children that correspont to the current insertionpoint.
31-
return (
32-
child.insertionPosition.insertionPoint == insertionPointKey &&
33-
child.insertionPosition.insertionPathPointer == null
34-
)
35-
})
36-
}
37-
3822
/**
3923
* @description gets document from database using documentKey and populates the data to the instance.
4024
*
@@ -55,16 +39,25 @@ export default ({ Superclass }) => {
5539
* @param {any} {insertionPointKey, insertionPath = null}
5640
* @returns
5741
*/
58-
async filterChildren({insertionPointKey}) {
59-
let ownFilteredChildren = await this.filterAndModifyChildrenArray(this.children, insertionPointKey, null)
42+
async filterAndOrderChildren({ insertionPointKey, children = this.children }) {
43+
let ownFilteredChildren = await this.filterAndModifyChildrenArray(children, insertionPointKey, null)
6044
let additionalFilteredChildren = await this.filterAndModifyChildrenArray(this.additionalChildNestedUnit, insertionPointKey, this.pathPointerKey)
6145
return await this.mergeAndOrderChildren(ownFilteredChildren, additionalFilteredChildren);
6246
}
6347

64-
async filterAndModifyChildrenArray(childrenArray, insertionPointKey, pathPointerKey) {
65-
return childrenArray.filter((child, index) => { // filter children that correspont to the current insertionpoint.
66-
let result = (child.insertionPosition.insertionPoint == insertionPointKey && child.insertionPosition.insertionPathPointer == pathPointerKey)
67-
// 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.
48+
/**
49+
* Get children corresponding to the current insertion point.
50+
* // Take into consideration the indirect children added from previous (inhereted) trees.
51+
* // filteredTreeChildren + immediateNextChildren
52+
* // let nextChildren;
53+
*/
54+
async filterAndModifyChildrenArray(children, insertionPointKey, pathPointerKey) {
55+
return children.filter((child, index) => { // filter children that correspont to the current insertionpoint.
56+
let result = (
57+
child.insertionPosition.insertionPoint == insertionPointKey &&
58+
child.insertionPosition.insertionPathPointer == pathPointerKey
59+
)
60+
// if (result) children.splice(index, 1); // was ment to increase the performance of the program, preventing rechecking of already checked array items. But it causes some issues.
6861
return result
6962
})
7063
}

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,28 @@ export default ({ Superclass }) => {
3636
let returnedValue;
3737
// get callback from subtrees
3838
for (let insertionPoint of this.insertionPoint) {
39-
returnedValue = await this.initializeInsertionPoint({ insertionPoint })
39+
// [1] get children immediate & relating to this insertion position.
40+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
41+
// let children = await this.filterChildrenOfCurrentInsertionPoint({ insertionPointKey: insertionPoint.key })
42+
returnedValue = await this.initializeInsertionPoint({ insertionPoint, children })
4043
if (returnedValue) break
4144
}
4245
return returnedValue;
4346
}
4447

45-
async initializeInsertionPoint({insertionPoint}) {
46-
let callback;
47-
// [1] get children immediate & relating to this insertion position.
48-
let filteredTreeChildren = await this.filterChildren({ insertionPointKey: insertionPoint.key })
49-
// Take into consideration the indirect children added from previous (inhereted) trees.
50-
// filteredTreeChildren + immediateNextChildren
51-
// let nextChildren;
52-
48+
async initializeInsertionPoint({ insertionPoint, children }) {
49+
let callback;
5350
// [2] check type of subtrees execution: race first, all ... .
5451
let executionTypeCallbackName;
5552
switch(insertionPoint.executionType) {
5653
case 'raceFirstPromise':
5754
executionTypeCallbackName = 'initializeConditionTreeInRaceExecutionType'
58-
break;
55+
break;
5956
default:
6057
console.log('executionType doesn\'t match any kind.')
6158
}
6259
// [3] call handler on them.
63-
callback = await this[executionTypeCallbackName](filteredTreeChildren)
64-
// [4] return callback
65-
return callback ? callback : false;
60+
return await this[executionTypeCallbackName](children)
6661
}
6762

6863
async initializeConditionTreeInRaceExecutionType(conditionTreeChildren) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ export default ({ Superclass }) => {
3838
// get callback from subtrees
3939
if(this.insertionPoint) {
4040
for (let insertionPoint of this.insertionPoint) {
41-
let children = await this.filterChildrenOfCurrentInsertionPoint({
42-
insertionPointKey: insertionPoint.key,
43-
children: this.children
44-
})
41+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
4542
let subsequentArray = await this.initializeInsertionPoint({ insertionPoint, children })
4643
if(array.length != 0) {
4744
await Array.prototype.push.apply(array, subsequentArray)

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ export default ({ Superclass }) => {
4040
let view = {}
4141
if(this.insertionPoint) {
4242
for (let insertionPoint of this.insertionPoint) {
43+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
4344
if(!(insertionPoint.name in view)) view[insertionPoint.name] = []
44-
Array.prototype.push.apply( view[insertionPoint.name], await this.initializeInsertionPoint({ insertionPoint }) )
45+
Array.prototype.push.apply(
46+
view[insertionPoint.name],
47+
await this.initializeInsertionPoint({ insertionPoint, children })
48+
)
4549
}
4650
}
4751
return view;
4852
}
4953

50-
async initializeInsertionPoint({insertionPoint}) {
51-
// [1] get children immediate & relating to this insertion position.
52-
let filteredChildren = this.children.filter(object => { // filter children that correspont to the current insertionpoint.
53-
return (object.insertionPosition.insertionPoint == insertionPoint.key && object.insertionPosition.insertionPathPointer == null)
54-
})
54+
async initializeInsertionPoint({ insertionPoint, children }) {
55+
5556
// [2] check type of subtrees execution: race first, all ... .
5657
let executionTypeCallbackName;
5758
switch(insertionPoint.executionType) {
@@ -62,7 +63,7 @@ export default ({ Superclass }) => {
6263
console.log(`"${insertionPoint.executionType}" executionType doesn\'t match any kind (in function called initializeInsertionPoint of NestedUnit.class.js file).`)
6364
}
6465
// [3] call handler on them & and return rendered template array corresponding to the specifc insertionPoint.
65-
return await this[executionTypeCallbackName](filteredChildren)
66+
return await this[executionTypeCallbackName](children)
6667
}
6768

6869
async initializeTreeInChronologicalSequence(treeChildren) {

0 commit comments

Comments
 (0)