@warp-drive/core 5.7.0-alpha.9 → 5.7.0-beta.0

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.
Files changed (103) hide show
  1. package/declarations/graph/-private/-diff.d.ts +7 -20
  2. package/declarations/graph/-private/-edge-definition.d.ts +3 -12
  3. package/declarations/graph/-private/-state.d.ts +0 -87
  4. package/declarations/graph/-private/-utils.d.ts +5 -11
  5. package/declarations/graph/-private/coerce-id.d.ts +0 -6
  6. package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +2 -14
  7. package/declarations/graph/-private/edges/collection.d.ts +10 -10
  8. package/declarations/graph/-private/edges/implicit.d.ts +5 -5
  9. package/declarations/graph/-private/edges/resource.d.ts +6 -7
  10. package/declarations/graph/-private/graph.d.ts +17 -51
  11. package/declarations/graph/-private/normalize-link.d.ts +0 -6
  12. package/declarations/graph/-private/operations/replace-related-records.d.ts +4 -59
  13. package/declarations/graph/-private/operations/update-relationship.d.ts +3 -7
  14. package/declarations/index.d.ts +1 -1
  15. package/declarations/reactive/-private/default-mode.d.ts +2 -2
  16. package/declarations/reactive/-private/document.d.ts +11 -27
  17. package/declarations/reactive/-private/fields/managed-array.d.ts +4 -6
  18. package/declarations/reactive/-private/fields/managed-object.d.ts +2 -8
  19. package/declarations/reactive/-private/fields/many-array-manager.d.ts +2 -2
  20. package/declarations/reactive/-private/hooks.d.ts +2 -2
  21. package/declarations/reactive/-private/record.d.ts +42 -30
  22. package/declarations/reactive/-private/schema.d.ts +11 -73
  23. package/declarations/reactive/-private/symbols.d.ts +2 -33
  24. package/declarations/reactive/-private.d.ts +1 -1
  25. package/declarations/reactive.d.ts +277 -1
  26. package/declarations/request/-private/context.d.ts +3 -5
  27. package/declarations/request/-private/fetch.d.ts +2 -2
  28. package/declarations/request/-private/manager.d.ts +24 -28
  29. package/declarations/request/-private/types.d.ts +22 -24
  30. package/declarations/request/-private/utils.d.ts +44 -2
  31. package/declarations/store/-private/cache-handler/handler.d.ts +2 -8
  32. package/declarations/store/-private/cache-handler/types.d.ts +10 -10
  33. package/declarations/store/-private/cache-handler/utils.d.ts +4 -5
  34. package/declarations/store/-private/caches/instance-cache.d.ts +21 -20
  35. package/declarations/store/-private/debug/utils.d.ts +1 -0
  36. package/declarations/store/-private/default-cache-policy.d.ts +25 -40
  37. package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +24 -15
  38. package/declarations/store/-private/{caches/identifier-cache.d.ts → managers/cache-key-manager.d.ts} +35 -53
  39. package/declarations/store/-private/managers/cache-manager.d.ts +46 -111
  40. package/declarations/store/-private/managers/notification-manager.d.ts +30 -45
  41. package/declarations/store/-private/managers/record-array-manager.d.ts +44 -41
  42. package/declarations/store/-private/network/request-cache.d.ts +21 -25
  43. package/declarations/store/-private/new-core-tmp/expensive-subscription.d.ts +24 -0
  44. package/declarations/store/-private/new-core-tmp/reactivity/configure.d.ts +3 -41
  45. package/declarations/store/-private/new-core-tmp/reactivity/internal.d.ts +14 -29
  46. package/declarations/store/-private/new-core-tmp/reactivity/signal.d.ts +24 -3
  47. package/declarations/store/-private/new-core-tmp/request-state.d.ts +132 -37
  48. package/declarations/store/-private/new-core-tmp/request-subscription.d.ts +51 -135
  49. package/declarations/store/-private/record-arrays/-utils.d.ts +80 -0
  50. package/declarations/store/-private/record-arrays/legacy-live-array.d.ts +81 -0
  51. package/declarations/store/-private/record-arrays/legacy-many-array.d.ts +133 -0
  52. package/declarations/store/-private/record-arrays/legacy-query.d.ts +81 -0
  53. package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +1 -124
  54. package/declarations/store/-private/record-arrays/resource-array.d.ts +67 -0
  55. package/declarations/store/-private/store-service.d.ts +156 -106
  56. package/declarations/store/-private/utils/coerce-id.d.ts +0 -6
  57. package/declarations/store/-private.d.ts +11 -13
  58. package/declarations/store/-types/q/cache-capabilities-manager.d.ts +15 -24
  59. package/declarations/store/-types/q/identifier.d.ts +9 -6
  60. package/declarations/store/-types/q/record-instance.d.ts +0 -1
  61. package/declarations/store/-types/q/schema-service.d.ts +9 -9
  62. package/declarations/store/-types/q/store.d.ts +6 -7
  63. package/declarations/store/deprecated/-private.d.ts +12 -24
  64. package/declarations/store/deprecated/store.d.ts +11 -16
  65. package/declarations/types/-private.d.ts +1 -1
  66. package/declarations/types/cache/aliases.d.ts +0 -11
  67. package/declarations/types/cache/change.d.ts +2 -2
  68. package/declarations/types/cache/mutations.d.ts +13 -37
  69. package/declarations/types/cache/operations.d.ts +115 -32
  70. package/declarations/types/cache/relationship.d.ts +4 -7
  71. package/declarations/types/cache.d.ts +51 -125
  72. package/declarations/types/graph.d.ts +12 -12
  73. package/declarations/types/identifier.d.ts +52 -78
  74. package/declarations/types/params.d.ts +2 -3
  75. package/declarations/types/request.d.ts +66 -42
  76. package/declarations/types/schema/concepts.d.ts +2 -2
  77. package/declarations/types/schema/fields.d.ts +30 -3
  78. package/declarations/types/spec/document.d.ts +6 -10
  79. package/declarations/types/spec/json-api-raw.d.ts +6 -9
  80. package/declarations/types.d.ts +0 -1
  81. package/declarations/utils/string.d.ts +2 -3
  82. package/dist/{configure-B48bFHOl.js → configure-C3x8YXzL.js} +5 -5
  83. package/dist/configure.js +1 -1
  84. package/dist/{context-COmAnXUQ.js → context-C_7OLieY.js} +48 -6
  85. package/dist/graph/-private.js +137 -144
  86. package/dist/index.js +25 -14
  87. package/dist/reactive/-private.js +1 -1
  88. package/dist/reactive.js +144 -1926
  89. package/dist/{request-state-CeN66aML.js → request-state-C955e0AL.js} +5968 -3033
  90. package/dist/request.js +1 -1
  91. package/dist/store/-private.js +2 -3
  92. package/dist/store.js +32 -44
  93. package/dist/{symbols-SIstXMLI.js → symbols-sql1_mdx.js} +3 -8
  94. package/dist/types/-private.js +1 -1
  95. package/dist/types/identifier.js +19 -45
  96. package/dist/types/request.js +45 -3
  97. package/dist/types/schema/fields.js +6 -0
  98. package/dist/utils/string.js +2 -2
  99. package/package.json +11 -11
  100. package/declarations/store/-private/caches/cache-utils.d.ts +0 -12
  101. package/declarations/store/-private/record-arrays/identifier-array.d.ts +0 -147
  102. package/declarations/store/-private/record-arrays/many-array.d.ts +0 -197
  103. package/dist/handler-SdXlte1w.js +0 -339
package/dist/reactive.js CHANGED
@@ -1,1926 +1,16 @@
1
+ import { L as ReactiveResource, M as isNonIdentityCacheableField, N as getFieldCacheKeyStrict, r as recordIdentifierFor, H as withSignalStore, G as createInternalMemo } from "./request-state-C955e0AL.js";
2
+ export { O as checkout, P as commit } from "./request-state-C955e0AL.js";
1
3
  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';
4
+ import { D as Destroy, C as Context } from "./symbols-sql1_mdx.js";
5
+ export { a as Checkout } from "./symbols-sql1_mdx.js";
4
6
  import { macroCondition, getGlobalConfig } from '@embroider/macros';
5
7
  import { warn, deprecate } from '@ember/debug';
8
+ import './index.js';
9
+ import './types/request.js';
6
10
  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";
11
+ import "./configure-C3x8YXzL.js";
10
12
  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
- const schemaFields = store.schema.fields({
1229
- type: context.field.type
1230
- });
1231
- for (const key of Object.keys(newValue)) {
1232
- if (!schemaFields.has(key)) {
1233
- throw new Error(`Field ${key} does not exist on schema object ${context.field.type}`);
1234
- }
1235
- }
1236
- } else {
1237
- ManagedObjectMap.delete(context.record);
1238
- }
1239
- store.cache.setAttr(context.resourceKey, context.path, newValue);
1240
- // const peeked = peekManagedObject(self, field);
1241
- // if (peeked) {
1242
- // const objSignal = peeked[OBJECT_SIGNAL];
1243
- // objSignal.isStale = true;
1244
- // }
1245
- return true;
1246
- }
1247
- const DefaultMode = {
1248
- '@hash': {
1249
- get: getHashField,
1250
- set: setHashField,
1251
- mutable: false,
1252
- enumerable: false,
1253
- serializable: false
1254
- },
1255
- '@id': {
1256
- get: getIdentityField,
1257
- set: setIdentityField,
1258
- mutable: true,
1259
- enumerable: true,
1260
- serializable: true
1261
- },
1262
- '@local': {
1263
- get: getLocalField,
1264
- set: setLocalField,
1265
- mutable: true,
1266
- enumerable: false,
1267
- serializable: false
1268
- },
1269
- alias: {
1270
- get: getAliasField,
1271
- set: setAliasField,
1272
- mutable: true,
1273
- enumerable: true,
1274
- serializable: false
1275
- },
1276
- array: {
1277
- get: getArrayField,
1278
- set: setArrayField,
1279
- mutable: true,
1280
- enumerable: true,
1281
- serializable: true
1282
- },
1283
- attribute: {
1284
- get: getAttributeField,
1285
- set: setAttributeField,
1286
- mutable: true,
1287
- enumerable: true,
1288
- serializable: true
1289
- },
1290
- belongsTo: {
1291
- get: getBelongsToField,
1292
- set: setBelongsToField,
1293
- mutable: true,
1294
- enumerable: true,
1295
- serializable: true
1296
- },
1297
- collection: {
1298
- get: getCollectionField,
1299
- set: setCollectionField,
1300
- mutable: true,
1301
- enumerable: true,
1302
- serializable: true
1303
- },
1304
- derived: {
1305
- get: getDerivedField,
1306
- set: setDerivedField,
1307
- mutable: true,
1308
- enumerable: true,
1309
- serializable: false
1310
- },
1311
- field: {
1312
- get: getGenericField,
1313
- set: setGenericField,
1314
- mutable: true,
1315
- enumerable: true,
1316
- serializable: true
1317
- },
1318
- hasMany: {
1319
- get: getHasManyField,
1320
- set: setHasManyField,
1321
- mutable: true,
1322
- enumerable: true,
1323
- serializable: true
1324
- },
1325
- object: {
1326
- get: getObjectField,
1327
- set: setObjectField,
1328
- mutable: true,
1329
- enumerable: true,
1330
- serializable: true
1331
- },
1332
- resource: {
1333
- get: getResourceField,
1334
- set: setResourceField,
1335
- mutable: true,
1336
- enumerable: true,
1337
- serializable: true
1338
- },
1339
- 'schema-array': {
1340
- get: getArrayField,
1341
- set: setSchemaArrayField,
1342
- mutable: true,
1343
- enumerable: true,
1344
- serializable: true
1345
- },
1346
- 'schema-object': {
1347
- get: getSchemaObjectField,
1348
- set: setSchemaObjectField,
1349
- mutable: true,
1350
- enumerable: true,
1351
- serializable: true
1352
- }
1353
- };
1354
- const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
1355
- const symbolList = [Destroy, RecordStore, Identifier, Editable, Parent, Checkout, Legacy, EmbeddedPath, EmbeddedField];
1356
- const RecordSymbols = new Set(symbolList);
1357
- function isPathMatch(a, b) {
1358
- return a.length === b.length && a.every((v, i) => v === b[i]);
1359
- }
1360
- function isNonEnumerableProp(prop) {
1361
- return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
1362
- }
1363
- const Editables = new WeakMap();
1364
- /**
1365
- * A class that uses a the ResourceSchema for a ResourceType
1366
- * and a ResouceKey to transform data from the cache into a rich, reactive
1367
- * object.
1368
- *
1369
- * This class is not directly instantiable. To use it, you should
1370
- * configure the store's `instantiateRecord` and `teardownRecord` hooks
1371
- * with the matching hooks provided by this package.
1372
- *
1373
- * @hideconstructor
1374
- * @public
1375
- */
1376
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
1377
- class ReactiveResource {
1378
- constructor(context) {
1379
- const {
1380
- store
1381
- } = context;
1382
- const identifier = context.resourceKey;
1383
- const embeddedField = context.field;
1384
- const embeddedPath = context.path;
1385
- const isEmbedded = context.field !== null;
1386
- this[RecordStore] = store;
1387
- if (isEmbedded) {
1388
- this[Parent] = identifier;
1389
- } else {
1390
- this[Identifier] = identifier;
1391
- }
1392
- const IS_EDITABLE = this[Editable] = context.editable ?? false;
1393
- const schema = store.schema;
1394
- this[Legacy] = context.legacy ?? false;
1395
- const objectType = isEmbedded ? context.value : identifier.type;
1396
- const ResourceSchema = schema.resource(isEmbedded ? {
1397
- type: objectType
1398
- } : identifier);
1399
- const identityField = ResourceSchema.identity;
1400
- const BoundFns = new Map();
1401
-
1402
- // prettier-ignore
1403
- const extensions = !context.legacy ? null : isEmbedded ? schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(embeddedField, objectType) : schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(identifier);
1404
- this[EmbeddedField] = embeddedField;
1405
- this[EmbeddedPath] = embeddedPath;
1406
- const fields = isEmbedded ? schema.fields({
1407
- type: objectType
1408
- }) : schema.fields(identifier);
1409
- const method = typeof schema.cacheFields === 'function' ? 'cacheFields' : 'fields';
1410
- const cacheFields = isEmbedded ? schema[method]({
1411
- type: objectType
1412
- }) : schema[method](identifier);
1413
- const signals = withSignalStore(this);
1414
- const proxy = new Proxy(this, {
1415
- ownKeys() {
1416
- const identityKey = identityField?.name;
1417
- const keys = Array.from(fields.keys());
1418
- if (identityKey) {
1419
- keys.unshift(identityKey);
1420
- }
1421
- return keys;
1422
- },
1423
- has(target, prop) {
1424
- if (prop === Destroy || prop === Checkout) {
1425
- return true;
1426
- }
1427
- return fields.has(prop);
1428
- },
1429
- getOwnPropertyDescriptor(target, prop) {
1430
- const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
1431
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1432
- if (!test) {
1433
- throw new Error(`No field named ${String(prop)} on ${identifier.type}`);
1434
- }
1435
- })(schemaForField) : {};
1436
- if (isNonEnumerableProp(prop)) {
1437
- return {
1438
- writable: false,
1439
- enumerable: false,
1440
- configurable: true
1441
- };
1442
- }
1443
- switch (schemaForField.kind) {
1444
- case 'derived':
1445
- return {
1446
- writable: false,
1447
- enumerable: true,
1448
- configurable: true
1449
- };
1450
- case '@id':
1451
- return {
1452
- writable: identifier.id === null,
1453
- enumerable: true,
1454
- configurable: true
1455
- };
1456
- case '@local':
1457
- case 'field':
1458
- case 'attribute':
1459
- case 'resource':
1460
- case 'alias':
1461
- case 'belongsTo':
1462
- case 'hasMany':
1463
- case 'collection':
1464
- case 'schema-array':
1465
- case 'array':
1466
- case 'schema-object':
1467
- case 'object':
1468
- return {
1469
- writable: IS_EDITABLE,
1470
- enumerable: true,
1471
- configurable: true
1472
- };
1473
- default:
1474
- return {
1475
- writable: false,
1476
- enumerable: false,
1477
- configurable: false
1478
- };
1479
- }
1480
- },
1481
- get(target, prop, receiver) {
1482
- if (RecordSymbols.has(prop)) {
1483
- if (prop === Destroy) {
1484
- return () => _DESTROY(receiver);
1485
- }
1486
- if (prop === Checkout) {
1487
- return () => _CHECKOUT(receiver);
1488
- }
1489
- return target[prop];
1490
- }
1491
- if (prop === Signals) {
1492
- return signals;
1493
- }
1494
-
1495
- // TODO make this a symbol
1496
- if (prop === '___notifications') {
1497
- return target.___notifications;
1498
- }
1499
-
1500
- // ReactiveResource reserves use of keys that begin with these characters
1501
- // for its own usage.
1502
- // _, @, $, *
1503
-
1504
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
1505
- if (!maybeField) {
1506
- if (IgnoredGlobalFields.has(prop)) {
1507
- return undefined;
1508
- }
1509
-
1510
- /////////////////////////////////////////////////////////////
1511
- //// Note these bound function behaviors are essentially ////
1512
- //// built-in but overrideable derivations. ////
1513
- //// ////
1514
- //// The bar for this has to be "basic expectations of ////
1515
- /// an object" – very, very high ////
1516
- /////////////////////////////////////////////////////////////
1517
-
1518
- if (prop === Symbol.toStringTag || prop === 'toString') {
1519
- let fn = BoundFns.get('toString');
1520
- if (!fn) {
1521
- fn = function () {
1522
- entangleSignal(signals, receiver, '@identity', null);
1523
- return `Record<${identifier.type}:${identifier.id} (${identifier.lid})>`;
1524
- };
1525
- BoundFns.set(prop, fn);
1526
- }
1527
- return fn;
1528
- }
1529
- if (prop === 'toHTML') {
1530
- let fn = BoundFns.get('toHTML');
1531
- if (!fn) {
1532
- fn = function () {
1533
- entangleSignal(signals, receiver, '@identity', null);
1534
- return `<span>Record<${identifier.type}:${identifier.id} (${identifier.lid})></span>`;
1535
- };
1536
- BoundFns.set(prop, fn);
1537
- }
1538
- return fn;
1539
- }
1540
- if (prop === 'toJSON') {
1541
- let fn = BoundFns.get('toJSON');
1542
- if (!fn) {
1543
- fn = function () {
1544
- const json = {};
1545
- for (const key in receiver) {
1546
- json[key] = receiver[key];
1547
- }
1548
- return json;
1549
- };
1550
- BoundFns.set(prop, fn);
1551
- }
1552
- return fn;
1553
- }
1554
- if (prop === Symbol.toPrimitive) return () => null;
1555
- if (prop === Symbol.iterator) {
1556
- let fn = BoundFns.get(Symbol.iterator);
1557
- if (!fn) {
1558
- fn = function* () {
1559
- for (const key in receiver) {
1560
- yield [key, receiver[key]];
1561
- }
1562
- };
1563
- BoundFns.set(Symbol.iterator, fn);
1564
- }
1565
- return fn;
1566
- }
1567
- if (prop === 'constructor') {
1568
- return ReactiveResource;
1569
- }
1570
- if (isExtensionProp(extensions, prop)) {
1571
- return performObjectExtensionGet(receiver, extensions, signals, prop);
1572
- }
1573
-
1574
- // too many things check for random symbols
1575
- if (typeof prop === 'symbol') return undefined;
1576
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1577
- {
1578
- throw new Error(`No field named ${String(prop)} on ${isEmbedded ? embeddedField.type : identifier.type}`);
1579
- }
1580
- })() : {};
1581
- return undefined;
1582
- }
1583
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
1584
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1585
- if (!test) {
1586
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
1587
- }
1588
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
1589
- /**
1590
- * Prop Array is the path from a resource to the field including
1591
- * intermediate "links" on arrays,objects,schema-arrays and schema-objects.
1592
- *
1593
- * E.g. in the following
1594
- *
1595
- * ```
1596
- * const user = {
1597
- * addresses: [{
1598
- * street: 'Sunset Blvd',
1599
- * zip: 90210
1600
- * }]
1601
- * }
1602
- * ```
1603
- *
1604
- * The propArray for "street" is ['addresses', 0, 'street']
1605
- *
1606
- * Prop Array follows the `cache` path to the value, not the ui path.
1607
- * Thus, if `addresses` has a sourceKey of `user_addresses` and
1608
- * `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
1609
- * ['user_addresses', 0, 'zip_code']
1610
- */
1611
- const propArray = isEmbedded ? embeddedPath.slice() : [];
1612
- // we use the field.name instead of prop here because we want to use the cache-path not
1613
- // the record path.
1614
- // SAFETY: we lie as string here because if we were to get null
1615
- // we would be in a field kind that won't use the propArray below.
1616
- const fieldCacheKey = getFieldCacheKey(field);
1617
- propArray.push(fieldCacheKey);
1618
- switch (field.kind) {
1619
- case '@id':
1620
- case '@hash':
1621
- case '@local':
1622
- case 'derived':
1623
- case 'field':
1624
- case 'attribute':
1625
- case 'schema-array':
1626
- case 'array':
1627
- case 'schema-object':
1628
- case 'object':
1629
- case 'resource':
1630
- case 'belongsTo':
1631
- case 'hasMany':
1632
- case 'collection':
1633
- return DefaultMode[field.kind].get({
1634
- store,
1635
- resourceKey: identifier,
1636
- modeName: context.modeName,
1637
- legacy: context.legacy,
1638
- editable: context.editable,
1639
- path: propArray,
1640
- field: field,
1641
- record: receiver,
1642
- signals,
1643
- value: null
1644
- });
1645
- default:
1646
- assertNeverField(identifier, field, propArray);
1647
- }
1648
- },
1649
- set(target, prop, value, receiver) {
1650
- if (!IS_EDITABLE) {
1651
- const type = isEmbedded ? embeddedField.type : identifier.type;
1652
- throw new Error(`Cannot set ${String(prop)} on ${type} because the record is not editable`);
1653
- }
1654
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
1655
- if (!maybeField) {
1656
- const type = isEmbedded ? embeddedField.type : identifier.type;
1657
- if (isExtensionProp(extensions, prop)) {
1658
- return performExtensionSet(receiver, extensions, signals, prop, value);
1659
- }
1660
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1661
- {
1662
- throw new Error(`There is no settable field named ${String(prop)} on ${type}`);
1663
- }
1664
- })() : {};
1665
- return false;
1666
- }
1667
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
1668
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1669
- if (!test) {
1670
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
1671
- }
1672
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
1673
- /**
1674
- * Prop Array is the path from a resource to the field including
1675
- * intermediate "links" on arrays,objects,schema-arrays and schema-objects.
1676
- *
1677
- * E.g. in the following
1678
- *
1679
- * ```
1680
- * const user = {
1681
- * addresses: [{
1682
- * street: 'Sunset Blvd',
1683
- * zip: 90210
1684
- * }]
1685
- * }
1686
- * ```
1687
- *
1688
- * The propArray for "street" is ['addresses', 0, 'street']
1689
- *
1690
- * Prop Array follows the `cache` path to the value, not the ui path.
1691
- * Thus, if `addresses` has a sourceKey of `user_addresses` and
1692
- * `zip` has a sourceKey of `zip_code` then the propArray for "zip" is
1693
- * ['user_addresses', 0, 'zip_code']
1694
- */
1695
- const propArray = isEmbedded ? embeddedPath.slice() : [];
1696
- // we use the field.name instead of prop here because we want to use the cache-path not
1697
- // the record path.
1698
- // SAFETY: we lie as string here because if we were to get null
1699
- // we would be in a field kind that won't use the propArray below.
1700
- const fieldCacheKey = getFieldCacheKey(field);
1701
- propArray.push(fieldCacheKey);
1702
- switch (field.kind) {
1703
- case '@id':
1704
- case '@hash':
1705
- case '@local':
1706
- case 'field':
1707
- case 'attribute':
1708
- case 'derived':
1709
- case 'array':
1710
- case 'schema-array':
1711
- case 'schema-object':
1712
- case 'object':
1713
- case 'resource':
1714
- case 'belongsTo':
1715
- case 'hasMany':
1716
- case 'collection':
1717
- return DefaultMode[field.kind].set({
1718
- store,
1719
- resourceKey: identifier,
1720
- modeName: context.modeName,
1721
- legacy: context.legacy,
1722
- editable: context.editable,
1723
- path: propArray,
1724
- field: field,
1725
- record: receiver,
1726
- signals,
1727
- value
1728
- });
1729
- default:
1730
- return assertNeverField(identifier, field, propArray);
1731
- }
1732
- }
1733
- });
1734
-
1735
- // what signal do we need for embedded record?
1736
- this.___notifications = store.notifications.subscribe(identifier, (_, type, key) => {
1737
- switch (type) {
1738
- case 'identity':
1739
- {
1740
- if (isEmbedded || !identityField) return; // base paths never apply to embedded records
1741
-
1742
- if (identityField.name && identityField.kind === '@id') {
1743
- const signal = signals.get('@identity');
1744
- if (signal) {
1745
- notifyInternalSignal(signal);
1746
- }
1747
- }
1748
- break;
1749
- }
1750
- case 'attributes':
1751
- if (key) {
1752
- if (Array.isArray(key)) {
1753
- if (!isEmbedded) return; // deep paths will be handled by embedded records
1754
- // TODO we should have the notification manager
1755
- // ensure it is safe for each callback to mutate this array
1756
- if (isPathMatch(embeddedPath, key)) {
1757
- // handle the notification
1758
- // TODO we should likely handle this notification here
1759
- // also we should add a LOGGING flag
1760
- // eslint-disable-next-line no-console
1761
- console.warn(`Notification unhandled for ${key.join(',')} on ${identifier.type}`, proxy);
1762
- return;
1763
- }
1764
-
1765
- // TODO we should add a LOGGING flag
1766
- // console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, proxy);
1767
- // deep notify the key path
1768
- } else {
1769
- if (isEmbedded) return; // base paths never apply to embedded records
1770
-
1771
- // TODO determine what LOGGING flag to wrap this in if any
1772
- // console.log(`Notification for ${key} on ${identifier.type}`, proxy);
1773
- const signal = signals.get(key);
1774
- if (signal) {
1775
- notifyInternalSignal(signal);
1776
- }
1777
- const field = cacheFields.get(key);
1778
- if (field?.kind === 'array' || field?.kind === 'schema-array') {
1779
- const peeked = signal?.value;
1780
- if (peeked) {
1781
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1782
- if (!test) {
1783
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1784
- }
1785
- })(ARRAY_SIGNAL in peeked) : {};
1786
- const arrSignal = peeked[ARRAY_SIGNAL];
1787
- notifyInternalSignal(arrSignal);
1788
- }
1789
- }
1790
- if (field?.kind === 'object') {
1791
- const peeked = peekManagedObject(proxy, field);
1792
- if (peeked) {
1793
- const objSignal = peeked[OBJECT_SIGNAL];
1794
- notifyInternalSignal(objSignal);
1795
- }
1796
- }
1797
- }
1798
- }
1799
- break;
1800
- case 'relationships':
1801
- if (key) {
1802
- if (Array.isArray(key)) ;else {
1803
- if (isEmbedded) return; // base paths never apply to embedded records
1804
-
1805
- const field = cacheFields.get(key);
1806
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1807
- if (!test) {
1808
- throw new Error(`Expected relationship ${key} to be the name of a field`);
1809
- }
1810
- })(field) : {};
1811
- if (field.kind === 'belongsTo') {
1812
- // TODO determine what LOGGING flag to wrap this in if any
1813
- // console.log(`Notification for ${key} on ${identifier.type}`, proxy);
1814
- const signal = signals.get(key);
1815
- if (signal) {
1816
- notifyInternalSignal(signal);
1817
- }
1818
- // FIXME
1819
- } else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
1820
- if (field.options.linksMode) {
1821
- const signal = signals.get(key);
1822
- if (signal) {
1823
- const peeked = signal.value;
1824
- if (peeked) {
1825
- notifyInternalSignal(peeked[ARRAY_SIGNAL]);
1826
- }
1827
- }
1828
- return;
1829
- }
1830
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1831
- if (!test) {
1832
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1833
- }
1834
- })(context.legacy) : {};
1835
- if (schema._kind('@legacy', 'hasMany').notify(store, proxy, identifier, field)) {
1836
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1837
- if (!test) {
1838
- throw new Error(`Expected options to exist on relationship meta`);
1839
- }
1840
- })(field.options) : {};
1841
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1842
- if (!test) {
1843
- throw new Error(`Expected async to exist on relationship meta options`);
1844
- }
1845
- })('async' in field.options) : {};
1846
- if (field.options.async) {
1847
- const signal = signals.get(key);
1848
- if (signal) {
1849
- notifyInternalSignal(signal);
1850
- }
1851
- }
1852
- }
1853
- } else if (field.kind === 'collection') ;
1854
- }
1855
- }
1856
- break;
1857
- }
1858
- });
1859
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
1860
- Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
1861
- enumerable: false,
1862
- configurable: true,
1863
- get() {
1864
- const data = {};
1865
- for (const key of fields.keys()) {
1866
- data[key] = proxy[key];
1867
- }
1868
- return data;
1869
- }
1870
- });
1871
- }
1872
- return proxy;
1873
- }
1874
- }
1875
- function _CHECKOUT(record) {
1876
- // IF we are already the editable record, throw an error
1877
- if (record[Editable]) {
1878
- throw new Error(`Cannot checkout an already editable record`);
1879
- }
1880
- const editable = Editables.get(record);
1881
- if (editable) {
1882
- return Promise.resolve(editable);
1883
- }
1884
- const embeddedType = record[EmbeddedField];
1885
- const embeddedPath = record[EmbeddedPath];
1886
- const isEmbedded = embeddedType !== null && embeddedPath !== null;
1887
- if (isEmbedded) {
1888
- throw new Error(`Cannot checkout an embedded record (yet)`);
1889
- }
1890
- const legacy = record[Legacy];
1891
- const editableRecord = new ReactiveResource({
1892
- store: record[RecordStore],
1893
- resourceKey: record[Identifier],
1894
- modeName: legacy ? 'legacy' : 'polaris',
1895
- legacy: legacy,
1896
- editable: true,
1897
- path: null,
1898
- field: null,
1899
- value: null
1900
- });
1901
- setRecordIdentifier(editableRecord, recordIdentifierFor(record));
1902
- return Promise.resolve(editableRecord);
1903
- }
1904
- function _DESTROY(record) {
1905
- if (record[Legacy]) {
1906
- // @ts-expect-error
1907
- record.isDestroying = true;
1908
- // @ts-expect-error
1909
- record.isDestroyed = true;
1910
- }
1911
- record[RecordStore].notifications.unsubscribe(record.___notifications);
1912
-
1913
- // FIXME we need a way to also unsubscribe all SchemaObjects when the primary
1914
- // resource is destroyed.
1915
- }
1916
- function assertNeverField(identifier, field, path) {
1917
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1918
- {
1919
- throw new Error(`Cannot use unknown field kind ${field.kind} on <${identifier.type}>.${Array.isArray(path) ? path.join('.') : path}`);
1920
- }
1921
- })() : {};
1922
- return false;
1923
- }
13
+ import { Type } from './types/symbols.js';
1924
14
  function instantiateRecord(store, identifier, createArgs) {
1925
15
  const schema = store.schema;
1926
16
  const resourceSchema = schema.resource(identifier);
@@ -1930,7 +20,7 @@ function instantiateRecord(store, identifier, createArgs) {
1930
20
  }
1931
21
  })(isResourceSchema(resourceSchema)) : {};
1932
22
  const legacy = resourceSchema?.legacy ?? false;
1933
- const editable = legacy || store.cache.isNew(identifier);
23
+ const editable = legacy;
1934
24
  const record = new ReactiveResource({
1935
25
  store,
1936
26
  resourceKey: identifier,
@@ -1941,7 +31,7 @@ function instantiateRecord(store, identifier, createArgs) {
1941
31
  field: null,
1942
32
  value: null
1943
33
  });
1944
- if (createArgs) {
34
+ if (createArgs && editable) {
1945
35
  Object.assign(record, createArgs);
1946
36
  }
1947
37
  return record;
@@ -2180,7 +270,8 @@ function withDefaults(schema) {
2180
270
  * @public
2181
271
  */
2182
272
  const fromIdentity = (record, options, key) => {
2183
- const identifier = record[Identifier];
273
+ const context = record[Context];
274
+ const identifier = context.resourceKey;
2184
275
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2185
276
  if (!test) {
2186
277
  throw new Error(`Cannot compute @identity for a record without an identifier`);
@@ -2204,7 +295,6 @@ fromIdentity[Type] = '@identity';
2204
295
  * ```
2205
296
  *
2206
297
  * @public
2207
- * @param {SchemaService} schema
2208
298
  */
2209
299
  function registerDerivations(schema) {
2210
300
  schema.registerDerivation(fromIdentity);
@@ -2221,10 +311,9 @@ function makeCachedDerivation(derivation) {
2221
311
  const signals = withSignalStore(record);
2222
312
  let signal = signals.get(prop);
2223
313
  if (!signal) {
2224
- signal = createMemo(record, prop, () => {
314
+ signal = createInternalMemo(signals, record, prop, () => {
2225
315
  return derivation(record, options, prop);
2226
316
  }); // a total lie, for convenience of reusing the storage
2227
- signals.set(prop, signal);
2228
317
  }
2229
318
  return signal();
2230
319
  };
@@ -2234,7 +323,6 @@ function makeCachedDerivation(derivation) {
2234
323
  /**
2235
324
  * A SchemaService designed to work with dynamically registered schemas.
2236
325
  *
2237
- * @class SchemaService
2238
326
  * @public
2239
327
  */
2240
328
  class SchemaService {
@@ -2253,6 +341,8 @@ class SchemaService {
2253
341
 
2254
342
  /** @internal */
2255
343
 
344
+ /** @internal */
345
+
2256
346
  constructor() {
2257
347
  this._schemas = new Map();
2258
348
  this._transforms = new Map();
@@ -2498,6 +588,13 @@ class SchemaService {
2498
588
  })(kinds[kind]) : {};
2499
589
  return kinds[kind];
2500
590
  }
591
+
592
+ /**
593
+ * Registers a {@link HashFn} for use with a {@link HashField} for
594
+ * either {@link ObjectSchema} identity or polymorphic type calculation.
595
+ *
596
+ * @public
597
+ */
2501
598
  registerHashFn(hashFn) {
2502
599
  this._hashFns.set(hashFn[Type], hashFn);
2503
600
  }
@@ -2682,4 +779,125 @@ function mergeMap(base, toApply) {
2682
779
  base.set(key, value);
2683
780
  }
2684
781
  }
2685
- export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };
782
+ const Subscriptions = new WeakMap();
783
+
784
+ /**
785
+ * `ExpensiveSubscription` is a mechanism for non-reactive
786
+ * frameworks such as `react` to integrate with WarpDrive.
787
+ *
788
+ * This mechanism should never be used by frameworks or libraries
789
+ * that support fine-grained reactivity.
790
+ *
791
+ * ExpensiveSubscription is expensive *because* it doubles the number
792
+ * of notification callbacks required for each resource contained in
793
+ * the request being subscribed to. The more requests in-use, the more
794
+ * this cost adds up.
795
+ */
796
+ class ExpensiveSubscription {
797
+ constructor(store, request) {
798
+ this._store = store;
799
+ this._request = request;
800
+ this._callbacks = new Set();
801
+ this._resources = new Map();
802
+ this._subscription = store.notifications.subscribe(request, this._notifyRequestChange);
803
+ this._updateResourceCallbacks();
804
+ }
805
+ _updateResourceCallbacks() {
806
+ const request = this._request;
807
+ const store = this._store;
808
+ const {
809
+ notifications
810
+ } = store;
811
+ const req = store.cache.peek(request);
812
+ const resources = this._resources;
813
+ const isInitialSubscription = resources.size === 0;
814
+ if (req && 'data' in req) {
815
+ if (Array.isArray(req.data)) {
816
+ for (const resourceKey of req.data) {
817
+ if (isInitialSubscription || !resources.has(resourceKey)) {
818
+ resources.set(resourceKey, notifications.subscribe(resourceKey, this._scheduleNotify));
819
+ }
820
+ }
821
+ } else if (req.data) {
822
+ if (isInitialSubscription || !resources.has(req.data)) {
823
+ resources.set(req.data, notifications.subscribe(req.data, this._scheduleNotify));
824
+ }
825
+ }
826
+ }
827
+ if (req && 'included' in req && Array.isArray(req.included)) {
828
+ for (const resourceKey of req.included) {
829
+ if (isInitialSubscription || !resources.has(resourceKey)) {
830
+ resources.set(resourceKey, notifications.subscribe(resourceKey, this._scheduleNotify));
831
+ }
832
+ }
833
+ }
834
+ }
835
+ _notifyRequestChange = () => {
836
+ this._updateResourceCallbacks();
837
+ this._scheduleNotify();
838
+ };
839
+ _scheduleNotify = () => {
840
+ this._notify = this._notify || Promise.resolve().then(() => {
841
+ for (const callback of this._callbacks) {
842
+ callback();
843
+ }
844
+ this._notify = null;
845
+ });
846
+ };
847
+ addWatcher(callback) {
848
+ this._callbacks.add(callback);
849
+ }
850
+ removeWatcher(callback) {
851
+ this._callbacks.delete(callback);
852
+ if (this._callbacks.size === 0) {
853
+ this.destroy();
854
+ }
855
+ }
856
+ destroy() {
857
+ Subscriptions.delete(this._request);
858
+ const {
859
+ notifications
860
+ } = this._store;
861
+ if (this._subscription) {
862
+ notifications.unsubscribe(this._subscription);
863
+ }
864
+ for (const token of this._resources.values()) {
865
+ notifications.unsubscribe(token);
866
+ }
867
+ this._callbacks.clear();
868
+ this._resources.clear();
869
+ }
870
+ }
871
+
872
+ /**
873
+ * Creates an {@link ExpensiveSubscription} for the {@link RequestKey}
874
+ * if one does not already exist and adds a watcher to it.
875
+ *
876
+ * Returns a cleanup function. This should be called on-mount by a component
877
+ * that wants to subscribe to a request and cleanup should be called on dismount.
878
+ *
879
+ * ::: warning ⚠️ Avoid Using If Your App Supports Fine-grained Reactivity
880
+ * This mechanism should never be used by frameworks or libraries
881
+ * that support fine-grained reactivity.
882
+ * :::
883
+ *
884
+ * `ExpensiveSubscription` is a mechanism for non-reactive
885
+ * frameworks such as `react` to integrate with WarpDrive, for instance
886
+ * by treating a request as an [external store](https://react.dev/reference/react/useSyncExternalStore)
887
+ *
888
+ * `ExpensiveSubscription` is expensive *because* it doubles the number
889
+ * of notification callbacks required for each resource contained in
890
+ * the request being subscribed to. The more requests in-use, the more
891
+ * this cost adds up.
892
+ */
893
+ function getExpensiveRequestSubscription(store, requestKey, callback) {
894
+ let subscription = Subscriptions.get(requestKey);
895
+ if (!subscription) {
896
+ subscription = new ExpensiveSubscription(store, requestKey);
897
+ }
898
+ subscription.addWatcher(callback);
899
+ return () => {
900
+ subscription.removeWatcher(callback);
901
+ };
902
+ }
903
+ export { SchemaService, fromIdentity, getExpensiveRequestSubscription, instantiateRecord, registerDerivations, teardownRecord, withDefaults };