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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/dist/-private.js +1 -1
  2. package/dist/{cache-handler-C5ilAUZ5.js → handler-CET_Ja8V.js} +333 -253
  3. package/dist/handler-CET_Ja8V.js.map +1 -0
  4. package/dist/index.js +1 -1
  5. package/package.json +17 -17
  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 +4 -2
  16. package/unstable-preview-types/-private/managers/notification-manager.d.ts.map +1 -1
  17. package/unstable-preview-types/-private/store-service.d.ts +27 -7
  18. package/unstable-preview-types/-private/store-service.d.ts.map +1 -1
  19. package/unstable-preview-types/-private.d.ts +3 -2
  20. package/unstable-preview-types/-private.d.ts.map +1 -1
  21. package/unstable-preview-types/index.d.ts +18 -16
  22. package/unstable-preview-types/index.d.ts.map +1 -1
  23. package/dist/cache-handler-C5ilAUZ5.js.map +0 -1
  24. package/unstable-preview-types/-private/cache-handler.d.ts.map +0 -1
@@ -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
  }
@@ -2158,7 +2159,7 @@ class CacheManager {
2158
2159
  */
2159
2160
  // eslint-disable-next-line no-restricted-imports
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?.();
@@ -3766,6 +3768,17 @@ class Store extends BaseClass {
3766
3768
 
3767
3769
  // Private
3768
3770
 
3771
+ /**
3772
+ * Async flush buffers notifications until flushed
3773
+ * by finalization of a future configured by store.request
3774
+ *
3775
+ * This is useful for ensuring that notifications are delivered
3776
+ * prior to the promise resolving but without risk of promise
3777
+ * interleaving.
3778
+ *
3779
+ * @internal
3780
+ */
3781
+
3769
3782
  // DEBUG-only properties
3770
3783
 
3771
3784
  get isDestroying() {
@@ -3839,6 +3852,16 @@ class Store extends BaseClass {
3839
3852
  this._cbs = null;
3840
3853
  }
3841
3854
  }
3855
+
3856
+ /**
3857
+ * Executes the callback, ensurng that any work that calls
3858
+ * store._schedule is executed after in the right order.
3859
+ *
3860
+ * When queues already exist, scheduled callbacks will
3861
+ * join the existing queue.
3862
+ *
3863
+ * @internal
3864
+ */
3842
3865
  _join(cb) {
3843
3866
  if (this._cbs) {
3844
3867
  cb();
@@ -4402,7 +4425,7 @@ class Store extends BaseClass {
4402
4425
  ```app/routes/post.js
4403
4426
  export default class PostRoute extends Route {
4404
4427
  model(params) {
4405
- return this.store.findRecord('post', params.post_id, { include: 'comments' });
4428
+ return this.store.findRecord('post', params.post_id, { include: ['comments'] });
4406
4429
  }
4407
4430
  }
4408
4431
  ```
@@ -4424,13 +4447,13 @@ class Store extends BaseClass {
4424
4447
  In this case, the post's comments would then be available in your template as
4425
4448
  `model.comments`.
4426
4449
  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
4450
+ list of relationship names, while nested relationships can be specified
4428
4451
  using a dot-separated sequence of relationship names. So to request both the post's
4429
4452
  comments and the authors of those comments the request would look like this:
4430
4453
  ```app/routes/post.js
4431
4454
  export default class PostRoute extends Route {
4432
4455
  model(params) {
4433
- return this.store.findRecord('post', params.post_id, { include: 'comments,comments.author' });
4456
+ return this.store.findRecord('post', params.post_id, { include: ['comments','comments.author'] });
4434
4457
  }
4435
4458
  }
4436
4459
  ```
@@ -4951,18 +4974,18 @@ class Store extends BaseClass {
4951
4974
  ```app/routes/posts.js
4952
4975
  export default class PostsRoute extends Route {
4953
4976
  model() {
4954
- return this.store.findAll('post', { include: 'comments' });
4977
+ return this.store.findAll('post', { include: ['comments'] });
4955
4978
  }
4956
4979
  }
4957
4980
  ```
4958
4981
  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
4982
+ list or relationship names, while nested relationships can be specified
4960
4983
  using a dot-separated sequence of relationship names. So to request both the posts'
4961
4984
  comments and the authors of those comments the request would look like this:
4962
4985
  ```app/routes/posts.js
4963
4986
  export default class PostsRoute extends Route {
4964
4987
  model() {
4965
- return this.store.findAll('post', { include: 'comments,comments.author' });
4988
+ return this.store.findAll('post', { include: ['comments','comments.author'] });
4966
4989
  }
4967
4990
  }
4968
4991
  ```
@@ -5238,7 +5261,7 @@ class Store extends BaseClass {
5238
5261
  const data = JSON.parse(JSON.stringify(jsonApiDoc));
5239
5262
  // eslint-disable-next-line no-console
5240
5263
  console.log('EmberData | Payload - push', data);
5241
- } catch (e) {
5264
+ } catch {
5242
5265
  // eslint-disable-next-line no-console
5243
5266
  console.log('EmberData | Payload - push', jsonApiDoc);
5244
5267
  }
@@ -5709,29 +5732,207 @@ defineSignal(Document.prototype, 'data');
5709
5732
  defineSignal(Document.prototype, 'links');
5710
5733
  defineSignal(Document.prototype, 'errors');
5711
5734
  defineSignal(Document.prototype, 'meta');
5735
+ const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
5736
+ function calcShouldFetch(store, request, hasCachedValue, identifier) {
5737
+ const {
5738
+ cacheOptions
5739
+ } = request;
5740
+ return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
5741
+ }
5742
+ function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
5743
+ const {
5744
+ cacheOptions
5745
+ } = request;
5746
+ return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
5747
+ }
5748
+ function isMutation(request) {
5749
+ return Boolean(request.op && MUTATION_OPS.has(request.op));
5750
+ }
5751
+ function copyDocumentProperties(target, source) {
5752
+ if ('links' in source) {
5753
+ target.links = source.links;
5754
+ }
5755
+ if ('meta' in source) {
5756
+ target.meta = source.meta;
5757
+ }
5758
+ if ('errors' in source) {
5759
+ target.errors = source.errors;
5760
+ }
5761
+ }
5762
+ function isCacheAffecting(document) {
5763
+ if (!isMutation(document.request)) {
5764
+ return true;
5765
+ }
5766
+ // a mutation combined with a 204 has no cache impact when no known records were involved
5767
+ // a createRecord with a 201 with an empty response and no known records should similarly
5768
+ // have no cache impact
5769
+
5770
+ if (document.request.op === 'createRecord' && document.response?.status === 201) {
5771
+ return document.content ? Object.keys(document.content).length > 0 : false;
5772
+ }
5773
+ return document.response?.status !== 204;
5774
+ }
5775
+ function isAggregateError(error) {
5776
+ return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
5777
+ }
5778
+ // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
5779
+ function cloneError(error) {
5780
+ const isAggregate = isAggregateError(error);
5781
+ const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
5782
+ cloned.stack = error.stack;
5783
+ cloned.error = error.error;
5784
+
5785
+ // copy over enumerable properties
5786
+ Object.assign(cloned, error);
5787
+ return cloned;
5788
+ }
5789
+ function isErrorDocument(document) {
5790
+ return 'errors' in document;
5791
+ }
5792
+ function getPriority(identifier, deduped, priority) {
5793
+ if (identifier) {
5794
+ const existing = deduped.get(identifier);
5795
+ if (existing) {
5796
+ return existing.priority;
5797
+ }
5798
+ }
5799
+ return priority;
5800
+ }
5712
5801
 
5713
5802
  /**
5714
5803
  * @module @ember-data/store
5715
5804
  */
5716
5805
 
5717
5806
  /**
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.
5807
+ * A CacheHandler that adds support for using an EmberData Cache with a RequestManager.
5721
5808
  *
5722
- * The default behavior for request lifetimes is to never expire
5723
- * unless manually refreshed via `cacheOptions.reload` or `cacheOptions.backgroundReload`.
5809
+ * This handler will only run when a request has supplied a `store` instance. Requests
5810
+ * issued by the store via `store.request()` will automatically have the `store` instance
5811
+ * attached to the request.
5724
5812
  *
5725
- * Implementing this service allows you to programatically define
5726
- * when a request should be considered expired.
5813
+ * ```ts
5814
+ * requestManager.request({
5815
+ * store: store,
5816
+ * url: '/api/posts',
5817
+ * method: 'GET'
5818
+ * });
5819
+ * ```
5727
5820
  *
5728
- * @class <Interface> CachePolicy
5729
- * @public
5821
+ * When this handler elects to handle a request, it will return the raw `StructuredDocument`
5822
+ * unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
5823
+ * return a `Document` instance that will automatically update the UI when the cache is updated
5824
+ * in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
5825
+ *
5826
+ * When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
5827
+ * means that if desired you can issue requests that utilize the cache without needing to also
5828
+ * utilize Record instances if desired.
5829
+ *
5830
+ * Said differently, you could elect to issue all requests via a RequestManager, without ever using
5831
+ * the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
5832
+ * necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
5833
+ * approach of EmberData allows for this flexibility.
5834
+ *
5835
+ * ```ts
5836
+ * import { EnableHydration } from '@warp-drive/core-types/request';
5837
+ *
5838
+ * requestManager.request({
5839
+ * store: store,
5840
+ * url: '/api/posts',
5841
+ * method: 'GET',
5842
+ * [EnableHydration]: true
5843
+ * });
5844
+ *
5845
+ * @typedoc
5730
5846
  */
5731
- const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
5732
- function isErrorDocument(document) {
5733
- return 'errors' in document;
5734
- }
5847
+ const CacheHandler = {
5848
+ request(context, next) {
5849
+ // if we have no cache or no cache-key skip cache handling
5850
+ if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
5851
+ return next(context.request);
5852
+ }
5853
+ const {
5854
+ store
5855
+ } = context.request;
5856
+ const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
5857
+ if (identifier) {
5858
+ context.setIdentifier(identifier);
5859
+ }
5860
+
5861
+ // used to dedupe existing requests that match
5862
+ const DEDUPE = store.requestManager._deduped;
5863
+ const activeRequest = identifier && DEDUPE.get(identifier);
5864
+ const peeked = identifier ? store.cache.peekRequest(identifier) : null;
5865
+
5866
+ // determine if we should skip cache
5867
+ if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
5868
+ if (activeRequest) {
5869
+ activeRequest.priority = {
5870
+ blocking: true
5871
+ };
5872
+ return activeRequest.promise;
5873
+ }
5874
+ let promise = fetchContentAndHydrate(next, context, identifier, {
5875
+ blocking: true
5876
+ });
5877
+ if (identifier) {
5878
+ promise = promise.finally(() => {
5879
+ DEDUPE.delete(identifier);
5880
+ store.notifications.notify(identifier, 'state');
5881
+ });
5882
+ DEDUPE.set(identifier, {
5883
+ priority: {
5884
+ blocking: true
5885
+ },
5886
+ promise
5887
+ });
5888
+ store.notifications.notify(identifier, 'state');
5889
+ }
5890
+ return promise;
5891
+ }
5892
+
5893
+ // if we have not skipped cache, determine if we should update behind the scenes
5894
+ if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
5895
+ let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
5896
+ blocking: false
5897
+ });
5898
+ if (identifier && !activeRequest) {
5899
+ promise = promise.finally(() => {
5900
+ DEDUPE.delete(identifier);
5901
+ store.notifications.notify(identifier, 'state');
5902
+ });
5903
+ DEDUPE.set(identifier, {
5904
+ priority: {
5905
+ blocking: false
5906
+ },
5907
+ promise
5908
+ });
5909
+ store.notifications.notify(identifier, 'state');
5910
+ }
5911
+ store.requestManager._pending.set(context.id, promise);
5912
+ }
5913
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
5914
+ if (!test) {
5915
+ throw new Error(`Expected a peeked request to be present`);
5916
+ }
5917
+ })(peeked) : {};
5918
+ const shouldHydrate = context.request[EnableHydration] || false;
5919
+ context.setResponse(peeked.response);
5920
+ if ('error' in peeked) {
5921
+ const content = shouldHydrate ? maybeUpdateErrorUiObjects(store, {
5922
+ shouldHydrate,
5923
+ identifier
5924
+ }, peeked.content, true) : peeked.content;
5925
+ const newError = cloneError(peeked);
5926
+ newError.content = content;
5927
+ throw newError;
5928
+ }
5929
+ const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
5930
+ shouldHydrate,
5931
+ identifier
5932
+ }, peeked.content, true) : peeked.content;
5933
+ return result;
5934
+ }
5935
+ };
5735
5936
  function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5736
5937
  const {
5737
5938
  identifier
@@ -5744,26 +5945,6 @@ function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5744
5945
  })(!options.shouldHydrate) : {};
5745
5946
  return document;
5746
5947
  }
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
5948
  if (Array.isArray(document.data)) {
5768
5949
  const {
5769
5950
  recordArrayManager
@@ -5831,26 +6012,119 @@ function maybeUpdateUiObjects(store, request, options, document, isFromCache) {
5831
6012
  return options.shouldHydrate ? doc : document;
5832
6013
  }
5833
6014
  }
5834
- function calcShouldFetch(store, request, hasCachedValue, identifier) {
6015
+ function maybeUpdateErrorUiObjects(store, options, document, isFromCache) {
5835
6016
  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);
6017
+ identifier
6018
+ } = options;
6019
+
6020
+ // TODO investigate why ResourceErrorDocument is insufficient for expressing all error types
6021
+ if (!isErrorDocument(document) || !identifier && !options.shouldHydrate) {
6022
+ return document;
6023
+ }
6024
+ let doc;
6025
+ if (identifier) {
6026
+ doc = store._documentCache.get(identifier);
6027
+ }
6028
+ if (!doc) {
6029
+ doc = new Document(store, identifier);
6030
+ copyDocumentProperties(doc, document);
6031
+ if (identifier) {
6032
+ store._documentCache.set(identifier, doc);
6033
+ }
6034
+ } else if (!isFromCache) {
6035
+ doc.data = undefined;
6036
+ copyDocumentProperties(doc, document);
6037
+ }
6038
+ return options.shouldHydrate ? doc : document;
5839
6039
  }
5840
- function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
6040
+ function updateCacheForSuccess(store, request, options, document) {
6041
+ let response = null;
6042
+ if (isMutation(request)) {
6043
+ const record = request.data?.record || request.records?.[0];
6044
+ if (record) {
6045
+ response = store.cache.didCommit(record, document);
6046
+
6047
+ // a mutation combined with a 204 has no cache impact when no known records were involved
6048
+ // a createRecord with a 201 with an empty response and no known records should similarly
6049
+ // have no cache impact
6050
+ } else if (isCacheAffecting(document)) {
6051
+ response = store.cache.put(document);
6052
+ }
6053
+ } else {
6054
+ response = store.cache.put(document);
6055
+ }
6056
+ return maybeUpdateUiObjects(store, request, options, response, false);
6057
+ }
6058
+ function handleFetchSuccess(store, context, options, document) {
5841
6059
  const {
5842
- cacheOptions
5843
- } = request;
5844
- return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
6060
+ request
6061
+ } = context;
6062
+ store.requestManager._pending.delete(context.id);
6063
+ store._enableAsyncFlush = true;
6064
+ let response;
6065
+ store._join(() => {
6066
+ response = updateCacheForSuccess(store, request, options, document);
6067
+ });
6068
+ store._enableAsyncFlush = null;
6069
+ if (store.lifetimes?.didRequest) {
6070
+ store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
6071
+ }
6072
+ const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
6073
+ if (finalPriority.blocking) {
6074
+ return response;
6075
+ } else {
6076
+ store.notifications._flush();
6077
+ }
5845
6078
  }
5846
- function isMutation(request) {
5847
- return Boolean(request.op && MUTATION_OPS.has(request.op));
6079
+ function updateCacheForError(store, context, options, error) {
6080
+ let response;
6081
+ if (isMutation(context.request)) {
6082
+ // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
6083
+ // currently we let the response remain undefiend.
6084
+ const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
6085
+ const record = context.request.data?.record || context.request.records?.[0];
6086
+ store.cache.commitWasRejected(record, errors);
6087
+ } else {
6088
+ response = store.cache.put(error);
6089
+ return maybeUpdateErrorUiObjects(store, options, response, false);
6090
+ }
5848
6091
  }
5849
- function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBackgroundFetch) {
6092
+ function handleFetchError(store, context, options, error) {
6093
+ store.requestManager._pending.delete(context.id);
6094
+ if (context.request.signal?.aborted) {
6095
+ throw error;
6096
+ }
6097
+ store._enableAsyncFlush = true;
6098
+ let response;
6099
+ store._join(() => {
6100
+ response = updateCacheForError(store, context, options, error);
6101
+ });
6102
+ store._enableAsyncFlush = null;
6103
+ if (options.identifier && store.lifetimes?.didRequest) {
6104
+ store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
6105
+ }
6106
+ if (isMutation(context.request)) {
6107
+ throw error;
6108
+ }
6109
+ const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
6110
+ if (finalPriority.blocking) {
6111
+ const newError = cloneError(error);
6112
+ newError.content = response;
6113
+ throw newError;
6114
+ } else {
6115
+ store.notifications._flush();
6116
+ }
6117
+ }
6118
+ function fetchContentAndHydrate(next, context, identifier, priority) {
5850
6119
  const {
5851
6120
  store
5852
6121
  } = context.request;
5853
6122
  const shouldHydrate = context.request[EnableHydration] || false;
6123
+ const options = {
6124
+ shouldHydrate,
6125
+ identifier,
6126
+ priority
6127
+ };
5854
6128
  let isMut = false;
5855
6129
  if (isMutation(context.request)) {
5856
6130
  isMut = true;
@@ -5869,78 +6143,9 @@ function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBa
5869
6143
  store.lifetimes.willRequest(context.request, identifier, store);
5870
6144
  }
5871
6145
  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
- }
6146
+ return handleFetchSuccess(store, context, options, document);
5906
6147
  }, 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
- }
6148
+ return handleFetchError(store, context, options, error);
5944
6149
  });
5945
6150
  if (!isMut) {
5946
6151
  return promise;
@@ -5962,129 +6167,4 @@ function fetchContentAndHydrate(next, context, identifier, shouldFetch, shouldBa
5962
6167
  }]
5963
6168
  });
5964
6169
  }
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
6170
  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 };