@warp-drive/core 5.7.0-alpha.10 → 5.7.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/reactive.js CHANGED
@@ -1,1927 +1,15 @@
1
1
  import { isResourceSchema } from './types/schema/fields.js';
2
- import { F as withSignalStore, T as isExtensionProp, U as performExtensionSet, H as consumeInternalSignal, V as performArrayExtensionGet, x as entangleSignal, E as peekInternalSignal, d as SOURCE$1, f as fastPush, l as RelatedCollection, J as getOrCreateInternalSignal, G as notifyInternalSignal, W as performObjectExtensionGet, y as defineSignal, B as Signals, h as setRecordIdentifier, r as recordIdentifierFor } from "./request-state-CeN66aML.js";
3
- import { EnableHydration, STRUCTURED } from './types/request.js';
2
+ import { H as ReactiveResource, J as isNonIdentityCacheableField, K as getFieldCacheKeyStrict, r as recordIdentifierFor, B as withSignalStore } from "./request-state-BWYju5O9.js";
3
+ import { D as Destroy, C as Context } from "./symbols-BoONANuz.js";
4
+ export { a as Checkout } from "./symbols-BoONANuz.js";
4
5
  import { macroCondition, getGlobalConfig } from '@embroider/macros';
5
6
  import { warn, deprecate } from '@ember/debug';
7
+ import './index.js';
8
+ import './types/request.js';
6
9
  import './utils/string.js';
7
- import { A as ARRAY_SIGNAL, O as OBJECT_SIGNAL, c as createMemo } from "./configure-B48bFHOl.js";
8
- import { RecordStore, Type } from './types/symbols.js';
9
- import { S as SOURCE, E as Editable, L as Legacy, D as Destroy, I as Identifier, P as Parent, a as EmbeddedPath, C as Checkout, b as EmbeddedField } from "./symbols-SIstXMLI.js";
10
+ import { c as createMemo } from "./configure-B48bFHOl.js";
10
11
  import { getOrSetGlobal } from './types/-private.js';
11
- import './index.js';
12
- function getAliasField(context) {
13
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
14
- {
15
- throw new Error(`Alias field access is not implemented`);
16
- }
17
- })() : {};
18
- }
19
- function setAliasField(context) {
20
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
21
- {
22
- throw new Error(`Alias field setting is not implemented`);
23
- }
24
- })() : {};
25
- return false;
26
- }
27
- const ARRAY_GETTER_METHODS = new Set([Symbol.iterator, 'concat', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flat', 'flatMap', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'map', 'reduce', 'reduceRight', 'slice', 'some', 'values']);
28
- // const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
29
- const SYNC_PROPS = new Set(['[]', 'length']);
30
- function isArrayGetter(prop) {
31
- return ARRAY_GETTER_METHODS.has(prop);
32
- }
33
- const ARRAY_SETTER_METHODS = new Set(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
34
- function isArraySetter(prop) {
35
- return ARRAY_SETTER_METHODS.has(prop);
36
- }
37
-
38
- // function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
39
- // return prop in self;
40
- // }
41
-
42
- function convertToInt(prop) {
43
- if (typeof prop === 'symbol') return null;
44
- const num = Number(prop);
45
- if (isNaN(num)) return null;
46
- return num % 1 === 0 ? num : null;
47
- }
48
- function safeForEach(instance, arr, store, callback, target) {
49
- if (target === undefined) {
50
- target = null;
51
- }
52
- // clone to prevent mutation
53
- arr = arr.slice();
54
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
55
- if (!test) {
56
- throw new Error('`forEach` expects a function as first argument.');
57
- }
58
- })(typeof callback === 'function') : {};
59
-
60
- // because we retrieveLatest above we need not worry if array is mutated during iteration
61
- // by unloadRecord/rollbackAttributes
62
- // push/add/removeObject may still be problematic
63
- // but this is a more traditionally expected forEach bug.
64
- const length = arr.length; // we need to access length to ensure we are consumed
65
-
66
- for (let index = 0; index < length; index++) {
67
- callback.call(target, arr[index], index, instance);
68
- }
69
- return instance;
70
- }
71
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
72
- class ManagedArray {
73
- constructor(context, owner, data) {
74
- // eslint-disable-next-line @typescript-eslint/no-this-alias
75
- const self = this;
76
- this[SOURCE] = data?.slice();
77
- const IS_EDITABLE = this[Editable] = context.editable ?? false;
78
- this[Legacy] = context.legacy;
79
- const schema = context.store.schema;
80
- const cache = context.store.cache;
81
- const {
82
- field
83
- } = context;
84
- const signals = withSignalStore(this);
85
- let _SIGNAL = null;
86
- const boundFns = new Map();
87
- this.identifier = context.resourceKey;
88
- this.path = context.path;
89
- this.owner = owner;
90
- let transaction = false;
91
- const KeyMode = field.options?.key ?? '@identity';
92
- // listener.
93
- const RefStorage = KeyMode === '@identity' ? WeakMap :
94
- // CAUTION CAUTION CAUTION
95
- // this is a pile of lies
96
- // the Map is Map<string, WeakRef<ReactiveResource>>
97
- // but TS does not understand how to juggle modes like this
98
- // internal to a method like ours without us duplicating the code
99
- // into two separate methods.
100
- Map;
101
- const ManagedRecordRefs = field.kind === 'schema-array' ? new RefStorage() : null;
102
- const extensions = context.legacy ? schema.CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) : null;
103
- const proxy = new Proxy(this[SOURCE], {
104
- get(target, prop, receiver) {
105
- if (prop === ARRAY_SIGNAL) {
106
- return _SIGNAL;
107
- }
108
- if (prop === 'identifier') {
109
- return self.identifier;
110
- }
111
- if (prop === 'owner') {
112
- return self.owner;
113
- }
114
- const index = convertToInt(prop);
115
- if (_SIGNAL.isStale && (index !== null || SYNC_PROPS.has(prop) || isArrayGetter(prop))) {
116
- _SIGNAL.isStale = false;
117
- const newData = cache.getAttr(context.resourceKey, context.path);
118
- if (newData && newData !== self[SOURCE]) {
119
- self[SOURCE].length = 0;
120
- self[SOURCE].push(...newData);
121
- }
122
- }
123
- if (prop === 'length') {
124
- return consumeInternalSignal(_SIGNAL), target.length;
125
- }
126
- if (prop === '[]') return consumeInternalSignal(_SIGNAL), receiver;
127
- if (index !== null) {
128
- if (!transaction) {
129
- consumeInternalSignal(_SIGNAL);
130
- }
131
- const rawValue = target[index];
132
- if (field.kind === 'array') {
133
- if (field.type) {
134
- const transform = schema.transformation(field);
135
- return transform.hydrate(rawValue, field.options ?? null, self.owner);
136
- }
137
- return rawValue;
138
- }
139
-
140
- /**
141
- * When the array is polymorphic, we need to determine the real type
142
- * in order to apply the correct identity as schema-object identity
143
- * is only required to be unique by type
144
- */
145
- let objectType;
146
- if (field.options?.polymorphic) {
147
- const typePath = field.options.type ?? 'type';
148
- // if we are polymorphic, then context.field.options.type will
149
- // either specify a path on the rawValue to use as the type, defaulting to "type" or
150
- // the special string "@hash" which tells us to treat field.type as a hashFn name with which
151
- // to calc the type.
152
- if (typePath === '@hash') {
153
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
154
- if (!test) {
155
- throw new Error(`Expected the field to define a hashFn as its type`);
156
- }
157
- })(field.type) : {};
158
- const hashFn = schema.hashFn({
159
- type: field.type
160
- });
161
- // TODO consider if there are better options and name args we could provide.
162
- objectType = hashFn(rawValue, null, null);
163
- } else {
164
- objectType = rawValue[typePath];
165
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
166
- if (!test) {
167
- throw new Error(`Expected the type path for the field to be a value on the raw object`);
168
- }
169
- })(typePath && objectType && typeof objectType === 'string') : {};
170
- }
171
- } else {
172
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
173
- if (!test) {
174
- throw new Error(`A non-polymorphic SchemaArrayField must provide a SchemaObject type in its definition`);
175
- }
176
- })(field.type) : {};
177
- objectType = field.type;
178
- }
179
-
180
- /**
181
- * When KeyMode=@hash the ReactiveResource is keyed into
182
- * ManagedRecordRefs by the return value of @hash on the rawValue.
183
- *
184
- * This means that we could find a way to only recompute the identity
185
- * when ARRAY_SIGNAL is dirty if hash performance becomes a bottleneck.
186
- */
187
- let schemaObjectKeyValue;
188
- if (KeyMode === '@hash') {
189
- const hashField = schema.resource({
190
- type: objectType
191
- }).identity;
192
- const hashFn = schema.hashFn(hashField);
193
- schemaObjectKeyValue = hashFn(rawValue, hashField.options ?? null, hashField.name);
194
- } else {
195
- // if mode is not @identity or @index, then access the key path.
196
- // we should assert that `mode` is a string
197
- // it should read directly from the cache value for that field (e.g. no derivation, no transformation)
198
- // and, we likely should lookup the associated field and throw an error IF
199
- // the given field does not exist OR
200
- // the field is anything other than a GenericField or LegacyAttributeField.
201
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
202
- const isPathKeyMode = KeyMode !== '@identity' && KeyMode !== '@index';
203
- if (isPathKeyMode) {
204
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
205
- if (!test) {
206
- throw new Error('mode must be a string');
207
- }
208
- })(typeof KeyMode === 'string' && KeyMode !== '') : {};
209
- const modeField = schema.fields({
210
- type: objectType
211
- }).get(KeyMode);
212
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
213
- if (!test) {
214
- throw new Error('field must exist in schema');
215
- }
216
- })(modeField) : {};
217
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
218
- if (!test) {
219
- throw new Error('field must be a GenericField or LegacyAttributeField');
220
- }
221
- })(modeField.kind === 'field' || modeField.kind === 'attribute') : {};
222
- }
223
- }
224
- schemaObjectKeyValue = KeyMode === '@identity' ? rawValue : KeyMode === '@index' ? index : rawValue[KeyMode];
225
- }
226
- if (!schemaObjectKeyValue) {
227
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
228
- {
229
- throw new Error(`Unexpected out of bounds access on SchemaArray`);
230
- }
231
- })() : {};
232
- return undefined;
233
- }
234
- const recordRef = ManagedRecordRefs.get(schemaObjectKeyValue);
235
- const record = recordRef?.value.deref();
236
-
237
- // confirm the type and key still match
238
- if (record && recordRef.type === objectType && recordRef.identity === schemaObjectKeyValue) {
239
- if (recordRef.index !== index) {
240
- recordRef.index = index;
241
- recordRef.context.path[recordRef.context.path.length - 1] = index;
242
- }
243
- return record;
244
- } else if (record) {
245
- // TODO schedule idle once we can
246
- void Promise.resolve().then(() => {
247
- record[Destroy]();
248
- });
249
- }
250
- const recordPath = context.path.slice();
251
- // this is a dirty lie since path is string[] but really we
252
- // should change the types for paths to `Array<string | number>`
253
- recordPath.push(index);
254
- const objectContext = {
255
- store: context.store,
256
- resourceKey: context.resourceKey,
257
- modeName: context.modeName,
258
- legacy: context.legacy,
259
- editable: context.editable,
260
- path: recordPath,
261
- field: field,
262
- value: objectType
263
- };
264
- const schemaObject = new ReactiveResource(objectContext);
265
- ManagedRecordRefs.set(schemaObjectKeyValue, {
266
- type: objectType,
267
- identity: schemaObjectKeyValue,
268
- index,
269
- context: objectContext,
270
- value: new WeakRef(schemaObject)
271
- });
272
- return schemaObject;
273
- }
274
- if (isArrayGetter(prop)) {
275
- let fn = boundFns.get(prop);
276
- if (fn === undefined) {
277
- if (prop === 'forEach') {
278
- fn = function () {
279
- consumeInternalSignal(_SIGNAL);
280
- transaction = true;
281
- const result = safeForEach(receiver, target, context.store, arguments[0], arguments[1]);
282
- transaction = false;
283
- return result;
284
- };
285
- } else {
286
- fn = function () {
287
- consumeInternalSignal(_SIGNAL);
288
- // array functions must run through Reflect to work properly
289
- // binding via other means will not work.
290
- transaction = true;
291
- const result = Reflect.apply(target[prop], receiver, arguments);
292
- transaction = false;
293
- return result;
294
- };
295
- }
296
- boundFns.set(prop, fn);
297
- }
298
- return fn;
299
- }
300
- if (isArraySetter(prop)) {
301
- let fn = boundFns.get(prop);
302
- if (fn === undefined) {
303
- fn = function () {
304
- if (!IS_EDITABLE) {
305
- throw new Error(`Mutating this array via ${String(prop)} is not allowed because the record is not editable`);
306
- }
307
- consumeInternalSignal(_SIGNAL);
308
- transaction = true;
309
- const result = Reflect.apply(target[prop], receiver, arguments);
310
- transaction = false;
311
- return result;
312
- };
313
- boundFns.set(prop, fn);
314
- }
315
- return fn;
316
- }
317
- if (isExtensionProp(extensions, prop)) {
318
- return performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, v => void (transaction = v));
319
- }
320
- return Reflect.get(target, prop, receiver);
321
- },
322
- set(target, prop, value, receiver) {
323
- if (!IS_EDITABLE) {
324
- let errorPath = context.resourceKey.type;
325
- if (context.path) {
326
- errorPath = context.path[context.path.length - 1];
327
- }
328
- throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
329
- }
330
- if (prop === 'identifier') {
331
- self.identifier = value;
332
- return true;
333
- }
334
- if (prop === 'owner') {
335
- self.owner = value;
336
- return true;
337
- }
338
- if (isExtensionProp(extensions, prop)) {
339
- return performExtensionSet(receiver, extensions, signals, prop, value);
340
- }
341
- const reflect = Reflect.set(target, prop, value, receiver);
342
- if (reflect) {
343
- if (!field.type) {
344
- cache.setAttr(context.resourceKey, context.path, self[SOURCE]);
345
- _SIGNAL.isStale = true;
346
- return true;
347
- }
348
- let rawValue = self[SOURCE];
349
- if (field.kind !== 'schema-array') {
350
- const transform = schema.transformation(field);
351
- if (!transform) {
352
- throw new Error(`No '${field.type}' transform defined for use by ${context.resourceKey.type}.${String(prop)}`);
353
- }
354
- rawValue = self[SOURCE].map(item => transform.serialize(item, field.options ?? null, self.owner));
355
- }
356
- cache.setAttr(context.resourceKey, context.path, rawValue);
357
- _SIGNAL.isStale = true;
358
- }
359
- return reflect;
360
- },
361
- has(target, prop) {
362
- if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
363
- return true;
364
- }
365
- return Reflect.has(target, prop);
366
- }
367
- });
368
-
369
- // we entangle the signal on the returned proxy since that is
370
- // the object that other code will be interfacing with.
371
- _SIGNAL = entangleSignal(signals, proxy, ARRAY_SIGNAL, undefined);
372
- return proxy;
373
- }
374
- }
375
-
376
- // this will error if someone tries to call
377
- // A(identifierArray) since it is not configurable
378
- // which is preferable to the `meta` override we used
379
- // before which required importing all of Ember
380
- const desc = {
381
- enumerable: true,
382
- configurable: false,
383
- get: function () {
384
- // here to support computed chains
385
- // and {{#each}}
386
- if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_COMPUTED_CHAINS)) {
387
- return this;
388
- }
389
- }
390
- };
391
- // compat(desc);
392
- Object.defineProperty(ManagedArray.prototype, '[]', desc);
393
- function getArrayField(context) {
394
- const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
395
- // the thing we hand out needs to know its owner and path in a private manner
396
- // its "address" is the parent identifier (identifier) + field name (field.name)
397
- // in the nested object case field name here is the full dot path from root resource to this value
398
- // its "key" is the field on the parent record
399
- // its "owner" is the parent record
400
- const {
401
- record
402
- } = context;
403
- let managedArray = signal.value;
404
- if (managedArray) {
405
- return managedArray;
406
- } else {
407
- const {
408
- store,
409
- resourceKey,
410
- path
411
- } = context;
412
- const {
413
- cache
414
- } = store;
415
- const rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
416
- if (!rawValue) {
417
- return null;
418
- }
419
- managedArray = new ManagedArray(context, record, rawValue);
420
- signal.value = managedArray;
421
- }
422
- return managedArray;
423
- }
424
- function setArrayField(context) {
425
- const {
426
- field,
427
- record,
428
- value
429
- } = context;
430
- const {
431
- cache,
432
- schema
433
- } = context.store;
434
- const fieldSignal = peekInternalSignal(context.signals, context.path.at(-1));
435
- const peeked = fieldSignal?.value;
436
- const transform = field.type ? schema.transformation(field) : null;
437
- const rawValue = field.type ? value.map(item => transform.serialize(item, field.options ?? null, record)) : value?.slice();
438
- cache.setAttr(context.resourceKey, context.path, rawValue);
439
- if (peeked) {
440
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
441
- if (!test) {
442
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
443
- }
444
- })(ARRAY_SIGNAL in peeked) : {};
445
- const arrSignal = peeked[ARRAY_SIGNAL];
446
- arrSignal.isStale = true;
447
- // TODO run array destroy?
448
- }
449
- if (!Array.isArray(rawValue) && fieldSignal) {
450
- fieldSignal.value = null;
451
- }
452
- return true;
453
- }
454
- function getAttributeField(context) {
455
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
456
- const {
457
- cache
458
- } = context.store;
459
- return context.editable ? cache.getAttr(context.resourceKey, context.path) : cache.getRemoteAttr(context.resourceKey, context.path);
460
- }
461
- function setAttributeField(context) {
462
- context.store.cache.setAttr(context.resourceKey, context.path, context.value);
463
- return true;
464
- }
465
- const InvalidKinds = ['alias', 'derived', '@local'];
466
- function isInvalidKind(kind) {
467
- return InvalidKinds.includes(kind);
468
- }
469
- function isNonIdentityCacheableField(field) {
470
- return !isInvalidKind(field.kind) && field.kind !== '@id' && field.kind !== '@hash';
471
- }
472
- function getFieldCacheKeyStrict(field) {
473
- return field.sourceKey || field.name;
474
- }
475
- function getFieldCacheKey(field) {
476
- return 'sourceKey' in field && field.sourceKey ? field.sourceKey : field.name;
477
- }
478
- function getBelongsToField(context) {
479
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
480
- const {
481
- field,
482
- resourceKey,
483
- store
484
- } = context;
485
- const {
486
- schema,
487
- cache
488
- } = store;
489
- if (field.options.linksMode) {
490
- const rawValue = context.editable ? cache.getRelationship(resourceKey, getFieldCacheKeyStrict(field)) : cache.getRemoteRelationship(resourceKey, getFieldCacheKeyStrict(field));
491
- return rawValue.data ? store.peekRecord(rawValue.data) : null;
492
- }
493
-
494
- // FIXME move this to a "LegacyMode" make this part of "PolarisMode"
495
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
496
- if (!test) {
497
- throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
498
- }
499
- })(context.legacy) : {};
500
- return schema._kind('@legacy', 'belongsTo').get(store, context.record, resourceKey, field);
501
- }
502
- function setBelongsToField(context) {
503
- const {
504
- store
505
- } = context;
506
- const {
507
- schema
508
- } = store;
509
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
510
- if (!test) {
511
- throw new Error(`Can only mutate belongsTo fields when the resource is in legacy mode`);
512
- }
513
- })(context.legacy) : {};
514
- schema._kind('@legacy', 'belongsTo').set(store, context.record, context.resourceKey, context.field, context.value);
515
- return true;
516
- }
517
- function getCollectionField(context) {
518
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
519
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
520
- {
521
- throw new Error(`Accessing collection fields is not yet implemented`);
522
- }
523
- })() : {};
524
- }
525
- function setCollectionField(context) {
526
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
527
- {
528
- throw new Error(`Setting collection fields is not yet implemented`);
529
- }
530
- })() : {};
531
- return false;
532
- }
533
- function getDerivedField(context) {
534
- const {
535
- schema
536
- } = context.store;
537
- return schema.derivation(context.field)(context.record, context.field.options ?? null, context.field.name);
538
- }
539
- function setDerivedField(context) {
540
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
541
- {
542
- throw new Error(`ILLEGAL SET: Cannot set '${context.path.join('.')}' on '${context.resourceKey.type}' as ${context.field.kind} fields are not mutable`);
543
- }
544
- })() : {};
545
- return false;
546
- }
547
- function getGenericField(context) {
548
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
549
- const {
550
- cache,
551
- schema
552
- } = context.store;
553
- const rawValue = context.editable ? cache.getAttr(context.resourceKey, context.path) : cache.getRemoteAttr(context.resourceKey, context.path);
554
- const {
555
- field
556
- } = context;
557
- if (!field.type) {
558
- return rawValue;
559
- }
560
- const transform = schema.transformation(field);
561
- return transform.hydrate(rawValue, field.options ?? null, context.record);
562
- }
563
- function setGenericField(context) {
564
- const {
565
- cache,
566
- schema
567
- } = context.store;
568
- const {
569
- field
570
- } = context;
571
- if (!field.type) {
572
- cache.setAttr(context.resourceKey, context.path, context.value);
573
- return true;
574
- }
575
- const transform = schema.transformation(field);
576
- const rawValue = transform.serialize(context.value, field.options ?? null, context.record);
577
- cache.setAttr(context.resourceKey, context.path, rawValue);
578
- return true;
579
- }
580
- class ManyArrayManager {
581
- constructor(record, editable) {
582
- this.record = record;
583
- this.store = record[RecordStore];
584
- this.identifier = record[Identifier];
585
- this.editable = editable;
586
- }
587
- _syncArray(array) {
588
- const method = this.editable ? 'getRelationship' : 'getRemoteRelationship';
589
- // FIXME field needs to use sourceKey
590
- const rawValue = this.store.cache[method](this.identifier, array.key);
591
- if (rawValue.meta) {
592
- array.meta = rawValue.meta;
593
- }
594
- if (rawValue.links) {
595
- array.links = rawValue.links;
596
- }
597
- const currentState = array[SOURCE$1];
598
-
599
- // unlike in the normal RecordArray case, we don't need to divorce the reference
600
- // because we don't need to worry about associate/disassociate since the graph
601
- // takes care of that for us
602
- if (currentState !== rawValue.data) {
603
- currentState.length = 0;
604
- fastPush(currentState, rawValue.data);
605
- }
606
- }
607
- reloadHasMany(key, options) {
608
- // FIXME field needs to use sourceKey
609
- const field = this.store.schema.fields(this.identifier).get(key);
610
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
611
- if (!test) {
612
- throw new Error(`Expected a hasMany field for ${key}`);
613
- }
614
- })(field?.kind === 'hasMany') : {};
615
- const cacheOptions = options ? extractCacheOptions(options) : {
616
- reload: true
617
- };
618
- cacheOptions.types = [field.type];
619
- const rawValue = this.store.cache.getRelationship(this.identifier, key);
620
- const req = {
621
- url: getRelatedLink(rawValue),
622
- op: 'findHasMany',
623
- method: 'GET',
624
- records: rawValue.data,
625
- cacheOptions,
626
- options: {
627
- field,
628
- identifier: this.identifier,
629
- links: rawValue.links,
630
- meta: rawValue.meta
631
- },
632
- [EnableHydration]: false
633
- };
634
- return this.store.request(req);
635
- }
636
- mutate(mutation) {
637
- this.store.cache.mutate(mutation);
638
- }
639
- }
640
- function getRelatedLink(resource) {
641
- const related = resource.links?.related;
642
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
643
- if (!test) {
644
- throw new Error(`Expected a related link`);
645
- }
646
- })(related) : {};
647
- return typeof related === 'object' ? related.href : related;
648
- }
649
- function extractCacheOptions(options) {
650
- const cacheOptions = {};
651
- if ('reload' in options) {
652
- cacheOptions.reload = options.reload;
653
- }
654
- if ('backgroundReload' in options) {
655
- cacheOptions.backgroundReload = options.backgroundReload;
656
- }
657
- return cacheOptions;
658
- }
659
- function getHasManyField(context) {
660
- const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
661
- const {
662
- store,
663
- field
664
- } = context;
665
- if (field.options.linksMode) {
666
- const {
667
- record
668
- } = context;
669
- // the thing we hand out needs to know its owner and path in a private manner
670
- // its "address" is the parent identifier (identifier) + field name (field.name)
671
- // in the nested object case field name here is the full dot path from root resource to this value
672
- // its "key" is the field on the parent record
673
- // its "owner" is the parent record
674
-
675
- const cached = signal.value;
676
- if (cached) {
677
- return cached;
678
- }
679
- const {
680
- editable,
681
- resourceKey
682
- } = context;
683
- const {
684
- cache
685
- } = store;
686
- const rawValue = cache.getRelationship(resourceKey, getFieldCacheKeyStrict(field));
687
- if (!rawValue) {
688
- return null;
689
- }
690
- const managedArray = new RelatedCollection({
691
- store,
692
- type: field.type,
693
- identifier: resourceKey,
694
- cache,
695
- field: context.legacy ? field : undefined,
696
- // we divorce the reference here because ManyArray mutates the target directly
697
- // before sending the mutation op to the cache. We may be able to avoid this in the future
698
- identifiers: rawValue.data?.slice(),
699
- key: field.name,
700
- meta: rawValue.meta || null,
701
- links: rawValue.links || null,
702
- isPolymorphic: field.options.polymorphic ?? false,
703
- isAsync: field.options.async ?? false,
704
- // TODO: Grab the proper value
705
- _inverseIsAsync: false,
706
- // @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
707
- manager: new ManyArrayManager(record, editable),
708
- isLoaded: true,
709
- allowMutation: editable
710
- });
711
- signal.value = managedArray;
712
- return managedArray;
713
- }
714
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
715
- if (!test) {
716
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
717
- }
718
- })(context.legacy) : {};
719
- return store.schema._kind('@legacy', 'hasMany').get(store, context.record, context.resourceKey, field);
720
- }
721
- function setHasManyField(context) {
722
- const {
723
- store
724
- } = context;
725
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
726
- if (!test) {
727
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
728
- }
729
- })(context.legacy) : {};
730
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
731
- if (!test) {
732
- throw new Error(`You must pass an array of records to set a hasMany relationship`);
733
- }
734
- })(Array.isArray(context.value)) : {};
735
- store.schema._kind('@legacy', 'hasMany').set(store, context.record, context.resourceKey, context.field, context.value);
736
- return true;
737
- }
738
- function getHashField(context) {
739
- const {
740
- field,
741
- path,
742
- resourceKey
743
- } = context;
744
- const {
745
- schema,
746
- cache
747
- } = context.store;
748
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
749
- if (!test) {
750
- throw new Error(`Cannot use a ${field.kind} directly on a resource.`);
751
- }
752
- })(Array.isArray(path) && path.length > 1) : {};
753
- const realPath = path.slice(0, -1);
754
- const rawData = context.editable ? cache.getAttr(resourceKey, realPath) : cache.getRemoteAttr(resourceKey, realPath);
755
- return schema.hashFn(field)(rawData, field.options ?? null, field.name ?? null);
756
- }
757
- function setHashField(context) {
758
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
759
- {
760
- throw new Error(`ILLEGAL SET: Cannot set '${Array.isArray(context.path) ? context.path.join('.') : context.path}' on '${context.resourceKey.type}' as ${context.field.kind} fields are not mutable`);
761
- }
762
- })() : {};
763
- return false;
764
- }
765
- function getIdentityField(context) {
766
- entangleSignal(context.signals, context.record, '@identity', null);
767
- return context.resourceKey.id;
768
- }
769
- function setIdentityField(context) {
770
- const {
771
- value,
772
- resourceKey,
773
- store
774
- } = context;
775
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
776
- if (!test) {
777
- throw new Error(`Expected to receive a string id`);
778
- }
779
- })(typeof value === 'string' && value.length) : {};
780
- const normalizedId = String(value);
781
- const didChange = normalizedId !== resourceKey.id;
782
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
783
- if (!test) {
784
- throw new Error(`Cannot set ${resourceKey.type} record's id to ${normalizedId}, because id is already ${resourceKey.id}`);
785
- }
786
- })(!didChange || resourceKey.id === null) : {};
787
- if (normalizedId !== null && didChange) {
788
- store._instanceCache.setRecordId(resourceKey, normalizedId);
789
- store.notifications.notify(resourceKey, 'identity');
790
- }
791
- return true;
792
- }
793
- function getLocalField(context) {
794
- const {
795
- field
796
- } = context;
797
- const signal = getOrCreateInternalSignal(context.signals, context.record, field.name, field.options?.defaultValue ?? null);
798
- consumeInternalSignal(signal);
799
- return signal.value;
800
- }
801
- function setLocalField(context) {
802
- const {
803
- value
804
- } = context;
805
- const signal = getOrCreateInternalSignal(context.signals, context.record, context.field.name, value);
806
- if (signal.value !== value) {
807
- signal.value = value;
808
- notifyInternalSignal(signal);
809
- }
810
- return true;
811
- }
812
- const ObjectSymbols = new Set([OBJECT_SIGNAL, Parent, SOURCE, Editable, EmbeddedPath]);
813
-
814
- // const ignoredGlobalFields = new Set<string>(['setInterval', 'nodeType', 'nodeName', 'length', 'document', STRUCTURED]);
815
-
816
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
817
- class ManagedObject {
818
- constructor(context) {
819
- const {
820
- field,
821
- path
822
- } = context;
823
- // eslint-disable-next-line @typescript-eslint/no-this-alias
824
- const self = this;
825
- this[SOURCE] = Object.assign({}, context.value);
826
- const signals = withSignalStore(this);
827
- const _SIGNAL = this[OBJECT_SIGNAL] = entangleSignal(signals, this, OBJECT_SIGNAL, undefined);
828
- this[Editable] = context.editable;
829
- this[Legacy] = context.legacy;
830
- this[Parent] = context.resourceKey;
831
- this[EmbeddedPath] = path;
832
- const identifier = context.resourceKey;
833
- const {
834
- cache,
835
- schema
836
- } = context.store;
837
-
838
- // prettier-ignore
839
- const extensions = !context.legacy ? null : schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(field, null);
840
- const proxy = new Proxy(this[SOURCE], {
841
- ownKeys() {
842
- return Object.keys(self[SOURCE]);
843
- },
844
- has(target, prop) {
845
- return prop in self[SOURCE];
846
- },
847
- getOwnPropertyDescriptor(target, prop) {
848
- return {
849
- writable: context.editable,
850
- enumerable: true,
851
- configurable: true
852
- };
853
- },
854
- get(target, prop, receiver) {
855
- if (ObjectSymbols.has(prop)) {
856
- return self[prop];
857
- }
858
- if (prop === Symbol.toPrimitive) {
859
- return () => null;
860
- }
861
- if (prop === Symbol.toStringTag) {
862
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
863
- }
864
- if (prop === 'constructor') {
865
- return Object;
866
- }
867
- if (prop === 'toString') {
868
- return function () {
869
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
870
- };
871
- }
872
- if (prop === 'toHTML') {
873
- return function () {
874
- return '<span>ManagedObject</span>';
875
- };
876
- }
877
- if (_SIGNAL.isStale) {
878
- _SIGNAL.isStale = false;
879
- let newData = cache.getAttr(identifier, path);
880
- if (newData && newData !== self[SOURCE]) {
881
- if (field.type) {
882
- const transform = schema.transformation(field);
883
- newData = transform.hydrate(newData, field.options ?? null, context.record);
884
- }
885
- self[SOURCE] = Object.assign({}, newData); // Add type assertion for newData
886
- }
887
- }
888
-
889
- // toJSON and extensions need to come after we update data if stale
890
- if (prop === 'toJSON') {
891
- return function () {
892
- return structuredClone(self[SOURCE]);
893
- };
894
- }
895
-
896
- // we always defer to data before extensions
897
- if (prop in self[SOURCE]) {
898
- consumeInternalSignal(_SIGNAL);
899
- return self[SOURCE][prop];
900
- }
901
- if (isExtensionProp(extensions, prop)) {
902
- return performObjectExtensionGet(receiver, extensions, signals, prop);
903
- }
904
- return Reflect.get(target, prop, receiver);
905
- },
906
- set(target, prop, value, receiver) {
907
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
908
- if (!test) {
909
- throw new Error(`Cannot set read-only property '${String(prop)}' on ManagedObject`);
910
- }
911
- })(context.editable) : {};
912
-
913
- // since objects function as dictionaries, we can't defer to schema/data before extensions
914
- // unless the prop is in the existing data.
915
- if (!(prop in self[SOURCE]) && isExtensionProp(extensions, prop)) {
916
- return performExtensionSet(receiver, extensions, signals, prop, value);
917
- }
918
- const reflect = Reflect.set(target, prop, value, receiver);
919
- if (!reflect) {
920
- return false;
921
- }
922
- if (!field.type) {
923
- cache.setAttr(identifier, path, self[SOURCE]);
924
- } else {
925
- const transform = schema.transformation(field);
926
- const val = transform.serialize(self[SOURCE], field.options ?? null, context.record);
927
- cache.setAttr(identifier, path, val);
928
- }
929
- _SIGNAL.isStale = true;
930
- return true;
931
- }
932
- });
933
- return proxy;
934
- }
935
- }
936
- const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
937
- function peekManagedObject(record, field) {
938
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
939
- if (managedObjectMapForRecord) {
940
- return managedObjectMapForRecord.get(field.name);
941
- }
942
- }
943
- function getObjectField(context) {
944
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
945
- const {
946
- record,
947
- field
948
- } = context;
949
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
950
- let managedObject;
951
- if (managedObjectMapForRecord) {
952
- managedObject = managedObjectMapForRecord.get(field.name);
953
- }
954
- if (managedObject) {
955
- return managedObject;
956
- } else {
957
- const {
958
- store,
959
- resourceKey,
960
- path
961
- } = context;
962
- const {
963
- cache,
964
- schema
965
- } = store;
966
- let rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
967
- if (!rawValue) {
968
- return null;
969
- }
970
- if (field.type) {
971
- const transform = schema.transformation(field);
972
- rawValue = transform.hydrate(rawValue, field.options ?? null, record);
973
- }
974
- managedObject = new ManagedObject({
975
- store,
976
- resourceKey,
977
- modeName: context.modeName,
978
- legacy: context.legacy,
979
- editable: context.editable,
980
- path,
981
- field,
982
- record,
983
- signals: context.signals,
984
- value: rawValue
985
- });
986
- if (!managedObjectMapForRecord) {
987
- ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
988
- } else {
989
- managedObjectMapForRecord.set(field.name, managedObject);
990
- }
991
- }
992
- return managedObject;
993
- }
994
- function setObjectField(context) {
995
- const {
996
- field,
997
- value,
998
- record
999
- } = context;
1000
- const {
1001
- cache,
1002
- schema
1003
- } = context.store;
1004
- if (!field.type) {
1005
- let newValue = value;
1006
- if (value !== null) {
1007
- newValue = {
1008
- ...value
1009
- };
1010
- } else {
1011
- ManagedObjectMap.delete(record);
1012
- }
1013
- cache.setAttr(context.resourceKey, context.path, newValue);
1014
- const peeked = peekManagedObject(record, field);
1015
- if (peeked) {
1016
- const objSignal = peeked[OBJECT_SIGNAL];
1017
- objSignal.isStale = true;
1018
- }
1019
- return true;
1020
- }
1021
- const transform = schema.transformation(field);
1022
- const rawValue = transform.serialize({
1023
- ...value
1024
- }, field.options ?? null, record);
1025
- cache.setAttr(context.resourceKey, context.path, rawValue);
1026
- const peeked = peekManagedObject(record, field);
1027
- if (peeked) {
1028
- const objSignal = peeked[OBJECT_SIGNAL];
1029
- objSignal.isStale = true;
1030
- }
1031
- return true;
1032
- }
1033
-
1034
- // TODO probably this should just be a Document
1035
- // but its separate until we work out the lid situation
1036
- class ResourceRelationship {
1037
- constructor(context) {
1038
- const {
1039
- store,
1040
- resourceKey
1041
- } = context;
1042
- const {
1043
- cache
1044
- } = store;
1045
- const name = getFieldCacheKeyStrict(context.field);
1046
- const rawValue = context.editable ? cache.getRelationship(resourceKey, name) : cache.getRemoteRelationship(resourceKey, name);
1047
-
1048
- // TODO setup true lids for relationship documents
1049
- // @ts-expect-error we need to give relationship documents a lid
1050
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1051
- this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${resourceKey.lid}.${name}`;
1052
- this.data = rawValue.data ? store.peekRecord(rawValue.data) : null;
1053
- this.name = name;
1054
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
1055
- this.links = Object.freeze(Object.assign({}, rawValue.links));
1056
- this.meta = Object.freeze(Object.assign({}, rawValue.meta));
1057
- } else {
1058
- this.links = rawValue.links ?? {};
1059
- this.meta = rawValue.meta ?? {};
1060
- }
1061
- this[RecordStore] = store;
1062
- this[Parent] = context.record;
1063
- }
1064
- fetch(options) {
1065
- const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
1066
- if (!url) {
1067
- throw new Error(`Cannot ${options?.method ?? 'fetch'} ${this[Parent][Identifier].type}.${String(this.name)} because it has no related link`);
1068
- }
1069
- const request = Object.assign({
1070
- url,
1071
- method: 'GET'
1072
- }, options);
1073
- return this[RecordStore].request(request);
1074
- }
1075
- }
1076
- defineSignal(ResourceRelationship.prototype, 'data', null);
1077
- defineSignal(ResourceRelationship.prototype, 'links', null);
1078
- defineSignal(ResourceRelationship.prototype, 'meta', null);
1079
- function getHref(link) {
1080
- if (!link) {
1081
- return null;
1082
- }
1083
- if (typeof link === 'string') {
1084
- return link;
1085
- }
1086
- return link.href;
1087
- }
1088
- function getResourceField(context) {
1089
- entangleSignal(context.signals, context.record, context.path.at(-1), null);
1090
- return new ResourceRelationship(context);
1091
- }
1092
- function setResourceField(context) {
1093
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1094
- {
1095
- throw new Error(`setting resource relationships is not yet supported`);
1096
- }
1097
- })() : {};
1098
- return false;
1099
- }
1100
- function setSchemaArrayField(context) {
1101
- const arrayValue = context.value?.slice();
1102
- const fieldSignal = peekInternalSignal(context.signals, context.path.at(-1));
1103
- const peeked = fieldSignal?.value;
1104
- context.store.cache.setAttr(context.resourceKey, context.path, arrayValue);
1105
- if (peeked) {
1106
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1107
- if (!test) {
1108
- throw new Error(`Expected the peekManagedArray for ${context.field.kind} to return a ManagedArray`);
1109
- }
1110
- })(ARRAY_SIGNAL in peeked) : {};
1111
- const arrSignal = peeked[ARRAY_SIGNAL];
1112
- arrSignal.isStale = true;
1113
- if (!Array.isArray(arrayValue)) {
1114
- fieldSignal.value = null;
1115
- }
1116
- }
1117
- return true;
1118
- }
1119
- function getSchemaObjectField(context) {
1120
- const signal = entangleSignal(context.signals, context.record, context.path.at(-1), null);
1121
- const {
1122
- store,
1123
- resourceKey,
1124
- path
1125
- } = context;
1126
- const {
1127
- cache
1128
- } = store;
1129
- const rawValue = context.editable ? cache.getAttr(resourceKey, path) : cache.getRemoteAttr(resourceKey, path);
1130
- if (!rawValue) {
1131
- if (signal.value) {
1132
- const value = signal.value;
1133
- // TODO if we had idle scheduling this should be done there.
1134
- void Promise.resolve().then(() => {
1135
- value.value[Destroy]();
1136
- });
1137
- signal.value = null;
1138
- }
1139
- return null;
1140
- }
1141
- const {
1142
- field
1143
- } = context;
1144
- const {
1145
- schema
1146
- } = store;
1147
- let objectType;
1148
- if (field.options?.polymorphic) {
1149
- const typePath = field.options.type ?? 'type';
1150
- // if we are polymorphic, then context.field.options.type will
1151
- // either specify a path on the rawValue to use as the type, defaulting to "type" or
1152
- // the special string "@hash" which tells us to treat field.type as a hashFn name with which
1153
- // to calc the type.
1154
- if (typePath === '@hash') {
1155
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1156
- if (!test) {
1157
- throw new Error(`Expected the field to define a hashFn as its type`);
1158
- }
1159
- })(field.type) : {};
1160
- const hashFn = schema.hashFn({
1161
- type: field.type
1162
- });
1163
- // TODO consider if there are better options and name args we could provide.
1164
- objectType = hashFn(rawValue, null, null);
1165
- } else {
1166
- objectType = rawValue[typePath];
1167
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1168
- if (!test) {
1169
- throw new Error(`Expected the type path for the field to be a value on the raw object`);
1170
- }
1171
- })(typePath && objectType && typeof objectType === 'string') : {};
1172
- }
1173
- } else {
1174
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1175
- if (!test) {
1176
- throw new Error(`A non-polymorphic SchemaObjectField must provide a SchemaObject type in its definition`);
1177
- }
1178
- })(field.type) : {};
1179
- objectType = field.type;
1180
- }
1181
- const hashField = schema.resource({
1182
- type: objectType
1183
- }).identity;
1184
- const identity = hashField ? schema.hashFn(hashField)(rawValue, hashField.options ?? null, hashField.name) : field.name;
1185
- const cachedSchemaObject = signal.value;
1186
- if (cachedSchemaObject) {
1187
- if (cachedSchemaObject.type === objectType && cachedSchemaObject.identity === identity) {
1188
- return cachedSchemaObject.value;
1189
- } else {
1190
- // TODO if we had idle scheduling this should be done there.
1191
- void Promise.resolve().then(() => {
1192
- cachedSchemaObject.value[Destroy]();
1193
- });
1194
- }
1195
- }
1196
- const schemaObject = new ReactiveResource({
1197
- store: context.store,
1198
- resourceKey: context.resourceKey,
1199
- modeName: context.modeName,
1200
- legacy: context.legacy,
1201
- editable: context.editable,
1202
- path: context.path,
1203
- field: context.field,
1204
- value: objectType
1205
- });
1206
- signal.value = {
1207
- type: objectType,
1208
- identity: identity,
1209
- value: schemaObject
1210
- };
1211
- return schemaObject;
1212
- }
1213
- function setSchemaObjectField(context) {
1214
- const {
1215
- store,
1216
- value
1217
- } = context;
1218
- let newValue = value;
1219
- if (value !== null) {
1220
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1221
- if (!test) {
1222
- throw new Error(`Expected value to be an object`);
1223
- }
1224
- })(typeof value === 'object') : {};
1225
- newValue = {
1226
- ...value
1227
- };
1228
- // FIXME the case of field.type to string here is likely incorrect
1229
- const schemaFields = store.schema.fields({
1230
- type: context.field.type
1231
- });
1232
- for (const key of Object.keys(newValue)) {
1233
- if (!schemaFields.has(key)) {
1234
- throw new Error(`Field ${key} does not exist on schema object ${context.field.type}`);
1235
- }
1236
- }
1237
- } else {
1238
- ManagedObjectMap.delete(context.record);
1239
- }
1240
- store.cache.setAttr(context.resourceKey, context.path, newValue);
1241
- // const peeked = peekManagedObject(self, field);
1242
- // if (peeked) {
1243
- // const objSignal = peeked[OBJECT_SIGNAL];
1244
- // objSignal.isStale = true;
1245
- // }
1246
- return true;
1247
- }
1248
- const DefaultMode = {
1249
- '@hash': {
1250
- get: getHashField,
1251
- set: setHashField,
1252
- mutable: false,
1253
- enumerable: false,
1254
- serializable: false
1255
- },
1256
- '@id': {
1257
- get: getIdentityField,
1258
- set: setIdentityField,
1259
- mutable: true,
1260
- enumerable: true,
1261
- serializable: true
1262
- },
1263
- '@local': {
1264
- get: getLocalField,
1265
- set: setLocalField,
1266
- mutable: true,
1267
- enumerable: false,
1268
- serializable: false
1269
- },
1270
- alias: {
1271
- get: getAliasField,
1272
- set: setAliasField,
1273
- mutable: true,
1274
- enumerable: true,
1275
- serializable: false
1276
- },
1277
- array: {
1278
- get: getArrayField,
1279
- set: setArrayField,
1280
- mutable: true,
1281
- enumerable: true,
1282
- serializable: true
1283
- },
1284
- attribute: {
1285
- get: getAttributeField,
1286
- set: setAttributeField,
1287
- mutable: true,
1288
- enumerable: true,
1289
- serializable: true
1290
- },
1291
- belongsTo: {
1292
- get: getBelongsToField,
1293
- set: setBelongsToField,
1294
- mutable: true,
1295
- enumerable: true,
1296
- serializable: true
1297
- },
1298
- collection: {
1299
- get: getCollectionField,
1300
- set: setCollectionField,
1301
- mutable: true,
1302
- enumerable: true,
1303
- serializable: true
1304
- },
1305
- derived: {
1306
- get: getDerivedField,
1307
- set: setDerivedField,
1308
- mutable: true,
1309
- enumerable: true,
1310
- serializable: false
1311
- },
1312
- field: {
1313
- get: getGenericField,
1314
- set: setGenericField,
1315
- mutable: true,
1316
- enumerable: true,
1317
- serializable: true
1318
- },
1319
- hasMany: {
1320
- get: getHasManyField,
1321
- set: setHasManyField,
1322
- mutable: true,
1323
- enumerable: true,
1324
- serializable: true
1325
- },
1326
- object: {
1327
- get: getObjectField,
1328
- set: setObjectField,
1329
- mutable: true,
1330
- enumerable: true,
1331
- serializable: true
1332
- },
1333
- resource: {
1334
- get: getResourceField,
1335
- set: setResourceField,
1336
- mutable: true,
1337
- enumerable: true,
1338
- serializable: true
1339
- },
1340
- 'schema-array': {
1341
- get: getArrayField,
1342
- set: setSchemaArrayField,
1343
- mutable: true,
1344
- enumerable: true,
1345
- serializable: true
1346
- },
1347
- 'schema-object': {
1348
- get: getSchemaObjectField,
1349
- set: setSchemaObjectField,
1350
- mutable: true,
1351
- enumerable: true,
1352
- serializable: true
1353
- }
1354
- };
1355
- const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
1356
- const symbolList = [Destroy, RecordStore, Identifier, Editable, Parent, Checkout, Legacy, EmbeddedPath, EmbeddedField];
1357
- const RecordSymbols = new Set(symbolList);
1358
- function isPathMatch(a, b) {
1359
- return a.length === b.length && a.every((v, i) => v === b[i]);
1360
- }
1361
- function isNonEnumerableProp(prop) {
1362
- return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
1363
- }
1364
- const Editables = new WeakMap();
1365
- /**
1366
- * A class that uses a the ResourceSchema for a ResourceType
1367
- * and a ResouceKey to transform data from the cache into a rich, reactive
1368
- * object.
1369
- *
1370
- * This class is not directly instantiable. To use it, you should
1371
- * configure the store's `instantiateRecord` and `teardownRecord` hooks
1372
- * with the matching hooks provided by this package.
1373
- *
1374
- * @hideconstructor
1375
- * @public
1376
- */
1377
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
1378
- class ReactiveResource {
1379
- constructor(context) {
1380
- const {
1381
- store
1382
- } = context;
1383
- const identifier = context.resourceKey;
1384
- const embeddedField = context.field;
1385
- const embeddedPath = context.path;
1386
- const isEmbedded = context.field !== null;
1387
- this[RecordStore] = store;
1388
- if (isEmbedded) {
1389
- this[Parent] = identifier;
1390
- } else {
1391
- this[Identifier] = identifier;
1392
- }
1393
- const IS_EDITABLE = this[Editable] = context.editable ?? false;
1394
- const schema = store.schema;
1395
- this[Legacy] = context.legacy ?? false;
1396
- const objectType = isEmbedded ? context.value : identifier.type;
1397
- const ResourceSchema = schema.resource(isEmbedded ? {
1398
- type: objectType
1399
- } : identifier);
1400
- const identityField = ResourceSchema.identity;
1401
- const BoundFns = new Map();
1402
-
1403
- // prettier-ignore
1404
- const extensions = !context.legacy ? null : isEmbedded ? schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(embeddedField, objectType) : schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(identifier);
1405
- this[EmbeddedField] = embeddedField;
1406
- this[EmbeddedPath] = embeddedPath;
1407
- const fields = isEmbedded ? schema.fields({
1408
- type: objectType
1409
- }) : schema.fields(identifier);
1410
- const method = typeof schema.cacheFields === 'function' ? 'cacheFields' : 'fields';
1411
- const cacheFields = isEmbedded ? schema[method]({
1412
- type: objectType
1413
- }) : schema[method](identifier);
1414
- const signals = withSignalStore(this);
1415
- const proxy = new Proxy(this, {
1416
- ownKeys() {
1417
- const identityKey = identityField?.name;
1418
- const keys = Array.from(fields.keys());
1419
- if (identityKey) {
1420
- keys.unshift(identityKey);
1421
- }
1422
- return keys;
1423
- },
1424
- has(target, prop) {
1425
- if (prop === Destroy || prop === Checkout) {
1426
- return true;
1427
- }
1428
- return fields.has(prop);
1429
- },
1430
- getOwnPropertyDescriptor(target, prop) {
1431
- const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
1432
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1433
- if (!test) {
1434
- throw new Error(`No field named ${String(prop)} on ${identifier.type}`);
1435
- }
1436
- })(schemaForField) : {};
1437
- if (isNonEnumerableProp(prop)) {
1438
- return {
1439
- writable: false,
1440
- enumerable: false,
1441
- configurable: true
1442
- };
1443
- }
1444
- switch (schemaForField.kind) {
1445
- case 'derived':
1446
- return {
1447
- writable: false,
1448
- enumerable: true,
1449
- configurable: true
1450
- };
1451
- case '@id':
1452
- return {
1453
- writable: identifier.id === null,
1454
- enumerable: true,
1455
- configurable: true
1456
- };
1457
- case '@local':
1458
- case 'field':
1459
- case 'attribute':
1460
- case 'resource':
1461
- case 'alias':
1462
- case 'belongsTo':
1463
- case 'hasMany':
1464
- case 'collection':
1465
- case 'schema-array':
1466
- case 'array':
1467
- case 'schema-object':
1468
- case 'object':
1469
- return {
1470
- writable: IS_EDITABLE,
1471
- enumerable: true,
1472
- configurable: true
1473
- };
1474
- default:
1475
- return {
1476
- writable: false,
1477
- enumerable: false,
1478
- configurable: false
1479
- };
1480
- }
1481
- },
1482
- get(target, prop, receiver) {
1483
- if (RecordSymbols.has(prop)) {
1484
- if (prop === Destroy) {
1485
- return () => _DESTROY(receiver);
1486
- }
1487
- if (prop === Checkout) {
1488
- return () => _CHECKOUT(receiver);
1489
- }
1490
- return target[prop];
1491
- }
1492
- if (prop === Signals) {
1493
- return signals;
1494
- }
1495
-
1496
- // TODO make this a symbol
1497
- if (prop === '___notifications') {
1498
- return target.___notifications;
1499
- }
1500
-
1501
- // ReactiveResource reserves use of keys that begin with these characters
1502
- // for its own usage.
1503
- // _, @, $, *
1504
-
1505
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
1506
- if (!maybeField) {
1507
- if (IgnoredGlobalFields.has(prop)) {
1508
- return undefined;
1509
- }
1510
-
1511
- /////////////////////////////////////////////////////////////
1512
- //// Note these bound function behaviors are essentially ////
1513
- //// built-in but overrideable derivations. ////
1514
- //// ////
1515
- //// The bar for this has to be "basic expectations of ////
1516
- /// an object" – very, very high ////
1517
- /////////////////////////////////////////////////////////////
1518
-
1519
- if (prop === Symbol.toStringTag || prop === 'toString') {
1520
- let fn = BoundFns.get('toString');
1521
- if (!fn) {
1522
- fn = function () {
1523
- entangleSignal(signals, receiver, '@identity', null);
1524
- return `Record<${identifier.type}:${identifier.id} (${identifier.lid})>`;
1525
- };
1526
- BoundFns.set(prop, fn);
1527
- }
1528
- return fn;
1529
- }
1530
- if (prop === 'toHTML') {
1531
- let fn = BoundFns.get('toHTML');
1532
- if (!fn) {
1533
- fn = function () {
1534
- entangleSignal(signals, receiver, '@identity', null);
1535
- return `<span>Record<${identifier.type}:${identifier.id} (${identifier.lid})></span>`;
1536
- };
1537
- BoundFns.set(prop, fn);
1538
- }
1539
- return fn;
1540
- }
1541
- if (prop === 'toJSON') {
1542
- let fn = BoundFns.get('toJSON');
1543
- if (!fn) {
1544
- fn = function () {
1545
- const json = {};
1546
- for (const key in receiver) {
1547
- json[key] = receiver[key];
1548
- }
1549
- return json;
1550
- };
1551
- BoundFns.set(prop, fn);
1552
- }
1553
- return fn;
1554
- }
1555
- if (prop === Symbol.toPrimitive) return () => null;
1556
- if (prop === Symbol.iterator) {
1557
- let fn = BoundFns.get(Symbol.iterator);
1558
- if (!fn) {
1559
- fn = function* () {
1560
- for (const key in receiver) {
1561
- yield [key, receiver[key]];
1562
- }
1563
- };
1564
- BoundFns.set(Symbol.iterator, fn);
1565
- }
1566
- return fn;
1567
- }
1568
- if (prop === 'constructor') {
1569
- return ReactiveResource;
1570
- }
1571
- if (isExtensionProp(extensions, prop)) {
1572
- return performObjectExtensionGet(receiver, extensions, signals, prop);
1573
- }
1574
-
1575
- // too many things check for random symbols
1576
- if (typeof prop === 'symbol') return undefined;
1577
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1578
- {
1579
- throw new Error(`No field named ${String(prop)} on ${isEmbedded ? embeddedField.type : identifier.type}`);
1580
- }
1581
- })() : {};
1582
- return undefined;
1583
- }
1584
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
1585
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1586
- if (!test) {
1587
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
1588
- }
1589
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
1590
- /**
1591
- * Prop Array is the path from a resource to the field including
1592
- * intermediate "links" on arrays,objects,schema-arrays and schema-objects.
1593
- *
1594
- * E.g. in the following
1595
- *
1596
- * ```
1597
- * const user = {
1598
- * addresses: [{
1599
- * street: 'Sunset Blvd',
1600
- * zip: 90210
1601
- * }]
1602
- * }
1603
- * ```
1604
- *
1605
- * The propArray for "street" is ['addresses', 0, 'street']
1606
- *
1607
- * Prop Array follows the `cache` path to the value, not the ui path.
1608
- * Thus, if `addresses` has a sourceKey of `user_addresses` and
1609
- * `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
1610
- * ['user_addresses', 0, 'zip_code']
1611
- */
1612
- const propArray = isEmbedded ? embeddedPath.slice() : [];
1613
- // we use the field.name instead of prop here because we want to use the cache-path not
1614
- // the record path.
1615
- // SAFETY: we lie as string here because if we were to get null
1616
- // we would be in a field kind that won't use the propArray below.
1617
- const fieldCacheKey = getFieldCacheKey(field);
1618
- propArray.push(fieldCacheKey);
1619
- switch (field.kind) {
1620
- case '@id':
1621
- case '@hash':
1622
- case '@local':
1623
- case 'derived':
1624
- case 'field':
1625
- case 'attribute':
1626
- case 'schema-array':
1627
- case 'array':
1628
- case 'schema-object':
1629
- case 'object':
1630
- case 'resource':
1631
- case 'belongsTo':
1632
- case 'hasMany':
1633
- case 'collection':
1634
- return DefaultMode[field.kind].get({
1635
- store,
1636
- resourceKey: identifier,
1637
- modeName: context.modeName,
1638
- legacy: context.legacy,
1639
- editable: context.editable,
1640
- path: propArray,
1641
- field: field,
1642
- record: receiver,
1643
- signals,
1644
- value: null
1645
- });
1646
- default:
1647
- assertNeverField(identifier, field, propArray);
1648
- }
1649
- },
1650
- set(target, prop, value, receiver) {
1651
- if (!IS_EDITABLE) {
1652
- const type = isEmbedded ? embeddedField.type : identifier.type;
1653
- throw new Error(`Cannot set ${String(prop)} on ${type} because the record is not editable`);
1654
- }
1655
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
1656
- if (!maybeField) {
1657
- const type = isEmbedded ? embeddedField.type : identifier.type;
1658
- if (isExtensionProp(extensions, prop)) {
1659
- return performExtensionSet(receiver, extensions, signals, prop, value);
1660
- }
1661
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1662
- {
1663
- throw new Error(`There is no settable field named ${String(prop)} on ${type}`);
1664
- }
1665
- })() : {};
1666
- return false;
1667
- }
1668
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
1669
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1670
- if (!test) {
1671
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
1672
- }
1673
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
1674
- /**
1675
- * Prop Array is the path from a resource to the field including
1676
- * intermediate "links" on arrays,objects,schema-arrays and schema-objects.
1677
- *
1678
- * E.g. in the following
1679
- *
1680
- * ```
1681
- * const user = {
1682
- * addresses: [{
1683
- * street: 'Sunset Blvd',
1684
- * zip: 90210
1685
- * }]
1686
- * }
1687
- * ```
1688
- *
1689
- * The propArray for "street" is ['addresses', 0, 'street']
1690
- *
1691
- * Prop Array follows the `cache` path to the value, not the ui path.
1692
- * Thus, if `addresses` has a sourceKey of `user_addresses` and
1693
- * `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
1694
- * ['user_addresses', 0, 'zip_code']
1695
- */
1696
- const propArray = isEmbedded ? embeddedPath.slice() : [];
1697
- // we use the field.name instead of prop here because we want to use the cache-path not
1698
- // the record path.
1699
- // SAFETY: we lie as string here because if we were to get null
1700
- // we would be in a field kind that won't use the propArray below.
1701
- const fieldCacheKey = getFieldCacheKey(field);
1702
- propArray.push(fieldCacheKey);
1703
- switch (field.kind) {
1704
- case '@id':
1705
- case '@hash':
1706
- case '@local':
1707
- case 'field':
1708
- case 'attribute':
1709
- case 'derived':
1710
- case 'array':
1711
- case 'schema-array':
1712
- case 'schema-object':
1713
- case 'object':
1714
- case 'resource':
1715
- case 'belongsTo':
1716
- case 'hasMany':
1717
- case 'collection':
1718
- return DefaultMode[field.kind].set({
1719
- store,
1720
- resourceKey: identifier,
1721
- modeName: context.modeName,
1722
- legacy: context.legacy,
1723
- editable: context.editable,
1724
- path: propArray,
1725
- field: field,
1726
- record: receiver,
1727
- signals,
1728
- value
1729
- });
1730
- default:
1731
- return assertNeverField(identifier, field, propArray);
1732
- }
1733
- }
1734
- });
1735
-
1736
- // what signal do we need for embedded record?
1737
- this.___notifications = store.notifications.subscribe(identifier, (_, type, key) => {
1738
- switch (type) {
1739
- case 'identity':
1740
- {
1741
- if (isEmbedded || !identityField) return; // base paths never apply to embedded records
1742
-
1743
- if (identityField.name && identityField.kind === '@id') {
1744
- const signal = signals.get('@identity');
1745
- if (signal) {
1746
- notifyInternalSignal(signal);
1747
- }
1748
- }
1749
- break;
1750
- }
1751
- case 'attributes':
1752
- if (key) {
1753
- if (Array.isArray(key)) {
1754
- if (!isEmbedded) return; // deep paths will be handled by embedded records
1755
- // TODO we should have the notification manager
1756
- // ensure it is safe for each callback to mutate this array
1757
- if (isPathMatch(embeddedPath, key)) {
1758
- // handle the notification
1759
- // TODO we should likely handle this notification here
1760
- // also we should add a LOGGING flag
1761
- // eslint-disable-next-line no-console
1762
- console.warn(`Notification unhandled for ${key.join(',')} on ${identifier.type}`, proxy);
1763
- return;
1764
- }
1765
-
1766
- // TODO we should add a LOGGING flag
1767
- // console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, proxy);
1768
- // deep notify the key path
1769
- } else {
1770
- if (isEmbedded) return; // base paths never apply to embedded records
1771
-
1772
- // TODO determine what LOGGING flag to wrap this in if any
1773
- // console.log(`Notification for ${key} on ${identifier.type}`, proxy);
1774
- const signal = signals.get(key);
1775
- if (signal) {
1776
- notifyInternalSignal(signal);
1777
- }
1778
- const field = cacheFields.get(key);
1779
- if (field?.kind === 'array' || field?.kind === 'schema-array') {
1780
- const peeked = signal?.value;
1781
- if (peeked) {
1782
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1783
- if (!test) {
1784
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1785
- }
1786
- })(ARRAY_SIGNAL in peeked) : {};
1787
- const arrSignal = peeked[ARRAY_SIGNAL];
1788
- notifyInternalSignal(arrSignal);
1789
- }
1790
- }
1791
- if (field?.kind === 'object') {
1792
- const peeked = peekManagedObject(proxy, field);
1793
- if (peeked) {
1794
- const objSignal = peeked[OBJECT_SIGNAL];
1795
- notifyInternalSignal(objSignal);
1796
- }
1797
- }
1798
- }
1799
- }
1800
- break;
1801
- case 'relationships':
1802
- if (key) {
1803
- if (Array.isArray(key)) ;else {
1804
- if (isEmbedded) return; // base paths never apply to embedded records
1805
-
1806
- const field = cacheFields.get(key);
1807
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1808
- if (!test) {
1809
- throw new Error(`Expected relationship ${key} to be the name of a field`);
1810
- }
1811
- })(field) : {};
1812
- if (field.kind === 'belongsTo') {
1813
- // TODO determine what LOGGING flag to wrap this in if any
1814
- // console.log(`Notification for ${key} on ${identifier.type}`, proxy);
1815
- const signal = signals.get(key);
1816
- if (signal) {
1817
- notifyInternalSignal(signal);
1818
- }
1819
- // FIXME
1820
- } else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
1821
- if (field.options.linksMode) {
1822
- const signal = signals.get(key);
1823
- if (signal) {
1824
- const peeked = signal.value;
1825
- if (peeked) {
1826
- notifyInternalSignal(peeked[ARRAY_SIGNAL]);
1827
- }
1828
- }
1829
- return;
1830
- }
1831
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1832
- if (!test) {
1833
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1834
- }
1835
- })(context.legacy) : {};
1836
- if (schema._kind('@legacy', 'hasMany').notify(store, proxy, identifier, field)) {
1837
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1838
- if (!test) {
1839
- throw new Error(`Expected options to exist on relationship meta`);
1840
- }
1841
- })(field.options) : {};
1842
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1843
- if (!test) {
1844
- throw new Error(`Expected async to exist on relationship meta options`);
1845
- }
1846
- })('async' in field.options) : {};
1847
- if (field.options.async) {
1848
- const signal = signals.get(key);
1849
- if (signal) {
1850
- notifyInternalSignal(signal);
1851
- }
1852
- }
1853
- }
1854
- } else if (field.kind === 'collection') ;
1855
- }
1856
- }
1857
- break;
1858
- }
1859
- });
1860
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
1861
- Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
1862
- enumerable: false,
1863
- configurable: true,
1864
- get() {
1865
- const data = {};
1866
- for (const key of fields.keys()) {
1867
- data[key] = proxy[key];
1868
- }
1869
- return data;
1870
- }
1871
- });
1872
- }
1873
- return proxy;
1874
- }
1875
- }
1876
- function _CHECKOUT(record) {
1877
- // IF we are already the editable record, throw an error
1878
- if (record[Editable]) {
1879
- throw new Error(`Cannot checkout an already editable record`);
1880
- }
1881
- const editable = Editables.get(record);
1882
- if (editable) {
1883
- return Promise.resolve(editable);
1884
- }
1885
- const embeddedType = record[EmbeddedField];
1886
- const embeddedPath = record[EmbeddedPath];
1887
- const isEmbedded = embeddedType !== null && embeddedPath !== null;
1888
- if (isEmbedded) {
1889
- throw new Error(`Cannot checkout an embedded record (yet)`);
1890
- }
1891
- const legacy = record[Legacy];
1892
- const editableRecord = new ReactiveResource({
1893
- store: record[RecordStore],
1894
- resourceKey: record[Identifier],
1895
- modeName: legacy ? 'legacy' : 'polaris',
1896
- legacy: legacy,
1897
- editable: true,
1898
- path: null,
1899
- field: null,
1900
- value: null
1901
- });
1902
- setRecordIdentifier(editableRecord, recordIdentifierFor(record));
1903
- return Promise.resolve(editableRecord);
1904
- }
1905
- function _DESTROY(record) {
1906
- if (record[Legacy]) {
1907
- // @ts-expect-error
1908
- record.isDestroying = true;
1909
- // @ts-expect-error
1910
- record.isDestroyed = true;
1911
- }
1912
- record[RecordStore].notifications.unsubscribe(record.___notifications);
1913
-
1914
- // FIXME we need a way to also unsubscribe all SchemaObjects when the primary
1915
- // resource is destroyed.
1916
- }
1917
- function assertNeverField(identifier, field, path) {
1918
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1919
- {
1920
- throw new Error(`Cannot use unknown field kind ${field.kind} on <${identifier.type}>.${Array.isArray(path) ? path.join('.') : path}`);
1921
- }
1922
- })() : {};
1923
- return false;
1924
- }
12
+ import { Type } from './types/symbols.js';
1925
13
  function instantiateRecord(store, identifier, createArgs) {
1926
14
  const schema = store.schema;
1927
15
  const resourceSchema = schema.resource(identifier);
@@ -1931,7 +19,7 @@ function instantiateRecord(store, identifier, createArgs) {
1931
19
  }
1932
20
  })(isResourceSchema(resourceSchema)) : {};
1933
21
  const legacy = resourceSchema?.legacy ?? false;
1934
- const editable = legacy || store.cache.isNew(identifier);
22
+ const editable = legacy;
1935
23
  const record = new ReactiveResource({
1936
24
  store,
1937
25
  resourceKey: identifier,
@@ -1942,7 +30,7 @@ function instantiateRecord(store, identifier, createArgs) {
1942
30
  field: null,
1943
31
  value: null
1944
32
  });
1945
- if (createArgs) {
33
+ if (createArgs && editable) {
1946
34
  Object.assign(record, createArgs);
1947
35
  }
1948
36
  return record;
@@ -2181,7 +269,8 @@ function withDefaults(schema) {
2181
269
  * @public
2182
270
  */
2183
271
  const fromIdentity = (record, options, key) => {
2184
- const identifier = record[Identifier];
272
+ const context = record[Context];
273
+ const identifier = context.resourceKey;
2185
274
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2186
275
  if (!test) {
2187
276
  throw new Error(`Cannot compute @identity for a record without an identifier`);
@@ -2683,4 +772,4 @@ function mergeMap(base, toApply) {
2683
772
  base.set(key, value);
2684
773
  }
2685
774
  }
2686
- export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };
775
+ export { SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };