-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmanaged-array.ts
367 lines (334 loc) · 12.7 KB
/
managed-array.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import type Store from '@ember-data/store';
import type { Signal } from '@ember-data/tracking/-private';
import { addToTransaction, createSignal, subscribe } from '@ember-data/tracking/-private';
import { assert } from '@warp-drive/build-config/macros';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { Cache } from '@warp-drive/core-types/cache';
import type { ArrayValue, ObjectValue, Value } from '@warp-drive/core-types/json/raw';
import type { OpaqueRecordInstance } from '@warp-drive/core-types/record';
import type { ArrayField, HashField, SchemaArrayField } from '@warp-drive/core-types/schema/fields';
import { SchemaRecord } from '../record';
import type { SchemaService } from '../schema';
import { ARRAY_SIGNAL, Editable, Identifier, Legacy, MUTATE, Parent, SOURCE } from '../symbols';
export function notifyArray(arr: ManagedArray) {
addToTransaction(arr[ARRAY_SIGNAL]);
}
type KeyType = string | symbol | number;
const ARRAY_GETTER_METHODS = new Set<KeyType>([
Symbol.iterator,
'concat',
'entries',
'every',
'fill',
'filter',
'find',
'findIndex',
'flat',
'flatMap',
'forEach',
'includes',
'indexOf',
'join',
'keys',
'lastIndexOf',
'map',
'reduce',
'reduceRight',
'slice',
'some',
'values',
]);
// const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
const SYNC_PROPS = new Set<KeyType>(['[]', 'length']);
function isArrayGetter<T>(prop: KeyType): prop is keyof Array<T> {
return ARRAY_GETTER_METHODS.has(prop);
}
const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
function isArraySetter<T>(prop: KeyType): prop is keyof Array<T> {
return ARRAY_SETTER_METHODS.has(prop);
}
// function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
// return prop in self;
// }
function convertToInt(prop: KeyType): number | null {
if (typeof prop === 'symbol') return null;
const num = Number(prop);
if (isNaN(num)) return null;
return num % 1 === 0 ? num : null;
}
type ProxiedMethod = (...args: unknown[]) => unknown;
type ForEachCB = (record: OpaqueRecordInstance, index: number, context: typeof Proxy<unknown[]>) => void;
function safeForEach(
instance: typeof Proxy<unknown[]>,
arr: unknown[],
store: Store,
callback: ForEachCB,
target: unknown
) {
if (target === undefined) {
target = null;
}
// clone to prevent mutation
arr = arr.slice();
assert('`forEach` expects a function as first argument.', typeof callback === 'function');
// because we retrieveLatest above we need not worry if array is mutated during iteration
// by unloadRecord/rollbackAttributes
// push/add/removeObject may still be problematic
// but this is a more traditionally expected forEach bug.
const length = arr.length; // we need to access length to ensure we are consumed
for (let index = 0; index < length; index++) {
callback.call(target, arr[index], index, instance);
}
return instance;
}
export interface ManagedArray extends Omit<Array<unknown>, '[]'> {
[MUTATE]?(
target: unknown[],
receiver: typeof Proxy<unknown[]>,
prop: string,
args: unknown[],
_SIGNAL: Signal
): unknown;
}
export class ManagedArray {
[SOURCE]: unknown[];
declare identifier: StableRecordIdentifier;
declare path: string[];
declare owner: SchemaRecord;
declare [ARRAY_SIGNAL]: Signal;
declare [Editable]: boolean;
declare [Legacy]: boolean;
constructor(
store: Store,
schema: SchemaService,
cache: Cache,
field: ArrayField | SchemaArrayField,
data: unknown[],
identifier: StableRecordIdentifier,
path: string[],
owner: SchemaRecord,
isSchemaArray: boolean,
editable: boolean,
legacy: boolean
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this[SOURCE] = data?.slice();
this[ARRAY_SIGNAL] = createSignal(this, 'length');
const IS_EDITABLE = (this[Editable] = editable ?? false);
this[Legacy] = legacy;
const _SIGNAL = this[ARRAY_SIGNAL];
const boundFns = new Map<KeyType, ProxiedMethod>();
this.identifier = identifier;
this.path = path;
this.owner = owner;
let transaction = false;
type StorageKlass = typeof WeakMap<object, WeakRef<SchemaRecord>>;
const mode = (field as SchemaArrayField).options?.key ?? '@identity';
const RefStorage: StorageKlass =
mode === '@identity'
? (WeakMap as unknown as StorageKlass)
: // CAUTION CAUTION CAUTION
// this is a pile of lies
// the Map is Map<string, WeakRef<SchemaRecord>>
// but TS does not understand how to juggle modes like this
// internal to a method like ours without us duplicating the code
// into two separate methods.
Map<object, WeakRef<SchemaRecord>>;
const ManagedRecordRefs = isSchemaArray ? new RefStorage() : null;
const proxy = new Proxy(this[SOURCE], {
get<R extends typeof Proxy<unknown[]>>(target: unknown[], prop: keyof R, receiver: R) {
if (prop === ARRAY_SIGNAL) {
return _SIGNAL;
}
if (prop === 'identifier') {
return self.identifier;
}
if (prop === 'owner') {
return self.owner;
}
const index = convertToInt(prop);
if (_SIGNAL.shouldReset && (index !== null || SYNC_PROPS.has(prop) || isArrayGetter(prop))) {
_SIGNAL.t = false;
_SIGNAL.shouldReset = false;
const newData = cache.getAttr(identifier, path);
if (newData && newData !== self[SOURCE]) {
self[SOURCE].length = 0;
self[SOURCE].push(...(newData as ArrayValue));
}
}
if (index !== null) {
let val;
if (mode === '@hash') {
val = target[index];
const hashField = schema.resource({ type: field.type! }).identity as HashField;
const hashFn = schema.hashFn(hashField);
val = hashFn(val as object, null, null);
} else {
// if mode is not @identity or @index, then access the key path.
// we should assert that `mode` is a string
// it should read directly from the cache value for that field (e.g. no derivation, no transformation)
// and, we likely should lookup the associated field and throw an error IF
// the given field does not exist OR
// the field is anything other than a GenericField or LegacyAttributeField.
if (mode !== '@identity' && mode !== '@index') {
assert('mode must be a string', typeof mode === 'string');
const modeField = schema.resource({ type: field.type! }).fields.find((f) => f.name === mode);
assert('field must exist in schema', modeField);
assert(
'field must be a GenericField or LegacyAttributeField',
modeField.kind === 'field' || modeField.kind === 'attribute'
);
}
val =
mode === '@identity'
? target[index]
: mode === '@index'
? '@index'
: (target[index] as ObjectValue)[mode];
}
if (isSchemaArray) {
if (!transaction) {
subscribe(_SIGNAL);
}
if (val) {
const recordRef = ManagedRecordRefs!.get(val);
let record = recordRef?.deref();
if (!record) {
const recordPath = path.slice();
// this is a dirty lie since path is string[] but really we
// should change the types for paths to `Array<string | number>`
// TODO we should allow the schema for the field to define a "key"
// for stability. Default should be `@identity` which means that
// same object reference from cache should result in same SchemaRecord
// embedded object.
recordPath.push(index as unknown as string);
const recordIdentifier = self.owner[Identifier] || self.owner[Parent];
record = new SchemaRecord(
store,
recordIdentifier,
{ [Editable]: self.owner[Editable], [Legacy]: self.owner[Legacy] },
true,
field.type,
recordPath
);
// if mode is not @identity or @index, then access the key path now
// to determine the key value.
// chris says we can implement this as a special kind `@hash` which
// would be a function that only has access to the cache value and not
// the record itself, so derivation is possible but intentionally limited
// and non-reactive?
ManagedRecordRefs!.set(val, new WeakRef(record));
} else {
// TODO update embeddedPath if required
}
return record;
}
return val;
}
if (!transaction) {
subscribe(_SIGNAL);
}
if (field.type) {
const transform = schema.transformation(field);
return transform.hydrate(val as Value, field.options ?? null, self.owner);
}
return val;
}
if (isArrayGetter(prop)) {
let fn = boundFns.get(prop);
if (fn === undefined) {
if (prop === 'forEach') {
fn = function () {
subscribe(_SIGNAL);
transaction = true;
const result = safeForEach(receiver, target, store, arguments[0] as ForEachCB, arguments[1]);
transaction = false;
return result;
};
} else {
fn = function () {
subscribe(_SIGNAL);
// array functions must run through Reflect to work properly
// binding via other means will not work.
transaction = true;
const result = Reflect.apply(target[prop] as ProxiedMethod, receiver, arguments) as unknown;
transaction = false;
return result;
};
}
boundFns.set(prop, fn);
}
return fn;
}
if (isArraySetter(prop)) {
let fn = boundFns.get(prop);
if (fn === undefined) {
fn = function () {
if (!IS_EDITABLE) {
throw new Error(
`Mutating this array via ${String(prop)} is not allowed because the record is not editable`
);
}
subscribe(_SIGNAL);
transaction = true;
const result = Reflect.apply(target[prop] as ProxiedMethod, receiver, arguments) as unknown;
transaction = false;
return result;
};
boundFns.set(prop, fn);
}
return fn;
}
return Reflect.get(target, prop, receiver);
},
set(target, prop: KeyType, value, receiver) {
if (!IS_EDITABLE) {
let errorPath = identifier.type;
if (path) {
errorPath = path[path.length - 1];
}
throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
}
if (prop === 'identifier') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
self.identifier = value;
return true;
}
if (prop === 'owner') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
self.owner = value;
return true;
}
const reflect = Reflect.set(target, prop, value, receiver);
if (reflect) {
if (!field.type) {
cache.setAttr(identifier, path, self[SOURCE] as Value);
_SIGNAL.shouldReset = true;
return true;
}
let rawValue = self[SOURCE] as ArrayValue;
if (!isSchemaArray) {
const transform = schema.transformation(field);
if (!transform) {
throw new Error(`No '${field.type}' transform defined for use by ${identifier.type}.${String(prop)}`);
}
rawValue = (self[SOURCE] as ArrayValue).map((item) =>
transform.serialize(item, field.options ?? null, self.owner)
);
}
cache.setAttr(identifier, path, rawValue as Value);
_SIGNAL.shouldReset = true;
}
return reflect;
},
has(target, prop) {
if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
return true;
}
return Reflect.has(target, prop);
},
}) as ManagedArray;
return proxy;
}
}