forked from vasturiano/force-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkapsule-link.ts
35 lines (29 loc) · 1006 Bytes
/
kapsule-link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export function linkKapsule(kapsulePropNames : string[], kapsuleType) {
const propNames = kapsulePropNames;
const dummyK = new kapsuleType(); // To extract defaults
dummyK._destructor && dummyK._destructor();
return {
linkProp: (prop) => { // link property config
return {
default: dummyK[prop](),
onChange(v, state) { propNames.forEach(propName => state[propName][prop](v)) },
triggerUpdate: false
}
},
linkMethod: (method) => { // link method pass-through
return (state, ...args) => {
const returnVals = [];
propNames.forEach(propName => {
const kapsuleInstance = state[propName];
const returnVal = kapsuleInstance[method](...args);
if (returnVal !== kapsuleInstance) {
returnVals.push(returnVal);
}
});
return returnVals.length
? returnVals[0]
: this; // chain based on the parent object, not the inner kapsule
}
}
}
}