forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxify.ts
More file actions
136 lines (111 loc) · 3.1 KB
/
Copy pathproxify.ts
File metadata and controls
136 lines (111 loc) · 3.1 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import isPlainObject from "is-plain-object";
export enum Status {
IDLE = 'IDLE',
TRACKING_PATHS = 'TRACKING_PATHS',
TRACKING_MUTATIONS = 'TRACKING_MUTATIONS'
}
export const IS_PROXY = Symbol("IS_PROXY");
function concat(path, prop) {
return path === undefined ? prop : path + "." + prop;
}
const arrayMutations = new Set([
"push",
"shift",
"pop",
"unshift",
"splice",
"reverse",
"sort",
"copyWithin"
]);
function createArrayProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
if (
prop === "length" ||
(typeof target[prop] === "function" && !arrayMutations.has(String(prop)))
) {
return target[prop];
}
const nestedPath = concat(path, prop);
if (tree.status === Status.TRACKING_PATHS) {
tree.paths[tree.paths.length - 1].add(nestedPath);
}
if (arrayMutations.has(String(prop))) {
if (tree.status !== Status.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
return (...args) => {
tree.mutations.push({
method: prop,
path: path,
args: args
});
return target[prop](...args);
};
}
return (target[prop] = proxify(tree, target[prop], nestedPath));
}
});
}
function createObjectProxy(tree, value, path) {
return new Proxy(value, {
get(target, prop) {
if (prop === IS_PROXY) return true;
const value = target[prop];
const nestedPath = concat(path, prop);
if (tree.status === Status.TRACKING_PATHS) {
tree.paths[tree.paths.length - 1].add(nestedPath);
}
if (typeof value === "function") {
return value(tree, nestedPath);
}
return (target[prop] = proxify(tree, value, nestedPath));
},
set(target, prop, value) {
const nestedPath = concat(path, prop);
if (tree.status !== Status.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
tree.mutations.push({
method: "set",
path: nestedPath,
args: [value]
});
return Reflect.set(target, prop, value);
},
deleteProperty(target, prop) {
const nestedPath = concat(path, prop);
if (tree.status !== Status.TRACKING_MUTATIONS) {
throw new Error(
`proxy-state-tree - You are mutating the path "${nestedPath}", but it is not allowed`
);
}
tree.mutations.push({
method: "unset",
path: nestedPath,
args: []
});
delete target[prop];
return true;
}
});
}
function proxify(tree, value, path?) {
if (value) {
if (value[IS_PROXY]) {
return value;
} else if (Array.isArray(value)) {
return createArrayProxy(tree, value, path);
} else if (isPlainObject(value)) {
return createObjectProxy(tree, value, path);
}
}
return value;
}
export default proxify;