@warp-drive/core 5.8.0-beta.0 → 5.8.0-beta.1
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/README.md +23 -36
- package/declarations/build-config.d.ts +18 -1
- package/declarations/configure.d.ts +1 -1
- package/declarations/graph/-private/-edge-definition.d.ts +12 -2
- package/declarations/index.d.ts +90 -8
- package/declarations/reactive/-private/default-mode.d.ts +1 -1
- package/declarations/reactive/-private/document.d.ts +58 -46
- package/declarations/reactive/-private/fields/extension.d.ts +1 -1
- package/declarations/reactive/-private/fields/managed-array.d.ts +2 -2
- package/declarations/reactive/-private/fields/managed-object.d.ts +1 -1
- package/declarations/reactive/-private/record.d.ts +10 -1
- package/declarations/reactive/-private/schema.d.ts +77 -4
- package/declarations/reactive/-private.d.ts +1 -0
- package/declarations/reactive.d.ts +13 -8
- package/declarations/request/-private/types.d.ts +1 -1
- package/declarations/request.d.ts +47 -0
- package/declarations/signals/-leaked.d.ts +2 -0
- package/declarations/signals/-private.d.ts +6 -0
- package/declarations/{store/-private/new-core-tmp → signals}/promise-state.d.ts +2 -1
- package/declarations/{store/-private/new-core-tmp → signals}/request-state.d.ts +6 -6
- package/declarations/{store/-private/new-core-tmp → signals}/request-subscription.d.ts +4 -4
- package/declarations/store/-private/cache-handler/types.d.ts +2 -16
- package/declarations/store/-private/caches/instance-cache.d.ts +5 -6
- package/declarations/store/-private/default-cache-policy.d.ts +147 -129
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/-private/managers/cache-key-manager.d.ts +26 -8
- package/declarations/store/-private/managers/cache-manager.d.ts +7 -18
- package/declarations/store/-private/managers/notification-manager.d.ts +1 -1
- package/declarations/store/-private/record-arrays/legacy-many-array.d.ts +1 -1
- package/declarations/store/-private/record-arrays/resource-array.d.ts +1 -1
- package/declarations/store/-private/store-service.d.ts +43 -64
- package/declarations/store/-private.d.ts +0 -6
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/deprecated/-private.d.ts +2 -2
- package/declarations/store/deprecated/store.d.ts +33 -32
- package/declarations/store.d.ts +1 -0
- package/declarations/types/cache.d.ts +8 -6
- package/declarations/types/record.d.ts +132 -0
- package/declarations/types/request.d.ts +26 -14
- package/declarations/types/schema/fields.d.ts +37 -13
- package/declarations/{store/-types/q → types/schema}/schema-service.d.ts +15 -13
- package/declarations/types/spec/document.d.ts +34 -0
- package/declarations/types/symbols.d.ts +2 -2
- package/declarations/types.d.ts +1 -1
- package/dist/build-config.js +1 -1
- package/dist/configure-DPUFCemT.js +1940 -0
- package/dist/configure.js +2 -1
- package/dist/default-cache-policy-D7_u4YRH.js +572 -0
- package/dist/{context-C_7OLieY.js → future-BKkJJkj7.js} +174 -174
- package/dist/graph/-private.js +16 -6
- package/dist/{request-state-C955e0AL.js → index-CQP2NSqg.js} +8830 -9427
- package/dist/index.js +6 -382
- package/dist/reactive/-private.js +1 -1
- package/dist/reactive.js +4 -903
- package/dist/request.js +49 -1
- package/dist/signals/-leaked.js +1 -0
- package/dist/store/-private.js +1 -2
- package/dist/store.js +1 -533
- package/dist/symbols-3C1OkYtZ.js +39 -0
- package/dist/types/-private.js +1 -1
- package/dist/types/record.js +127 -0
- package/dist/types/request.js +14 -12
- package/dist/types/runtime.js +1 -1
- package/dist/types/schema/fields.js +14 -0
- package/dist/types/schema/schema-service.js +0 -0
- package/dist/types/symbols.js +2 -2
- package/dist/unpkg/dev/-leaked-Co0EI6Go.js +1939 -0
- package/dist/unpkg/dev/build-config/babel-macros.js +1 -0
- package/dist/unpkg/dev/build-config/canary-features.js +1 -0
- package/dist/unpkg/dev/build-config/debugging.js +1 -0
- package/dist/unpkg/dev/build-config/deprecations.js +1 -0
- package/dist/unpkg/dev/build-config/env.js +1 -0
- package/dist/unpkg/dev/build-config/macros.js +1 -0
- package/dist/unpkg/dev/build-config.js +1 -0
- package/dist/unpkg/dev/configure.js +1 -0
- package/dist/unpkg/dev/future-DFfOzSoe.js +672 -0
- package/dist/unpkg/dev/graph/-private.js +3132 -0
- package/dist/unpkg/dev/index-BzcBOnw9.js +9392 -0
- package/dist/unpkg/dev/index.js +6 -0
- package/dist/unpkg/dev/reactive/-private.js +1 -0
- package/dist/unpkg/dev/reactive.js +3 -0
- package/dist/unpkg/dev/request.js +49 -0
- package/dist/unpkg/dev/runtime-E1M51_-O.js +135 -0
- package/dist/unpkg/dev/signals/-leaked.js +1 -0
- package/dist/unpkg/dev/store/-private.js +55 -0
- package/dist/unpkg/dev/store.js +558 -0
- package/dist/unpkg/dev/types/-private.js +69 -0
- package/dist/unpkg/dev/types/cache/aliases.js +0 -0
- package/dist/unpkg/dev/types/cache/change.js +0 -0
- package/dist/unpkg/dev/types/cache/mutations.js +0 -0
- package/dist/unpkg/dev/types/cache/operations.js +0 -0
- package/dist/unpkg/dev/types/cache/relationship.js +0 -0
- package/dist/unpkg/dev/types/cache.js +0 -0
- package/dist/unpkg/dev/types/graph.js +0 -0
- package/dist/unpkg/dev/types/identifier.js +61 -0
- package/dist/unpkg/dev/types/json/raw.js +0 -0
- package/dist/unpkg/dev/types/params.js +0 -0
- package/dist/unpkg/dev/types/record.js +191 -0
- package/dist/unpkg/dev/types/request.js +77 -0
- package/dist/unpkg/dev/types/runtime.js +34 -0
- package/dist/unpkg/dev/types/schema/concepts.js +0 -0
- package/dist/unpkg/dev/types/schema/fields.js +505 -0
- package/dist/unpkg/dev/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/dev/types/schema/schema-service.js +0 -0
- package/dist/unpkg/dev/types/spec/document.js +0 -0
- package/dist/unpkg/dev/types/spec/error.js +0 -0
- package/dist/unpkg/dev/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/dev/types/symbols.js +84 -0
- package/dist/unpkg/dev/types/utils.js +0 -0
- package/dist/unpkg/dev/types.js +0 -0
- package/dist/unpkg/dev/utils/string.js +91 -0
- package/dist/unpkg/dev-deprecated/-leaked-DjMeRqdU.js +1939 -0
- package/dist/unpkg/dev-deprecated/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/dev-deprecated/build-config/babel-macros.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/canary-features.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/debugging.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/deprecations.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/env.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/macros.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config.js +1 -0
- package/dist/unpkg/dev-deprecated/configure.js +1 -0
- package/dist/unpkg/dev-deprecated/future-DFfOzSoe.js +672 -0
- package/dist/unpkg/dev-deprecated/graph/-private.js +3327 -0
- package/dist/unpkg/dev-deprecated/index-6TnTv-WG.js +10007 -0
- package/dist/unpkg/dev-deprecated/index.js +5 -0
- package/dist/unpkg/dev-deprecated/reactive/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/reactive.js +3 -0
- package/dist/unpkg/dev-deprecated/request.js +49 -0
- package/dist/unpkg/dev-deprecated/runtime-DAu5b0IR.js +135 -0
- package/dist/unpkg/dev-deprecated/signals/-leaked.js +1 -0
- package/dist/unpkg/dev-deprecated/store/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/store.js +558 -0
- package/dist/unpkg/dev-deprecated/types/-private.js +69 -0
- package/dist/unpkg/dev-deprecated/types/cache/aliases.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/change.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/mutations.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/operations.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/relationship.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache.js +0 -0
- package/dist/unpkg/dev-deprecated/types/graph.js +0 -0
- package/dist/unpkg/dev-deprecated/types/identifier.js +61 -0
- package/dist/unpkg/dev-deprecated/types/json/raw.js +0 -0
- package/dist/unpkg/dev-deprecated/types/params.js +0 -0
- package/dist/unpkg/dev-deprecated/types/record.js +191 -0
- package/dist/unpkg/dev-deprecated/types/request.js +77 -0
- package/dist/unpkg/dev-deprecated/types/runtime.js +34 -0
- package/dist/unpkg/dev-deprecated/types/schema/concepts.js +0 -0
- package/dist/unpkg/dev-deprecated/types/schema/fields.js +505 -0
- package/dist/unpkg/dev-deprecated/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/dev-deprecated/types/schema/schema-service.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/document.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/error.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/dev-deprecated/types/symbols.js +84 -0
- package/dist/unpkg/dev-deprecated/types/utils.js +0 -0
- package/dist/unpkg/dev-deprecated/types.js +0 -0
- package/dist/unpkg/dev-deprecated/utils/string.js +91 -0
- package/dist/unpkg/prod/-leaked-DUONXQDB.js +1676 -0
- package/dist/unpkg/prod/-private-sql1_mdx.js +39 -0
- package/dist/unpkg/prod/build-config/babel-macros.js +1 -0
- package/dist/unpkg/prod/build-config/canary-features.js +1 -0
- package/dist/unpkg/prod/build-config/debugging.js +1 -0
- package/dist/unpkg/prod/build-config/deprecations.js +1 -0
- package/dist/unpkg/prod/build-config/env.js +1 -0
- package/dist/unpkg/prod/build-config/macros.js +1 -0
- package/dist/unpkg/prod/build-config.js +1 -0
- package/dist/unpkg/prod/configure.js +2 -0
- package/dist/unpkg/prod/graph/-private.js +2235 -0
- package/dist/unpkg/prod/handler-EU_8ncB2.js +1619 -0
- package/dist/unpkg/prod/index.js +483 -0
- package/dist/unpkg/prod/promise-cache-DIT8Ypjq.js +19 -0
- package/dist/unpkg/prod/reactive/-private.js +1 -0
- package/dist/unpkg/prod/reactive.js +30 -0
- package/dist/unpkg/prod/request-BrJSCG6r.js +421 -0
- package/dist/unpkg/prod/request.js +2 -0
- package/dist/unpkg/prod/schema-BSkHyoWz.js +5219 -0
- package/dist/unpkg/prod/signals/-leaked.js +1 -0
- package/dist/unpkg/prod/store/-private.js +126 -0
- package/dist/unpkg/prod/store.js +437 -0
- package/dist/unpkg/prod/types/-private.js +49 -0
- package/dist/unpkg/prod/types/cache/aliases.js +0 -0
- package/dist/unpkg/prod/types/cache/change.js +0 -0
- package/dist/unpkg/prod/types/cache/mutations.js +0 -0
- package/dist/unpkg/prod/types/cache/operations.js +0 -0
- package/dist/unpkg/prod/types/cache/relationship.js +0 -0
- package/dist/unpkg/prod/types/cache.js +0 -0
- package/dist/unpkg/prod/types/graph.js +0 -0
- package/dist/unpkg/prod/types/identifier.js +61 -0
- package/dist/unpkg/prod/types/json/raw.js +0 -0
- package/dist/unpkg/prod/types/params.js +0 -0
- package/dist/unpkg/prod/types/record.js +191 -0
- package/dist/unpkg/prod/types/request.js +77 -0
- package/dist/unpkg/prod/types/runtime.js +34 -0
- package/dist/unpkg/prod/types/schema/concepts.js +0 -0
- package/dist/unpkg/prod/types/schema/fields.js +505 -0
- package/dist/unpkg/prod/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/prod/types/schema/schema-service.js +0 -0
- package/dist/unpkg/prod/types/spec/document.js +0 -0
- package/dist/unpkg/prod/types/spec/error.js +0 -0
- package/dist/unpkg/prod/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/prod/types/symbols.js +84 -0
- package/dist/unpkg/prod/types/utils.js +0 -0
- package/dist/unpkg/prod/types.js +0 -0
- package/dist/unpkg/prod/utils/string.js +72 -0
- package/dist/unpkg/prod-deprecated/-leaked-DRNv9VIX.js +1676 -0
- package/dist/unpkg/prod-deprecated/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/prod-deprecated/build-config/babel-macros.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/canary-features.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/debugging.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/deprecations.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/env.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/macros.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config.js +1 -0
- package/dist/unpkg/prod-deprecated/configure.js +2 -0
- package/dist/unpkg/prod-deprecated/graph/-private.js +2408 -0
- package/dist/unpkg/prod-deprecated/handler-CCIu4sQ3.js +334 -0
- package/dist/unpkg/prod-deprecated/hooks-Dv4Np0MY.js +26 -0
- package/dist/unpkg/prod-deprecated/index.js +483 -0
- package/dist/unpkg/prod-deprecated/promise-cache-DIT8Ypjq.js +19 -0
- package/dist/unpkg/prod-deprecated/reactive/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/reactive.js +5 -0
- package/dist/unpkg/prod-deprecated/request-BrJSCG6r.js +421 -0
- package/dist/unpkg/prod-deprecated/request.js +2 -0
- package/dist/unpkg/prod-deprecated/schema-CJcjHv0E.js +6939 -0
- package/dist/unpkg/prod-deprecated/signals/-leaked.js +1 -0
- package/dist/unpkg/prod-deprecated/store/-private.js +88 -0
- package/dist/unpkg/prod-deprecated/store.js +437 -0
- package/dist/unpkg/prod-deprecated/types/-private.js +49 -0
- package/dist/unpkg/prod-deprecated/types/cache/aliases.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/change.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/mutations.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/operations.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/relationship.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache.js +0 -0
- package/dist/unpkg/prod-deprecated/types/graph.js +0 -0
- package/dist/unpkg/prod-deprecated/types/identifier.js +61 -0
- package/dist/unpkg/prod-deprecated/types/json/raw.js +0 -0
- package/dist/unpkg/prod-deprecated/types/params.js +0 -0
- package/dist/unpkg/prod-deprecated/types/record.js +191 -0
- package/dist/unpkg/prod-deprecated/types/request.js +77 -0
- package/dist/unpkg/prod-deprecated/types/runtime.js +34 -0
- package/dist/unpkg/prod-deprecated/types/schema/concepts.js +0 -0
- package/dist/unpkg/prod-deprecated/types/schema/fields.js +505 -0
- package/dist/unpkg/prod-deprecated/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/prod-deprecated/types/schema/schema-service.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/document.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/error.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/prod-deprecated/types/symbols.js +84 -0
- package/dist/unpkg/prod-deprecated/types/utils.js +0 -0
- package/dist/unpkg/prod-deprecated/types.js +0 -0
- package/dist/unpkg/prod-deprecated/utils/string.js +72 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +12 -4
- package/declarations/store/-private/new-core-tmp/expensive-subscription.d.ts +0 -24
- package/dist/configure-C3x8YXzL.js +0 -181
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
- /package/declarations/{store/-private/new-core-tmp → signals}/reactivity/configure.d.ts +0 -0
- /package/declarations/{store/-private/new-core-tmp → signals}/reactivity/internal.d.ts +0 -0
- /package/declarations/{store/-private/new-core-tmp → signals}/reactivity/signal.d.ts +0 -0
- /package/dist/{symbols-sql1_mdx.js → unpkg/dev/-private-sql1_mdx.js} +0 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { v as createReactiveDocument } from "./schema-CJcjHv0E.js";
|
|
2
|
+
import { SkipCache, EnableHydration } from './types/request.js';
|
|
3
|
+
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
4
|
+
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
5
|
+
const {
|
|
6
|
+
cacheOptions
|
|
7
|
+
} = request;
|
|
8
|
+
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
|
|
9
|
+
}
|
|
10
|
+
function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
|
|
11
|
+
const {
|
|
12
|
+
cacheOptions
|
|
13
|
+
} = request;
|
|
14
|
+
return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
15
|
+
}
|
|
16
|
+
function isMutation(request) {
|
|
17
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
18
|
+
}
|
|
19
|
+
function isCacheAffecting(document) {
|
|
20
|
+
if (!isMutation(document.request)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
24
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
25
|
+
// have no cache impact
|
|
26
|
+
|
|
27
|
+
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
28
|
+
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
29
|
+
}
|
|
30
|
+
return document.response?.status !== 204;
|
|
31
|
+
}
|
|
32
|
+
function isAggregateError(error) {
|
|
33
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
34
|
+
}
|
|
35
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
36
|
+
function cloneError(error) {
|
|
37
|
+
const isAggregate = isAggregateError(error);
|
|
38
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
39
|
+
cloned.stack = error.stack;
|
|
40
|
+
cloned.error = error.error;
|
|
41
|
+
|
|
42
|
+
// copy over enumerable properties
|
|
43
|
+
Object.assign(cloned, error);
|
|
44
|
+
return cloned;
|
|
45
|
+
}
|
|
46
|
+
function getPriority(identifier, deduped, priority) {
|
|
47
|
+
if (identifier) {
|
|
48
|
+
const existing = deduped.get(identifier);
|
|
49
|
+
if (existing) {
|
|
50
|
+
return existing.priority;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return priority;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A CacheHandler that adds support for using an WarpDrive Cache with a RequestManager.
|
|
58
|
+
*
|
|
59
|
+
* This handler will only run when a request has supplied a `store` instance. Requests
|
|
60
|
+
* issued by the store via `store.request()` will automatically have the `store` instance
|
|
61
|
+
* attached to the request.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* requestManager.request({
|
|
65
|
+
* store: store,
|
|
66
|
+
* url: '/api/posts',
|
|
67
|
+
* method: 'GET'
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* When this handler elects to handle a request, it will return the raw `StructuredDocument`
|
|
72
|
+
* unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
|
|
73
|
+
* return a `Document` instance that will automatically update the UI when the cache is updated
|
|
74
|
+
* in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
|
|
75
|
+
*
|
|
76
|
+
* When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
|
|
77
|
+
* means that if desired you can issue requests that utilize the cache without needing to also
|
|
78
|
+
* utilize Record instances if desired.
|
|
79
|
+
*
|
|
80
|
+
* Said differently, you could elect to issue all requests via a RequestManager, without ever using
|
|
81
|
+
* the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
|
|
82
|
+
* necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
|
|
83
|
+
* approach of WarpDrive allows for this flexibility.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { EnableHydration } from '@warp-drive/core/types/request';
|
|
87
|
+
*
|
|
88
|
+
* requestManager.request({
|
|
89
|
+
* store: store,
|
|
90
|
+
* url: '/api/posts',
|
|
91
|
+
* method: 'GET',
|
|
92
|
+
* [EnableHydration]: true
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
const CacheHandler = {
|
|
97
|
+
request(context, next) {
|
|
98
|
+
// if we have no cache or no cache-key skip cache handling
|
|
99
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
|
|
100
|
+
return next(context.request);
|
|
101
|
+
}
|
|
102
|
+
const {
|
|
103
|
+
store
|
|
104
|
+
} = context.request;
|
|
105
|
+
const identifier = store.cacheKeyManager.getOrCreateDocumentIdentifier(context.request);
|
|
106
|
+
if (identifier) {
|
|
107
|
+
context.setIdentifier(identifier);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// used to dedupe existing requests that match
|
|
111
|
+
const DEDUPE = store.requestManager._deduped;
|
|
112
|
+
const activeRequest = identifier && DEDUPE.get(identifier);
|
|
113
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
114
|
+
|
|
115
|
+
// determine if we should skip cache
|
|
116
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
117
|
+
if (activeRequest) {
|
|
118
|
+
activeRequest.priority = {
|
|
119
|
+
blocking: true
|
|
120
|
+
};
|
|
121
|
+
return activeRequest.promise;
|
|
122
|
+
}
|
|
123
|
+
let promise = fetchContentAndHydrate(next, context, identifier, {
|
|
124
|
+
blocking: true
|
|
125
|
+
});
|
|
126
|
+
if (identifier) {
|
|
127
|
+
promise = promise.finally(() => {
|
|
128
|
+
DEDUPE.delete(identifier);
|
|
129
|
+
store.notifications.notify(identifier, 'state', null);
|
|
130
|
+
});
|
|
131
|
+
DEDUPE.set(identifier, {
|
|
132
|
+
priority: {
|
|
133
|
+
blocking: true
|
|
134
|
+
},
|
|
135
|
+
promise
|
|
136
|
+
});
|
|
137
|
+
queueMicrotask(() => {
|
|
138
|
+
store.notifications.notify(identifier, 'state', null);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
store.requestManager._pending.set(context.id, promise);
|
|
142
|
+
return promise;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// if we have not skipped cache, determine if we should update behind the scenes
|
|
146
|
+
if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
|
|
147
|
+
let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
|
|
148
|
+
blocking: false
|
|
149
|
+
});
|
|
150
|
+
if (identifier && !activeRequest) {
|
|
151
|
+
promise = promise.finally(() => {
|
|
152
|
+
DEDUPE.delete(identifier);
|
|
153
|
+
store.notifications.notify(identifier, 'state', null);
|
|
154
|
+
});
|
|
155
|
+
DEDUPE.set(identifier, {
|
|
156
|
+
priority: {
|
|
157
|
+
blocking: false
|
|
158
|
+
},
|
|
159
|
+
promise
|
|
160
|
+
});
|
|
161
|
+
queueMicrotask(() => {
|
|
162
|
+
store.notifications.notify(identifier, 'state', null);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
store.requestManager._pending.set(context.id, promise);
|
|
166
|
+
}
|
|
167
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
168
|
+
context.setResponse(peeked.response);
|
|
169
|
+
if ('error' in peeked) {
|
|
170
|
+
const content = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
171
|
+
shouldHydrate,
|
|
172
|
+
identifier
|
|
173
|
+
}, peeked.content) : peeked.content;
|
|
174
|
+
const newError = cloneError(peeked);
|
|
175
|
+
newError.content = content;
|
|
176
|
+
throw newError;
|
|
177
|
+
}
|
|
178
|
+
const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
179
|
+
shouldHydrate,
|
|
180
|
+
identifier
|
|
181
|
+
}, peeked.content) : peeked.content;
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
function maybeUpdateUiObjects(store, request, options, document) {
|
|
186
|
+
const {
|
|
187
|
+
identifier
|
|
188
|
+
} = options;
|
|
189
|
+
if (!document || !options.shouldHydrate) {
|
|
190
|
+
return document ?? null;
|
|
191
|
+
}
|
|
192
|
+
if (identifier) {
|
|
193
|
+
return store._instanceCache.getDocument(identifier);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// if we don't have an identifier, we give the document
|
|
197
|
+
// its own local cache
|
|
198
|
+
return createReactiveDocument(store, null, {
|
|
199
|
+
request,
|
|
200
|
+
document
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
function updateCacheForSuccess(store, request, options, document) {
|
|
204
|
+
let response = null;
|
|
205
|
+
if (isMutation(request)) {
|
|
206
|
+
if (Array.isArray(request.records)) {
|
|
207
|
+
response = store.cache.didCommit(request.records, document);
|
|
208
|
+
} else if (request.data?.record) {
|
|
209
|
+
// legacy fallback, the data option should no longer be used for this
|
|
210
|
+
response = store.cache.didCommit(request.data.record, document);
|
|
211
|
+
|
|
212
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
213
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
214
|
+
// have no cache impact
|
|
215
|
+
} else if (isCacheAffecting(document)) {
|
|
216
|
+
response = store.cache.put(document);
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
response = store.cache.put(document);
|
|
220
|
+
}
|
|
221
|
+
return maybeUpdateUiObjects(store, request, options, response);
|
|
222
|
+
}
|
|
223
|
+
function handleFetchSuccess(store, context, options, document) {
|
|
224
|
+
const {
|
|
225
|
+
request
|
|
226
|
+
} = context;
|
|
227
|
+
store.requestManager._pending.delete(context.id);
|
|
228
|
+
store._enableAsyncFlush = true;
|
|
229
|
+
let response;
|
|
230
|
+
store._join(() => {
|
|
231
|
+
response = updateCacheForSuccess(store, request, options, document);
|
|
232
|
+
});
|
|
233
|
+
store._enableAsyncFlush = null;
|
|
234
|
+
if (store.lifetimes?.didRequest) {
|
|
235
|
+
store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
|
|
236
|
+
}
|
|
237
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
238
|
+
if (finalPriority.blocking) {
|
|
239
|
+
return response;
|
|
240
|
+
} else {
|
|
241
|
+
store.notifications._flush();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function updateCacheForError(store, context, options, error) {
|
|
245
|
+
let response;
|
|
246
|
+
if (isMutation(context.request)) {
|
|
247
|
+
// TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
|
|
248
|
+
// currently we let the response remain undefiend.
|
|
249
|
+
const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
|
|
250
|
+
if (Array.isArray(context.request.records)) {
|
|
251
|
+
store.cache.commitWasRejected(context.request.records, errors);
|
|
252
|
+
} else if (context.request.data?.record) {
|
|
253
|
+
// legacy fallback, the data option should no longer be used for this
|
|
254
|
+
store.cache.commitWasRejected(context.request.data.record, errors);
|
|
255
|
+
} else {
|
|
256
|
+
store.cache.put(error);
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
response = store.cache.put(error);
|
|
260
|
+
return maybeUpdateUiObjects(store, context.request, options, response);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function handleFetchError(store, context, options, error) {
|
|
264
|
+
store.requestManager._pending.delete(context.id);
|
|
265
|
+
if (context.request.signal?.aborted) {
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
store._enableAsyncFlush = true;
|
|
269
|
+
let response;
|
|
270
|
+
store._join(() => {
|
|
271
|
+
response = updateCacheForError(store, context, options, error);
|
|
272
|
+
});
|
|
273
|
+
store._enableAsyncFlush = null;
|
|
274
|
+
if (options.identifier && store.lifetimes?.didRequest) {
|
|
275
|
+
store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
|
|
276
|
+
}
|
|
277
|
+
if (isMutation(context.request)) {
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
281
|
+
if (finalPriority.blocking) {
|
|
282
|
+
const newError = cloneError(error);
|
|
283
|
+
newError.content = response;
|
|
284
|
+
throw newError;
|
|
285
|
+
} else {
|
|
286
|
+
store.notifications._flush();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function fetchContentAndHydrate(next, context, identifier, priority) {
|
|
290
|
+
const {
|
|
291
|
+
store
|
|
292
|
+
} = context.request;
|
|
293
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
294
|
+
const options = {
|
|
295
|
+
shouldHydrate,
|
|
296
|
+
identifier,
|
|
297
|
+
priority
|
|
298
|
+
};
|
|
299
|
+
let isMut = false;
|
|
300
|
+
if (isMutation(context.request)) {
|
|
301
|
+
isMut = true;
|
|
302
|
+
if (Array.isArray(context.request.records)) {
|
|
303
|
+
context.request.records.forEach(record => {
|
|
304
|
+
store.cache.willCommit(record, context);
|
|
305
|
+
});
|
|
306
|
+
} else if (context.request.data?.record) {
|
|
307
|
+
// legacy fallback, the data option should no longer be used for this
|
|
308
|
+
store.cache.willCommit(context.request.data.record, context);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (store.lifetimes?.willRequest) {
|
|
312
|
+
store.lifetimes.willRequest(context.request, identifier, store);
|
|
313
|
+
}
|
|
314
|
+
const promise = next(context.request).then(document => {
|
|
315
|
+
return handleFetchSuccess(store, context, options, document);
|
|
316
|
+
}, error => {
|
|
317
|
+
return handleFetchError(store, context, options, error);
|
|
318
|
+
});
|
|
319
|
+
if (!isMut) {
|
|
320
|
+
return promise;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// for mutations we need to enqueue the promise with the requestStateService
|
|
324
|
+
// TODO should we enque a request per record in records?
|
|
325
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
326
|
+
return store._requestCache._enqueue(promise, {
|
|
327
|
+
data: [{
|
|
328
|
+
op: 'saveRecord',
|
|
329
|
+
recordIdentifier: record,
|
|
330
|
+
options: undefined
|
|
331
|
+
}]
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
export { CacheHandler as C };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { u as ReactiveResource } from "./schema-CJcjHv0E.js";
|
|
2
|
+
import { D as Destroy } from "./-private-3C1OkYtZ.js";
|
|
3
|
+
function instantiateRecord(store, identifier, createArgs) {
|
|
4
|
+
const schema = store.schema;
|
|
5
|
+
const resourceSchema = schema.resource(identifier);
|
|
6
|
+
const legacy = resourceSchema?.legacy ?? false;
|
|
7
|
+
const editable = legacy;
|
|
8
|
+
const record = new ReactiveResource({
|
|
9
|
+
store,
|
|
10
|
+
resourceKey: identifier,
|
|
11
|
+
modeName: legacy ? 'legacy' : 'polaris',
|
|
12
|
+
legacy: legacy,
|
|
13
|
+
editable: editable,
|
|
14
|
+
path: null,
|
|
15
|
+
field: null,
|
|
16
|
+
value: null
|
|
17
|
+
});
|
|
18
|
+
if (createArgs && editable) {
|
|
19
|
+
Object.assign(record, createArgs);
|
|
20
|
+
}
|
|
21
|
+
return record;
|
|
22
|
+
}
|
|
23
|
+
function teardownRecord(record) {
|
|
24
|
+
record[Destroy]();
|
|
25
|
+
}
|
|
26
|
+
export { instantiateRecord as i, teardownRecord as t };
|