@warp-drive/core 5.7.0-alpha.3 → 5.7.0-alpha.31

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 (116) 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 +2 -2
  4. package/declarations/graph/-private/-utils.d.ts +5 -5
  5. package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +3 -3
  6. package/declarations/graph/-private/edges/collection.d.ts +10 -10
  7. package/declarations/graph/-private/edges/implicit.d.ts +5 -5
  8. package/declarations/graph/-private/edges/resource.d.ts +6 -7
  9. package/declarations/graph/-private/graph.d.ts +17 -15
  10. package/declarations/graph/-private/operations/replace-related-records.d.ts +4 -4
  11. package/declarations/graph/-private/operations/update-relationship.d.ts +3 -3
  12. package/declarations/index.d.ts +1 -1
  13. package/declarations/reactive/-private/default-mode.d.ts +73 -0
  14. package/declarations/reactive/-private/document.d.ts +11 -21
  15. package/declarations/reactive/-private/fields/get-field-key.d.ts +8 -0
  16. package/declarations/reactive/-private/fields/managed-array.d.ts +7 -10
  17. package/declarations/reactive/-private/fields/managed-object.d.ts +7 -9
  18. package/declarations/reactive/-private/fields/many-array-manager.d.ts +2 -2
  19. package/declarations/reactive/-private/hooks.d.ts +2 -2
  20. package/declarations/reactive/-private/kind/alias-field.d.ts +4 -0
  21. package/declarations/reactive/-private/kind/array-field.d.ts +4 -0
  22. package/declarations/reactive/-private/kind/attribute-field.d.ts +4 -0
  23. package/declarations/reactive/-private/kind/belongs-to-field.d.ts +4 -0
  24. package/declarations/reactive/-private/kind/collection-field.d.ts +4 -0
  25. package/declarations/reactive/-private/kind/derived-field.d.ts +4 -0
  26. package/declarations/reactive/-private/kind/generic-field.d.ts +4 -0
  27. package/declarations/reactive/-private/kind/has-many-field.d.ts +4 -0
  28. package/declarations/reactive/-private/kind/hash-field.d.ts +4 -0
  29. package/declarations/reactive/-private/kind/identity-field.d.ts +4 -0
  30. package/declarations/reactive/-private/kind/local-field.d.ts +4 -0
  31. package/declarations/reactive/-private/kind/object-field.d.ts +4 -0
  32. package/declarations/reactive/-private/kind/resource-field.d.ts +4 -0
  33. package/declarations/reactive/-private/kind/schema-array-field.d.ts +4 -0
  34. package/declarations/reactive/-private/kind/schema-object-field.d.ts +4 -0
  35. package/declarations/reactive/-private/record.d.ts +44 -33
  36. package/declarations/reactive/-private/schema.d.ts +16 -72
  37. package/declarations/reactive/-private/symbols.d.ts +2 -7
  38. package/declarations/reactive/-private.d.ts +1 -1
  39. package/declarations/reactive.d.ts +278 -1
  40. package/declarations/request/-private/context.d.ts +3 -3
  41. package/declarations/request/-private/fetch.d.ts +2 -0
  42. package/declarations/request/-private/manager.d.ts +24 -28
  43. package/declarations/request/-private/types.d.ts +22 -23
  44. package/declarations/request/-private/utils.d.ts +44 -2
  45. package/declarations/store/-private/cache-handler/handler.d.ts +2 -8
  46. package/declarations/store/-private/cache-handler/types.d.ts +10 -10
  47. package/declarations/store/-private/cache-handler/utils.d.ts +4 -4
  48. package/declarations/store/-private/caches/instance-cache.d.ts +21 -19
  49. package/declarations/store/-private/debug/utils.d.ts +1 -0
  50. package/declarations/store/-private/default-cache-policy.d.ts +25 -38
  51. package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +24 -15
  52. package/declarations/store/-private/{caches/identifier-cache.d.ts → managers/cache-key-manager.d.ts} +38 -52
  53. package/declarations/store/-private/managers/cache-manager.d.ts +47 -95
  54. package/declarations/store/-private/managers/notification-manager.d.ts +30 -42
  55. package/declarations/store/-private/managers/record-array-manager.d.ts +45 -41
  56. package/declarations/store/-private/network/request-cache.d.ts +21 -21
  57. package/declarations/store/-private/new-core-tmp/expensive-subscription.d.ts +24 -0
  58. package/declarations/store/-private/new-core-tmp/reactivity/configure.d.ts +3 -41
  59. package/declarations/store/-private/new-core-tmp/reactivity/internal.d.ts +14 -29
  60. package/declarations/store/-private/new-core-tmp/reactivity/signal.d.ts +24 -3
  61. package/declarations/store/-private/new-core-tmp/request-state.d.ts +129 -22
  62. package/declarations/store/-private/new-core-tmp/request-subscription.d.ts +51 -123
  63. package/declarations/store/-private/record-arrays/-utils.d.ts +80 -0
  64. package/declarations/store/-private/record-arrays/legacy-live-array.d.ts +81 -0
  65. package/declarations/store/-private/record-arrays/legacy-many-array.d.ts +133 -0
  66. package/declarations/store/-private/record-arrays/legacy-query.d.ts +81 -0
  67. package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +1 -124
  68. package/declarations/store/-private/record-arrays/resource-array.d.ts +75 -0
  69. package/declarations/store/-private/store-service.d.ts +156 -101
  70. package/declarations/store/-private.d.ts +12 -9
  71. package/declarations/store/-types/q/cache-capabilities-manager.d.ts +15 -24
  72. package/declarations/store/-types/q/identifier.d.ts +9 -6
  73. package/declarations/store/-types/q/record-instance.d.ts +0 -1
  74. package/declarations/store/-types/q/schema-service.d.ts +28 -40
  75. package/declarations/store/-types/q/store.d.ts +6 -7
  76. package/declarations/store/deprecated/-private.d.ts +12 -23
  77. package/declarations/store/deprecated/store.d.ts +11 -12
  78. package/declarations/types/-private.d.ts +1 -1
  79. package/declarations/types/cache/aliases.d.ts +2 -2
  80. package/declarations/types/cache/change.d.ts +2 -2
  81. package/declarations/types/cache/mutations.d.ts +13 -13
  82. package/declarations/types/cache/operations.d.ts +115 -32
  83. package/declarations/types/cache/relationship.d.ts +4 -4
  84. package/declarations/types/cache.d.ts +51 -113
  85. package/declarations/types/graph.d.ts +12 -12
  86. package/declarations/types/identifier.d.ts +52 -76
  87. package/declarations/types/params.d.ts +2 -3
  88. package/declarations/types/request.d.ts +69 -42
  89. package/declarations/types/schema/concepts.d.ts +2 -2
  90. package/declarations/types/schema/fields.d.ts +378 -14
  91. package/declarations/types/spec/document.d.ts +6 -6
  92. package/declarations/types/spec/json-api-raw.d.ts +6 -8
  93. package/declarations/utils/string.d.ts +2 -2
  94. package/dist/{configure-B48bFHOl.js → configure-C3x8YXzL.js} +5 -5
  95. package/dist/configure.js +1 -1
  96. package/dist/{context-COmAnXUQ.js → context-Bh-MA_tH.js} +40 -6
  97. package/dist/graph/-private.js +137 -144
  98. package/dist/index.js +25 -14
  99. package/dist/reactive/-private.js +1 -1
  100. package/dist/reactive.js +203 -1413
  101. package/dist/{request-state-CejVJgdj.js → request-state-DGyt5EV8.js} +5674 -2812
  102. package/dist/request.js +1 -1
  103. package/dist/store/-private.js +2 -3
  104. package/dist/store.js +32 -44
  105. package/dist/{symbols-SIstXMLI.js → symbols-sql1_mdx.js} +3 -8
  106. package/dist/types/-private.js +1 -1
  107. package/dist/types/identifier.js +19 -45
  108. package/dist/types/request.js +45 -3
  109. package/dist/types/schema/fields.js +23 -2
  110. package/dist/utils/string.js +2 -2
  111. package/package.json +10 -10
  112. package/declarations/reactive/-private/fields/compute.d.ts +0 -43
  113. package/declarations/store/-private/caches/cache-utils.d.ts +0 -12
  114. package/declarations/store/-private/record-arrays/identifier-array.d.ts +0 -147
  115. package/declarations/store/-private/record-arrays/many-array.d.ts +0 -197
  116. package/dist/handler-D2jjnIA-.js +0 -339
package/dist/reactive.js CHANGED
@@ -1,1402 +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-DGyt5EV8.js";
2
+ export { O as checkout, P as commit } from "./request-state-DGyt5EV8.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, W as performObjectExtensionGet, d as SOURCE$1, f as fastPush, y as defineSignal, l as RelatedCollection, J as getOrCreateInternalSignal, G as notifyInternalSignal, B as Signals, h as setRecordIdentifier, r as recordIdentifierFor } from "./request-state-CejVJgdj.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';
11
+ import "./configure-C3x8YXzL.js";
9
12
  import { getOrSetGlobal } from './types/-private.js';
10
- import { S as SOURCE, E as Editable, L as Legacy, I as Identifier, P as Parent, a as EmbeddedPath, D as Destroy, C as Checkout, b as EmbeddedField } from "./symbols-SIstXMLI.js";
11
- import './index.js';
12
- 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']);
13
- // const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
14
- const SYNC_PROPS = new Set(['[]', 'length']);
15
- function isArrayGetter(prop) {
16
- return ARRAY_GETTER_METHODS.has(prop);
17
- }
18
- const ARRAY_SETTER_METHODS = new Set(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
19
- function isArraySetter(prop) {
20
- return ARRAY_SETTER_METHODS.has(prop);
21
- }
22
-
23
- // function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
24
- // return prop in self;
25
- // }
26
-
27
- function convertToInt(prop) {
28
- if (typeof prop === 'symbol') return null;
29
- const num = Number(prop);
30
- if (isNaN(num)) return null;
31
- return num % 1 === 0 ? num : null;
32
- }
33
- function safeForEach(instance, arr, store, callback, target) {
34
- if (target === undefined) {
35
- target = null;
36
- }
37
- // clone to prevent mutation
38
- arr = arr.slice();
39
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
40
- if (!test) {
41
- throw new Error('`forEach` expects a function as first argument.');
42
- }
43
- })(typeof callback === 'function') : {};
44
-
45
- // because we retrieveLatest above we need not worry if array is mutated during iteration
46
- // by unloadRecord/rollbackAttributes
47
- // push/add/removeObject may still be problematic
48
- // but this is a more traditionally expected forEach bug.
49
- const length = arr.length; // we need to access length to ensure we are consumed
50
-
51
- for (let index = 0; index < length; index++) {
52
- callback.call(target, arr[index], index, instance);
53
- }
54
- return instance;
55
- }
56
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
57
- class ManagedArray {
58
- constructor(store, schema, cache, field, data, identifier, path, owner, isSchemaArray, editable, legacy) {
59
- // eslint-disable-next-line @typescript-eslint/no-this-alias
60
- const self = this;
61
- this[SOURCE] = data?.slice();
62
- const IS_EDITABLE = this[Editable] = editable ?? false;
63
- this[Legacy] = legacy;
64
- const signals = withSignalStore(this);
65
- let _SIGNAL = null;
66
- const boundFns = new Map();
67
- this.identifier = identifier;
68
- this.path = path;
69
- this.owner = owner;
70
- let transaction = false;
71
- const mode = field.options?.key ?? '@identity';
72
- const RefStorage = mode === '@identity' ? WeakMap :
73
- // CAUTION CAUTION CAUTION
74
- // this is a pile of lies
75
- // the Map is Map<string, WeakRef<ReactiveResource>>
76
- // but TS does not understand how to juggle modes like this
77
- // internal to a method like ours without us duplicating the code
78
- // into two separate methods.
79
- Map;
80
- const ManagedRecordRefs = isSchemaArray ? new RefStorage() : null;
81
- const extensions = legacy ? schema.CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) : null;
82
- const proxy = new Proxy(this[SOURCE], {
83
- get(target, prop, receiver) {
84
- if (prop === ARRAY_SIGNAL) {
85
- return _SIGNAL;
86
- }
87
- if (prop === 'identifier') {
88
- return self.identifier;
89
- }
90
- if (prop === 'owner') {
91
- return self.owner;
92
- }
93
- const index = convertToInt(prop);
94
- if (_SIGNAL.isStale && (index !== null || SYNC_PROPS.has(prop) || isArrayGetter(prop))) {
95
- _SIGNAL.isStale = false;
96
- const newData = cache.getAttr(identifier, path);
97
- if (newData && newData !== self[SOURCE]) {
98
- self[SOURCE].length = 0;
99
- self[SOURCE].push(...newData);
100
- }
101
- }
102
- if (prop === 'length') {
103
- return consumeInternalSignal(_SIGNAL), target.length;
104
- }
105
- if (prop === '[]') return consumeInternalSignal(_SIGNAL), receiver;
106
- if (index !== null) {
107
- let val;
108
- if (mode === '@hash') {
109
- val = target[index];
110
- const hashField = schema.resource({
111
- type: field.type
112
- }).identity;
113
- const hashFn = schema.hashFn(hashField);
114
- val = hashFn(val, null, null);
115
- } else {
116
- // if mode is not @identity or @index, then access the key path.
117
- // we should assert that `mode` is a string
118
- // it should read directly from the cache value for that field (e.g. no derivation, no transformation)
119
- // and, we likely should lookup the associated field and throw an error IF
120
- // the given field does not exist OR
121
- // the field is anything other than a GenericField or LegacyAttributeField.
122
- if (mode !== '@identity' && mode !== '@index') {
123
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
124
- if (!test) {
125
- throw new Error('mode must be a string');
126
- }
127
- })(typeof mode === 'string') : {};
128
- const modeField = schema.resource({
129
- type: field.type
130
- }).fields.find(f => f.name === mode);
131
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
132
- if (!test) {
133
- throw new Error('field must exist in schema');
134
- }
135
- })(modeField) : {};
136
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
137
- if (!test) {
138
- throw new Error('field must be a GenericField or LegacyAttributeField');
139
- }
140
- })(modeField.kind === 'field' || modeField.kind === 'attribute') : {};
141
- }
142
- val = mode === '@identity' ? target[index] : mode === '@index' ? '@index' : target[index][mode];
143
- }
144
- if (isSchemaArray) {
145
- if (!transaction) {
146
- consumeInternalSignal(_SIGNAL);
147
- }
148
- if (val) {
149
- const recordRef = ManagedRecordRefs.get(val);
150
- let record = recordRef?.deref();
151
- if (!record) {
152
- const recordPath = path.slice();
153
- // this is a dirty lie since path is string[] but really we
154
- // should change the types for paths to `Array<string | number>`
155
- // TODO we should allow the schema for the field to define a "key"
156
- // for stability. Default should be `@identity` which means that
157
- // same object reference from cache should result in same ReactiveResource
158
- // embedded object.
159
- recordPath.push(index);
160
- const recordIdentifier = self.owner[Identifier] || self.owner[Parent];
161
- record = new ReactiveResource(store, recordIdentifier, {
162
- [Editable]: self.owner[Editable],
163
- [Legacy]: self.owner[Legacy]
164
- }, true, field, recordPath);
165
- // if mode is not @identity or @index, then access the key path now
166
- // to determine the key value.
167
- // chris says we can implement this as a special kind `@hash` which
168
- // would be a function that only has access to the cache value and not
169
- // the record itself, so derivation is possible but intentionally limited
170
- // and non-reactive?
171
- ManagedRecordRefs.set(val, new WeakRef(record));
172
- }
173
- return record;
174
- }
175
- return val;
176
- }
177
- if (!transaction) {
178
- consumeInternalSignal(_SIGNAL);
179
- }
180
- if (field.type) {
181
- const transform = schema.transformation(field);
182
- return transform.hydrate(val, field.options ?? null, self.owner);
183
- }
184
- return val;
185
- }
186
- if (isArrayGetter(prop)) {
187
- let fn = boundFns.get(prop);
188
- if (fn === undefined) {
189
- if (prop === 'forEach') {
190
- fn = function () {
191
- consumeInternalSignal(_SIGNAL);
192
- transaction = true;
193
- const result = safeForEach(receiver, target, store, arguments[0], arguments[1]);
194
- transaction = false;
195
- return result;
196
- };
197
- } else {
198
- fn = function () {
199
- consumeInternalSignal(_SIGNAL);
200
- // array functions must run through Reflect to work properly
201
- // binding via other means will not work.
202
- transaction = true;
203
- const result = Reflect.apply(target[prop], receiver, arguments);
204
- transaction = false;
205
- return result;
206
- };
207
- }
208
- boundFns.set(prop, fn);
209
- }
210
- return fn;
211
- }
212
- if (isArraySetter(prop)) {
213
- let fn = boundFns.get(prop);
214
- if (fn === undefined) {
215
- fn = function () {
216
- if (!IS_EDITABLE) {
217
- throw new Error(`Mutating this array via ${String(prop)} is not allowed because the record is not editable`);
218
- }
219
- consumeInternalSignal(_SIGNAL);
220
- transaction = true;
221
- const result = Reflect.apply(target[prop], receiver, arguments);
222
- transaction = false;
223
- return result;
224
- };
225
- boundFns.set(prop, fn);
226
- }
227
- return fn;
228
- }
229
- if (isExtensionProp(extensions, prop)) {
230
- return performArrayExtensionGet(receiver, extensions, signals, prop, _SIGNAL, boundFns, v => void (transaction = v));
231
- }
232
- return Reflect.get(target, prop, receiver);
233
- },
234
- set(target, prop, value, receiver) {
235
- if (!IS_EDITABLE) {
236
- let errorPath = identifier.type;
237
- if (path) {
238
- errorPath = path[path.length - 1];
239
- }
240
- throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
241
- }
242
- if (prop === 'identifier') {
243
- self.identifier = value;
244
- return true;
245
- }
246
- if (prop === 'owner') {
247
- self.owner = value;
248
- return true;
249
- }
250
- if (isExtensionProp(extensions, prop)) {
251
- return performExtensionSet(receiver, extensions, signals, prop, value);
252
- }
253
- const reflect = Reflect.set(target, prop, value, receiver);
254
- if (reflect) {
255
- if (!field.type) {
256
- cache.setAttr(identifier, path, self[SOURCE]);
257
- _SIGNAL.isStale = true;
258
- return true;
259
- }
260
- let rawValue = self[SOURCE];
261
- if (!isSchemaArray) {
262
- const transform = schema.transformation(field);
263
- if (!transform) {
264
- throw new Error(`No '${field.type}' transform defined for use by ${identifier.type}.${String(prop)}`);
265
- }
266
- rawValue = self[SOURCE].map(item => transform.serialize(item, field.options ?? null, self.owner));
267
- }
268
- cache.setAttr(identifier, path, rawValue);
269
- _SIGNAL.isStale = true;
270
- }
271
- return reflect;
272
- },
273
- has(target, prop) {
274
- if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
275
- return true;
276
- }
277
- return Reflect.has(target, prop);
278
- }
279
- });
280
-
281
- // we entangle the signal on the returned proxy since that is
282
- // the object that other code will be interfacing with.
283
- _SIGNAL = entangleSignal(signals, proxy, ARRAY_SIGNAL, undefined);
284
- return proxy;
285
- }
286
- }
287
-
288
- // this will error if someone tries to call
289
- // A(identifierArray) since it is not configurable
290
- // which is preferable to the `meta` override we used
291
- // before which required importing all of Ember
292
- const desc = {
293
- enumerable: true,
294
- configurable: false,
295
- get: function () {
296
- // here to support computed chains
297
- // and {{#each}}
298
- if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_COMPUTED_CHAINS)) {
299
- return this;
300
- }
301
- }
302
- };
303
- // compat(desc);
304
- Object.defineProperty(ManagedArray.prototype, '[]', desc);
305
- const ObjectSymbols = new Set([OBJECT_SIGNAL, Parent, SOURCE, Editable, EmbeddedPath]);
306
-
307
- // const ignoredGlobalFields = new Set<string>(['setInterval', 'nodeType', 'nodeName', 'length', 'document', STRUCTURED]);
308
-
309
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
310
- class ManagedObject {
311
- constructor(schema, cache, field, data, identifier, path, owner, editable, legacy) {
312
- // eslint-disable-next-line @typescript-eslint/no-this-alias
313
- const self = this;
314
- this[SOURCE] = {
315
- ...data
316
- };
317
- const signals = withSignalStore(this);
318
- const _SIGNAL = this[OBJECT_SIGNAL] = entangleSignal(signals, this, OBJECT_SIGNAL, undefined);
319
- this[Editable] = editable;
320
- this[Legacy] = legacy;
321
- this[Parent] = identifier;
322
- this[EmbeddedPath] = path;
323
-
324
- // prettier-ignore
325
- const extensions = !legacy ? null : schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(field);
326
- const proxy = new Proxy(this[SOURCE], {
327
- ownKeys() {
328
- return Object.keys(self[SOURCE]);
329
- },
330
- has(target, prop) {
331
- return prop in self[SOURCE];
332
- },
333
- getOwnPropertyDescriptor(target, prop) {
334
- return {
335
- writable: editable,
336
- enumerable: true,
337
- configurable: true
338
- };
339
- },
340
- get(target, prop, receiver) {
341
- if (ObjectSymbols.has(prop)) {
342
- return self[prop];
343
- }
344
- if (prop === Symbol.toPrimitive) {
345
- return () => null;
346
- }
347
- if (prop === Symbol.toStringTag) {
348
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
349
- }
350
- if (prop === 'constructor') {
351
- return Object;
352
- }
353
- if (prop === 'toString') {
354
- return function () {
355
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
356
- };
357
- }
358
- if (prop === 'toHTML') {
359
- return function () {
360
- return '<span>ManagedObject</span>';
361
- };
362
- }
363
- if (_SIGNAL.isStale) {
364
- _SIGNAL.isStale = false;
365
- let newData = cache.getAttr(identifier, path);
366
- if (newData && newData !== self[SOURCE]) {
367
- if (field.type) {
368
- const transform = schema.transformation(field);
369
- newData = transform.hydrate(newData, field.options ?? null, owner);
370
- }
371
- self[SOURCE] = {
372
- ...newData
373
- }; // Add type assertion for newData
374
- }
375
- }
376
-
377
- // toJSON and extensions need to come after we update data if stale
378
- if (prop === 'toJSON') {
379
- return function () {
380
- return structuredClone(self[SOURCE]);
381
- };
382
- }
383
-
384
- // we always defer to data before extensions
385
- if (prop in self[SOURCE]) {
386
- consumeInternalSignal(_SIGNAL);
387
- return self[SOURCE][prop];
388
- }
389
- if (isExtensionProp(extensions, prop)) {
390
- return performObjectExtensionGet(receiver, extensions, signals, prop);
391
- }
392
- return Reflect.get(target, prop, receiver);
393
- },
394
- set(target, prop, value, receiver) {
395
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
396
- if (!test) {
397
- throw new Error(`Cannot set read-only property '${String(prop)}' on ManagedObject`);
398
- }
399
- })(editable) : {};
400
-
401
- // since objects function as dictionaries, we can't defer to schema/data before extensions
402
- // unless the prop is in the existing data.
403
- if (!(prop in self[SOURCE]) && isExtensionProp(extensions, prop)) {
404
- return performExtensionSet(receiver, extensions, signals, prop, value);
405
- }
406
- const reflect = Reflect.set(target, prop, value, receiver);
407
- if (!reflect) {
408
- return false;
409
- }
410
- if (!field.type) {
411
- cache.setAttr(identifier, path, self[SOURCE]);
412
- } else {
413
- const transform = schema.transformation(field);
414
- const val = transform.serialize(self[SOURCE], field.options ?? null, owner);
415
- cache.setAttr(identifier, path, val);
416
- }
417
- _SIGNAL.isStale = true;
418
- return true;
419
- }
420
- });
421
- return proxy;
422
- }
423
- }
424
- class ManyArrayManager {
425
- constructor(record, editable) {
426
- this.record = record;
427
- this.store = record[RecordStore];
428
- this.identifier = record[Identifier];
429
- this.editable = editable;
430
- }
431
- _syncArray(array) {
432
- const method = this.editable ? 'getRelationship' : 'getRemoteRelationship';
433
- const rawValue = this.store.cache[method](this.identifier, array.key);
434
- if (rawValue.meta) {
435
- array.meta = rawValue.meta;
436
- }
437
- if (rawValue.links) {
438
- array.links = rawValue.links;
439
- }
440
- const currentState = array[SOURCE$1];
441
-
442
- // unlike in the normal RecordArray case, we don't need to divorce the reference
443
- // because we don't need to worry about associate/disassociate since the graph
444
- // takes care of that for us
445
- if (currentState !== rawValue.data) {
446
- currentState.length = 0;
447
- fastPush(currentState, rawValue.data);
448
- }
449
- }
450
- reloadHasMany(key, options) {
451
- const field = this.store.schema.fields(this.identifier).get(key);
452
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
453
- if (!test) {
454
- throw new Error(`Expected a hasMany field for ${key}`);
455
- }
456
- })(field?.kind === 'hasMany') : {};
457
- const cacheOptions = options ? extractCacheOptions(options) : {
458
- reload: true
459
- };
460
- cacheOptions.types = [field.type];
461
- const rawValue = this.store.cache.getRelationship(this.identifier, key);
462
- const req = {
463
- url: getRelatedLink(rawValue),
464
- op: 'findHasMany',
465
- method: 'GET',
466
- records: rawValue.data,
467
- cacheOptions,
468
- options: {
469
- field,
470
- identifier: this.identifier,
471
- links: rawValue.links,
472
- meta: rawValue.meta
473
- },
474
- [EnableHydration]: false
475
- };
476
- return this.store.request(req);
477
- }
478
- mutate(mutation) {
479
- this.store.cache.mutate(mutation);
480
- }
481
- }
482
- function getRelatedLink(resource) {
483
- const related = resource.links?.related;
484
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
485
- if (!test) {
486
- throw new Error(`Expected a related link`);
487
- }
488
- })(related) : {};
489
- return typeof related === 'object' ? related.href : related;
490
- }
491
- function extractCacheOptions(options) {
492
- const cacheOptions = {};
493
- if ('reload' in options) {
494
- cacheOptions.reload = options.reload;
495
- }
496
- if ('backgroundReload' in options) {
497
- cacheOptions.backgroundReload = options.backgroundReload;
498
- }
499
- return cacheOptions;
500
- }
501
- const ManagedArrayMap = getOrSetGlobal('ManagedArrayMap', new Map());
502
- const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
503
- function computeLocal(record, field, prop) {
504
- const signals = withSignalStore(record);
505
- const signal = getOrCreateInternalSignal(signals, record, prop, field.options?.defaultValue ?? null);
506
- consumeInternalSignal(signal);
507
- return signal.value;
508
- }
509
- function peekManagedArray(record, field) {
510
- const managedArrayMapForRecord = ManagedArrayMap.get(record);
511
- if (managedArrayMapForRecord) {
512
- return managedArrayMapForRecord.get(field.name);
513
- }
514
- }
515
- function peekManagedObject(record, field) {
516
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
517
- if (managedObjectMapForRecord) {
518
- return managedObjectMapForRecord.get(field.name);
519
- }
520
- }
521
- function computeField(schema, cache, record, identifier, field, prop, editable) {
522
- const rawValue = editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
523
- if (!field.type) {
524
- return rawValue;
525
- }
526
- const transform = schema.transformation(field);
527
- return transform.hydrate(rawValue, field.options ?? null, record);
528
- }
529
- function computeArray(store, schema, cache, record, identifier, field, path, editable, legacy) {
530
- const isSchemaArray = field.kind === 'schema-array';
531
- // the thing we hand out needs to know its owner and path in a private manner
532
- // its "address" is the parent identifier (identifier) + field name (field.name)
533
- // in the nested object case field name here is the full dot path from root resource to this value
534
- // its "key" is the field on the parent record
535
- // its "owner" is the parent record
536
-
537
- const managedArrayMapForRecord = ManagedArrayMap.get(record);
538
- let managedArray;
539
- if (managedArrayMapForRecord) {
540
- managedArray = managedArrayMapForRecord.get(field.name);
541
- }
542
- if (managedArray) {
543
- return managedArray;
544
- } else {
545
- const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
546
- if (!rawValue) {
547
- return null;
548
- }
549
- managedArray = new ManagedArray(store, schema, cache, field, rawValue, identifier, path, record, isSchemaArray, editable, legacy);
550
- if (!managedArrayMapForRecord) {
551
- ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
552
- } else {
553
- managedArrayMapForRecord.set(field.name, managedArray);
554
- }
555
- }
556
- return managedArray;
557
- }
558
- function computeObject(schema, cache, record, identifier, field, path, editable, legacy) {
559
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
560
- let managedObject;
561
- if (managedObjectMapForRecord) {
562
- managedObject = managedObjectMapForRecord.get(field.name);
563
- }
564
- if (managedObject) {
565
- return managedObject;
566
- } else {
567
- let rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
568
- if (!rawValue) {
569
- return null;
570
- }
571
- if (field.type) {
572
- const transform = schema.transformation(field);
573
- rawValue = transform.hydrate(rawValue, field.options ?? null, record);
574
- }
575
- managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);
576
- if (!managedObjectMapForRecord) {
577
- ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
578
- } else {
579
- managedObjectMapForRecord.set(field.name, managedObject);
580
- }
581
- }
582
- return managedObject;
583
- }
584
- function computeSchemaObject(store, cache, record, identifier, field, path, legacy, editable) {
585
- const schemaObjectMapForRecord = ManagedObjectMap.get(record);
586
- let schemaObject;
587
- if (schemaObjectMapForRecord) {
588
- schemaObject = schemaObjectMapForRecord.get(field.name);
589
- }
590
- if (schemaObject) {
591
- return schemaObject;
592
- } else {
593
- const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
594
- if (!rawValue) {
595
- return null;
596
- }
597
- const embeddedPath = path.slice();
598
- schemaObject = new ReactiveResource(store, identifier, {
599
- [Editable]: editable,
600
- [Legacy]: legacy
601
- }, true, field, embeddedPath);
602
- }
603
- if (!schemaObjectMapForRecord) {
604
- ManagedObjectMap.set(record, new Map([[field.name, schemaObject]]));
605
- } else {
606
- schemaObjectMapForRecord.set(field.name, schemaObject);
607
- }
608
- return schemaObject;
609
- }
610
- function computeAttribute(cache, identifier, prop, editable) {
611
- return editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
612
- }
613
- function computeDerivation(schema, record, identifier, field, prop) {
614
- return schema.derivation(field)(record, field.options ?? null, prop);
615
- }
616
- // TODO probably this should just be a Document
617
- // but its separate until we work out the lid situation
618
- class ResourceRelationship {
619
- constructor(store, cache, parent, identifier, field, name, editable) {
620
- const rawValue = editable ? cache.getRelationship(identifier, name) : cache.getRemoteRelationship(identifier, name);
621
-
622
- // TODO setup true lids for relationship documents
623
- // @ts-expect-error we need to give relationship documents a lid
624
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
625
- this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${identifier.lid}.${name}`;
626
- this.data = rawValue.data ? store.peekRecord(rawValue.data) : null;
627
- this.name = name;
628
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
629
- this.links = Object.freeze(Object.assign({}, rawValue.links));
630
- this.meta = Object.freeze(Object.assign({}, rawValue.meta));
631
- } else {
632
- this.links = rawValue.links ?? {};
633
- this.meta = rawValue.meta ?? {};
634
- }
635
- this[RecordStore] = store;
636
- this[Parent] = parent;
637
- }
638
- fetch(options) {
639
- const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
640
- if (!url) {
641
- throw new Error(`Cannot ${options?.method ?? 'fetch'} ${this[Parent][Identifier].type}.${String(this.name)} because it has no related link`);
642
- }
643
- const request = Object.assign({
644
- url,
645
- method: 'GET'
646
- }, options);
647
- return this[RecordStore].request(request);
648
- }
649
- }
650
- defineSignal(ResourceRelationship.prototype, 'data', null);
651
- defineSignal(ResourceRelationship.prototype, 'links', null);
652
- defineSignal(ResourceRelationship.prototype, 'meta', null);
653
- function getHref(link) {
654
- if (!link) {
655
- return null;
656
- }
657
- if (typeof link === 'string') {
658
- return link;
659
- }
660
- return link.href;
661
- }
662
- function computeResource(store, cache, parent, identifier, field, prop, editable) {
663
- if (field.kind !== 'resource') {
664
- throw new Error(`The schema for ${identifier.type}.${String(prop)} is not a resource relationship`);
665
- }
666
- return new ResourceRelationship(store, cache, parent, identifier, field, prop, editable);
667
- }
668
- function computeHasMany(store, schema, cache, record, identifier, field, path, editable, legacy) {
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 managedArrayMapForRecord = ManagedArrayMap.get(record);
676
- let managedArray;
677
- if (managedArrayMapForRecord) {
678
- managedArray = managedArrayMapForRecord.get(field.name);
679
- }
680
- if (managedArray) {
681
- return managedArray;
682
- } else {
683
- const rawValue = cache.getRelationship(identifier, field.name);
684
- if (!rawValue) {
685
- return null;
686
- }
687
- managedArray = new RelatedCollection({
688
- store,
689
- type: field.type,
690
- identifier,
691
- cache,
692
- field: legacy ? field : undefined,
693
- // we divorce the reference here because ManyArray mutates the target directly
694
- // before sending the mutation op to the cache. We may be able to avoid this in the future
695
- identifiers: rawValue.data?.slice(),
696
- key: field.name,
697
- meta: rawValue.meta || null,
698
- links: rawValue.links || null,
699
- isPolymorphic: field.options.polymorphic ?? false,
700
- isAsync: field.options.async ?? false,
701
- // TODO: Grab the proper value
702
- _inverseIsAsync: false,
703
- // @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
704
- manager: new ManyArrayManager(record, editable),
705
- isLoaded: true,
706
- allowMutation: editable
707
- });
708
- if (!managedArrayMapForRecord) {
709
- ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
710
- } else {
711
- managedArrayMapForRecord.set(field.name, managedArray);
712
- }
713
- }
714
- return managedArray;
715
- }
716
- const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
717
- const symbolList = [Destroy, RecordStore, Identifier, Editable, Parent, Checkout, Legacy, EmbeddedPath, EmbeddedField];
718
- const RecordSymbols = new Set(symbolList);
719
- function isPathMatch(a, b) {
720
- return a.length === b.length && a.every((v, i) => v === b[i]);
721
- }
722
- function isNonEnumerableProp(prop) {
723
- return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
724
- }
725
- const Editables = new WeakMap();
726
- /**
727
- * A class that uses a the ResourceSchema for a ResourceType
728
- * and a ResouceKey to transform data from the cache into a rich, reactive
729
- * object.
730
- *
731
- * This class is not directly instantiable. To use it, you should
732
- * configure the store's `instantiateRecord` and `teardownRecord` hooks
733
- * with the matching hooks provided by this package.
734
- *
735
- * @hideconstructor
736
- * @public
737
- */
738
- // eslint-disable-next-line @typescript-eslint/no-extraneous-class
739
- class ReactiveResource {
740
- constructor(store, identifier, Mode, isEmbedded = false, embeddedField = null, embeddedPath = null) {
741
- // eslint-disable-next-line @typescript-eslint/no-this-alias
742
- const self = this;
743
- this[RecordStore] = store;
744
- if (isEmbedded) {
745
- this[Parent] = identifier;
746
- } else {
747
- this[Identifier] = identifier;
748
- }
749
- const IS_EDITABLE = this[Editable] = Mode[Editable] ?? false;
750
- this[Legacy] = Mode[Legacy] ?? false;
751
- const schema = store.schema;
752
- const cache = store.cache;
753
- const identityField = schema.resource(isEmbedded ? embeddedField : identifier).identity;
754
- const BoundFns = new Map();
755
-
756
- // prettier-ignore
757
- const extensions = !Mode[Legacy] ? null : isEmbedded ? schema.CAUTION_MEGA_DANGER_ZONE_objectExtensions(embeddedField) : schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(identifier);
758
- this[EmbeddedField] = embeddedField;
759
- this[EmbeddedPath] = embeddedPath;
760
- const fields = isEmbedded ? schema.fields(embeddedField) : schema.fields(identifier);
761
- const signals = withSignalStore(this);
762
- const proxy = new Proxy(this, {
763
- ownKeys() {
764
- const identityKey = identityField?.name;
765
- const keys = Array.from(fields.keys());
766
- if (identityKey) {
767
- keys.unshift(identityKey);
768
- }
769
- return keys;
770
- },
771
- has(target, prop) {
772
- if (prop === Destroy || prop === Checkout) {
773
- return true;
774
- }
775
- return fields.has(prop);
776
- },
777
- getOwnPropertyDescriptor(target, prop) {
778
- const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
779
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
780
- if (!test) {
781
- throw new Error(`No field named ${String(prop)} on ${identifier.type}`);
782
- }
783
- })(schemaForField) : {};
784
- if (isNonEnumerableProp(prop)) {
785
- return {
786
- writable: false,
787
- enumerable: false,
788
- configurable: true
789
- };
790
- }
791
- switch (schemaForField.kind) {
792
- case 'derived':
793
- return {
794
- writable: false,
795
- enumerable: true,
796
- configurable: true
797
- };
798
- case '@id':
799
- return {
800
- writable: identifier.id === null,
801
- enumerable: true,
802
- configurable: true
803
- };
804
- case '@local':
805
- case 'field':
806
- case 'attribute':
807
- case 'resource':
808
- case 'alias':
809
- case 'belongsTo':
810
- case 'hasMany':
811
- case 'collection':
812
- case 'schema-array':
813
- case 'array':
814
- case 'schema-object':
815
- case 'object':
816
- return {
817
- writable: IS_EDITABLE,
818
- enumerable: true,
819
- configurable: true
820
- };
821
- default:
822
- return {
823
- writable: false,
824
- enumerable: false,
825
- configurable: false
826
- };
827
- }
828
- },
829
- get(target, prop, receiver) {
830
- if (RecordSymbols.has(prop)) {
831
- if (prop === Destroy) {
832
- return () => _DESTROY(receiver);
833
- }
834
- if (prop === Checkout) {
835
- return () => _CHECKOUT(receiver);
836
- }
837
- return target[prop];
838
- }
839
- if (prop === Signals) {
840
- return signals;
841
- }
842
-
843
- // TODO make this a symbol
844
- if (prop === '___notifications') {
845
- return target.___notifications;
846
- }
847
-
848
- // ReactiveResource reserves use of keys that begin with these characters
849
- // for its own usage.
850
- // _, @, $, *
851
-
852
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
853
- if (!maybeField) {
854
- if (IgnoredGlobalFields.has(prop)) {
855
- return undefined;
856
- }
857
-
858
- /////////////////////////////////////////////////////////////
859
- //// Note these bound function behaviors are essentially ////
860
- //// built-in but overrideable derivations. ////
861
- //// ////
862
- //// The bar for this has to be "basic expectations of ////
863
- /// an object" – very, very high ////
864
- /////////////////////////////////////////////////////////////
865
-
866
- if (prop === Symbol.toStringTag || prop === 'toString') {
867
- let fn = BoundFns.get('toString');
868
- if (!fn) {
869
- fn = function () {
870
- entangleSignal(signals, receiver, '@identity', null);
871
- return `Record<${identifier.type}:${identifier.id} (${identifier.lid})>`;
872
- };
873
- BoundFns.set(prop, fn);
874
- }
875
- return fn;
876
- }
877
- if (prop === 'toHTML') {
878
- let fn = BoundFns.get('toHTML');
879
- if (!fn) {
880
- fn = function () {
881
- entangleSignal(signals, receiver, '@identity', null);
882
- return `<span>Record<${identifier.type}:${identifier.id} (${identifier.lid})></span>`;
883
- };
884
- BoundFns.set(prop, fn);
885
- }
886
- return fn;
887
- }
888
- if (prop === 'toJSON') {
889
- let fn = BoundFns.get('toJSON');
890
- if (!fn) {
891
- fn = function () {
892
- const json = {};
893
- for (const key in receiver) {
894
- json[key] = receiver[key];
895
- }
896
- return json;
897
- };
898
- BoundFns.set(prop, fn);
899
- }
900
- return fn;
901
- }
902
- if (prop === Symbol.toPrimitive) return () => null;
903
- if (prop === Symbol.iterator) {
904
- let fn = BoundFns.get(Symbol.iterator);
905
- if (!fn) {
906
- fn = function* () {
907
- for (const key in receiver) {
908
- yield [key, receiver[key]];
909
- }
910
- };
911
- BoundFns.set(Symbol.iterator, fn);
912
- }
913
- return fn;
914
- }
915
- if (prop === 'constructor') {
916
- return ReactiveResource;
917
- }
918
- if (isExtensionProp(extensions, prop)) {
919
- return performObjectExtensionGet(receiver, extensions, signals, prop);
920
- }
921
-
922
- // too many things check for random symbols
923
- if (typeof prop === 'symbol') return undefined;
924
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
925
- {
926
- throw new Error(`No field named ${String(prop)} on ${isEmbedded ? embeddedField.type : identifier.type}`);
927
- }
928
- })() : {};
929
- return undefined;
930
- }
931
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
932
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
933
- if (!test) {
934
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
935
- }
936
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
937
- const propArray = isEmbedded ? embeddedPath.slice() : [];
938
- // we use the field.name instead of prop here because we want to use the cache-path not
939
- // the record path.
940
- propArray.push(field.name);
941
- // propArray.push(prop as string);
942
-
943
- switch (field.kind) {
944
- case '@id':
945
- entangleSignal(signals, receiver, '@identity', null);
946
- return identifier.id;
947
- case '@hash':
948
- // TODO pass actual cache value not {}
949
- return schema.hashFn(field)({}, field.options ?? null, field.name ?? null);
950
- case '@local':
951
- {
952
- return computeLocal(receiver, field, prop);
953
- }
954
- case 'field':
955
- entangleSignal(signals, receiver, field.name, null);
956
- return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
957
- case 'attribute':
958
- entangleSignal(signals, receiver, field.name, null);
959
- return computeAttribute(cache, identifier, prop, IS_EDITABLE);
960
- case 'resource':
961
- entangleSignal(signals, receiver, field.name, null);
962
- return computeResource(store, cache, target, identifier, field, prop, IS_EDITABLE);
963
- case 'derived':
964
- return computeDerivation(schema, receiver, identifier, field, prop);
965
- case 'schema-array':
966
- case 'array':
967
- entangleSignal(signals, receiver, field.name, null);
968
- return computeArray(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
969
- case 'object':
970
- entangleSignal(signals, receiver, field.name, null);
971
- return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
972
- case 'schema-object':
973
- entangleSignal(signals, receiver, field.name, null);
974
- // run transform, then use that value as the object to manage
975
- return computeSchemaObject(store, cache, target, identifier, field, propArray, Mode[Legacy], Mode[Editable]);
976
- case 'belongsTo':
977
- if (field.options.linksMode) {
978
- entangleSignal(signals, receiver, field.name, null);
979
- const rawValue = IS_EDITABLE ? cache.getRelationship(identifier, field.name) : cache.getRemoteRelationship(identifier, field.name);
980
-
981
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
982
- return rawValue.data ? store.peekRecord(rawValue.data) : null;
983
- }
984
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
985
- if (!test) {
986
- throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
987
- }
988
- })(Mode[Legacy]) : {};
989
- entangleSignal(signals, receiver, field.name, null);
990
- return schema._kind('@legacy', 'belongsTo').get(store, receiver, identifier, field);
991
- case 'hasMany':
992
- if (field.options.linksMode) {
993
- entangleSignal(signals, receiver, field.name, null);
994
- return computeHasMany(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
995
- }
996
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
997
- if (!test) {
998
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
999
- }
1000
- })(Mode[Legacy]) : {};
1001
- entangleSignal(signals, receiver, field.name, null);
1002
- return schema._kind('@legacy', 'hasMany').get(store, receiver, identifier, field);
1003
- default:
1004
- throw new Error(`Field '${String(prop)}' on '${identifier.type}' has the unknown kind '${field.kind}'`);
1005
- }
1006
- },
1007
- set(target, prop, value, receiver) {
1008
- if (!IS_EDITABLE) {
1009
- const type = isEmbedded ? embeddedField.type : identifier.type;
1010
- throw new Error(`Cannot set ${String(prop)} on ${type} because the record is not editable`);
1011
- }
1012
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
1013
- if (!maybeField) {
1014
- const type = isEmbedded ? embeddedField.type : identifier.type;
1015
- if (isExtensionProp(extensions, prop)) {
1016
- return performExtensionSet(receiver, extensions, signals, prop, value);
1017
- }
1018
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1019
- {
1020
- throw new Error(`There is no settable field named ${String(prop)} on ${type}`);
1021
- }
1022
- })() : {};
1023
- return false;
1024
- }
1025
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
1026
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1027
- if (!test) {
1028
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
1029
- }
1030
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
1031
- const propArray = isEmbedded ? embeddedPath.slice() : [];
1032
- // we use the field.name instead of prop here because we want to use the cache-path not
1033
- // the record path.
1034
- propArray.push(field.name);
1035
- // propArray.push(prop as string);
1036
-
1037
- switch (field.kind) {
1038
- case '@id':
1039
- {
1040
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1041
- if (!test) {
1042
- throw new Error(`Expected to receive a string id`);
1043
- }
1044
- })(typeof value === 'string' && value.length) : {};
1045
- const normalizedId = String(value);
1046
- const didChange = normalizedId !== identifier.id;
1047
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1048
- if (!test) {
1049
- throw new Error(`Cannot set ${identifier.type} record's id to ${normalizedId}, because id is already ${identifier.id}`);
1050
- }
1051
- })(!didChange || identifier.id === null) : {};
1052
- if (normalizedId !== null && didChange) {
1053
- store._instanceCache.setRecordId(identifier, normalizedId);
1054
- store.notifications.notify(identifier, 'identity');
1055
- }
1056
- return true;
1057
- }
1058
- case '@local':
1059
- {
1060
- const signal = getOrCreateInternalSignal(signals, receiver, prop, field.options?.defaultValue ?? null);
1061
- if (signal.value !== value) {
1062
- signal.value = value;
1063
- notifyInternalSignal(signal);
1064
- }
1065
- return true;
1066
- }
1067
- case 'field':
1068
- {
1069
- if (!field.type) {
1070
- cache.setAttr(identifier, propArray, value);
1071
- return true;
1072
- }
1073
- const transform = schema.transformation(field);
1074
- const rawValue = transform.serialize(value, field.options ?? null, target);
1075
- cache.setAttr(identifier, propArray, rawValue);
1076
- return true;
1077
- }
1078
- case 'attribute':
1079
- {
1080
- cache.setAttr(identifier, propArray, value);
1081
- return true;
1082
- }
1083
- case 'array':
1084
- {
1085
- if (!field.type) {
1086
- cache.setAttr(identifier, propArray, value?.slice());
1087
- const peeked = peekManagedArray(self, field);
1088
- if (peeked) {
1089
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1090
- if (!test) {
1091
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1092
- }
1093
- })(ARRAY_SIGNAL in peeked) : {};
1094
- const arrSignal = peeked[ARRAY_SIGNAL];
1095
- arrSignal.isStale = true;
1096
- }
1097
- if (!Array.isArray(value)) {
1098
- ManagedArrayMap.delete(target);
1099
- }
1100
- return true;
1101
- }
1102
- const transform = schema.transformation(field);
1103
- const rawValue = value.map(item => transform.serialize(item, field.options ?? null, target));
1104
- cache.setAttr(identifier, propArray, rawValue);
1105
- const peeked = peekManagedArray(self, field);
1106
- if (peeked) {
1107
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1108
- if (!test) {
1109
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1110
- }
1111
- })(ARRAY_SIGNAL in peeked) : {};
1112
- const arrSignal = peeked[ARRAY_SIGNAL];
1113
- arrSignal.isStale = true;
1114
- }
1115
- return true;
1116
- }
1117
- case 'schema-array':
1118
- {
1119
- const arrayValue = value?.slice();
1120
- if (!Array.isArray(arrayValue)) {
1121
- ManagedArrayMap.delete(target);
1122
- }
1123
- cache.setAttr(identifier, propArray, arrayValue);
1124
- const peeked = peekManagedArray(self, field);
1125
- if (peeked) {
1126
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1127
- if (!test) {
1128
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1129
- }
1130
- })(ARRAY_SIGNAL in peeked) : {};
1131
- const arrSignal = peeked[ARRAY_SIGNAL];
1132
- arrSignal.isStale = true;
1133
- }
1134
- if (!Array.isArray(value)) {
1135
- ManagedArrayMap.delete(target);
1136
- }
1137
- return true;
1138
- }
1139
- case 'object':
1140
- {
1141
- if (!field.type) {
1142
- let newValue = value;
1143
- if (value !== null) {
1144
- newValue = {
1145
- ...value
1146
- };
1147
- } else {
1148
- ManagedObjectMap.delete(target);
1149
- }
1150
- cache.setAttr(identifier, propArray, newValue);
1151
- const peeked = peekManagedObject(self, field);
1152
- if (peeked) {
1153
- const objSignal = peeked[OBJECT_SIGNAL];
1154
- objSignal.isStale = true;
1155
- }
1156
- return true;
1157
- }
1158
- const transform = schema.transformation(field);
1159
- const rawValue = transform.serialize({
1160
- ...value
1161
- }, field.options ?? null, target);
1162
- cache.setAttr(identifier, propArray, rawValue);
1163
- const peeked = peekManagedObject(self, field);
1164
- if (peeked) {
1165
- const objSignal = peeked[OBJECT_SIGNAL];
1166
- objSignal.isStale = true;
1167
- }
1168
- return true;
1169
- }
1170
- case 'schema-object':
1171
- {
1172
- let newValue = value;
1173
- if (value !== null) {
1174
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1175
- if (!test) {
1176
- throw new Error(`Expected value to be an object`);
1177
- }
1178
- })(typeof value === 'object') : {};
1179
- newValue = {
1180
- ...value
1181
- };
1182
- const schemaFields = schema.fields({
1183
- type: field.type
1184
- });
1185
- for (const key of Object.keys(newValue)) {
1186
- if (!schemaFields.has(key)) {
1187
- throw new Error(`Field ${key} does not exist on schema object ${field.type}`);
1188
- }
1189
- }
1190
- } else {
1191
- ManagedObjectMap.delete(target);
1192
- }
1193
- cache.setAttr(identifier, propArray, newValue);
1194
- // const peeked = peekManagedObject(self, field);
1195
- // if (peeked) {
1196
- // const objSignal = peeked[OBJECT_SIGNAL];
1197
- // objSignal.isStale = true;
1198
- // }
1199
- return true;
1200
- }
1201
- case 'derived':
1202
- {
1203
- throw new Error(`Cannot set ${String(prop)} on ${identifier.type} because it is derived`);
1204
- }
1205
- case 'belongsTo':
1206
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1207
- if (!test) {
1208
- throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
1209
- }
1210
- })(Mode[Legacy]) : {};
1211
- schema._kind('@legacy', 'belongsTo').set(store, receiver, identifier, field, value);
1212
- return true;
1213
- case 'hasMany':
1214
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1215
- if (!test) {
1216
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1217
- }
1218
- })(Mode[Legacy]) : {};
1219
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1220
- if (!test) {
1221
- throw new Error(`You must pass an array of records to set a hasMany relationship`);
1222
- }
1223
- })(Array.isArray(value)) : {};
1224
- schema._kind('@legacy', 'hasMany').set(store, receiver, identifier, field, value);
1225
- return true;
1226
- default:
1227
- throw new Error(`Unknown field kind ${field.kind}`);
1228
- }
1229
- }
1230
- });
1231
-
1232
- // what signal do we need for embedded record?
1233
- this.___notifications = store.notifications.subscribe(identifier, (_, type, key) => {
1234
- switch (type) {
1235
- case 'identity':
1236
- {
1237
- if (isEmbedded || !identityField) return; // base paths never apply to embedded records
1238
-
1239
- if (identityField.name && identityField.kind === '@id') {
1240
- const signal = signals.get('@identity');
1241
- if (signal) {
1242
- notifyInternalSignal(signal);
1243
- }
1244
- }
1245
- break;
1246
- }
1247
- case 'attributes':
1248
- if (key) {
1249
- if (Array.isArray(key)) {
1250
- if (!isEmbedded) return; // deep paths will be handled by embedded records
1251
- // TODO we should have the notification manager
1252
- // ensure it is safe for each callback to mutate this array
1253
- if (isPathMatch(embeddedPath, key)) {
1254
- // handle the notification
1255
- // TODO we should likely handle this notification here
1256
- // also we should add a LOGGING flag
1257
- // eslint-disable-next-line no-console
1258
- console.warn(`Notification unhandled for ${key.join(',')} on ${identifier.type}`, self);
1259
- return;
1260
- }
1261
-
1262
- // TODO we should add a LOGGING flag
1263
- // console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, self);
1264
- // deep notify the key path
1265
- } else {
1266
- if (isEmbedded) return; // base paths never apply to embedded records
1267
-
1268
- // TODO determine what LOGGING flag to wrap this in if any
1269
- // console.log(`Notification for ${key} on ${identifier.type}`, self);
1270
- const signal = signals.get(key);
1271
- if (signal) {
1272
- notifyInternalSignal(signal);
1273
- }
1274
- const field = fields.get(key);
1275
- if (field?.kind === 'array' || field?.kind === 'schema-array') {
1276
- const peeked = peekManagedArray(self, field);
1277
- if (peeked) {
1278
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1279
- if (!test) {
1280
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1281
- }
1282
- })(ARRAY_SIGNAL in peeked) : {};
1283
- const arrSignal = peeked[ARRAY_SIGNAL];
1284
- notifyInternalSignal(arrSignal);
1285
- }
1286
- }
1287
- if (field?.kind === 'object') {
1288
- const peeked = peekManagedObject(self, field);
1289
- if (peeked) {
1290
- const objSignal = peeked[OBJECT_SIGNAL];
1291
- notifyInternalSignal(objSignal);
1292
- }
1293
- }
1294
- }
1295
- }
1296
- break;
1297
- case 'relationships':
1298
- if (key) {
1299
- if (Array.isArray(key)) ;else {
1300
- if (isEmbedded) return; // base paths never apply to embedded records
1301
-
1302
- const field = fields.get(key);
1303
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1304
- if (!test) {
1305
- throw new Error(`Expected relationshp ${key} to be the name of a field`);
1306
- }
1307
- })(field) : {};
1308
- if (field.kind === 'belongsTo') {
1309
- // TODO determine what LOGGING flag to wrap this in if any
1310
- // console.log(`Notification for ${key} on ${identifier.type}`, self);
1311
- const signal = signals.get(key);
1312
- if (signal) {
1313
- notifyInternalSignal(signal);
1314
- }
1315
- // FIXME
1316
- } else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
1317
- if (field.options.linksMode) {
1318
- const peeked = peekManagedArray(self, field);
1319
- if (peeked) {
1320
- notifyInternalSignal(peeked[ARRAY_SIGNAL]);
1321
- }
1322
- return;
1323
- }
1324
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1325
- if (!test) {
1326
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1327
- }
1328
- })(Mode[Legacy]) : {};
1329
- if (schema._kind('@legacy', 'hasMany').notify(store, proxy, identifier, field)) {
1330
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1331
- if (!test) {
1332
- throw new Error(`Expected options to exist on relationship meta`);
1333
- }
1334
- })(field.options) : {};
1335
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1336
- if (!test) {
1337
- throw new Error(`Expected async to exist on relationship meta options`);
1338
- }
1339
- })('async' in field.options) : {};
1340
- if (field.options.async) {
1341
- const signal = signals.get(key);
1342
- if (signal) {
1343
- notifyInternalSignal(signal);
1344
- }
1345
- }
1346
- }
1347
- } else if (field.kind === 'collection') ;
1348
- }
1349
- }
1350
- break;
1351
- }
1352
- });
1353
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
1354
- Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
1355
- enumerable: false,
1356
- configurable: true,
1357
- get() {
1358
- const data = {};
1359
- for (const key of fields.keys()) {
1360
- data[key] = proxy[key];
1361
- }
1362
- return data;
1363
- }
1364
- });
1365
- }
1366
- return proxy;
1367
- }
1368
- }
1369
- function _CHECKOUT(record) {
1370
- // IF we are already the editable record, throw an error
1371
- if (record[Editable]) {
1372
- throw new Error(`Cannot checkout an already editable record`);
1373
- }
1374
- const editable = Editables.get(record);
1375
- if (editable) {
1376
- return Promise.resolve(editable);
1377
- }
1378
- const embeddedType = record[EmbeddedField];
1379
- const embeddedPath = record[EmbeddedPath];
1380
- const isEmbedded = embeddedType !== null && embeddedPath !== null;
1381
- if (isEmbedded) {
1382
- throw new Error(`Cannot checkout an embedded record (yet)`);
1383
- }
1384
- const editableRecord = new ReactiveResource(record[RecordStore], record[Identifier], {
1385
- [Editable]: true,
1386
- [Legacy]: record[Legacy]
1387
- }, isEmbedded, embeddedType, embeddedPath);
1388
- setRecordIdentifier(editableRecord, recordIdentifierFor(record));
1389
- return Promise.resolve(editableRecord);
1390
- }
1391
- function _DESTROY(record) {
1392
- if (record[Legacy]) {
1393
- // @ts-expect-error
1394
- record.isDestroying = true;
1395
- // @ts-expect-error
1396
- record.isDestroyed = true;
1397
- }
1398
- record[RecordStore].notifications.unsubscribe(record.___notifications);
1399
- }
13
+ import { Type } from './types/symbols.js';
1400
14
  function instantiateRecord(store, identifier, createArgs) {
1401
15
  const schema = store.schema;
1402
16
  const resourceSchema = schema.resource(identifier);
@@ -1405,13 +19,19 @@ function instantiateRecord(store, identifier, createArgs) {
1405
19
  throw new Error(`Expected a resource schema`);
1406
20
  }
1407
21
  })(isResourceSchema(resourceSchema)) : {};
1408
- const isLegacy = resourceSchema?.legacy ?? false;
1409
- const isEditable = isLegacy || store.cache.isNew(identifier);
1410
- const record = new ReactiveResource(store, identifier, {
1411
- [Editable]: isEditable,
1412
- [Legacy]: isLegacy
22
+ const legacy = resourceSchema?.legacy ?? false;
23
+ const editable = legacy;
24
+ const record = new ReactiveResource({
25
+ store,
26
+ resourceKey: identifier,
27
+ modeName: legacy ? 'legacy' : 'polaris',
28
+ legacy: legacy,
29
+ editable: editable,
30
+ path: null,
31
+ field: null,
32
+ value: null
1413
33
  });
1414
- if (createArgs) {
34
+ if (createArgs && editable) {
1415
35
  Object.assign(record, createArgs);
1416
36
  }
1417
37
  return record;
@@ -1546,7 +166,7 @@ function getExt(extCache, type, extName) {
1546
166
  function hasObjectSchema(field) {
1547
167
  return 'kind' in field && (field.kind === 'schema-array' || field.kind === 'schema-object');
1548
168
  }
1549
- function processExtensions(schema, field, scenario) {
169
+ function processExtensions(schema, field, scenario, resolvedType) {
1550
170
  // if we're looking up extensions for a resource, there is no
1551
171
  // merging required so if we have no objectExtensions
1552
172
  // we are done.
@@ -1573,12 +193,16 @@ function processExtensions(schema, field, scenario) {
1573
193
  if (!hasObjectSchema(field)) {
1574
194
  return null;
1575
195
  }
1576
- return schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field);
196
+ return schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resolvedType ? {
197
+ type: resolvedType
198
+ } : field);
1577
199
  }
1578
200
 
1579
201
  // if we have made it here, we have extensions, lets check if there's
1580
202
  // a cached version we can use
1581
- const baseExtensions = scenario === 'resource' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field) : scenario === 'object' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field) : null;
203
+ const baseExtensions = scenario === 'resource' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(field) : scenario === 'object' && hasObjectSchema(field) ? schema.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resolvedType ? {
204
+ type: resolvedType
205
+ } : field) : null;
1582
206
  if (!baseExtensions && extensions.length === 1) {
1583
207
  const value = getExt(extCache, type, extensions[0]);
1584
208
  fieldCache[type].set(field, value);
@@ -1646,7 +270,8 @@ function withDefaults(schema) {
1646
270
  * @public
1647
271
  */
1648
272
  const fromIdentity = (record, options, key) => {
1649
- const identifier = record[Identifier];
273
+ const context = record[Context];
274
+ const identifier = context.resourceKey;
1650
275
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1651
276
  if (!test) {
1652
277
  throw new Error(`Cannot compute @identity for a record without an identifier`);
@@ -1670,7 +295,6 @@ fromIdentity[Type] = '@identity';
1670
295
  * ```
1671
296
  *
1672
297
  * @public
1673
- * @param {SchemaService} schema
1674
298
  */
1675
299
  function registerDerivations(schema) {
1676
300
  schema.registerDerivation(fromIdentity);
@@ -1687,10 +311,9 @@ function makeCachedDerivation(derivation) {
1687
311
  const signals = withSignalStore(record);
1688
312
  let signal = signals.get(prop);
1689
313
  if (!signal) {
1690
- signal = createMemo(record, prop, () => {
314
+ signal = createInternalMemo(signals, record, prop, () => {
1691
315
  return derivation(record, options, prop);
1692
316
  }); // a total lie, for convenience of reusing the storage
1693
- signals.set(prop, signal);
1694
317
  }
1695
318
  return signal();
1696
319
  };
@@ -1700,7 +323,6 @@ function makeCachedDerivation(derivation) {
1700
323
  /**
1701
324
  * A SchemaService designed to work with dynamically registered schemas.
1702
325
  *
1703
- * @class SchemaService
1704
326
  * @public
1705
327
  */
1706
328
  class SchemaService {
@@ -1719,6 +341,8 @@ class SchemaService {
1719
341
 
1720
342
  /** @internal */
1721
343
 
344
+ /** @internal */
345
+
1722
346
  constructor() {
1723
347
  this._schemas = new Map();
1724
348
  this._transforms = new Map();
@@ -1836,16 +460,21 @@ class SchemaService {
1836
460
  relationships[field.name] = field;
1837
461
  }
1838
462
  }
463
+ const cacheFields = null;
1839
464
  const traits = new Set(isResourceSchema(schema) ? schema.traits : []);
1840
465
  const finalized = traits.size === 0;
1841
466
  const internalSchema = {
1842
467
  original: schema,
1843
468
  finalized,
1844
469
  fields,
470
+ cacheFields,
1845
471
  relationships,
1846
472
  attributes,
1847
473
  traits
1848
474
  };
475
+ if (traits.size === 0) {
476
+ internalSchema.cacheFields = getCacheFields(internalSchema);
477
+ }
1849
478
  this._schemas.set(schema.type, internalSchema);
1850
479
  }
1851
480
 
@@ -1902,13 +531,13 @@ class SchemaService {
1902
531
  }
1903
532
  CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resource) {
1904
533
  const schema = this.resource(resource);
1905
- return processExtensions(this, schema, 'resource');
534
+ return processExtensions(this, schema, 'resource', null);
1906
535
  }
1907
- CAUTION_MEGA_DANGER_ZONE_objectExtensions(field) {
1908
- return processExtensions(this, field, 'object');
536
+ CAUTION_MEGA_DANGER_ZONE_objectExtensions(field, resolvedType) {
537
+ return processExtensions(this, field, 'object', resolvedType);
1909
538
  }
1910
539
  CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) {
1911
- return processExtensions(this, field, 'array');
540
+ return processExtensions(this, field, 'array', null);
1912
541
  }
1913
542
  CAUTION_MEGA_DANGER_ZONE_hasExtension(ext) {
1914
543
  return this._extensions[ext.kind].has(ext.name);
@@ -1959,6 +588,13 @@ class SchemaService {
1959
588
  })(kinds[kind]) : {};
1960
589
  return kinds[kind];
1961
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
+ */
1962
598
  registerHashFn(hashFn) {
1963
599
  this._hashFns.set(hashFn[Type], hashFn);
1964
600
  }
@@ -1976,6 +612,20 @@ class SchemaService {
1976
612
  }
1977
613
  return schema.fields;
1978
614
  }
615
+ cacheFields({
616
+ type
617
+ }) {
618
+ const schema = this._schemas.get(type);
619
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
620
+ if (!test) {
621
+ throw new Error(`No schema defined for ${type}`);
622
+ }
623
+ })(schema) : {};
624
+ if (!schema.finalized) {
625
+ finalizeResource(this, schema);
626
+ }
627
+ return schema.cacheFields;
628
+ }
1979
629
  hasResource(resource) {
1980
630
  return this._schemas.has(resource.type);
1981
631
  }
@@ -2064,8 +714,27 @@ function finalizeResource(schema, resource) {
2064
714
  }
2065
715
  mergeMap(fields, resource.fields);
2066
716
  resource.fields = fields;
717
+ resource.cacheFields = getCacheFields(resource);
2067
718
  resource.finalized = true;
2068
719
  }
720
+ function getCacheFields(resource) {
721
+ const {
722
+ fields
723
+ } = resource;
724
+ const cacheFields = new Map();
725
+ for (const [key, value] of fields) {
726
+ if (isNonIdentityCacheableField(value)) {
727
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
728
+ if (!test) {
729
+ throw new Error(`The sourceKey '${value.sourceKey}' for the field '${key}' on ${resource.original.type} is invalid because it matches the name of an existing field`);
730
+ }
731
+ })(!value.sourceKey || value.sourceKey === key || !fields.has(value.sourceKey)) : {};
732
+ const cacheKey = getFieldCacheKeyStrict(value);
733
+ cacheFields.set(cacheKey, value);
734
+ }
735
+ }
736
+ return cacheFields;
737
+ }
2069
738
  function walkTrait(schema, trait, fields, seen, type, debugPath) {
2070
739
  if (seen.has(trait)) {
2071
740
  // if the trait is in the current path, we throw a cycle error in dev.
@@ -2110,4 +779,125 @@ function mergeMap(base, toApply) {
2110
779
  base.set(key, value);
2111
780
  }
2112
781
  }
2113
- 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 };