forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtree.ts
More file actions
182 lines (155 loc) · 4.88 KB
/
tree.ts
File metadata and controls
182 lines (155 loc) · 4.88 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
/* eslint-disable @typescript-eslint/ban-types */
import * as json1 from "ot-json1";
const contextSym = Symbol("proxy.context");
type Consumer = (ops: json1.JSONOp) => void;
// eslint-disable-next-line @typescript-eslint/no-wrapper-object-types
interface IProxy extends Object {
[contextSym]: { parent?: IProxy; parentKey: json1.Key };
}
function getPath(target: IProxy, key: json1.Key, path: json1.Key[] = []): json1.Path {
const { parent, parentKey } = target[contextSym];
if (parent !== undefined) {
getPath(parent, parentKey, path);
}
path.push(key);
return path;
}
const cache = new WeakMap<object, IProxy>();
const arrayPatch = {
push:
(target: unknown[], consumer: Consumer, receiver: IProxy) =>
(...item: json1.Doc[]): number => {
const path = getPath(receiver, 0);
path.pop();
const start = target.length;
consumer(
item
.map((value, index) => json1.insertOp([...path, start + index], value))
// eslint-disable-next-line unicorn/no-array-reduce, unicorn/no-array-callback-reference
.reduce(json1.type.compose),
);
return target.push(...item);
},
pop:
(target: unknown[], consumer: Consumer, receiver: IProxy) => (): unknown | undefined => {
const length = target.length;
if (length > 0) {
consumer(json1.removeOp(getPath(receiver, length - 1)));
}
return target.pop();
},
};
const createObjectProxy = (
subject: object,
consumer: Consumer,
parent?: IProxy,
parentKey?: json1.Key,
): object => {
const handler: ProxyHandler<IProxy> = {
get: (target: IProxy, key: string | symbol, receiver: IProxy) => {
if (key === contextSym) {
return { parent, parentKey };
}
const value: unknown = target[key];
return value !== null && typeof value === "object"
? getProxy(/* target: */ value, consumer, /* parent: */ receiver, key as string)
: value;
},
set: (target: IProxy, key: string | symbol, value: json1.Doc, receiver: IProxy) => {
const path = getPath(receiver, key as json1.Key);
if (Object.prototype.hasOwnProperty.call(target, key)) {
consumer(
json1.replaceOp(path, /* oldVal: */ target[key] as json1.Doc, /* newVal: */ value),
);
} else {
consumer(json1.insertOp(path, value));
}
target[key] = value;
return true;
},
};
return new Proxy(subject, handler);
};
// If given key is a string containing an integer then convert it to an integer,
// otherwise return the key unmodified.
// Used when proxying an array to undo the Proxy's coercion of numbers to strings.
// This is required because, while the underlying array will accept string as indices,
// the 'ot-json1' uses indexer semantics for numeric paths and property semantics for
// string paths.
function indexify(key: string | symbol): string | symbol | number {
if (typeof key === "string") {
const asNumber = +key;
if (Math.trunc(asNumber) === asNumber) {
return asNumber;
}
}
return key;
}
const createArrayProxy = (
subject: object,
consumer: Consumer,
parent?: IProxy,
parentKey?: json1.Key,
): object =>
new Proxy(subject, {
get: (target: object, key: string | symbol, receiver: IProxy): unknown => {
if (key === contextSym) {
return { parent, parentKey };
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const maybePatch = arrayPatch[key];
if (maybePatch !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
return maybePatch(target, consumer, receiver);
}
// eslint-disable-next-line no-param-reassign
key = indexify(key as string) as string;
const value = target[key] as object;
return value !== null && typeof value === "object"
? getProxy(/* target: */ value, consumer, /* parent: */ receiver, key)
: value;
},
set: (
target: object,
key: string | symbol,
value: json1.Doc,
receiver: IProxy,
): boolean => {
const indexifiedKey = indexify(key as string) as string;
const path = getPath(receiver, indexifiedKey);
if (Object.prototype.hasOwnProperty.call(target, indexifiedKey)) {
consumer(
json1.replaceOp(
path,
/* oldVal: */ target[indexifiedKey] as json1.Doc,
/* newVal: */ value,
),
);
} else {
consumer(json1.insertOp(path, value));
}
target[indexifiedKey] = value;
return true;
},
});
function getProxy(
target: object,
consumer: Consumer,
parent?: IProxy,
parentKey?: json1.Key,
): IProxy {
let self: IProxy | undefined = cache.get(target);
if (self === undefined) {
self = Array.isArray(target)
? (createArrayProxy(target, consumer, parent, parentKey) as IProxy)
: (createObjectProxy(target, consumer, parent, parentKey) as IProxy);
cache.set(target, self);
}
return self;
}
export const observe = <T extends object>(target: T, consumer: Consumer): T =>
getProxy(target, consumer) as unknown as T;