@warp-drive/core 5.8.0-alpha.5 → 5.8.0-alpha.6

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/dist/index.js CHANGED
@@ -1,7 +1,8 @@
1
- import './types/runtime.js';
2
- export { C as CacheHandler, U as Fetch, V as RequestManager, S as Store, r as recordIdentifierFor, Y as setIdentifierForgetMethod, W as setIdentifierGenerationMethod, Z as setIdentifierResetMethod, X as setIdentifierUpdateMethod, $ as setKeyInfoForResource, s as storeFor } from "./index-MiSBsI57.js";
1
+ export { C as CacheHandler, V as Fetch, W as RequestManager, S as Store, r as recordIdentifierFor, $ as setIdentifierForgetMethod, Y as setIdentifierGenerationMethod, a0 as setIdentifierResetMethod, Z as setIdentifierUpdateMethod, a1 as setKeyInfoForResource, s as storeFor, X as useRecommendedStore } from "./index-Cg2akouS.js";
2
+ import "./symbols-sql1_mdx.js";
3
+ import "./default-cache-policy-D7_u4YRH.js";
3
4
  import '@ember/debug';
4
5
  import '@embroider/macros';
5
6
  import './utils/string.js';
6
- import "./symbols-sql1_mdx.js";
7
- import "./configure-C3x8YXzL.js";
7
+ import "./configure-C3x8YXzL.js";
8
+ import './types/runtime.js';
package/dist/reactive.js CHANGED
@@ -1,45 +1,5 @@
1
- import { L as ReactiveResource } from "./index-MiSBsI57.js";
2
- export { N as SchemaService, M as checkout, T as commit, P as fromIdentity, Q as registerDerivations, O as withDefaults } from "./index-MiSBsI57.js";
3
- import { isResourceSchema } from './types/schema/fields.js';
4
- import { D as Destroy } from "./symbols-sql1_mdx.js";
1
+ export { O as SchemaService, L as checkout, U as commit, Q as fromIdentity, M as instantiateRecord, T as registerDerivations, N as teardownRecord, P as withDefaults } from "./index-Cg2akouS.js";
5
2
  export { a as Checkout } from "./symbols-sql1_mdx.js";
6
- import { macroCondition, getGlobalConfig } from '@embroider/macros';
7
- function instantiateRecord(store, identifier, createArgs) {
8
- const schema = store.schema;
9
- const resourceSchema = schema.resource(identifier);
10
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
11
- if (!test) {
12
- throw new Error(`Expected a resource schema`);
13
- }
14
- })(isResourceSchema(resourceSchema)) : {};
15
- const legacy = resourceSchema?.legacy ?? false;
16
- const editable = legacy;
17
- const record = new ReactiveResource({
18
- store,
19
- resourceKey: identifier,
20
- modeName: legacy ? 'legacy' : 'polaris',
21
- legacy: legacy,
22
- editable: editable,
23
- path: null,
24
- field: null,
25
- value: null
26
- });
27
- if (createArgs && editable) {
28
- Object.assign(record, createArgs);
29
- }
30
- return record;
31
- }
32
- function assertReactiveResource(record) {
33
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
34
- if (!test) {
35
- throw new Error('Expected a ReactiveResource');
36
- }
37
- })(record && typeof record === 'object' && Destroy in record) : {};
38
- }
39
- function teardownRecord(record) {
40
- assertReactiveResource(record);
41
- record[Destroy]();
42
- }
43
3
  const Subscriptions = new WeakMap();
44
4
 
45
5
  /**
@@ -161,4 +121,4 @@ function getExpensiveRequestSubscription(store, requestKey, callback) {
161
121
  subscription.removeWatcher(callback);
162
122
  };
163
123
  }
164
- export { getExpensiveRequestSubscription, instantiateRecord, teardownRecord };
124
+ export { getExpensiveRequestSubscription };
package/dist/request.js CHANGED
@@ -1 +1 @@
1
- export { c as createDeferred, g as getPromiseResult, s as setPromiseResult } from "./context-C_7OLieY.js";
1
+ export { c as createDeferred, g as getPromiseResult, s as setPromiseResult } from "./future-BKkJJkj7.js";
@@ -1,2 +1,2 @@
1
- export { C as CacheHandler, D as DISPOSE, R as RecordArrayManager, E as Signals, S as Store, k as StoreMap, _ as _clearCaches, n as _deprecatingNormalize, h as assertPrivateCapabilities, d as assertPrivateStore, b as coerceId, c as constructResource, J as consumeInternalSignal, G as createInternalMemo, l as createLegacyManyArray, q as createRequestSubscription, A as defineGate, B as defineNonEnumerableSignal, z as defineSignal, e as ensureStringId, y as entangleInitiallyStaleSignal, x as entangleSignal, f as fastPush, w as gate, K as getOrCreateInternalSignal, p as getPromiseState, t as getRequestState, g as isPrivateStore, a as isRequestKey, i as isResourceKey, m as log, o as logGroup, v as memoized, I as notifyInternalSignal, F as peekInternalSignal, r as recordIdentifierFor, j as setRecordIdentifier, u as signal, s as storeFor, H as withSignalStore } from "../index-MiSBsI57.js";
1
+ export { C as CacheHandler, D as DISPOSE, R as RecordArrayManager, E as Signals, S as Store, k as StoreMap, _ as _clearCaches, n as _deprecatingNormalize, h as assertPrivateCapabilities, d as assertPrivateStore, b as coerceId, c as constructResource, J as consumeInternalSignal, G as createInternalMemo, l as createLegacyManyArray, q as createRequestSubscription, A as defineGate, B as defineNonEnumerableSignal, z as defineSignal, e as ensureStringId, y as entangleInitiallyStaleSignal, x as entangleSignal, f as fastPush, w as gate, K as getOrCreateInternalSignal, p as getPromiseState, t as getRequestState, g as isPrivateStore, a as isRequestKey, i as isResourceKey, m as log, o as logGroup, v as memoized, I as notifyInternalSignal, F as peekInternalSignal, r as recordIdentifierFor, j as setRecordIdentifier, u as signal, s as storeFor, H as withSignalStore } from "../index-Cg2akouS.js";
2
2
  export { A as ARRAY_SIGNAL, O as OBJECT_SIGNAL, w as waitFor } from "../configure-C3x8YXzL.js";
package/dist/store.js CHANGED
@@ -1,570 +1 @@
1
- import { deprecate } from '@ember/debug';
2
- import { LRUCache } from './utils/string.js';
3
- import { macroCondition, getGlobalConfig } from '@embroider/macros';
4
-
5
- /**
6
- * Interface of a parsed Cache-Control header value.
7
- *
8
- * - [MDN Cache-Control Reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control)
9
- */
10
- const NUMERIC_KEYS = new Set(['max-age', 's-maxage', 'stale-if-error', 'stale-while-revalidate']);
11
-
12
- /**
13
- * Parses a string Cache-Control header value into an object with the following structure:
14
- *
15
- * ```ts
16
- * interface CacheControlValue {
17
- * immutable?: boolean;
18
- * 'max-age'?: number;
19
- * 'must-revalidate'?: boolean;
20
- * 'must-understand'?: boolean;
21
- * 'no-cache'?: boolean;
22
- * 'no-store'?: boolean;
23
- * 'no-transform'?: boolean;
24
- * 'only-if-cached'?: boolean;
25
- * private?: boolean;
26
- * 'proxy-revalidate'?: boolean;
27
- * public?: boolean;
28
- * 's-maxage'?: number;
29
- * 'stale-if-error'?: number;
30
- * 'stale-while-revalidate'?: number;
31
- * }
32
- * ```
33
- *
34
- * See also {@link CacheControlValue} and [Response Directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control#response_directives)
35
- *
36
- * @public
37
- * @param {String} header
38
- * @return {CacheControlValue}
39
- */
40
- function parseCacheControl(header) {
41
- return CACHE_CONTROL_CACHE.get(header);
42
- }
43
- const CACHE_CONTROL_CACHE = new LRUCache(header => {
44
- let key = '';
45
- let value = '';
46
- let isParsingKey = true;
47
- const cacheControlValue = {};
48
- for (let i = 0; i < header.length; i++) {
49
- const char = header.charAt(i);
50
- if (char === ',') {
51
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
52
- if (!test) {
53
- throw new Error(`Invalid Cache-Control value, expected a value`);
54
- }
55
- })(!isParsingKey || !NUMERIC_KEYS.has(key)) : {};
56
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
57
- if (!test) {
58
- throw new Error(`Invalid Cache-Control value, expected a value after "=" but got ","`);
59
- }
60
- })(i === 0 || header.charAt(i - 1) !== '=') : {};
61
- isParsingKey = true;
62
- // @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
63
- cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
64
- key = '';
65
- value = '';
66
- continue;
67
- } else if (char === '=') {
68
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
69
- if (!test) {
70
- throw new Error(`Invalid Cache-Control value, expected a value after "="`);
71
- }
72
- })(i + 1 !== header.length) : {};
73
- isParsingKey = false;
74
- } else if (char === ' ' || char === `\t` || char === `\n`) {
75
- continue;
76
- } else if (isParsingKey) {
77
- key += char;
78
- } else {
79
- value += char;
80
- }
81
- if (i === header.length - 1) {
82
- // @ts-expect-error TS incorrectly thinks that optional keys must have a type that includes undefined
83
- cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
84
- }
85
- }
86
- return cacheControlValue;
87
- }, 200);
88
- function parseCacheControlValue(stringToParse) {
89
- const parsedValue = Number.parseInt(stringToParse);
90
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
91
- if (!test) {
92
- throw new Error(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`);
93
- }
94
- })(!Number.isNaN(parsedValue)) : {};
95
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
96
- if (!test) {
97
- throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${stringToParse}`);
98
- }
99
- })(parsedValue >= 0) : {};
100
- if (Number.isNaN(parsedValue) || parsedValue < 0) {
101
- return 0;
102
- }
103
- return parsedValue;
104
- }
105
- function isExpired(cacheKey, request, config) {
106
- const {
107
- constraints
108
- } = config;
109
- if (constraints?.isExpired) {
110
- const result = constraints.isExpired(request);
111
- if (result !== null) {
112
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
113
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
114
- // eslint-disable-next-line no-console
115
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because constraints.isExpired returned ${result}`);
116
- }
117
- }
118
- return result;
119
- }
120
- }
121
- const {
122
- headers
123
- } = request.response;
124
- if (!headers) {
125
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
126
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
127
- // eslint-disable-next-line no-console
128
- console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no headers were provided`);
129
- }
130
- }
131
-
132
- // if we have no headers then both the headers based expiration
133
- // and the time based expiration will be considered expired
134
- return true;
135
- }
136
-
137
- // check for X-WarpDrive-Expires
138
- const now = Date.now();
139
- const date = headers.get('Date');
140
- if (constraints?.headers) {
141
- if (constraints.headers['X-WarpDrive-Expires']) {
142
- const xWarpDriveExpires = headers.get('X-WarpDrive-Expires');
143
- if (xWarpDriveExpires) {
144
- const expirationTime = new Date(xWarpDriveExpires).getTime();
145
- const result = now >= expirationTime;
146
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
147
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
148
- // eslint-disable-next-line no-console
149
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by X-WarpDrive-Expires header is ${result ? 'in the past' : 'in the future'}`);
150
- }
151
- }
152
- return result;
153
- }
154
- }
155
-
156
- // check for Cache-Control
157
- if (constraints.headers['Cache-Control']) {
158
- const cacheControl = headers.get('Cache-Control');
159
- const age = headers.get('Age');
160
- if (cacheControl && age && date) {
161
- const cacheControlValue = parseCacheControl(cacheControl);
162
-
163
- // max-age and s-maxage are stored in
164
- const maxAge = cacheControlValue['max-age'] || cacheControlValue['s-maxage'];
165
- if (maxAge) {
166
- // age is stored in seconds
167
- const ageValue = parseInt(age, 10);
168
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
169
- if (!test) {
170
- throw new Error(`Invalid Cache-Control value, expected a number but got - ${age}`);
171
- }
172
- })(!Number.isNaN(ageValue)) : {};
173
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
174
- if (!test) {
175
- throw new Error(`Invalid Cache-Control value, expected a number greater than 0 but got - ${age}`);
176
- }
177
- })(ageValue >= 0) : {};
178
- if (!Number.isNaN(ageValue) && ageValue >= 0) {
179
- const dateValue = new Date(date).getTime();
180
- const expirationTime = dateValue + (maxAge - ageValue) * 1000;
181
- const result = now >= expirationTime;
182
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
183
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
184
- // eslint-disable-next-line no-console
185
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Cache-Control header is ${result ? 'in the past' : 'in the future'}`);
186
- }
187
- }
188
- return result;
189
- }
190
- }
191
- }
192
- }
193
-
194
- // check for Expires
195
- if (constraints.headers.Expires) {
196
- const expires = headers.get('Expires');
197
- if (expires) {
198
- const expirationTime = new Date(expires).getTime();
199
- const result = now >= expirationTime;
200
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
201
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
202
- // eslint-disable-next-line no-console
203
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the time set by Expires header is ${result ? 'in the past' : 'in the future'}`);
204
- }
205
- }
206
- return result;
207
- }
208
- }
209
- }
210
-
211
- // check for Date
212
- if (!date) {
213
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
214
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
215
- // eslint-disable-next-line no-console
216
- console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no Date header was provided`);
217
- }
218
- }
219
- return true;
220
- }
221
- let expirationTime = config.apiCacheHardExpires;
222
- if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
223
- if (!config.disableTestOptimization) {
224
- expirationTime = config.apiCacheSoftExpires;
225
- }
226
- }
227
- const time = new Date(date).getTime();
228
- const deadline = time + expirationTime;
229
- const result = now >= deadline;
230
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
231
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
232
- // eslint-disable-next-line no-console
233
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'EXPIRED' : 'NOT expired'} because the apiCacheHardExpires time since the response's Date header is ${result ? 'in the past' : 'in the future'}`);
234
- }
235
- }
236
- return result;
237
- }
238
-
239
- /**
240
- * The configuration options for the {@link DefaultCachePolicy}
241
- *
242
- * ```ts [app/services/store.ts]
243
- * import { Store } from '@warp-drive/core';
244
- * import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
245
- *
246
- * export default class AppStore extends Store {
247
- * lifetimes = new DefaultCachePolicy({ // [!code focus:3]
248
- * // ... PolicyConfig Settings ... //
249
- * });
250
- * }
251
- * ```
252
- *
253
- */
254
-
255
- /**
256
- * A basic {@link CachePolicy} that can be added to the Store service.
257
- *
258
- * ```ts [app/services/store.ts]
259
- * import { Store } from '@warp-drive/core';
260
- * import { DefaultCachePolicy } from '@warp-drive/core/store'; // [!code focus]
261
- *
262
- * export default class AppStore extends Store {
263
- * lifetimes = new DefaultCachePolicy({ // [!code focus:5]
264
- * apiCacheSoftExpires: 30_000,
265
- * apiCacheHardExpires: 60_000,
266
- * // ... Other PolicyConfig Settings ... //
267
- * });
268
- * }
269
- * ```
270
- *
271
- * :::tip 💡 TIP
272
- * Date headers do not have millisecond precision, so expiration times should
273
- * generally be larger than 1000ms.
274
- * :::
275
- *
276
- * See also {@link PolicyConfig} for configuration options.
277
- *
278
- * ### The Mechanics
279
- *
280
- * This policy determines staleness based on various configurable constraints falling back to a simple
281
- * check of the time elapsed since the request was last received from the API using the `date` header
282
- * from the last response.
283
- *
284
- * :::tip 💡 TIP
285
- * The {@link Fetch} handler provided by `@warp-drive/core` will automatically
286
- * add the `date` header to responses if it is not present.
287
- * :::
288
- *
289
- * - For manual override of reload see {@link RequestInfo.cacheOptions.reload | cacheOptions.reload}
290
- * - For manual override of background reload see {@link RequestInfo.cacheOptions.backgroundReload | cacheOptions.backgroundReload}
291
- *
292
- * In order expiration is determined by:
293
- *
294
- * ```md
295
- * Is explicitly invalidated by `cacheOptions.reload`
296
- * ↳ (if falsey) if the request has been explicitly invalidated
297
- * since the last request (see Automatic Invalidation below)
298
- * ↳ (if false) (If Active) isExpired function
299
- * ↳ (if null) (If Active) X-WarpDrive-Expires header
300
- * ↳ (if null) (If Active) Cache-Control header
301
- * ↳ (if null) (If Active) Expires header
302
- * ↳ (if null) Date header + apiCacheHardExpires < current time
303
- *
304
- * -- <if above is false, a background request is issued if> --
305
- *
306
- * ↳ is invalidated by `cacheOptions.backgroundReload`
307
- * ↳ (if falsey) Date header + apiCacheSoftExpires < current time
308
- * ```
309
- *
310
- * ### Automatic Invalidation / Entanglement
311
- *
312
- * It also invalidates any request with an {@link RequestInfo.op | OpCode} of `"query"`
313
- * for which {@link RequestInfo.cacheOptions.types | cacheOptions.types} was provided
314
- * when a request with an `OpCode` of `"createRecord"` is successful and also includes
315
- * a matching type in its own `cacheOptions.types` array.
316
-
317
- * :::tip 💡 TIP
318
- * Abstracting this behavior via builders is recommended to ensure consistency.
319
- * :::
320
- *
321
- * ### Testing
322
- *
323
- * In Testing environments:
324
- *
325
- * - `apiCacheSoftExpires` will always be `false`
326
- * - `apiCacheHardExpires` will use the `apiCacheSoftExpires` value.
327
- *
328
- * This helps reduce flakiness and produce predictably rendered results in test suites.
329
- *
330
- * Requests that specifically set `cacheOptions.backgroundReload = true` will
331
- * still be background reloaded in tests.
332
- *
333
- * This behavior can be opted out of by setting `disableTestOptimization = true`
334
- * in the policy config.
335
- *
336
- * @public
337
- */
338
- class DefaultCachePolicy {
339
- /**
340
- * @internal
341
- */
342
-
343
- /**
344
- * @internal
345
- */
346
-
347
- /**
348
- * @internal
349
- */
350
- _getStore(store) {
351
- let set = this._stores.get(store);
352
- if (!set) {
353
- set = {
354
- invalidated: new Set(),
355
- types: new Map()
356
- };
357
- this._stores.set(store, set);
358
- }
359
- return set;
360
- }
361
- constructor(config) {
362
- this._stores = new WeakMap();
363
- const _config = arguments.length === 1 ? config : arguments[1];
364
- deprecate(`Passing a Store to the CachePolicy is deprecated, please pass only a config instead.`, arguments.length === 1, {
365
- id: 'ember-data:request-utils:lifetimes-service-store-arg',
366
- since: {
367
- enabled: '5.4',
368
- available: '4.13'
369
- },
370
- for: '@ember-data/request-utils',
371
- until: '6.0'
372
- });
373
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
374
- if (!test) {
375
- throw new Error(`You must pass a config to the CachePolicy`);
376
- }
377
- })(_config) : {};
378
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
379
- if (!test) {
380
- throw new Error(`You must pass a apiCacheSoftExpires to the CachePolicy`);
381
- }
382
- })(typeof _config.apiCacheSoftExpires === 'number') : {};
383
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
384
- if (!test) {
385
- throw new Error(`You must pass a apiCacheHardExpires to the CachePolicy`);
386
- }
387
- })(typeof _config.apiCacheHardExpires === 'number') : {};
388
- this.config = _config;
389
- }
390
-
391
- /**
392
- * Invalidate a request by its CacheKey for the given store instance.
393
- *
394
- * While the store argument may seem redundant, the CachePolicy
395
- * is designed to be shared across multiple stores / forks
396
- * of the store.
397
- *
398
- * ```ts
399
- * store.lifetimes.invalidateRequest(store, cacheKey);
400
- * ```
401
- *
402
- * @public
403
- */
404
- invalidateRequest(cacheKey, store) {
405
- this._getStore(store).invalidated.add(cacheKey);
406
- }
407
-
408
- /**
409
- * Invalidate all requests associated to a specific type
410
- * for a given store instance.
411
- *
412
- * While the store argument may seem redundant, the CachePolicy
413
- * is designed to be shared across multiple stores / forks
414
- * of the store.
415
- *
416
- * This invalidation is done automatically when using this service
417
- * for both the CacheHandler and the LegacyNetworkHandler.
418
- *
419
- * ```ts
420
- * store.lifetimes.invalidateRequestsForType(store, 'person');
421
- * ```
422
- *
423
- * @public
424
- */
425
- invalidateRequestsForType(type, store) {
426
- const storeCache = this._getStore(store);
427
- const set = storeCache.types.get(type);
428
- const notifications = store.notifications;
429
- if (set) {
430
- // TODO batch notifications
431
- set.forEach(id => {
432
- storeCache.invalidated.add(id);
433
- // @ts-expect-error
434
- notifications.notify(id, 'invalidated', null);
435
- });
436
- }
437
- }
438
-
439
- /**
440
- * Invoked when a request has been fulfilled from the configured request handlers.
441
- * This is invoked by the CacheHandler for both foreground and background requests
442
- * once the cache has been updated.
443
- *
444
- * Note, this is invoked by the CacheHandler regardless of whether
445
- * the request has a cache-key.
446
- *
447
- * This method should not be invoked directly by consumers.
448
- *
449
- * @public
450
- */
451
- didRequest(request, response, cacheKey, store) {
452
- // if this is a successful createRecord request, invalidate the cacheKey for the type
453
- if (request.op === 'createRecord') {
454
- const statusNumber = response?.status ?? 0;
455
- if (statusNumber >= 200 && statusNumber < 400) {
456
- const types = new Set(request.records?.map(r => r.type));
457
- const additionalTypes = request.cacheOptions?.types;
458
- additionalTypes?.forEach(type => {
459
- types.add(type);
460
- });
461
- types.forEach(type => {
462
- this.invalidateRequestsForType(type, store);
463
- });
464
- }
465
-
466
- // add this document's cacheKey to a map for all associated types
467
- // it is recommended to only use this for queries
468
- } else if (cacheKey && request.cacheOptions?.types?.length) {
469
- const storeCache = this._getStore(store);
470
- request.cacheOptions?.types.forEach(type => {
471
- const set = storeCache.types.get(type);
472
- if (set) {
473
- set.add(cacheKey);
474
- storeCache.invalidated.delete(cacheKey);
475
- } else {
476
- storeCache.types.set(type, new Set([cacheKey]));
477
- }
478
- });
479
- }
480
- }
481
-
482
- /**
483
- * Invoked to determine if the request may be fulfilled from cache
484
- * if possible.
485
- *
486
- * Note, this is only invoked by the CacheHandler if the request has
487
- * a cache-key.
488
- *
489
- * If no cache entry is found or the entry is hard expired,
490
- * the request will be fulfilled from the configured request handlers
491
- * and the cache will be updated before returning the response.
492
- *
493
- * @public
494
- * @return true if the request is considered hard expired
495
- */
496
- isHardExpired(cacheKey, store) {
497
- // if we are explicitly invalidated, we are hard expired
498
- const storeCache = this._getStore(store);
499
- if (storeCache.invalidated.has(cacheKey)) {
500
- return true;
501
- }
502
- const cache = store.cache;
503
- const cached = cache.peekRequest(cacheKey);
504
- if (!cached?.response) {
505
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
506
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
507
- // eslint-disable-next-line no-console
508
- console.log(`CachePolicy: ${cacheKey.lid} is EXPIRED because no cache entry was found`);
509
- }
510
- }
511
- return true;
512
- }
513
- return isExpired(cacheKey, cached, this.config);
514
- }
515
-
516
- /**
517
- * Invoked if `isHardExpired` is false to determine if the request
518
- * should be update behind the scenes if cache data is already available.
519
- *
520
- * Note, this is only invoked by the CacheHandler if the request has
521
- * a cache-key.
522
- *
523
- * If true, the request will be fulfilled from cache while a backgrounded
524
- * request is made to update the cache via the configured request handlers.
525
- *
526
- * @public
527
- * @return true if the request is considered soft expired
528
- */
529
- isSoftExpired(cacheKey, store) {
530
- if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
531
- if (!this.config.disableTestOptimization) {
532
- return false;
533
- }
534
- }
535
- const cache = store.cache;
536
- const cached = cache.peekRequest(cacheKey);
537
- if (cached?.response) {
538
- const date = cached.response.headers.get('date');
539
- if (!date) {
540
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
541
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
542
- // eslint-disable-next-line no-console
543
- console.log(`CachePolicy: ${cacheKey.lid} is STALE because no date header was found`);
544
- }
545
- }
546
- return true;
547
- } else {
548
- const time = new Date(date).getTime();
549
- const now = Date.now();
550
- const deadline = time + this.config.apiCacheSoftExpires;
551
- const result = now >= deadline;
552
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
553
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
554
- // eslint-disable-next-line no-console
555
- console.log(`CachePolicy: ${cacheKey.lid} is ${result ? 'STALE' : 'NOT stale'}. Expiration time: ${deadline}, now: ${now}`);
556
- }
557
- }
558
- return result;
559
- }
560
- }
561
- if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_CACHE_POLICY)) {
562
- if (getGlobalConfig().WarpDrive.debug.LOG_CACHE_POLICY || globalThis.getWarpDriveRuntimeConfig().debug.LOG_CACHE_POLICY) {
563
- // eslint-disable-next-line no-console
564
- console.log(`CachePolicy: ${cacheKey.lid} is STALE because no cache entry was found`);
565
- }
566
- }
567
- return true;
568
- }
569
- }
570
- export { DefaultCachePolicy, parseCacheControl };
1
+ export { D as DefaultCachePolicy, p as parseCacheControl } from "./default-cache-policy-D7_u4YRH.js";
@@ -1,6 +1,6 @@
1
1
  import { macroCondition, getGlobalConfig } from '@embroider/macros';
2
2
  const name = "@warp-drive/core";
3
- const version = "5.8.0-alpha.5";
3
+ const version = "5.8.0-alpha.6";
4
4
 
5
5
  // in testing mode, we utilize globals to ensure only one copy exists of
6
6
  // these maps, due to bugs in ember-auto-import
@@ -8,7 +8,8 @@ const IS_FUTURE = getOrSetGlobal('IS_FUTURE', Symbol('IS_FUTURE'));
8
8
  const STRUCTURED = getOrSetGlobal('DOC', Symbol('DOC'));
9
9
 
10
10
  /**
11
- * Use these options to adjust CacheHandler behavior for a request.
11
+ * Use these options to adjust {@link CacheHandler} behavior for a request
12
+ * via {@link RequestInfo.cacheOptions}.
12
13
  *
13
14
  */
14
15
 
@@ -443,6 +443,20 @@
443
443
  * @public
444
444
  */
445
445
 
446
+ /**
447
+ * A trait for use on a PolarisMode record
448
+ */
449
+
450
+ /**
451
+ * A trait for use on a LegacyMode record
452
+ */
453
+
454
+ /**
455
+ * A union of
456
+ * - {@link LegacyTrait}
457
+ * - {@link PolarisTrait}
458
+ */
459
+
446
460
  /**
447
461
  * A no-op type utility that enables type-checking resource schema
448
462
  * definitions.