Skip to content

Commit c2322c2

Browse files
add root substitute graph
1 parent 990b743 commit c2322c2

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

src/Substitute.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1-
import { Context } from './Context';
2-
import { ObjectSubstitute, OmitProxyMethods, DisabledSubstituteObject } from './Transformations';
1+
import { ContextNode } from './Context'
2+
import { Graph } from './linked-list/Graph'
3+
import { ObjectSubstitute, OmitProxyMethods } from './Transformations'
34

4-
export const HandlerKey = Symbol();
5-
export const AreProxiesDisabledKey = Symbol();
5+
export type SubstituteOf<T extends Object> = ObjectSubstitute<OmitProxyMethods<T>, T> & T
66

7-
export type SubstituteOf<T extends Object> = ObjectSubstitute<OmitProxyMethods<T>, T> & T;
7+
export class Substitute extends Graph {
8+
private _proxy: () => void
89

9-
export class Substitute {
10-
static for<T>(): SubstituteOf<T> {
11-
const objectContext = new Context();
12-
return objectContext.rootProxy;
13-
}
10+
private constructor() {
11+
super()
12+
this._proxy = new Proxy(() => { }, {
13+
getPrototypeOf() { // Set custom prototype -> overrideSubstitutePrototype/Instance?
14+
return Substitute.prototype
15+
},
16+
get: (_target, _property) => {
17+
const node = new ContextNode(_property)
18+
node.parent = this
1419

15-
static disableFor<T extends ObjectSubstitute<OmitProxyMethods<any>>>(substitute: T): DisabledSubstituteObject<T> {
16-
const thisProxy = substitute as any; // rootProxy
17-
const thisExposedProxy = thisProxy[HandlerKey]; // Context
20+
this.addNodeBranch(node, _property)
21+
return node.returnProxyOrSubstitution()
22+
}
23+
})
24+
}
1825

19-
const disableProxy = <K extends Function>(f: K): K => {
20-
return function () {
21-
thisProxy[AreProxiesDisabledKey] = true;
22-
const returnValue = f.call(thisExposedProxy, ...arguments);
23-
thisProxy[AreProxiesDisabledKey] = false;
24-
return returnValue;
25-
} as any;
26-
};
26+
static for<T>(): SubstituteOf<T> {
27+
const substitute = new this()
28+
return substitute.proxy as unknown as SubstituteOf<T>
29+
}
2730

28-
return new Proxy(() => { }, {
29-
apply: function (_target, _this, args) {
30-
return disableProxy(thisExposedProxy.getStateApply)(...arguments)
31-
},
32-
set: function (_target, property, value) {
33-
return disableProxy(thisExposedProxy.setStateSet)(...arguments)
34-
},
35-
get: function (_target, property) {
36-
thisExposedProxy._initialState.handleGet(thisExposedProxy, property)
37-
return disableProxy(thisExposedProxy.getStateGet)(...arguments)
38-
}
39-
}) as any;
40-
}
31+
get proxy() {
32+
return this._proxy
33+
}
34+
35+
public getSiblingsOf(node: ContextNode): ContextNode[] {
36+
const siblingNodes = this.indexedRecords.get(node.property) as ContextNode[]
37+
return siblingNodes.filter(siblingNode => siblingNode != node)
38+
}
4139
}

0 commit comments

Comments
 (0)