@ember-data/store 5.4.0-beta.11 → 5.4.0-beta.13

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 (27) hide show
  1. package/dist/-private.js +1 -1
  2. package/dist/{cache-handler-C5ilAUZ5.js → handler-CW2kp6Ua.js} +350 -371
  3. package/dist/handler-CW2kp6Ua.js.map +1 -0
  4. package/dist/index.js +1 -1
  5. package/package.json +35 -24
  6. package/unstable-preview-types/-private/cache-handler/handler.d.ts +64 -0
  7. package/unstable-preview-types/-private/cache-handler/handler.d.ts.map +1 -0
  8. package/unstable-preview-types/-private/{cache-handler.d.ts → cache-handler/types.d.ts} +25 -62
  9. package/unstable-preview-types/-private/cache-handler/types.d.ts.map +1 -0
  10. package/unstable-preview-types/-private/cache-handler/utils.d.ts +39 -0
  11. package/unstable-preview-types/-private/cache-handler/utils.d.ts.map +1 -0
  12. package/unstable-preview-types/-private/legacy-model-support/record-reference.d.ts.map +1 -1
  13. package/unstable-preview-types/-private/managers/cache-manager.d.ts +1 -1
  14. package/unstable-preview-types/-private/managers/cache-manager.d.ts.map +1 -1
  15. package/unstable-preview-types/-private/managers/notification-manager.d.ts +7 -2
  16. package/unstable-preview-types/-private/managers/notification-manager.d.ts.map +1 -1
  17. package/unstable-preview-types/-private/record-arrays/identifier-array.d.ts.map +1 -1
  18. package/unstable-preview-types/-private/record-arrays/native-proxy-type-fix.d.ts +3 -3
  19. package/unstable-preview-types/-private/store-service.d.ts +27 -7
  20. package/unstable-preview-types/-private/store-service.d.ts.map +1 -1
  21. package/unstable-preview-types/-private.d.ts +3 -2
  22. package/unstable-preview-types/-private.d.ts.map +1 -1
  23. package/unstable-preview-types/-types/q/schema-service.d.ts +3 -3
  24. package/unstable-preview-types/index.d.ts +25 -23
  25. package/unstable-preview-types/index.d.ts.map +1 -1
  26. package/dist/cache-handler-C5ilAUZ5.js.map +0 -1
  27. package/unstable-preview-types/-private/cache-handler.d.ts.map +0 -1
@@ -25,7 +25,7 @@ function coerceId(id) {
25
25
  until: '6.0',
26
26
  for: 'ember-data',
27
27
  since: {
28
- available: '5.3',
28
+ available: '4.13',
29
29
  enabled: '5.3'
30
30
  }
31
31
  });
@@ -60,7 +60,7 @@ function normalizeModelName(type) {
60
60
  until: '6.0',
61
61
  for: 'ember-data',
62
62
  since: {
63
- available: '5.3',
63
+ available: '4.13',
64
64
  enabled: '5.3'
65
65
  }
66
66
  });
@@ -75,7 +75,7 @@ function normalizeModelName(type) {
75
75
 
76
76
  function installPolyfill() {
77
77
  const isFastBoot = typeof FastBoot !== 'undefined';
78
- const CRYPTO = isFastBoot ? FastBoot.require('crypto') : window.crypto;
78
+ const CRYPTO = isFastBoot ? FastBoot.require('crypto') : globalThis.crypto;
79
79
  if (!CRYPTO.randomUUID) {
80
80
  // we might be able to optimize this by requesting more bytes than we need at a time
81
81
  const rng = function () {
@@ -138,7 +138,7 @@ function isDocumentIdentifier(identifier) {
138
138
  return DOCUMENTS.has(identifier);
139
139
  }
140
140
  const isFastBoot = typeof FastBoot !== 'undefined';
141
- const _crypto = isFastBoot ? FastBoot.require('crypto') : window.crypto;
141
+ const _crypto = isFastBoot ? FastBoot.require('crypto') : globalThis.crypto;
142
142
  if (macroCondition(getGlobalConfig().WarpDrive.polyfillUUID)) {
143
143
  installPolyfill();
144
144
  }
@@ -842,6 +842,7 @@ class RecordReference {
842
842
  @return {String} The id of the record.
843
843
  */
844
844
  id() {
845
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
845
846
  this._ref; // consume the tracked prop
846
847
  return this.___identifier.id;
847
848
  }
@@ -2156,9 +2157,9 @@ class CacheManager {
2156
2157
  /**
2157
2158
  * @module @ember-data/store
2158
2159
  */
2159
- // eslint-disable-next-line no-restricted-imports
2160
+
2160
2161
  let tokenId = 0;
2161
- const CacheOperations = new Set(['added', 'removed', 'state', 'updated']);
2162
+ const CacheOperations = new Set(['added', 'removed', 'state', 'updated', 'invalidated']);
2162
2163
  function isCacheOperationValue(value) {
2163
2164
  return CacheOperations.has(value);
2164
2165
  }
@@ -2318,14 +2319,15 @@ class NotificationManager {
2318
2319
  this._flush();
2319
2320
  }
2320
2321
  _flush() {
2321
- if (this._buffered.size) {
2322
- this._buffered.forEach((states, identifier) => {
2322
+ const buffered = this._buffered;
2323
+ if (buffered.size) {
2324
+ this._buffered = new Map();
2325
+ buffered.forEach((states, identifier) => {
2323
2326
  states.forEach(args => {
2324
2327
  // @ts-expect-error
2325
2328
  this._flushNotification(identifier, args[0], args[1]);
2326
2329
  });
2327
2330
  });
2328
- this._buffered = new Map();
2329
2331
  }
2330
2332
  this._hasFlush = false;
2331
2333
  this._onFlushCB?.();
@@ -2372,71 +2374,6 @@ class NotificationManager {
2372
2374
  */
2373
2375
 
2374
2376
  const NativeProxy = Proxy;
2375
- var __defProp = Object.defineProperty;
2376
- var __export = (target, all) => {
2377
- for (var name in all) __defProp(target, name, {
2378
- get: all[name],
2379
- enumerable: true
2380
- });
2381
- };
2382
-
2383
- // src/runtime.ts
2384
- var runtime_exports = {};
2385
- __export(runtime_exports, {
2386
- c: () => decorateClass,
2387
- f: () => decorateFieldV1,
2388
- g: () => decorateFieldV2,
2389
- i: () => initializeDeferredDecorator,
2390
- m: () => decorateMethodV1,
2391
- n: () => decorateMethodV2,
2392
- p: () => decoratePOJO
2393
- });
2394
- var deferred = /* @__PURE__ */new WeakMap();
2395
- function deferDecorator(proto, prop, desc) {
2396
- let map = deferred.get(proto);
2397
- if (!map) {
2398
- map = /* @__PURE__ */new Map();
2399
- deferred.set(proto, map);
2400
- }
2401
- map.set(prop, desc);
2402
- }
2403
- function findDeferredDecorator(target, prop) {
2404
- let cursor = target.prototype;
2405
- while (cursor) {
2406
- let desc = deferred.get(cursor)?.get(prop);
2407
- if (desc) {
2408
- return desc;
2409
- }
2410
- cursor = cursor.prototype;
2411
- }
2412
- }
2413
- function decorateFieldV1(target, prop, decorators, initializer) {
2414
- return decorateFieldV2(target.prototype, prop, decorators, initializer);
2415
- }
2416
- function decorateFieldV2(prototype, prop, decorators, initializer) {
2417
- let desc = {
2418
- configurable: true,
2419
- enumerable: true,
2420
- writable: true,
2421
- initializer: null
2422
- };
2423
- if (initializer) {
2424
- desc.initializer = initializer;
2425
- }
2426
- for (let decorator of decorators) {
2427
- desc = decorator(prototype, prop, desc) || desc;
2428
- }
2429
- if (desc.initializer === void 0) {
2430
- Object.defineProperty(prototype, prop, desc);
2431
- } else {
2432
- deferDecorator(prototype, prop, desc);
2433
- }
2434
- }
2435
- function decorateMethodV1({
2436
- prototype
2437
- }, prop, decorators) {
2438
- return decorateMethodV2(prototype, prop, decorators);
2439
- }
2440
2377
  function decorateMethodV2(prototype, prop, decorators) {
2441
2378
  const origDesc = Object.getOwnPropertyDescriptor(prototype, prop);
2442
2379
  let desc = {
@@ -2451,46 +2388,6 @@ function decorateMethodV2(prototype, prop, decorators) {
2451
2388
  }
2452
2389
  Object.defineProperty(prototype, prop, desc);
2453
2390
  }
2454
- function initializeDeferredDecorator(target, prop) {
2455
- let desc = findDeferredDecorator(target.constructor, prop);
2456
- if (desc) {
2457
- Object.defineProperty(target, prop, {
2458
- enumerable: desc.enumerable,
2459
- configurable: desc.configurable,
2460
- writable: desc.writable,
2461
- value: desc.initializer ? desc.initializer.call(target) : void 0
2462
- });
2463
- }
2464
- }
2465
- function decorateClass(target, decorators) {
2466
- return decorators.reduce((accum, decorator) => decorator(accum) || accum, target);
2467
- }
2468
- function decoratePOJO(pojo, decorated) {
2469
- for (let [type, prop, decorators] of decorated) {
2470
- if (type === "field") {
2471
- decoratePojoField(pojo, prop, decorators);
2472
- } else {
2473
- decorateMethodV2(pojo, prop, decorators);
2474
- }
2475
- }
2476
- return pojo;
2477
- }
2478
- function decoratePojoField(pojo, prop, decorators) {
2479
- let desc = {
2480
- configurable: true,
2481
- enumerable: true,
2482
- writable: true,
2483
- initializer: () => Object.getOwnPropertyDescriptor(pojo, prop)?.value
2484
- };
2485
- for (let decorator of decorators) {
2486
- desc = decorator(pojo, prop, desc) || desc;
2487
- }
2488
- if (desc.initializer) {
2489
- desc.value = desc.initializer.call(pojo);
2490
- delete desc.initializer;
2491
- }
2492
- Object.defineProperty(pojo, prop, desc);
2493
- }
2494
2391
 
2495
2392
  /**
2496
2393
  @module @ember-data/store
@@ -2786,7 +2683,6 @@ class IdentifierArray {
2786
2683
  }
2787
2684
  const original = target[index];
2788
2685
  const newIdentifier = extractIdentifierFromRecord$1(value);
2789
- target[index] = newIdentifier;
2790
2686
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2791
2687
  if (!test) {
2792
2688
  throw new Error(`Expected a record`);
@@ -2915,7 +2811,11 @@ const desc = {
2915
2811
  enumerable: true,
2916
2812
  configurable: false,
2917
2813
  get: function () {
2918
- return this;
2814
+ // here to support computed chains
2815
+ // and {{#each}}
2816
+ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_COMPUTED_CHAINS)) {
2817
+ return this;
2818
+ }
2919
2819
  }
2920
2820
  };
2921
2821
  compat(desc);
@@ -3639,7 +3539,8 @@ const EmptyClass = class {
3639
3539
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
3640
3540
  constructor(args) {}
3641
3541
  };
3642
- const BaseClass = macroCondition(dependencySatisfies('ember-source', '*')) ? macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_STORE_EXTENDS_EMBER_OBJECT) ? importSync('@ember/object') : EmptyClass : EmptyClass;
3542
+ const _BaseClass = macroCondition(dependencySatisfies('ember-source', '*')) ? macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_STORE_EXTENDS_EMBER_OBJECT) ? importSync('@ember/object') : EmptyClass : EmptyClass;
3543
+ const BaseClass = _BaseClass.default ? _BaseClass.default : _BaseClass;
3643
3544
  if (BaseClass !== EmptyClass) {
3644
3545
  deprecate(`The Store class extending from EmberObject is deprecated.
3645
3546
  Please remove usage of EmberObject APIs and mark your class as not requiring it.
@@ -3661,7 +3562,7 @@ const app = new EmberApp(defaults, {
3661
3562
  until: '6.0',
3662
3563
  for: 'ember-data',
3663
3564
  since: {
3664
- available: '5.4',
3565
+ available: '4.13',
3665
3566
  enabled: '5.4'
3666
3567
  }
3667
3568
  });
@@ -3766,6 +3667,17 @@ class Store extends BaseClass {
3766
3667
 
3767
3668
  // Private
3768
3669
 
3670
+ /**
3671
+ * Async flush buffers notifications until flushed
3672
+ * by finalization of a future configured by store.request
3673
+ *
3674
+ * This is useful for ensuring that notifications are delivered
3675
+ * prior to the promise resolving but without risk of promise
3676
+ * interleaving.
3677
+ *
3678
+ * @internal
3679
+ */
3680
+
3769
3681
  // DEBUG-only properties
3770
3682
 
3771
3683
  get isDestroying() {
@@ -3839,6 +3751,16 @@ class Store extends BaseClass {
3839
3751
  this._cbs = null;
3840
3752
  }
3841
3753
  }
3754
+
3755
+ /**
3756
+ * Executes the callback, ensurng that any work that calls
3757
+ * store._schedule is executed after in the right order.
3758
+ *
3759
+ * When queues already exist, scheduled callbacks will
3760
+ * join the existing queue.
3761
+ *
3762
+ * @internal
3763
+ */
3842
3764
  _join(cb) {
3843
3765
  if (this._cbs) {
3844
3766
  cb();
@@ -4402,7 +4324,7 @@ class Store extends BaseClass {
4402
4324
  ```app/routes/post.js
4403
4325
  export default class PostRoute extends Route {
4404
4326
  model(params) {
4405
- return this.store.findRecord('post', params.post_id, { include: 'comments' });
4327
+ return this.store.findRecord('post', params.post_id, { include: ['comments'] });
4406
4328
  }
4407
4329
  }
4408
4330
  ```
@@ -4424,13 +4346,13 @@ class Store extends BaseClass {
4424
4346
  In this case, the post's comments would then be available in your template as
4425
4347
  `model.comments`.
4426
4348
  Multiple relationships can be requested using an `include` parameter consisting of a
4427
- comma-separated list (without white-space) while nested relationships can be specified
4349
+ list of relationship names, while nested relationships can be specified
4428
4350
  using a dot-separated sequence of relationship names. So to request both the post's
4429
4351
  comments and the authors of those comments the request would look like this:
4430
4352
  ```app/routes/post.js
4431
4353
  export default class PostRoute extends Route {
4432
4354
  model(params) {
4433
- return this.store.findRecord('post', params.post_id, { include: 'comments,comments.author' });
4355
+ return this.store.findRecord('post', params.post_id, { include: ['comments','comments.author'] });
4434
4356
  }
4435
4357
  }
4436
4358
  ```
@@ -4951,18 +4873,18 @@ class Store extends BaseClass {
4951
4873
  ```app/routes/posts.js
4952
4874
  export default class PostsRoute extends Route {
4953
4875
  model() {
4954
- return this.store.findAll('post', { include: 'comments' });
4876
+ return this.store.findAll('post', { include: ['comments'] });
4955
4877
  }
4956
4878
  }
4957
4879
  ```
4958
4880
  Multiple relationships can be requested using an `include` parameter consisting of a
4959
- comma-separated list (without white-space) while nested relationships can be specified
4881
+ list or relationship names, while nested relationships can be specified
4960
4882
  using a dot-separated sequence of relationship names. So to request both the posts'
4961
4883
  comments and the authors of those comments the request would look like this:
4962
4884
  ```app/routes/posts.js
4963
4885
  export default class PostsRoute extends Route {
4964
4886
  model() {
4965
- return this.store.findAll('post', { include: 'comments,comments.author' });
4887
+ return this.store.findAll('post', { include: ['comments','comments.author'] });
4966
4888
  }
4967
4889
  }
4968
4890
  ```
@@ -5238,7 +5160,7 @@ class Store extends BaseClass {
5238
5160
  const data = JSON.parse(JSON.stringify(jsonApiDoc));
5239
5161
  // eslint-disable-next-line no-console
5240
5162
  console.log('EmberData | Payload - push', data);
5241
- } catch (e) {
5163
+ } catch {
5242
5164
  // eslint-disable-next-line no-console
5243
5165
  console.log('EmberData | Payload - push', jsonApiDoc);
5244
5166
  }
@@ -5373,10 +5295,10 @@ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA
5373
5295
  })(this._schema) : {};
5374
5296
  deprecate(`Use \`store.schema\` instead of \`store.getSchemaDefinitionService()\``, false, {
5375
5297
  id: 'ember-data:schema-service-updates',
5376
- until: '5.0',
5298
+ until: '6.0',
5377
5299
  for: 'ember-data',
5378
5300
  since: {
5379
- available: '5.4',
5301
+ available: '4.13',
5380
5302
  enabled: '5.4'
5381
5303
  }
5382
5304
  });
@@ -5385,10 +5307,10 @@ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA
5385
5307
  Store.prototype.registerSchemaDefinitionService = function (schema) {
5386
5308
  deprecate(`Use \`store.createSchemaService\` instead of \`store.registerSchemaDefinitionService()\``, false, {
5387
5309
  id: 'ember-data:schema-service-updates',
5388
- until: '5.0',
5310
+ until: '6.0',
5389
5311
  for: 'ember-data',
5390
5312
  since: {
5391
- available: '5.4',
5313
+ available: '4.13',
5392
5314
  enabled: '5.4'
5393
5315
  }
5394
5316
  });
@@ -5397,10 +5319,10 @@ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA
5397
5319
  Store.prototype.registerSchema = function (schema) {
5398
5320
  deprecate(`Use \`store.createSchemaService\` instead of \`store.registerSchema()\``, false, {
5399
5321
  id: 'ember-data:schema-service-updates',
5400
- until: '5.0',
5322
+ until: '6.0',
5401
5323
  for: 'ember-data',
5402
5324
  since: {
5403
- available: '5.4',
5325
+ available: '4.13',
5404
5326
  enabled: '5.4'
5405
5327
  }
5406
5328
  });
@@ -5709,29 +5631,207 @@ defineSignal(Document.prototype, 'data');
5709
5631
  defineSignal(Document.prototype, 'links');
5710
5632
  defineSignal(Document.prototype, 'errors');
5711
5633
  defineSignal(Document.prototype, 'meta');
5634
+ const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
5635
+ function calcShouldFetch(store, request, hasCachedValue, identifier) {
5636
+ const {
5637
+ cacheOptions
5638
+ } = request;
5639
+ return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
5640
+ }
5641
+ function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
5642
+ const {
5643
+ cacheOptions
5644
+ } = request;
5645
+ return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
5646
+ }
5647
+ function isMutation(request) {
5648
+ return Boolean(request.op && MUTATION_OPS.has(request.op));
5649
+ }
5650
+ function copyDocumentProperties(target, source) {
5651
+ if ('links' in source) {
5652
+ target.links = source.links;
5653
+ }
5654
+ if ('meta' in source) {
5655
+ target.meta = source.meta;
5656
+ }
5657
+ if ('errors' in source) {
5658
+ target.errors = source.errors;
5659
+ }
5660
+ }
5661
+ function isCacheAffecting(document) {
5662
+ if (!isMutation(document.request)) {
5663
+ return true;
5664
+ }
5665
+ // a mutation combined with a 204 has no cache impact when no known records were involved
5666
+ // a createRecord with a 201 with an empty response and no known records should similarly
5667
+ // have no cache impact
5668
+
5669
+ if (document.request.op === 'createRecord' && document.response?.status === 201) {
5670
+ return document.content ? Object.keys(document.content).length > 0 : false;
5671
+ }
5672
+ return document.response?.status !== 204;
5673
+ }
5674
+ function isAggregateError(error) {
5675
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
5676
+ }
5677
+ // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
5678
+ function cloneError(error) {
5679
+ const isAggregate = isAggregateError(error);
5680
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
5681
+ cloned.stack = error.stack;
5682
+ cloned.error = error.error;
5683
+
5684
+ // copy over enumerable properties
5685
+ Object.assign(cloned, error);
5686
+ return cloned;
5687
+ }
5688
+ function isErrorDocument(document) {
5689
+ return 'errors' in document;
5690
+ }
5691
+ function getPriority(identifier, deduped, priority) {
5692
+ if (identifier) {
5693
+ const existing = deduped.get(identifier);
5694
+ if (existing) {
5695
+ return existing.priority;
5696
+ }
5697
+ }
5698
+ return priority;
5699
+ }
5712
5700
 
5713
5701
  /**
5714
5702
  * @module @ember-data/store
5715
5703
  */
5716
5704
 
5717
5705
  /**
5718
- * A service which an application may provide to the store via
5719
- * the store's `lifetimes` property to configure the behavior
5720
- * of the CacheHandler.
5706
+ * A CacheHandler that adds support for using an EmberData Cache with a RequestManager.
5707
+ *
5708
+ * This handler will only run when a request has supplied a `store` instance. Requests
5709
+ * issued by the store via `store.request()` will automatically have the `store` instance
5710
+ * attached to the request.
5721
5711
  *
5722
- * The default behavior for request lifetimes is to never expire
5723
- * unless manually refreshed via `cacheOptions.reload` or `cacheOptions.backgroundReload`.
5712
+ * ```ts
5713
+ * requestManager.request({
5714
+ * store: store,
5715
+ * url: '/api/posts',
5716
+ * method: 'GET'
5717
+ * });
5718
+ * ```
5724
5719
  *
5725
- * Implementing this service allows you to programatically define
5726
- * when a request should be considered expired.
5720
+ * When this handler elects to handle a request, it will return the raw `StructuredDocument`
5721
+ * unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
5722
+ * return a `Document` instance that will automatically update the UI when the cache is updated
5723
+ * in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
5727
5724
  *
5728
- * @class <Interface> CachePolicy
5729
- * @public
5725
+ * When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
5726
+ * means that if desired you can issue requests that utilize the cache without needing to also
5727
+ * utilize Record instances if desired.
5728
+ *
5729
+ * Said differently, you could elect to issue all requests via a RequestManager, without ever using
5730
+ * the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
5731
+ * necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
5732
+ * approach of EmberData allows for this flexibility.
5733
+ *
5734
+ * ```ts
5735
+ * import { EnableHydration } from '@warp-drive/core-types/request';
5736
+ *
5737
+ * requestManager.request({
5738
+ * store: store,
5739
+ * url: '/api/posts',
5740
+ * method: 'GET',
5741
+ * [EnableHydration]: true
5742
+ * });
5743
+ *
5744
+ * @typedoc
5730
5745
  */
5731
- const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
5732
- function isErrorDocument(document) {
5733
- return 'errors' in document;
5734
- }
5746
+ const CacheHandler = {
5747
+ request(context, next) {
5748
+ // if we have no cache or no cache-key skip cache handling
5749
+ if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
5750
+ return next(context.request);
5751
+ }
5752
+ const {
5753
+ store
5754
+ } = context.request;
5755
+ const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
5756
+ if (identifier) {
5757
+ context.setIdentifier(identifier);
5758
+ }
5759
+
5760
+ // used to dedupe existing requests that match
5761
+ const DEDUPE = store.requestManager._deduped;
5762
+ const activeRequest = identifier && DEDUPE.get(identifier);
5763
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
5764
+
5765
+ // determine if we should skip cache
5766
+ if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
5767
+ if (activeRequest) {
5768
+ activeRequest.priority = {
5769
+ blocking: true
5770
+ };
5771
+ return activeRequest.promise;
5772
+ }
5773
+ let promise = fetchContentAndHydrate(next, context, identifier, {
5774
+ blocking: true
5775
+ });
5776
+ if (identifier) {
5777
+ promise = promise.finally(() => {
5778
+ DEDUPE.delete(identifier);
5779
+ store.notifications.notify(identifier, 'state');
5780
+ });
5781
+ DEDUPE.set(identifier, {
5782
+ priority: {
5783
+ blocking: true
5784
+ },
5785
+ promise
5786
+ });
5787
+ store.notifications.notify(identifier, 'state');
5788
+ }
5789
+ return promise;
5790
+ }
5791
+
5792
+ // if we have not skipped cache, determine if we should update behind the scenes
5793
+ if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
5794
+ let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
5795
+ blocking: false
5796
+ });
5797
+ if (identifier && !activeRequest) {
5798
+ promise = promise.finally(() => {
5799
+ DEDUPE.delete(identifier);
5800
+ store.notifications.notify(identifier, 'state');
5801
+ });
5802
+ DEDUPE.set(identifier, {
5803
+ priority: {
5804
+ blocking: false
5805
+ },
5806
+ promise
5807
+ });
5808
+ store.notifications.notify(identifier, 'state');
5809
+ }
5810
+ store.requestManager._pending.set(context.id, promise);
5811
+ }
5812
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
5813
+ if (!test) {
5814
+ throw new Error(`Expected a peeked request to be present`);
5815
+ }
5816
+ })(peeked) : {};
5817
+ const shouldHydrate = context.request[EnableHydration] || false;
5818
+ context.setResponse(peeked.response);
5819
+ if ('error' in peeked) {
5820
+ const content = shouldHydrate ? maybeUpdateErrorUiObjects(store, {
5821
+ shouldHydrate,
5822
+ identifier
5823
+ }, peeked.content, true) : peeked.content;
5824
+ const newError = cloneError(peeked);
5825
+ newError.content = content;
5826
+ throw newError;
5827
+ }
5828
+ const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
5829
+ shouldHydrate,
5830
+ identifier
5831
+ }, peeked.content, true) : peeked.content;
5832
+ return result;
5833
+ }
5834
+ };
5735
5835
  function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5736
5836
  const {
5737
5837
  identifier
@@ -5744,26 +5844,6 @@ function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5744
5844
  })(!options.shouldHydrate) : {};
5745
5845
  return document;
5746
5846
  }
5747
- if (isErrorDocument(document)) {
5748
- if (!identifier && !options.shouldHydrate) {
5749
- return document;
5750
- }
5751
- let doc;
5752
- if (identifier) {
5753
- doc = store._documentCache.get(identifier);
5754
- }
5755
- if (!doc) {
5756
- doc = new Document(store, identifier);
5757
- copyDocumentProperties(doc, document);
5758
- if (identifier) {
5759
- store._documentCache.set(identifier, doc);
5760
- }
5761
- } else if (!isFromCache) {
5762
- doc.data = undefined;
5763
- copyDocumentProperties(doc, document);
5764
- }
5765
- return options.shouldHydrate ? doc : document;
5766
- }
5767
5847
  if (Array.isArray(document.data)) {
5768
5848
  const {
5769
5849
  recordArrayManager
@@ -5831,26 +5911,119 @@ function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5831
5911
  return options.shouldHydrate ? doc : document;
5832
5912
  }
5833
5913
  }
5834
- function calcShouldFetch(store, request, hasCachedValue, identifier) {
5914
+ function maybeUpdateErrorUiObjects(store, options, document, isFromCache) {
5835
5915
  const {
5836
- cacheOptions
5837
- } = request;
5838
- return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
5916
+ identifier
5917
+ } = options;
5918
+
5919
+ // TODO investigate why ResourceErrorDocument is insufficient for expressing all error types
5920
+ if (!isErrorDocument(document) || !identifier && !options.shouldHydrate) {
5921
+ return document;
5922
+ }
5923
+ let doc;
5924
+ if (identifier) {
5925
+ doc = store._documentCache.get(identifier);
5926
+ }
5927
+ if (!doc) {
5928
+ doc = new Document(store, identifier);
5929
+ copyDocumentProperties(doc, document);
5930
+ if (identifier) {
5931
+ store._documentCache.set(identifier, doc);
5932
+ }
5933
+ } else if (!isFromCache) {
5934
+ doc.data = undefined;
5935
+ copyDocumentProperties(doc, document);
5936
+ }
5937
+ return options.shouldHydrate ? doc : document;
5839
5938
  }
5840
- function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
5939
+ function updateCacheForSuccess(store, request, options, document) {
5940
+ let response = null;
5941
+ if (isMutation(request)) {
5942
+ const record = request.data?.record || request.records?.[0];
5943
+ if (record) {
5944
+ response = store.cache.didCommit(record, document);
5945
+
5946
+ // a mutation combined with a 204 has no cache impact when no known records were involved
5947
+ // a createRecord with a 201 with an empty response and no known records should similarly
5948
+ // have no cache impact
5949
+ } else if (isCacheAffecting(document)) {
5950
+ response = store.cache.put(document);
5951
+ }
5952
+ } else {
5953
+ response = store.cache.put(document);
5954
+ }
5955
+ return maybeUpdateUiObjects(store, request, options, response, false);
5956
+ }
5957
+ function handleFetchSuccess(store, context, options, document) {
5841
5958
  const {
5842
- cacheOptions
5843
- } = request;
5844
- return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
5959
+ request
5960
+ } = context;
5961
+ store.requestManager._pending.delete(context.id);
5962
+ store._enableAsyncFlush = true;
5963
+ let response;
5964
+ store._join(() => {
5965
+ response = updateCacheForSuccess(store, request, options, document);
5966
+ });
5967
+ store._enableAsyncFlush = null;
5968
+ if (store.lifetimes?.didRequest) {
5969
+ store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
5970
+ }
5971
+ const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
5972
+ if (finalPriority.blocking) {
5973
+ return response;
5974
+ } else {
5975
+ store.notifications._flush();
5976
+ }
5845
5977
  }
5846
- function isMutation(request) {
5847
- return Boolean(request.op && MUTATION_OPS.has(request.op));
5978
+ function updateCacheForError(store, context, options, error) {
5979
+ let response;
5980
+ if (isMutation(context.request)) {
5981
+ // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
5982
+ // currently we let the response remain undefiend.
5983
+ const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
5984
+ const record = context.request.data?.record || context.request.records?.[0];
5985
+ store.cache.commitWasRejected(record, errors);
5986
+ } else {
5987
+ response = store.cache.put(error);
5988
+ return maybeUpdateErrorUiObjects(store, options, response, false);
5989
+ }
5990
+ }
5991
+ function handleFetchError(store, context, options, error) {
5992
+ store.requestManager._pending.delete(context.id);
5993
+ if (context.request.signal?.aborted) {
5994
+ throw error;
5995
+ }
5996
+ store._enableAsyncFlush = true;
5997
+ let response;
5998
+ store._join(() => {
5999
+ response = updateCacheForError(store, context, options, error);
6000
+ });
6001
+ store._enableAsyncFlush = null;
6002
+ if (options.identifier && store.lifetimes?.didRequest) {
6003
+ store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
6004
+ }
6005
+ if (isMutation(context.request)) {
6006
+ throw error;
6007
+ }
6008
+ const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
6009
+ if (finalPriority.blocking) {
6010
+ const newError = cloneError(error);
6011
+ newError.content = response;
6012
+ throw newError;
6013
+ } else {
6014
+ store.notifications._flush();
6015
+ }
5848
6016
  }
5849
- function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBackgroundFetch) {
6017
+ function fetchContentAndHydrate(next, context, identifier, priority) {
5850
6018
  const {
5851
6019
  store
5852
6020
  } = context.request;
5853
6021
  const shouldHydrate = context.request[EnableHydration] || false;
6022
+ const options = {
6023
+ shouldHydrate,
6024
+ identifier,
6025
+ priority
6026
+ };
5854
6027
  let isMut = false;
5855
6028
  if (isMutation(context.request)) {
5856
6029
  isMut = true;
@@ -5869,78 +6042,9 @@ function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBa
5869
6042
  store.lifetimes.willRequest(context.request, identifier, store);
5870
6043
  }
5871
6044
  const promise = next(context.request).then(document => {
5872
- store.requestManager._pending.delete(context.id);
5873
- store._enableAsyncFlush = true;
5874
- let response;
5875
- store._join(() => {
5876
- if (isMutation(context.request)) {
5877
- const record = context.request.data?.record || context.request.records?.[0];
5878
- if (record) {
5879
- response = store.cache.didCommit(record, document);
5880
-
5881
- // a mutation combined with a 204 has no cache impact when no known records were involved
5882
- // a createRecord with a 201 with an empty response and no known records should similarly
5883
- // have no cache impact
5884
- } else if (isCacheAffecting(document)) {
5885
- response = store.cache.put(document);
5886
- }
5887
- } else {
5888
- response = store.cache.put(document);
5889
- }
5890
- response = maybeUpdateUiObjects(store, context.request, {
5891
- shouldHydrate,
5892
- shouldFetch,
5893
- shouldBackgroundFetch,
5894
- identifier
5895
- }, response, false);
5896
- });
5897
- store._enableAsyncFlush = null;
5898
- if (store.lifetimes?.didRequest) {
5899
- store.lifetimes.didRequest(context.request, document.response, identifier, store);
5900
- }
5901
- if (shouldFetch) {
5902
- return response;
5903
- } else if (shouldBackgroundFetch) {
5904
- store.notifications._flush();
5905
- }
6045
+ return handleFetchSuccess(store, context, options, document);
5906
6046
  }, error => {
5907
- store.requestManager._pending.delete(context.id);
5908
- if (context.request.signal?.aborted) {
5909
- throw error;
5910
- }
5911
- store.requestManager._pending.delete(context.id);
5912
- store._enableAsyncFlush = true;
5913
- let response;
5914
- store._join(() => {
5915
- if (isMutation(context.request)) {
5916
- // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
5917
- // currently we let the response remain undefiend.
5918
- const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
5919
- const record = context.request.data?.record || context.request.records?.[0];
5920
- store.cache.commitWasRejected(record, errors);
5921
- // re-throw the original error to preserve `errors` property.
5922
- throw error;
5923
- } else {
5924
- response = store.cache.put(error);
5925
- response = maybeUpdateUiObjects(store, context.request, {
5926
- shouldHydrate,
5927
- shouldFetch,
5928
- shouldBackgroundFetch,
5929
- identifier
5930
- }, response, false);
5931
- }
5932
- });
5933
- store._enableAsyncFlush = null;
5934
- if (identifier && store.lifetimes?.didRequest) {
5935
- store.lifetimes.didRequest(context.request, error.response, identifier, store);
5936
- }
5937
- if (!shouldBackgroundFetch) {
5938
- const newError = cloneError(error);
5939
- newError.content = response;
5940
- throw newError;
5941
- } else {
5942
- store.notifications._flush();
5943
- }
6047
+ return handleFetchError(store, context, options, error);
5944
6048
  });
5945
6049
  if (!isMut) {
5946
6050
  return promise;
@@ -5962,129 +6066,4 @@ function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBa
5962
6066
  }]
5963
6067
  });
5964
6068
  }
5965
- function isAggregateError(error) {
5966
- return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
5967
- }
5968
- // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
5969
- function cloneError(error) {
5970
- const isAggregate = isAggregateError(error);
5971
- const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
5972
- cloned.stack = error.stack;
5973
- cloned.error = error.error;
5974
-
5975
- // copy over enumerable properties
5976
- Object.assign(cloned, error);
5977
- return cloned;
5978
- }
5979
-
5980
- /**
5981
- * A CacheHandler that adds support for using an EmberData Cache with a RequestManager.
5982
- *
5983
- * This handler will only run when a request has supplied a `store` instance. Requests
5984
- * issued by the store via `store.request()` will automatically have the `store` instance
5985
- * attached to the request.
5986
- *
5987
- * ```ts
5988
- * requestManager.request({
5989
- * store: store,
5990
- * url: '/api/posts',
5991
- * method: 'GET'
5992
- * });
5993
- * ```
5994
- *
5995
- * When this handler elects to handle a request, it will return the raw `StructuredDocument`
5996
- * unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
5997
- * return a `Document` instance that will automatically update the UI when the cache is updated
5998
- * in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
5999
- *
6000
- * When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
6001
- * means that if desired you can issue requests that utilize the cache without needing to also
6002
- * utilize Record instances if desired.
6003
- *
6004
- * Said differently, you could elect to issue all requests via a RequestManager, without ever using
6005
- * the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
6006
- * necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
6007
- * approach of EmberData allows for this flexibility.
6008
- *
6009
- * ```ts
6010
- * import { EnableHydration } from '@warp-drive/core-types/request';
6011
- *
6012
- * requestManager.request({
6013
- * store: store,
6014
- * url: '/api/posts',
6015
- * method: 'GET',
6016
- * [EnableHydration]: true
6017
- * });
6018
- *
6019
- * @typedoc
6020
- */
6021
- const CacheHandler = {
6022
- request(context, next) {
6023
- // if we have no cache or no cache-key skip cache handling
6024
- if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
6025
- return next(context.request);
6026
- }
6027
- const {
6028
- store
6029
- } = context.request;
6030
- const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
6031
- const peeked = identifier ? store.cache.peekRequest(identifier) : null;
6032
-
6033
- // determine if we should skip cache
6034
- if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
6035
- return fetchContentAndHydrate(next, context, identifier, true, false);
6036
- }
6037
-
6038
- // if we have not skipped cache, determine if we should update behind the scenes
6039
- if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
6040
- const promise = fetchContentAndHydrate(next, context, identifier, false, true);
6041
- store.requestManager._pending.set(context.id, promise);
6042
- }
6043
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
6044
- if (!test) {
6045
- throw new Error(`Expected a peeked request to be present`);
6046
- }
6047
- })(peeked) : {};
6048
- const shouldHydrate = context.request[EnableHydration] || false;
6049
- context.setResponse(peeked.response);
6050
- if ('error' in peeked) {
6051
- const content = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
6052
- shouldHydrate,
6053
- identifier
6054
- }, peeked.content, true) : peeked.content;
6055
- const newError = cloneError(peeked);
6056
- newError.content = content;
6057
- throw newError;
6058
- }
6059
- const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
6060
- shouldHydrate,
6061
- identifier
6062
- }, peeked.content, true) : peeked.content;
6063
- return result;
6064
- }
6065
- };
6066
- function copyDocumentProperties(target, source) {
6067
- if ('links' in source) {
6068
- target.links = source.links;
6069
- }
6070
- if ('meta' in source) {
6071
- target.meta = source.meta;
6072
- }
6073
- if ('errors' in source) {
6074
- target.errors = source.errors;
6075
- }
6076
- }
6077
- function isCacheAffecting(document) {
6078
- if (!isMutation(document.request)) {
6079
- return true;
6080
- }
6081
- // a mutation combined with a 204 has no cache impact when no known records were involved
6082
- // a createRecord with a 201 with an empty response and no known records should similarly
6083
- // have no cache impact
6084
-
6085
- if (document.request.op === 'createRecord' && document.response?.status === 201) {
6086
- return document.content ? Object.keys(document.content).length > 0 : false;
6087
- }
6088
- return document.response?.status !== 204;
6089
- }
6090
6069
  export { ARRAY_SIGNAL as A, CacheHandler as C, IdentifierArray as I, MUTATE as M, RecordArrayManager as R, Store as S, _clearCaches as _, setIdentifierGenerationMethod as a, setIdentifierUpdateMethod as b, setIdentifierForgetMethod as c, setIdentifierResetMethod as d, setKeyInfoForResource as e, constructResource as f, coerceId as g, ensureStringId as h, isStableIdentifier as i, Collection as j, SOURCE as k, fastPush as l, removeRecordDataFor as m, notifyArray as n, setRecordIdentifier as o, peekCache as p, StoreMap as q, recordIdentifierFor as r, storeFor as s, setCacheFor as t, normalizeModelName as u };