-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathcompute.ts
390 lines (360 loc) · 12 KB
/
compute.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import type { Future } from '@ember-data/request';
import type Store from '@ember-data/store';
import type { StoreRequestInput } from '@ember-data/store';
import { RelatedCollection as ManyArray } from '@ember-data/store/-private';
import { defineSignal, getSignal, peekSignal } from '@ember-data/tracking/-private';
import { DEBUG } from '@warp-drive/build-config/env';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import { getOrSetGlobal } from '@warp-drive/core-types/-private';
import type { Cache } from '@warp-drive/core-types/cache';
import type { ResourceRelationship as SingleResourceRelationship } from '@warp-drive/core-types/cache/relationship';
import type { ObjectValue } from '@warp-drive/core-types/json/raw';
import type {
ArrayField,
DerivedField,
FieldSchema,
GenericField,
LegacyHasManyField,
LocalField,
ObjectField,
SchemaArrayField,
SchemaObjectField,
} from '@warp-drive/core-types/schema/fields';
import type { CollectionResourceRelationship, Link, Links } from '@warp-drive/core-types/spec/json-api-raw';
import { RecordStore } from '@warp-drive/core-types/symbols';
import { SchemaRecord } from '../record';
import type { SchemaService } from '../schema';
import { Editable, Identifier, Legacy, Parent } from '../symbols';
import { ManagedArray } from './managed-array';
import { ManagedObject } from './managed-object';
import { ManyArrayManager } from './many-array-manager';
export const ManagedArrayMap = getOrSetGlobal(
'ManagedArrayMap',
new Map<SchemaRecord, Map<string, ManagedArray | ManyArray>>()
);
export const ManagedObjectMap = getOrSetGlobal(
'ManagedObjectMap',
new Map<SchemaRecord, Map<string, ManagedObject | SchemaRecord>>()
);
export function computeLocal(record: typeof Proxy<SchemaRecord>, field: LocalField, prop: string): unknown {
let signal = peekSignal(record, prop);
if (!signal) {
signal = getSignal(record, prop, false);
signal.lastValue = field.options?.defaultValue ?? null;
}
return signal.lastValue;
}
export function peekManagedArray(record: SchemaRecord, field: FieldSchema): ManyArray | ManagedArray | undefined {
const managedArrayMapForRecord = ManagedArrayMap.get(record);
if (managedArrayMapForRecord) {
return managedArrayMapForRecord.get(field.name);
}
}
export function peekManagedObject(record: SchemaRecord, field: ObjectField): ManagedObject | undefined;
export function peekManagedObject(record: SchemaRecord, field: SchemaObjectField): SchemaRecord | undefined;
export function peekManagedObject(
record: SchemaRecord,
field: ObjectField | SchemaObjectField
): ManagedObject | SchemaRecord | undefined {
const managedObjectMapForRecord = ManagedObjectMap.get(record);
if (managedObjectMapForRecord) {
return managedObjectMapForRecord.get(field.name);
}
}
export function computeField(
schema: SchemaService,
cache: Cache,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: GenericField,
prop: string | string[],
editable: boolean
): unknown {
const rawValue = editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
if (!field.type) {
return rawValue;
}
const transform = schema.transformation(field);
return transform.hydrate(rawValue, field.options ?? null, record);
}
export function computeArray(
store: Store,
schema: SchemaService,
cache: Cache,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: ArrayField | SchemaArrayField,
path: string[],
editable: boolean,
legacy: boolean
) {
const isSchemaArray = field.kind === 'schema-array';
// the thing we hand out needs to know its owner and path in a private manner
// its "address" is the parent identifier (identifier) + field name (field.name)
// in the nested object case field name here is the full dot path from root resource to this value
// its "key" is the field on the parent record
// its "owner" is the parent record
const managedArrayMapForRecord = ManagedArrayMap.get(record);
let managedArray: ManagedArray | undefined;
if (managedArrayMapForRecord) {
managedArray = managedArrayMapForRecord.get(field.name) as ManagedArray | undefined;
}
if (managedArray) {
return managedArray;
} else {
const rawValue = (editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path)) as unknown[];
if (!rawValue) {
return null;
}
managedArray = new ManagedArray(
store,
schema,
cache,
field,
rawValue,
identifier,
path,
record,
isSchemaArray,
editable,
legacy
);
if (!managedArrayMapForRecord) {
ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
} else {
managedArrayMapForRecord.set(field.name, managedArray);
}
}
return managedArray;
}
export function computeObject(
schema: SchemaService,
cache: Cache,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: ObjectField,
path: string[],
editable: boolean,
legacy: boolean
) {
const managedObjectMapForRecord = ManagedObjectMap.get(record);
let managedObject;
if (managedObjectMapForRecord) {
managedObject = managedObjectMapForRecord.get(field.name);
}
if (managedObject) {
return managedObject;
} else {
let rawValue = (editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path)) as object;
if (!rawValue) {
return null;
}
if (field.type) {
const transform = schema.transformation(field);
rawValue = transform.hydrate(rawValue as ObjectValue, field.options ?? null, record) as object;
}
managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);
if (!managedObjectMapForRecord) {
ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
} else {
managedObjectMapForRecord.set(field.name, managedObject);
}
}
return managedObject;
}
export function computeSchemaObject(
store: Store,
cache: Cache,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: SchemaObjectField,
path: string[],
legacy: boolean,
editable: boolean
) {
const schemaObjectMapForRecord = ManagedObjectMap.get(record);
let schemaObject;
if (schemaObjectMapForRecord) {
schemaObject = schemaObjectMapForRecord.get(field.name);
}
if (schemaObject) {
return schemaObject;
} else {
const rawValue = (editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path)) as object;
if (!rawValue) {
return null;
}
const embeddedPath = path.slice();
schemaObject = new SchemaRecord(
store,
identifier,
{
[Editable]: editable,
[Legacy]: legacy,
},
true,
field.type,
embeddedPath
);
}
if (!schemaObjectMapForRecord) {
ManagedObjectMap.set(record, new Map([[field.name, schemaObject]]));
} else {
schemaObjectMapForRecord.set(field.name, schemaObject);
}
return schemaObject;
}
export function computeAttribute(
cache: Cache,
identifier: StableRecordIdentifier,
prop: string,
editable: boolean
): unknown {
return editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
}
export function computeDerivation(
schema: SchemaService,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: DerivedField,
prop: string
): unknown {
return schema.derivation(field)(record, field.options ?? null, prop);
}
// TODO probably this should just be a Document
// but its separate until we work out the lid situation
class ResourceRelationship<T extends SchemaRecord = SchemaRecord> {
declare lid: string;
declare [Parent]: SchemaRecord;
declare [RecordStore]: Store;
declare name: string;
declare data: T | null;
declare links: Links;
declare meta: Record<string, unknown>;
constructor(
store: Store,
cache: Cache,
parent: SchemaRecord,
identifier: StableRecordIdentifier,
field: FieldSchema,
name: string,
editable: boolean
) {
const rawValue = (
editable ? cache.getRelationship(identifier, name) : cache.getRemoteRelationship(identifier, name)
) as SingleResourceRelationship;
// TODO setup true lids for relationship documents
// @ts-expect-error we need to give relationship documents a lid
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${identifier.lid}.${name}`;
this.data = rawValue.data ? store.peekRecord<T>(rawValue.data) : null;
this.name = name;
if (DEBUG) {
this.links = Object.freeze(Object.assign({}, rawValue.links));
this.meta = Object.freeze(Object.assign({}, rawValue.meta));
} else {
this.links = rawValue.links ?? {};
this.meta = rawValue.meta ?? {};
}
this[RecordStore] = store;
this[Parent] = parent;
}
fetch(options?: StoreRequestInput<T, T>): Future<T> {
const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
if (!url) {
throw new Error(
`Cannot ${options?.method ?? 'fetch'} ${this[Parent][Identifier].type}.${String(
this.name
)} because it has no related link`
);
}
const request = Object.assign(
{
url,
method: 'GET',
},
options
);
return this[RecordStore].request<T>(request);
}
}
defineSignal(ResourceRelationship.prototype, 'data');
defineSignal(ResourceRelationship.prototype, 'links');
defineSignal(ResourceRelationship.prototype, 'meta');
function getHref(link?: Link | null): string | null {
if (!link) {
return null;
}
if (typeof link === 'string') {
return link;
}
return link.href;
}
export function computeResource<T extends SchemaRecord>(
store: Store,
cache: Cache,
parent: SchemaRecord,
identifier: StableRecordIdentifier,
field: FieldSchema,
prop: string,
editable: boolean
): ResourceRelationship<T> {
if (field.kind !== 'resource') {
throw new Error(`The schema for ${identifier.type}.${String(prop)} is not a resource relationship`);
}
return new ResourceRelationship<T>(store, cache, parent, identifier, field, prop, editable);
}
export function computeHasMany(
store: Store,
schema: SchemaService,
cache: Cache,
record: SchemaRecord,
identifier: StableRecordIdentifier,
field: LegacyHasManyField,
path: string[],
editable: boolean,
legacy: boolean
) {
// the thing we hand out needs to know its owner and path in a private manner
// its "address" is the parent identifier (identifier) + field name (field.name)
// in the nested object case field name here is the full dot path from root resource to this value
// its "key" is the field on the parent record
// its "owner" is the parent record
const managedArrayMapForRecord = ManagedArrayMap.get(record);
let managedArray: ManyArray | undefined;
if (managedArrayMapForRecord) {
managedArray = managedArrayMapForRecord.get(field.name) as ManyArray | undefined;
}
if (managedArray) {
return managedArray;
} else {
const rawValue = cache.getRelationship(identifier, field.name) as CollectionResourceRelationship;
if (!rawValue) {
return null;
}
managedArray = new ManyArray<unknown>({
store,
type: field.type,
identifier,
cache,
identifiers: rawValue.data as StableRecordIdentifier[],
key: field.name,
meta: rawValue.meta || null,
links: rawValue.links || null,
isPolymorphic: field.options.polymorphic ?? false,
isAsync: field.options.async ?? false,
// TODO: Grab the proper value
_inverseIsAsync: false,
// @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
manager: new ManyArrayManager(record),
isLoaded: true,
allowMutation: editable,
});
if (!managedArrayMapForRecord) {
ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
} else {
managedArrayMapForRecord.set(field.name, managedArray);
}
}
return managedArray;
}