@ember-data/store 4.12.0-alpha.13 → 4.12.0-alpha.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/addon/-private.js CHANGED
@@ -1 +1 @@
1
- export { C as AdapterPopulatedRecordArray, h as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, g as SOURCE, S as Store, _ as _clearCaches, e as coerceId, j as fastPush, i as isStableIdentifier, n as normalizeModelName, f as notifyArray, p as peekCache, r as recordIdentifierFor, k as removeRecordDataFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./index-5cfdce8a";
1
+ export { f as AdapterPopulatedRecordArray, C as CacheHandler, j as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, h as SOURCE, S as Store, _ as _clearCaches, e as coerceId, k as fastPush, i as isStableIdentifier, n as normalizeModelName, g as notifyArray, p as peekCache, r as recordIdentifierFor, l as removeRecordDataFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./index-35424a6b";
@@ -35,103 +35,6 @@ import ObjectProxy from '@ember/object/proxy';
35
35
  function normalizeModelName$1(modelName) {
36
36
  return dasherize(modelName);
37
37
  }
38
- function getHydratedContent(store, request, document) {
39
- if (Array.isArray(document.data)) {
40
- const {
41
- lid
42
- } = document;
43
- const {
44
- recordArrayManager
45
- } = store;
46
- if (!lid) {
47
- return recordArrayManager.createArray({
48
- identifiers: document.data,
49
- doc: document,
50
- query: request
51
- });
52
- }
53
- let managed = recordArrayManager._keyedArrays.get(lid);
54
- if (!managed) {
55
- managed = recordArrayManager.createArray({
56
- identifiers: document.data,
57
- doc: document
58
- });
59
- recordArrayManager._keyedArrays.set(lid, managed);
60
- } else {
61
- recordArrayManager.populateManagedArray(managed, document.data, document);
62
- }
63
- return managed;
64
- } else {
65
- return document.data ? store.peekRecord(document.data) : null;
66
- }
67
- }
68
- function calcShouldFetch(store, request, hasCachedValue, lid) {
69
- const {
70
- cacheOptions,
71
- url,
72
- method
73
- } = request;
74
- return cacheOptions?.reload || !hasCachedValue || store.lifetimes && lid && url && method ? store.lifetimes.isHardExpired(lid, url, method) : false;
75
- }
76
- function calcShouldBackgroundFetch(store, request, willFetch, lid) {
77
- const {
78
- cacheOptions,
79
- url,
80
- method
81
- } = request;
82
- return !willFetch && (cacheOptions?.backgroundReload || store.lifetimes && lid && url && method ? store.lifetimes.isSoftExpired(lid, url, method) : false);
83
- }
84
- function fetchContentAndHydrate(next, context, shouldFetch, shouldBackgroundFetch) {
85
- const {
86
- store
87
- } = context.request;
88
- return next(context.request).then(document => {
89
- const response = store.cache.put(document);
90
- if (shouldFetch) {
91
- return getHydratedContent(store, context.request, response);
92
- }
93
- }, error => {
94
- store.cache.put(error);
95
- // TODO @runspired this is probably not the right thing to throw so make sure we add a test
96
- if (!shouldBackgroundFetch) {
97
- throw error;
98
- }
99
- });
100
- }
101
- const CacheHandler = {
102
- request(context, next) {
103
- // if we are a legacy request, skip cache handling
104
- if (context.request.op && !context.request.url) {
105
- return next(context.request);
106
- }
107
- const {
108
- store
109
- } = context.request;
110
- const {
111
- cacheOptions,
112
- url,
113
- method
114
- } = context.request;
115
- const lid = cacheOptions?.key || method === 'GET' && url ? url : null;
116
- const peeked = lid ? store.cache.peekRequest({
117
- lid
118
- }) : null;
119
-
120
- // determine if we should skip cache
121
- if (calcShouldFetch(store, context.request, !!peeked, lid)) {
122
- return fetchContentAndHydrate(next, context, true, false);
123
- }
124
-
125
- // if we have not skipped cache, determine if we should update behind the scenes
126
- if (calcShouldBackgroundFetch(store, context.request, false, lid)) {
127
- void fetchContentAndHydrate(next, context, false, true);
128
- }
129
- if ('error' in peeked) {
130
- throw peeked.error;
131
- }
132
- return Promise.resolve(getHydratedContent(store, context.request, peeked.content));
133
- }
134
- };
135
38
 
136
39
  /*
137
40
  * Returns the Cache instance associated with a given
@@ -824,9 +727,9 @@ function _applyDecoratedDescriptor(target, property, decorators, descriptor, con
824
727
  return desc;
825
728
  }
826
729
  let tokenId = 0;
827
- const CacheOperations = new Set(['added', 'removed', 'state', 'updated']);
730
+ const CacheOperations$1 = new Set(['added', 'removed', 'state', 'updated']);
828
731
  function isCacheOperationValue(value) {
829
- return CacheOperations.has(value);
732
+ return CacheOperations$1.has(value);
830
733
  }
831
734
  function runLoopIsFlushing() {
832
735
  //@ts-expect-error
@@ -2923,7 +2826,7 @@ class InstanceCache {
2923
2826
  return reference;
2924
2827
  }
2925
2828
  recordIsLoaded(identifier, filterDeleted = false) {
2926
- const cache = macroCondition(getOwnConfig().deprecations.DEPRECATE_V1_RECORD_DATA) ? this.__instances.resourceCache.get(identifier) : this.cache;
2829
+ const cache = macroCondition(getOwnConfig().deprecations.DEPRECATE_V1_RECORD_DATA) ? this.__instances.resourceCache.get(identifier) || this.cache : this.cache;
2927
2830
  if (!cache) {
2928
2831
  return false;
2929
2832
  }
@@ -4391,6 +4294,7 @@ class RecordArrayManager {
4391
4294
  this._managed = new Set();
4392
4295
  this._pending = new Map();
4393
4296
  this._staged = new Map();
4297
+ this._keyedArrays = new Map();
4394
4298
  this._identifiers = RecordArraysCache;
4395
4299
  this._subscription = this.store.notifications.subscribe('resource', (identifier, type) => {
4396
4300
  if (type === 'added') {
@@ -4828,7 +4732,7 @@ class Store {
4828
4732
  * it as a service or by initializing it.
4829
4733
  *
4830
4734
  * ```ts
4831
- * import Store from '@ember-data/store';
4735
+ * import Store, { CacheHandler } from '@ember-data/store';
4832
4736
  * import RequestManager from '@ember-data/request';
4833
4737
  * import Fetch from '@ember/data/request/fetch';
4834
4738
  *
@@ -4837,6 +4741,7 @@ class Store {
4837
4741
  * super(...arguments);
4838
4742
  * this.requestManager = new RequestManager();
4839
4743
  * this.requestManager.use([Fetch]);
4744
+ * this.requestManager.useCache(CacheHandler);
4840
4745
  * }
4841
4746
  * }
4842
4747
  * ```
@@ -4849,13 +4754,22 @@ class Store {
4849
4754
  * A Property which an App may set to provide a Lifetimes Service
4850
4755
  * to control when a cached request becomes stale.
4851
4756
  *
4757
+ * Note, when defined, these methods will only be invoked if `key` `url` and `method`
4758
+ * are all present.
4759
+ *
4760
+ * `isSoftExpired` will only be invoked if `isHardExpired` returns `false`.
4761
+ *
4852
4762
  * ```ts
4853
4763
  * store.lifetimes = {
4854
4764
  * // make the request and ignore the current cache state
4855
- * isHardExpired() {}
4765
+ * isHardExpired(key: string, url: string, method?: HTTPMethod): boolean {
4766
+ * return false;
4767
+ * }
4856
4768
  *
4857
4769
  * // make the request in the background if true, return cache state
4858
- * isSoftExpired() {}
4770
+ * isSoftExpired(key: string, url: string, method: HTTPMethod): boolean {
4771
+ * return false;
4772
+ * }
4859
4773
  * }
4860
4774
  * ```
4861
4775
  *
@@ -4890,13 +4804,6 @@ class Store {
4890
4804
  this._serializerCache = Object.create(null);
4891
4805
  this._modelFactoryCache = Object.create(null);
4892
4806
  }
4893
- _registerCacheHandler() {
4894
- if (this._hasRegisteredCacheHandler) {
4895
- return;
4896
- }
4897
- this.requestManager.useCache(CacheHandler);
4898
- this._hasRegisteredCacheHandler = true;
4899
- }
4900
4807
  _run(cb) {
4901
4808
  assert(`EmberData should never encounter a nested run`, !this._cbs);
4902
4809
  const _cbs = this._cbs = {};
@@ -4981,7 +4888,6 @@ class Store {
4981
4888
  // we lazily set the cache handler when we issue the first request
4982
4889
  // because constructor doesn't allow for this to run after
4983
4890
  // the user has had the chance to set the prop.
4984
- this._registerCacheHandler();
4985
4891
  let opts = {
4986
4892
  store: this
4987
4893
  };
@@ -6892,6 +6798,127 @@ function secretInit(record, cache, identifier, store) {
6892
6798
  StoreMap.set(record, store);
6893
6799
  setCacheFor(record, cache);
6894
6800
  }
6801
+ const CacheOperations = new Set(['findRecord', 'findAll', 'query', 'queryRecord', 'findBelongsTo', 'findHasMany', 'updateRecord', 'createRecord', 'deleteRecord']);
6802
+ function getHydratedContent(store, request, document) {
6803
+ if (!request.op || !CacheOperations.has(request.op)) {
6804
+ return document;
6805
+ }
6806
+ if (Array.isArray(document.data)) {
6807
+ const {
6808
+ lid
6809
+ } = document;
6810
+ const {
6811
+ recordArrayManager
6812
+ } = store;
6813
+ if (!lid) {
6814
+ return recordArrayManager.createArray({
6815
+ identifiers: document.data,
6816
+ doc: document,
6817
+ query: request
6818
+ });
6819
+ }
6820
+ let managed = recordArrayManager._keyedArrays.get(lid);
6821
+ if (!managed) {
6822
+ managed = recordArrayManager.createArray({
6823
+ identifiers: document.data,
6824
+ doc: document
6825
+ });
6826
+ recordArrayManager._keyedArrays.set(lid, managed);
6827
+ } else {
6828
+ recordArrayManager.populateManagedArray(managed, document.data, document);
6829
+ }
6830
+ return managed;
6831
+ } else {
6832
+ switch (request.op) {
6833
+ case 'findBelongsTo':
6834
+ case 'queryRecord':
6835
+ case 'findRecord':
6836
+ return document.data ? store.peekRecord(document.data) : null;
6837
+ default:
6838
+ return document.data;
6839
+ }
6840
+ }
6841
+ }
6842
+ function calcShouldFetch(store, request, hasCachedValue, lid) {
6843
+ const {
6844
+ cacheOptions,
6845
+ url,
6846
+ method
6847
+ } = request;
6848
+ return cacheOptions?.reload || !hasCachedValue || (store.lifetimes && lid && url && method ? store.lifetimes.isHardExpired(lid, url, method) : false);
6849
+ }
6850
+ function calcShouldBackgroundFetch(store, request, willFetch, lid) {
6851
+ const {
6852
+ cacheOptions,
6853
+ url,
6854
+ method
6855
+ } = request;
6856
+ return !willFetch && (cacheOptions?.backgroundReload || (store.lifetimes && lid && url && method ? store.lifetimes.isSoftExpired(lid, url, method) : false));
6857
+ }
6858
+ function fetchContentAndHydrate(next, context, shouldFetch, shouldBackgroundFetch) {
6859
+ const {
6860
+ store
6861
+ } = context.request;
6862
+ return next(context.request).then(document => {
6863
+ store._enableAsyncFlush = true;
6864
+ let response;
6865
+ store._join(() => {
6866
+ response = store.cache.put(document);
6867
+ if (shouldFetch) {
6868
+ response = getHydratedContent(store, context.request, response);
6869
+ }
6870
+ });
6871
+ store._enableAsyncFlush = null;
6872
+ if (shouldFetch) {
6873
+ return response;
6874
+ }
6875
+ }, error => {
6876
+ store._enableAsyncFlush = true;
6877
+ store._join(() => {
6878
+ store.cache.put(error);
6879
+ });
6880
+ store._enableAsyncFlush = null;
6881
+
6882
+ // TODO @runspired this is probably not the right thing to throw so make sure we add a test
6883
+ if (!shouldBackgroundFetch) {
6884
+ throw error;
6885
+ }
6886
+ });
6887
+ }
6888
+ const CacheHandler = {
6889
+ request(context, next) {
6890
+ // if we are a legacy request or did not originate from the store, skip cache handling
6891
+ if (!context.request.store || context.request.op && CacheOperations.has(context.request.op) && !context.request.url) {
6892
+ return next(context.request);
6893
+ }
6894
+ const {
6895
+ store
6896
+ } = context.request;
6897
+ const {
6898
+ cacheOptions,
6899
+ url,
6900
+ method
6901
+ } = context.request;
6902
+ const lid = cacheOptions?.key || method === 'GET' && url ? url : null;
6903
+ const peeked = lid ? store.cache.peekRequest({
6904
+ lid
6905
+ }) : null;
6906
+
6907
+ // determine if we should skip cache
6908
+ if (calcShouldFetch(store, context.request, !!peeked, lid)) {
6909
+ return fetchContentAndHydrate(next, context, true, false);
6910
+ }
6911
+
6912
+ // if we have not skipped cache, determine if we should update behind the scenes
6913
+ if (calcShouldBackgroundFetch(store, context.request, false, lid)) {
6914
+ void fetchContentAndHydrate(next, context, false, true);
6915
+ }
6916
+ if ('error' in peeked) {
6917
+ throw peeked.error;
6918
+ }
6919
+ return Promise.resolve(getHydratedContent(store, context.request, peeked.content));
6920
+ }
6921
+ };
6895
6922
  function normalizeModelName(modelName) {
6896
6923
  if (macroCondition(getOwnConfig().deprecations.DEPRECATE_HELPERS)) {
6897
6924
  deprecate(`the helper function normalizeModelName is deprecated. You should use model names that are already normalized, or use string helpers of your own. This function is primarily an alias for dasherize from @ember/string.`, false, {
@@ -6907,4 +6934,4 @@ function normalizeModelName(modelName) {
6907
6934
  }
6908
6935
  assert(`normalizeModelName support has been removed`);
6909
6936
  }
6910
- export { Collection 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, coerceId as e, notifyArray as f, SOURCE as g, IDENTIFIER_ARRAY_TAG as h, isStableIdentifier as i, fastPush as j, removeRecordDataFor as k, normalizeModelName as n, peekCache as p, recordIdentifierFor as r, storeFor as s };
6937
+ export { 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, coerceId as e, Collection as f, notifyArray as g, SOURCE as h, isStableIdentifier as i, IDENTIFIER_ARRAY_TAG as j, fastPush as k, removeRecordDataFor as l, normalizeModelName as n, peekCache as p, recordIdentifierFor as r, storeFor as s };