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

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-123f3e7e";
@@ -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
@@ -4828,7 +4731,7 @@ class Store {
4828
4731
  * it as a service or by initializing it.
4829
4732
  *
4830
4733
  * ```ts
4831
- * import Store from '@ember-data/store';
4734
+ * import Store, { CacheHandler } from '@ember-data/store';
4832
4735
  * import RequestManager from '@ember-data/request';
4833
4736
  * import Fetch from '@ember/data/request/fetch';
4834
4737
  *
@@ -4837,6 +4740,7 @@ class Store {
4837
4740
  * super(...arguments);
4838
4741
  * this.requestManager = new RequestManager();
4839
4742
  * this.requestManager.use([Fetch]);
4743
+ * this.requestManager.useCache(CacheHandler);
4840
4744
  * }
4841
4745
  * }
4842
4746
  * ```
@@ -4849,13 +4753,22 @@ class Store {
4849
4753
  * A Property which an App may set to provide a Lifetimes Service
4850
4754
  * to control when a cached request becomes stale.
4851
4755
  *
4756
+ * Note, when defined, these methods will only be invoked if `key` `url` and `method`
4757
+ * are all present.
4758
+ *
4759
+ * `isSoftExpired` will only be invoked if `isHardExpired` returns `false`.
4760
+ *
4852
4761
  * ```ts
4853
4762
  * store.lifetimes = {
4854
4763
  * // make the request and ignore the current cache state
4855
- * isHardExpired() {}
4764
+ * isHardExpired(key: string, url: string, method?: HTTPMethod): boolean {
4765
+ * return false;
4766
+ * }
4856
4767
  *
4857
4768
  * // make the request in the background if true, return cache state
4858
- * isSoftExpired() {}
4769
+ * isSoftExpired(key: string, url: string, method: HTTPMethod): boolean {
4770
+ * return false;
4771
+ * }
4859
4772
  * }
4860
4773
  * ```
4861
4774
  *
@@ -4890,13 +4803,6 @@ class Store {
4890
4803
  this._serializerCache = Object.create(null);
4891
4804
  this._modelFactoryCache = Object.create(null);
4892
4805
  }
4893
- _registerCacheHandler() {
4894
- if (this._hasRegisteredCacheHandler) {
4895
- return;
4896
- }
4897
- this.requestManager.useCache(CacheHandler);
4898
- this._hasRegisteredCacheHandler = true;
4899
- }
4900
4806
  _run(cb) {
4901
4807
  assert(`EmberData should never encounter a nested run`, !this._cbs);
4902
4808
  const _cbs = this._cbs = {};
@@ -4981,7 +4887,6 @@ class Store {
4981
4887
  // we lazily set the cache handler when we issue the first request
4982
4888
  // because constructor doesn't allow for this to run after
4983
4889
  // the user has had the chance to set the prop.
4984
- this._registerCacheHandler();
4985
4890
  let opts = {
4986
4891
  store: this
4987
4892
  };
@@ -6892,6 +6797,103 @@ function secretInit(record, cache, identifier, store) {
6892
6797
  StoreMap.set(record, store);
6893
6798
  setCacheFor(record, cache);
6894
6799
  }
6800
+ function getHydratedContent(store, request, document) {
6801
+ if (Array.isArray(document.data)) {
6802
+ const {
6803
+ lid
6804
+ } = document;
6805
+ const {
6806
+ recordArrayManager
6807
+ } = store;
6808
+ if (!lid) {
6809
+ return recordArrayManager.createArray({
6810
+ identifiers: document.data,
6811
+ doc: document,
6812
+ query: request
6813
+ });
6814
+ }
6815
+ let managed = recordArrayManager._keyedArrays.get(lid);
6816
+ if (!managed) {
6817
+ managed = recordArrayManager.createArray({
6818
+ identifiers: document.data,
6819
+ doc: document
6820
+ });
6821
+ recordArrayManager._keyedArrays.set(lid, managed);
6822
+ } else {
6823
+ recordArrayManager.populateManagedArray(managed, document.data, document);
6824
+ }
6825
+ return managed;
6826
+ } else {
6827
+ return document.data ? store.peekRecord(document.data) : null;
6828
+ }
6829
+ }
6830
+ function calcShouldFetch(store, request, hasCachedValue, lid) {
6831
+ const {
6832
+ cacheOptions,
6833
+ url,
6834
+ method
6835
+ } = request;
6836
+ return cacheOptions?.reload || !hasCachedValue || store.lifetimes && lid && url && method ? store.lifetimes.isHardExpired(lid, url, method) : false;
6837
+ }
6838
+ function calcShouldBackgroundFetch(store, request, willFetch, lid) {
6839
+ const {
6840
+ cacheOptions,
6841
+ url,
6842
+ method
6843
+ } = request;
6844
+ return !willFetch && (cacheOptions?.backgroundReload || store.lifetimes && lid && url && method ? store.lifetimes.isSoftExpired(lid, url, method) : false);
6845
+ }
6846
+ function fetchContentAndHydrate(next, context, shouldFetch, shouldBackgroundFetch) {
6847
+ const {
6848
+ store
6849
+ } = context.request;
6850
+ return next(context.request).then(document => {
6851
+ const response = store.cache.put(document);
6852
+ if (shouldFetch) {
6853
+ return getHydratedContent(store, context.request, response);
6854
+ }
6855
+ }, error => {
6856
+ store.cache.put(error);
6857
+ // TODO @runspired this is probably not the right thing to throw so make sure we add a test
6858
+ if (!shouldBackgroundFetch) {
6859
+ throw error;
6860
+ }
6861
+ });
6862
+ }
6863
+ const CacheHandler = {
6864
+ request(context, next) {
6865
+ // if we are a legacy request or did not originate from the store, skip cache handling
6866
+ if (!context.request.store || context.request.op && !context.request.url) {
6867
+ return next(context.request);
6868
+ }
6869
+ const {
6870
+ store
6871
+ } = context.request;
6872
+ const {
6873
+ cacheOptions,
6874
+ url,
6875
+ method
6876
+ } = context.request;
6877
+ const lid = cacheOptions?.key || method === 'GET' && url ? url : null;
6878
+ const peeked = lid ? store.cache.peekRequest({
6879
+ lid
6880
+ }) : null;
6881
+
6882
+ // determine if we should skip cache
6883
+ if (calcShouldFetch(store, context.request, !!peeked, lid)) {
6884
+ return fetchContentAndHydrate(next, context, true, false);
6885
+ }
6886
+
6887
+ // if we have not skipped cache, determine if we should update behind the scenes
6888
+ if (calcShouldBackgroundFetch(store, context.request, false, lid)) {
6889
+ void fetchContentAndHydrate(next, context, false, true);
6890
+ }
6891
+ if ('error' in peeked) {
6892
+ throw peeked.error;
6893
+ }
6894
+ return Promise.resolve(getHydratedContent(store, context.request, peeked.content));
6895
+ }
6896
+ };
6895
6897
  function normalizeModelName(modelName) {
6896
6898
  if (macroCondition(getOwnConfig().deprecations.DEPRECATE_HELPERS)) {
6897
6899
  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 +6909,4 @@ function normalizeModelName(modelName) {
6907
6909
  }
6908
6910
  assert(`normalizeModelName support has been removed`);
6909
6911
  }
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 };
6912
+ 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 };