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

Commit 0e6f3a8

Browse files
author
Dr. Safi
committed
Refactor code - extracted common methods and aspect code to decorators.
1 parent 197298b commit 0e6f3a8

File tree

13 files changed

+141
-134
lines changed

13 files changed

+141
-134
lines changed

module/reusableNestedUnit/NestedUnit.class.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,73 @@ export default ({ Superclass }) => {
2020
return this
2121
}
2222

23+
/**
24+
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
25+
*
26+
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
27+
* @returns undifiend for false or any type of value depending on the module being applied.
28+
*/
29+
/**
30+
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
31+
*
32+
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
33+
* @returns undifiend for false or any type of value depending on the module being applied.
34+
*/
35+
async loopInsertionPoint({ type }) {
36+
switch (type) {
37+
case 'aggregateIntoObject':
38+
let view = {}
39+
if(this.insertionPoint) {
40+
for (let insertionPoint of this.insertionPoint) {
41+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
42+
let subsequent = await this.initializeInsertionPoint({ insertionPoint, children })
43+
if(!(insertionPoint.name in view)) view[insertionPoint.name] = []
44+
Array.prototype.push.apply(
45+
view[insertionPoint.name],
46+
subsequent
47+
)
48+
}
49+
}
50+
return view;
51+
break;
52+
case 'aggregateIntoArray':
53+
let array = []
54+
if(this.insertionPoint) { // get callback from subtrees
55+
for (let insertionPoint of this.insertionPoint) {
56+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
57+
let subsequentArray = await this.initializeInsertionPoint({ insertionPoint, children })
58+
if(array.length != 0) {
59+
await Array.prototype.push.apply(array, subsequentArray)
60+
} else {
61+
array = await subsequentArray.slice()
62+
}
63+
}
64+
}
65+
return array;
66+
break;
67+
/**
68+
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
69+
*
70+
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
71+
* @returns undifiend for false or any type of value depending on the module being applied.
72+
*/
73+
case 'returnedFirstValue':
74+
let returned;
75+
// get callback from subtrees
76+
for (let insertionPoint of this.insertionPoint) {
77+
// [1] get children immediate & relating to this insertion position.
78+
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
79+
// let children = await this.filterChildrenOfCurrentInsertionPoint({ insertionPointKey: insertionPoint.key })
80+
returned = await this.initializeInsertionPoint({ insertionPoint, children })
81+
if (returned) break
82+
}
83+
return returned;
84+
default:
85+
console.log(`"${type}" type doesn\'t match any kind.`)
86+
break;
87+
}
88+
}
89+
2390
/**
2491
* @description gets document from database using documentKey and populates the data to the instance.
2592
*

module/reusableNestedUnit/Unit.class.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
2-
import { add, execute, applyMixin, conditional } from 'appscript/utilityFunction/decoratorUtility.js'
2+
import { add, execute, applyMixin, conditional, executeOnceForEachInstance } from 'appscript/utilityFunction/decoratorUtility.js'
33

44
export default ({ Superclass }) => {
55
let self =
@@ -17,6 +17,19 @@ export default ({ Superclass }) => {
1717
this.key = databaseDocumentKey
1818
return this
1919
}
20+
21+
@executeOnceForEachInstance()
22+
async pupolateUnitWithFile({
23+
fileKey,
24+
getDocument, // function
25+
extract = null // object with two properties - extract: { sourceKey: 'key from source object', destinationKey: 'key to "this" destination' }
26+
}) {
27+
let File = await getDocument({
28+
key: fileKey,
29+
connection: self.rethinkdbConnection
30+
})
31+
if(extract) this[extract.destinationKey] = (extract.sourceKey) ? File[extract.sourceKey] : File;
32+
}
2033
}
2134

2235
return self

module/reusableNestedUnit/entrypoint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function createStaticInstanceClasses({
4848
break;
4949

5050
default:
51-
console.log('No implementation chosen for building class tree in ReusableNestedUnit.')
51+
console.log('⚠️ No implementation chosen for building class tree in ReusableNestedUnit.')
5252
break;
5353
}
5454

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,7 @@ export default ({ Superclass }) => {
1818
})
1919
@extendedSubclassPattern.Subclass()
2020
class NestedUnit extends Superclass {
21-
// static getDocumentQuery(connection, conditionTreeKey) {
22-
// getConditionTreeQuery(connection, conditionTreeKey)
23-
// }
2421

25-
// static createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback) {
26-
// NestedUnit.createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback)
27-
// }
28-
29-
/**
30-
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
31-
*
32-
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
33-
* @returns undifiend for false or any type of value depending on the module being applied.
34-
*/
35-
async loopInsertionPoint() {
36-
let returnedValue;
37-
// get callback from subtrees
38-
for (let insertionPoint of this.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 })
43-
if (returnedValue) break
44-
}
45-
return returnedValue;
46-
}
47-
4822
}
4923

5024
return self

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { classDecorator as prototypeChainDebug} from 'appscript/module/prototypeChainDebug'
2-
import { add, execute, applyMixin, conditional } from 'appscript/utilityFunction/decoratorUtility.js'
2+
import { add, execute, applyMixin, conditional, executeOnceForEachInstance } from 'appscript/utilityFunction/decoratorUtility.js'
33
import { extendedSubclassPattern } from 'appscript/utilityFunction/extendedSubclassPattern.js'
44
import { curried as getTableDocumentCurried } from "appscript/utilityFunction/database/query/getTableDocument.query.js";
55

@@ -17,14 +17,16 @@ export default ({ Superclass }) => {
1717
})
1818
@extendedSubclassPattern.Subclass()
1919
class Unit extends Superclass {
20+
async pupolateUnitWithFile() {
21+
await super.pupolateUnitWithFile({
22+
getDocument: getDocument['File'],
23+
fileKey: this.valueReturningFileKey,
24+
extract: { destinationKey: 'valueReturningFile' }
25+
})
26+
}
27+
28+
@executeOnceForEachInstance()
2029
async checkCondition() {
21-
let valueReturningFileKey = this.valueReturningFileKey
22-
if(!('valueReturningFile' in this)) {
23-
this.valueReturningFile = await getDocument['File']({
24-
key: valueReturningFileKey,
25-
connection: self.rethinkdbConnection
26-
})
27-
}
2830
// [2] require & check condition
2931
if(!this.conditionResult) {
3032
let expectedReturn = this.expectedReturn

module/reusableNestedUnit/implementation/condition/controllerMixin.mixin.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ export default Mixin(({ Superclass }) => {
1616
* @param {any} {nestedUnitKey}
1717
* @returns { Object || False } Object containing instruction settings to be used through an implementing module.
1818
*/
19-
async initializeNestedUnit({ nestedUnitKey, additionalChildNestedUnit = [], pathPointerKey = null}) { // Entrypoint Instance
20-
// self.debug.push(nestedUnitKey)
21-
// let nestedUnitInstance = await ConditionTree.createInstance(this.instance.nestedUnit, nestedUnitKey, ConditionTree.getDocumentQuery)
22-
19+
async initializeNestedUnit({ nestedUnitKey, additionalChildNestedUnit = [], pathPointerKey = null}) { // Entrypoint Instance
2320
// [1] get nestedUnit
2421
let nestedUnitInstance = await this.getNestedUnit({ nestedUnitKey, additionalChildNestedUnit, pathPointerKey })
25-
26-
// [2] Check condition.
27-
let {conditionImplementation:unitKey} = nestedUnitInstance
22+
let { conditionImplementation:unitKey} = nestedUnitInstance
2823
let unitInstance = await this.getUnit({unitKey})
29-
24+
await unitInstance.pupolateUnitWithFile()
3025
let conditionMet = await unitInstance.checkCondition()
3126

3227
// [3] Iterate over insertion points
3328
let callback;
3429
if (conditionMet) {
35-
callback = await nestedUnitInstance.loopInsertionPoint()
30+
callback = await nestedUnitInstance.loopInsertionPoint({ type: 'returnedFirstValue' })
3631
// if all subtrees rejected, get immediate callback
3732
if(!callback && 'callback' in nestedUnitInstance) callback = nestedUnitInstance.callback // fallback to immediate callback of instance.
3833
}

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,6 @@ export default ({ Superclass }) => {
1919
@extendedSubclassPattern.Subclass()
2020
class NestedUnit extends Superclass {
2121

22-
// static getDocumentQuery(connection, conditionTreeKey) {
23-
// getConditionTreeQuery(connection, conditionTreeKey)
24-
// }
25-
26-
// static createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback) {
27-
// NestedUnit.createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback)
28-
// }
29-
30-
/**
31-
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
32-
*
33-
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
34-
* @returns undifiend for false or any type of value depending on the module being applied.
35-
*/
36-
async loopInsertionPoint() {
37-
let array = []
38-
// get callback from subtrees
39-
if(this.insertionPoint) {
40-
for (let insertionPoint of this.insertionPoint) {
41-
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
42-
let subsequentArray = await this.initializeInsertionPoint({ insertionPoint, children })
43-
if(array.length != 0) {
44-
await Array.prototype.push.apply(array, subsequentArray)
45-
} else {
46-
array = await subsequentArray.slice()
47-
}
48-
}
49-
}
50-
return array;
51-
}
52-
5322
}
5423

5524
return self

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ export default ({ Superclass }) => {
1717
})
1818
@extendedSubclassPattern.Subclass()
1919
class Unit extends Superclass {
20-
async pupolateMiddlewareFile() {
21-
let middlewareFileKey = this.middlewareFile
22-
if (!('functionPath' in this)) {
23-
let middlewareFile = await getDocument['File']({
24-
key: middlewareFileKey,
25-
connection: self.rethinkdbConnection
26-
})
27-
this.functionPath = middlewareFile.filePath
28-
}
20+
async pupolateUnitWithFile() {
21+
await super.pupolateUnitWithFile({
22+
getDocument: getDocument['File'],
23+
fileKey: this.middlewareFile,
24+
extract: { sourceKey: 'filePath', destinationKey: 'functionPath' }
25+
})
2926
}
3027
}
3128
return self

module/reusableNestedUnit/implementation/middleware/controllerMixin.mixin.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ export default Mixin(({ Superclass }) => {
1717
async initializeNestedUnit({ nestedUnitKey, additionalChildNestedUnit = [], pathPointerKey = null }) { // Entrypoint Instance
1818
// [1] get nestedUnit
1919
let nestedUnitInstance = await this.getNestedUnit({ nestedUnitKey, additionalChildNestedUnit, pathPointerKey })
20-
// [2] get unit.
21-
let { middlewareImplementation: unitKey } = nestedUnitInstance
20+
let { middlewareImplementation: unitKey } = nestedUnitInstance
2221
let unitInstance = await this.getUnit({ unitKey })
23-
await unitInstance.pupolateMiddlewareFile()
24-
22+
await unitInstance.pupolateUnitWithFile()
23+
2524
let middlewareArray = []
2625
middlewareArray.push(unitInstance)
2726

2827
// [3] Iterate over insertion points
29-
let subsequentMiddleware = await nestedUnitInstance.loopInsertionPoint()
28+
let subsequentMiddleware = await nestedUnitInstance.loopInsertionPoint({ type: 'aggregateIntoArray' })
3029

3130
if(middlewareArray.length != 0) {
3231
await Array.prototype.push.apply(middlewareArray, subsequentMiddleware)

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

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,19 @@ import { extendedSubclassPattern } from 'appscript/utilityFunction/extendedSubcl
88
import { curried as getTableDocumentCurried } from "appscript/utilityFunction/database/query/getTableDocument.query.js";
99

1010
let getDocument = {
11-
Unit: getTableDocumentCurried({ documentId: 'template_viewNestedUnit' }),
11+
NestedUnit: getTableDocumentCurried({ documentId: 'template_viewNestedUnit' }),
1212
}
1313

1414
export default ({ Superclass }) => {
1515
let self =
1616
@conditional({ decorator: prototypeChainDebug, condition: process.env.SZN_DEBUG })
1717
@execute({
1818
staticMethod: 'initializeStaticClass',
19-
args: [ getDocument['Unit'] ]
19+
args: [ getDocument['NestedUnit'] ]
2020
})
2121
@extendedSubclassPattern.Subclass()
2222
class NestedUnit extends Superclass {
2323

24-
// static getDocumentQuery(connection, conditionTreeKey) {
25-
// getConditionTreeQuery(connection, conditionTreeKey)
26-
// }
27-
28-
// static createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback) {
29-
// NestedUnit.createInstance(controllerInstanceArray, dataKey, getDocumentQueryCallback)
30-
// }
31-
32-
/**
33-
* @description loops through all the insertion points and initializes each one to execute the children specific for it.
34-
*
35-
* @param {Class Instance} nestedUnitInstance Tree instance of the module using "reusableNestedUnit" pattern. instance should have "initializeInsertionPoint" function & "insertionPoint" Array.
36-
* @returns undifiend for false or any type of value depending on the module being applied.
37-
*/
38-
async loopInsertionPoint() {
39-
let view = {}
40-
if(this.insertionPoint) {
41-
for (let insertionPoint of this.insertionPoint) {
42-
let children = await this.filterAndOrderChildren({ insertionPointKey: insertionPoint.key })
43-
if(!(insertionPoint.name in view)) view[insertionPoint.name] = []
44-
Array.prototype.push.apply(
45-
view[insertionPoint.name],
46-
await this.initializeInsertionPoint({ insertionPoint, children })
47-
)
48-
}
49-
}
50-
return view;
51-
}
52-
5324
}
5425

5526
return self

0 commit comments

Comments
 (0)