forked from ffMathy/FluffySpoon.JavaScript.Testing.Faking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.ts
More file actions
79 lines (64 loc) · 2.05 KB
/
Copy pathContext.ts
File metadata and controls
79 lines (64 loc) · 2.05 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { ContextState } from "./states/ContextState";
import { InitialState } from "./states/InitialState";
import { HandlerKey } from "./Substitute";
export class Context {
private _initialState: InitialState;
private _proxy: any;
private _rootProxy: any;
private _state: ContextState;
constructor() {
this._initialState = new InitialState();
this._state = this._initialState;
this._proxy = new Proxy(() => { }, {
apply: (_target, _this, args) => {
return this.apply(args);
},
set: (_target, property, value) => {
this.set(property, value);
return true;
},
get: (_target, property) => {
return this.get(property);
}
});
this._rootProxy = new Proxy(() => { }, {
apply: (_target, _this, args) => {
return this.initialState.apply(this, args);
},
set: (_target, property, value) => {
this.initialState.set(this, property, value);
return true;
},
get: (_target, property) => {
return this.initialState.get(this, property);
}
});
}
apply(args: any[]) {
return this._state.apply(this, args);
}
set(property: PropertyKey, value: any) {
return this._state.set(this, property, value);
}
get(property: PropertyKey) {
if(property === HandlerKey)
return this;
return this._state.get(this, property);
}
public get proxy() {
return this._proxy;
}
public get rootProxy() {
return this._rootProxy;
}
public get initialState() {
return this._initialState;
}
public set state(state: ContextState) {
if(this._state === state)
return;
this._state = state;
if(state.onSwitchedTo)
state.onSwitchedTo(this);
}
}