@warp-drive-mirror/core 5.6.0-beta.0 → 5.6.0-beta.2

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 (119) hide show
  1. package/declarations/graph/-private/-diff.d.ts +7 -20
  2. package/declarations/graph/-private/-edge-definition.d.ts +3 -12
  3. package/declarations/graph/-private/-state.d.ts +2 -2
  4. package/declarations/graph/-private/-utils.d.ts +5 -5
  5. package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +3 -3
  6. package/declarations/graph/-private/edges/collection.d.ts +10 -10
  7. package/declarations/graph/-private/edges/implicit.d.ts +5 -5
  8. package/declarations/graph/-private/edges/resource.d.ts +6 -7
  9. package/declarations/graph/-private/graph.d.ts +17 -15
  10. package/declarations/graph/-private/operations/replace-related-records.d.ts +4 -4
  11. package/declarations/graph/-private/operations/update-relationship.d.ts +3 -3
  12. package/declarations/index.d.ts +1 -1
  13. package/declarations/reactive/-private/default-mode.d.ts +73 -0
  14. package/declarations/reactive/-private/document.d.ts +11 -27
  15. package/declarations/reactive/-private/fields/get-field-key.d.ts +8 -0
  16. package/declarations/reactive/-private/fields/managed-array.d.ts +7 -10
  17. package/declarations/reactive/-private/fields/managed-object.d.ts +7 -9
  18. package/declarations/reactive/-private/fields/many-array-manager.d.ts +2 -2
  19. package/declarations/reactive/-private/hooks.d.ts +2 -2
  20. package/declarations/reactive/-private/kind/alias-field.d.ts +4 -0
  21. package/declarations/reactive/-private/kind/array-field.d.ts +4 -0
  22. package/declarations/reactive/-private/kind/attribute-field.d.ts +4 -0
  23. package/declarations/reactive/-private/kind/belongs-to-field.d.ts +4 -0
  24. package/declarations/reactive/-private/kind/collection-field.d.ts +4 -0
  25. package/declarations/reactive/-private/kind/derived-field.d.ts +4 -0
  26. package/declarations/reactive/-private/kind/generic-field.d.ts +4 -0
  27. package/declarations/reactive/-private/kind/has-many-field.d.ts +4 -0
  28. package/declarations/reactive/-private/kind/hash-field.d.ts +4 -0
  29. package/declarations/reactive/-private/kind/identity-field.d.ts +4 -0
  30. package/declarations/reactive/-private/kind/local-field.d.ts +4 -0
  31. package/declarations/reactive/-private/kind/object-field.d.ts +4 -0
  32. package/declarations/reactive/-private/kind/resource-field.d.ts +4 -0
  33. package/declarations/reactive/-private/kind/schema-array-field.d.ts +4 -0
  34. package/declarations/reactive/-private/kind/schema-object-field.d.ts +4 -0
  35. package/declarations/reactive/-private/record.d.ts +44 -33
  36. package/declarations/reactive/-private/schema.d.ts +50 -66
  37. package/declarations/reactive/-private/symbols.d.ts +2 -7
  38. package/declarations/reactive/-private.d.ts +1 -1
  39. package/declarations/reactive.d.ts +278 -1
  40. package/declarations/request/-private/context.d.ts +3 -5
  41. package/declarations/request/-private/fetch.d.ts +2 -0
  42. package/declarations/request/-private/manager.d.ts +24 -28
  43. package/declarations/request/-private/types.d.ts +22 -23
  44. package/declarations/request/-private/utils.d.ts +44 -2
  45. package/declarations/store/-private/cache-handler/handler.d.ts +2 -8
  46. package/declarations/store/-private/cache-handler/types.d.ts +10 -10
  47. package/declarations/store/-private/cache-handler/utils.d.ts +4 -4
  48. package/declarations/store/-private/caches/instance-cache.d.ts +22 -27
  49. package/declarations/store/-private/debug/utils.d.ts +1 -0
  50. package/declarations/store/-private/default-cache-policy.d.ts +25 -38
  51. package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +24 -15
  52. package/declarations/store/-private/{caches/identifier-cache.d.ts → managers/cache-key-manager.d.ts} +38 -52
  53. package/declarations/store/-private/managers/cache-manager.d.ts +46 -95
  54. package/declarations/store/-private/managers/notification-manager.d.ts +30 -43
  55. package/declarations/store/-private/managers/record-array-manager.d.ts +44 -41
  56. package/declarations/store/-private/network/request-cache.d.ts +21 -24
  57. package/declarations/store/-private/new-core-tmp/expensive-subscription.d.ts +24 -0
  58. package/declarations/store/-private/new-core-tmp/reactivity/configure.d.ts +3 -41
  59. package/declarations/store/-private/new-core-tmp/reactivity/internal.d.ts +14 -29
  60. package/declarations/store/-private/new-core-tmp/reactivity/signal.d.ts +24 -3
  61. package/declarations/store/-private/new-core-tmp/request-state.d.ts +133 -37
  62. package/declarations/store/-private/new-core-tmp/request-subscription.d.ts +51 -133
  63. package/declarations/store/-private/record-arrays/-utils.d.ts +80 -0
  64. package/declarations/store/-private/record-arrays/legacy-live-array.d.ts +81 -0
  65. package/declarations/store/-private/record-arrays/{many-array.d.ts → legacy-many-array.d.ts} +40 -104
  66. package/declarations/store/-private/record-arrays/legacy-query.d.ts +81 -0
  67. package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +1 -124
  68. package/declarations/store/-private/record-arrays/resource-array.d.ts +75 -0
  69. package/declarations/store/-private/store-service.d.ts +167 -872
  70. package/declarations/store/-private.d.ts +14 -10
  71. package/declarations/store/-types/q/cache-capabilities-manager.d.ts +15 -24
  72. package/declarations/store/-types/q/identifier.d.ts +9 -6
  73. package/declarations/store/-types/q/record-instance.d.ts +0 -1
  74. package/declarations/store/-types/q/schema-service.d.ts +64 -40
  75. package/declarations/store/-types/q/store.d.ts +6 -7
  76. package/declarations/store/deprecated/-private.d.ts +224 -0
  77. package/declarations/store/deprecated/store.d.ts +787 -0
  78. package/declarations/types/-private.d.ts +1 -1
  79. package/declarations/types/cache/aliases.d.ts +2 -2
  80. package/declarations/types/cache/change.d.ts +2 -2
  81. package/declarations/types/cache/mutations.d.ts +13 -13
  82. package/declarations/types/cache/operations.d.ts +115 -32
  83. package/declarations/types/cache/relationship.d.ts +4 -4
  84. package/declarations/types/cache.d.ts +51 -115
  85. package/declarations/types/graph.d.ts +12 -12
  86. package/declarations/types/identifier.d.ts +52 -76
  87. package/declarations/types/params.d.ts +2 -3
  88. package/declarations/types/request.d.ts +69 -42
  89. package/declarations/types/schema/concepts.d.ts +2 -2
  90. package/declarations/types/schema/fields.d.ts +391 -14
  91. package/declarations/types/spec/document.d.ts +6 -6
  92. package/declarations/types/spec/json-api-raw.d.ts +6 -8
  93. package/declarations/types.d.ts +2 -1
  94. package/declarations/utils/string.d.ts +2 -2
  95. package/dist/{configure-B48bFHOl.js → configure-C3x8YXzL.js} +5 -5
  96. package/dist/configure.js +1 -1
  97. package/dist/{context-COmAnXUQ.js → context-C_7OLieY.js} +48 -6
  98. package/dist/graph/-private.js +137 -144
  99. package/dist/index.js +25 -14
  100. package/dist/reactive/-private.js +1 -1
  101. package/dist/reactive.js +337 -1422
  102. package/dist/{request-state-CjLph1LP.js → request-state-C955e0AL.js} +8352 -5912
  103. package/dist/request.js +1 -1
  104. package/dist/store/-private.js +2 -3
  105. package/dist/store.js +32 -44
  106. package/dist/{symbols-SIstXMLI.js → symbols-sql1_mdx.js} +3 -8
  107. package/dist/types/-private.js +1 -1
  108. package/dist/types/identifier.js +19 -45
  109. package/dist/types/request.js +45 -3
  110. package/dist/types/schema/fields.js +23 -2
  111. package/dist/utils/string.js +2 -2
  112. package/package.json +10 -10
  113. package/declarations/reactive/-private/fields/compute.d.ts +0 -43
  114. package/declarations/store/-private/caches/cache-utils.d.ts +0 -12
  115. package/declarations/store/-private/legacy-model-support/record-reference.d.ts +0 -159
  116. package/declarations/store/-private/legacy-model-support/shim-model-class.d.ts +0 -17
  117. package/declarations/store/-private/record-arrays/identifier-array.d.ts +0 -147
  118. package/declarations/store/-types/q/ds-model.d.ts +0 -21
  119. package/dist/handler-C2T-IyJK.js +0 -339
@@ -1,159 +0,0 @@
1
- import type { StableRecordIdentifier } from "../../../types/identifier.js";
2
- import type { SingleResourceDocument } from "../../../types/spec/json-api-raw.js";
3
- import type { OpaqueRecordInstance } from "../../-types/q/record-instance.js";
4
- import type { Store } from "../store-service.js";
5
- /**
6
- A `RecordReference` is a low-level API that allows users and
7
- addon authors to perform meta-operations on a record.
8
-
9
- @hideconstructor
10
- @public
11
- */
12
- export default class RecordReference {
13
- private store;
14
- // unsubscribe token given to us by the notification manager
15
- private ___token;
16
- private ___identifier;
17
- private _ref;
18
- constructor(store: Store, identifier: StableRecordIdentifier);
19
- /** @internal */
20
- destroy(): void;
21
- get type(): string;
22
- /**
23
- The `id` of the record that this reference refers to.
24
-
25
- Together, the `type` and `id` properties form a composite key for
26
- the identity map.
27
-
28
- Example
29
-
30
- ```javascript
31
- let userRef = store.getReference('user', 1);
32
-
33
- userRef.id(); // '1'
34
- ```
35
-
36
- @public
37
- */
38
- id(): string | null;
39
- /**
40
- The `identifier` of the record that this reference refers to.
41
-
42
- Together, the `type` and `id` properties form a composite key for
43
- the identity map.
44
-
45
- Example
46
-
47
- ```javascript
48
- let userRef = store.getReference('user', 1);
49
-
50
- userRef.identifier(); // '1'
51
- ```
52
-
53
- @public
54
- */
55
- identifier(): StableRecordIdentifier;
56
- /**
57
- How the reference will be looked up when it is loaded. Currently
58
- this always returns `identity` to signify that a record will be
59
- loaded by its `type` and `id`.
60
-
61
- Example
62
-
63
- ```javascript
64
- const userRef = store.getReference('user', 1);
65
-
66
- userRef.remoteType(); // 'identity'
67
- ```
68
-
69
- @public
70
- */
71
- remoteType(): "identity";
72
- /**
73
- This API allows you to provide a reference with new data. The
74
- simplest usage of this API is similar to `store.push`: you provide a
75
- normalized hash of data and the object represented by the reference
76
- will update.
77
-
78
- If you pass a promise to `push`, Ember Data will not ask the adapter
79
- for the data if another attempt to fetch it is made in the
80
- interim. When the promise resolves, the underlying object is updated
81
- with the new data, and the promise returned by *this function* is resolved
82
- with that object.
83
-
84
- For example, `recordReference.push(promise)` will be resolved with a
85
- record.
86
-
87
- Example
88
-
89
- ```javascript
90
- let userRef = store.getReference('user', 1);
91
-
92
- // provide data for reference
93
- userRef.push({
94
- data: {
95
- id: "1",
96
- type: "user",
97
- attributes: {
98
- username: "@user"
99
- }
100
- }
101
- }).then(function(user) {
102
- userRef.value() === user;
103
- });
104
- ```
105
-
106
- @public
107
- @param objectOrPromise a JSON:API ResourceDocument or a promise resolving to one
108
- @return a promise for the value (record or relationship)
109
- */
110
- push(objectOrPromise: SingleResourceDocument | Promise<SingleResourceDocument>): Promise<OpaqueRecordInstance>;
111
- /**
112
- If the entity referred to by the reference is already loaded, it is
113
- present as `reference.value`. Otherwise the value returned by this function
114
- is `null`.
115
-
116
- Example
117
-
118
- ```javascript
119
- let userRef = store.getReference('user', 1);
120
-
121
- userRef.value(); // user
122
- ```
123
-
124
- @public
125
- */
126
- value(): OpaqueRecordInstance | null;
127
- /**
128
- Triggers a fetch for the backing entity based on its `remoteType`
129
- (see `remoteType` definitions per reference type).
130
-
131
- Example
132
-
133
- ```javascript
134
- let userRef = store.getReference('user', 1);
135
-
136
- // load user (via store.find)
137
- userRef.load().then(...)
138
- ```
139
-
140
- @public
141
- */
142
- load(): Promise<OpaqueRecordInstance>;
143
- /**
144
- Reloads the record if it is already loaded. If the record is not
145
- loaded it will load the record via `store.findRecord`
146
-
147
- Example
148
-
149
- ```javascript
150
- let userRef = store.getReference('user', 1);
151
-
152
- // or trigger a reload
153
- userRef.reload().then(...)
154
- ```
155
-
156
- @public
157
- */
158
- reload(): Promise<OpaqueRecordInstance>;
159
- }
@@ -1,17 +0,0 @@
1
- import type { TypedRecordInstance, TypeFromInstance } from "../../../types/record.js";
2
- import type { LegacyAttributeField, LegacyRelationshipField } from "../../../types/schema/fields.js";
3
- import type { KeyOrString, ModelSchema } from "../../-types/q/ds-model.js";
4
- import type { Store } from "../store-service.js";
5
- export declare function getShimClass<T>(store: Store, modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string): ShimModelClass<T>;
6
- // Mimics the static apis of @ember-data-mirror/model
7
- export default class ShimModelClass<T = unknown> implements ModelSchema<T> {
8
- private __store;
9
- modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string;
10
- constructor(store: Store, modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string);
11
- get fields(): Map<KeyOrString<T>, "attribute" | "belongsTo" | "hasMany">;
12
- get attributes(): Map<KeyOrString<T>, LegacyAttributeField>;
13
- get relationshipsByName(): Map<KeyOrString<T>, LegacyRelationshipField>;
14
- eachAttribute<K extends KeyOrString<T>>(callback: (key: K, attribute: LegacyAttributeField) => void, binding?: T): void;
15
- eachRelationship<K extends KeyOrString<T>>(callback: (key: K, relationship: LegacyRelationshipField) => void, binding?: T): void;
16
- eachTransformedAttribute<K extends KeyOrString<T>>(callback: (key: K, type: string | null) => void, binding?: T): void;
17
- }
@@ -1,147 +0,0 @@
1
- import type { BaseFinderOptions } from "../../../types.js";
2
- import type { LocalRelationshipOperation } from "../../../types/graph.js";
3
- import type { StableDocumentIdentifier, StableRecordIdentifier } from "../../../types/identifier.js";
4
- import type { TypeFromInstanceOrString } from "../../../types/record.js";
5
- import type { ImmutableRequestInfo } from "../../../types/request.js";
6
- import type { LegacyHasManyField, LinksModeHasManyField } from "../../../types/schema/fields.js";
7
- import type { Links, PaginationLinks } from "../../../types/spec/json-api-raw.js";
8
- import type { RecordArrayManager } from "../managers/record-array-manager.js";
9
- import type { WarpDriveSignal } from "../new-core-tmp/reactivity/internal.js";
10
- import { ARRAY_SIGNAL } from "../new-core-tmp/reactivity/internal.js";
11
- import type { Store } from "../store-service.js";
12
- import { NativeProxy } from "./native-proxy-type-fix.js";
13
- export declare const SOURCE: "___(unique) Symbol(#source)";
14
- export declare const MUTATE: "___(unique) Symbol(#update)";
15
- declare const IS_COLLECTION: "___(unique) Symbol(IS_COLLECTION)";
16
- export type IdentifierArrayCreateOptions<T = unknown> = {
17
- identifiers: StableRecordIdentifier<TypeFromInstanceOrString<T>>[];
18
- type?: TypeFromInstanceOrString<T>;
19
- store: Store;
20
- allowMutation: boolean;
21
- manager: MinimumManager;
22
- field?: LegacyHasManyField | LinksModeHasManyField;
23
- links?: Links | PaginationLinks | null;
24
- meta?: Record<string, unknown> | null;
25
- identifier?: StableDocumentIdentifier | null;
26
- [MUTATE]?(target: StableRecordIdentifier[], receiver: typeof NativeProxy<StableRecordIdentifier[], T[]>, prop: string, args: unknown[], _SIGNAL: WarpDriveSignal): unknown;
27
- };
28
- type PromiseTo<T> = Omit<Promise<T>, typeof Symbol.toStringTag>;
29
- type PromiseManyArray<T> = {
30
- length: number;
31
- content: IdentifierArray<T> | null;
32
- promise: Promise<IdentifierArray<T>> | null;
33
- } & PromiseTo<IdentifierArray<T>>;
34
- export type MinimumManager = {
35
- _syncArray: (array: IdentifierArray) => void;
36
- mutate?(mutation: LocalRelationshipOperation): void;
37
- reloadHasMany?<T>(key: string, options?: BaseFinderOptions): Promise<IdentifierArray<T>> | PromiseManyArray<T>;
38
- };
39
- /**
40
- A record array is an array that contains records of a certain type (or modelName).
41
- The record array materializes records as needed when they are retrieved for the first
42
- time. You should not create record arrays yourself. Instead, an instance of
43
- `RecordArray` or its subclasses will be returned by your application's store
44
- in response to queries.
45
-
46
- This class should not be imported and instantiated by consuming applications.
47
-
48
- @class RecordArray
49
- @public
50
- */
51
- export interface IdentifierArray<T = unknown> extends Omit<Array<T>, "[]"> {
52
- [IS_COLLECTION]: boolean;
53
- [ARRAY_SIGNAL]: WarpDriveSignal;
54
- [SOURCE]: StableRecordIdentifier[];
55
- }
56
- export declare class IdentifierArray<T = unknown> {
57
- DEPRECATED_CLASS_NAME: string;
58
- /**
59
- The flag to signal a `RecordArray` is currently loading data.
60
- Example
61
- ```javascript
62
- let people = store.peekAll('person');
63
- people.isUpdating; // false
64
- people.update();
65
- people.isUpdating; // true
66
- ```
67
- @property isUpdating
68
- @public
69
- @type Boolean
70
- */
71
- isUpdating: boolean;
72
- isLoaded: boolean;
73
- isDestroying: boolean;
74
- isDestroyed: boolean;
75
- _updatingPromise: Promise<IdentifierArray<T>> | null;
76
- readonly identifier: StableDocumentIdentifier | null;
77
- links: Links | PaginationLinks | null;
78
- meta: Record<string, unknown> | null;
79
- modelName?: TypeFromInstanceOrString<T>;
80
- /**
81
- The store that created this record array.
82
-
83
- @property store
84
- @private
85
- @type Store
86
- */
87
- store: Store;
88
- _manager: MinimumManager;
89
- destroy(clear: boolean): void;
90
- constructor(options: IdentifierArrayCreateOptions<T>);
91
- /**
92
- Used to get the latest version of all of the records in this array
93
- from the adapter.
94
-
95
- Example
96
-
97
- ```javascript
98
- let people = store.peekAll('person');
99
- people.isUpdating; // false
100
-
101
- people.update().then(function() {
102
- people.isUpdating; // false
103
- });
104
-
105
- people.isUpdating; // true
106
- ```
107
-
108
- @public
109
- */
110
- update(): Promise<IdentifierArray<T>>;
111
- /*
112
- Update this Array and return a promise which resolves once the update
113
- is finished.
114
- */
115
- _update(): Promise<IdentifierArray<T>>;
116
- // TODO deprecate
117
- /**
118
- Saves all of the records in the `RecordArray`.
119
-
120
- Example
121
-
122
- ```javascript
123
- let messages = store.peekAll('message');
124
- messages.forEach(function(message) {
125
- message.hasBeenSeen = true;
126
- });
127
- messages.save();
128
- ```
129
-
130
- @public
131
- @return {Promise<IdentifierArray>} promise
132
- */
133
- save(): Promise<IdentifierArray>;
134
- }
135
- export type CollectionCreateOptions = IdentifierArrayCreateOptions & {
136
- manager: RecordArrayManager;
137
- query: ImmutableRequestInfo | Record<string, unknown> | null;
138
- isLoaded: boolean;
139
- };
140
- export declare class Collection<T = unknown> extends IdentifierArray<T> {
141
- query: ImmutableRequestInfo | Record<string, unknown> | null;
142
- _manager: RecordArrayManager;
143
- constructor(options: CollectionCreateOptions);
144
- _update(): Promise<Collection<T>>;
145
- destroy(clear: boolean): void;
146
- }
147
- export {};
@@ -1,21 +0,0 @@
1
- import type { TypedRecordInstance, TypeFromInstance } from "../../../types/record.js";
2
- import type { LegacyAttributeField, LegacyRelationshipField } from "../../../types/schema/fields.js";
3
- export type KeyOrString<T> = keyof T & string extends never ? string : keyof T & string;
4
- /**
5
- * Minimum subset of static schema methods and properties on the
6
- * "model" class.
7
- *
8
- * Only used when using the legacy schema-service implementation
9
- * for @ember-data-mirror/model or when wrapping schema for legacy
10
- * Adapters/Serializers.
11
- *
12
- */
13
- export interface ModelSchema<T = unknown> {
14
- modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string;
15
- fields: Map<KeyOrString<T>, "attribute" | "belongsTo" | "hasMany">;
16
- attributes: Map<KeyOrString<T>, LegacyAttributeField>;
17
- relationshipsByName: Map<KeyOrString<T>, LegacyRelationshipField>;
18
- eachAttribute<K extends KeyOrString<T>>(callback: (this: ModelSchema<T>, key: K, attribute: LegacyAttributeField) => void, binding?: T): void;
19
- eachRelationship<K extends KeyOrString<T>>(callback: (this: ModelSchema<T>, key: K, relationship: LegacyRelationshipField) => void, binding?: T): void;
20
- eachTransformedAttribute<K extends KeyOrString<T>>(callback: (this: ModelSchema<T>, key: K, type: string | null) => void, binding?: T): void;
21
- }
@@ -1,339 +0,0 @@
1
- import { J as ReactiveDocument } from "./request-state-CjLph1LP.js";
2
- import { SkipCache, EnableHydration } from './types/request.js';
3
- import { macroCondition, getGlobalConfig } from '@embroider/macros';
4
- const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
5
- function calcShouldFetch(store, request, hasCachedValue, identifier) {
6
- const {
7
- cacheOptions
8
- } = request;
9
- return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
10
- }
11
- function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
12
- const {
13
- cacheOptions
14
- } = request;
15
- return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
16
- }
17
- function isMutation(request) {
18
- return Boolean(request.op && MUTATION_OPS.has(request.op));
19
- }
20
- function isCacheAffecting(document) {
21
- if (!isMutation(document.request)) {
22
- return true;
23
- }
24
- // a mutation combined with a 204 has no cache impact when no known records were involved
25
- // a createRecord with a 201 with an empty response and no known records should similarly
26
- // have no cache impact
27
-
28
- if (document.request.op === 'createRecord' && document.response?.status === 201) {
29
- return document.content ? Object.keys(document.content).length > 0 : false;
30
- }
31
- return document.response?.status !== 204;
32
- }
33
- function isAggregateError(error) {
34
- return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
35
- }
36
- // TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
37
- function cloneError(error) {
38
- const isAggregate = isAggregateError(error);
39
- const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
40
- cloned.stack = error.stack;
41
- cloned.error = error.error;
42
-
43
- // copy over enumerable properties
44
- Object.assign(cloned, error);
45
- return cloned;
46
- }
47
- function getPriority(identifier, deduped, priority) {
48
- if (identifier) {
49
- const existing = deduped.get(identifier);
50
- if (existing) {
51
- return existing.priority;
52
- }
53
- }
54
- return priority;
55
- }
56
-
57
- /**
58
- * A CacheHandler that adds support for using an WarpDrive Cache with a RequestManager.
59
- *
60
- * This handler will only run when a request has supplied a `store` instance. Requests
61
- * issued by the store via `store.request()` will automatically have the `store` instance
62
- * attached to the request.
63
- *
64
- * ```ts
65
- * requestManager.request({
66
- * store: store,
67
- * url: '/api/posts',
68
- * method: 'GET'
69
- * });
70
- * ```
71
- *
72
- * When this handler elects to handle a request, it will return the raw `StructuredDocument`
73
- * unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
74
- * return a `Document` instance that will automatically update the UI when the cache is updated
75
- * in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
76
- *
77
- * When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
78
- * means that if desired you can issue requests that utilize the cache without needing to also
79
- * utilize Record instances if desired.
80
- *
81
- * Said differently, you could elect to issue all requests via a RequestManager, without ever using
82
- * the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
83
- * necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
84
- * approach of WarpDrive allows for this flexibility.
85
- *
86
- * ```ts
87
- * import { EnableHydration } from '@warp-drive-mirror/core/types/request';
88
- *
89
- * requestManager.request({
90
- * store: store,
91
- * url: '/api/posts',
92
- * method: 'GET',
93
- * [EnableHydration]: true
94
- * });
95
- *
96
- */
97
- const CacheHandler = {
98
- request(context, next) {
99
- // if we have no cache or no cache-key skip cache handling
100
- if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
101
- return next(context.request);
102
- }
103
- const {
104
- store
105
- } = context.request;
106
- const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
107
- if (identifier) {
108
- context.setIdentifier(identifier);
109
- }
110
-
111
- // used to dedupe existing requests that match
112
- const DEDUPE = store.requestManager._deduped;
113
- const activeRequest = identifier && DEDUPE.get(identifier);
114
- const peeked = identifier ? store.cache.peekRequest(identifier) : null;
115
-
116
- // determine if we should skip cache
117
- if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
118
- if (activeRequest) {
119
- activeRequest.priority = {
120
- blocking: true
121
- };
122
- return activeRequest.promise;
123
- }
124
- let promise = fetchContentAndHydrate(next, context, identifier, {
125
- blocking: true
126
- });
127
- if (identifier) {
128
- promise = promise.finally(() => {
129
- DEDUPE.delete(identifier);
130
- store.notifications.notify(identifier, 'state');
131
- });
132
- DEDUPE.set(identifier, {
133
- priority: {
134
- blocking: true
135
- },
136
- promise
137
- });
138
- store.notifications.notify(identifier, 'state');
139
- }
140
- return promise;
141
- }
142
-
143
- // if we have not skipped cache, determine if we should update behind the scenes
144
- if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
145
- let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
146
- blocking: false
147
- });
148
- if (identifier && !activeRequest) {
149
- promise = promise.finally(() => {
150
- DEDUPE.delete(identifier);
151
- store.notifications.notify(identifier, 'state');
152
- });
153
- DEDUPE.set(identifier, {
154
- priority: {
155
- blocking: false
156
- },
157
- promise
158
- });
159
- store.notifications.notify(identifier, 'state');
160
- }
161
- store.requestManager._pending.set(context.id, promise);
162
- }
163
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
164
- if (!test) {
165
- throw new Error(`Expected a peeked request to be present`);
166
- }
167
- })(peeked) : {};
168
- const shouldHydrate = context.request[EnableHydration] || false;
169
- context.setResponse(peeked.response);
170
- if ('error' in peeked) {
171
- const content = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
172
- shouldHydrate,
173
- identifier
174
- }, peeked.content) : peeked.content;
175
- const newError = cloneError(peeked);
176
- newError.content = content;
177
- throw newError;
178
- }
179
- const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
180
- shouldHydrate,
181
- identifier
182
- }, peeked.content) : peeked.content;
183
- return result;
184
- }
185
- };
186
- function maybeUpdateUiObjects(store, request, options, document) {
187
- const {
188
- identifier
189
- } = options;
190
- if (!document || !options.shouldHydrate) {
191
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
192
- if (!test) {
193
- throw new Error(`The CacheHandler expected response content but none was found`);
194
- }
195
- })(!options.shouldHydrate) : {};
196
- return document ?? null;
197
- }
198
- if (identifier) {
199
- return store._instanceCache.getDocument(identifier);
200
- }
201
-
202
- // if we don't have an identifier, we give the document
203
- // its own local cache
204
- return new ReactiveDocument(store, null, {
205
- request,
206
- document
207
- });
208
- }
209
- function updateCacheForSuccess(store, request, options, document) {
210
- let response = null;
211
- if (isMutation(request)) {
212
- const record = request.data?.record || request.records?.[0];
213
- if (record) {
214
- response = store.cache.didCommit(record, document);
215
-
216
- // a mutation combined with a 204 has no cache impact when no known records were involved
217
- // a createRecord with a 201 with an empty response and no known records should similarly
218
- // have no cache impact
219
- } else if (isCacheAffecting(document)) {
220
- response = store.cache.put(document);
221
- }
222
- } else {
223
- response = store.cache.put(document);
224
- }
225
- return maybeUpdateUiObjects(store, request, options, response);
226
- }
227
- function handleFetchSuccess(store, context, options, document) {
228
- const {
229
- request
230
- } = context;
231
- store.requestManager._pending.delete(context.id);
232
- store._enableAsyncFlush = true;
233
- let response;
234
- store._join(() => {
235
- response = updateCacheForSuccess(store, request, options, document);
236
- });
237
- store._enableAsyncFlush = null;
238
- if (store.lifetimes?.didRequest) {
239
- store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
240
- }
241
- const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
242
- if (finalPriority.blocking) {
243
- return response;
244
- } else {
245
- store.notifications._flush();
246
- }
247
- }
248
- function updateCacheForError(store, context, options, error) {
249
- let response;
250
- if (isMutation(context.request)) {
251
- // TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
252
- // currently we let the response remain undefiend.
253
- const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
254
- const record = context.request.data?.record || context.request.records?.[0];
255
- store.cache.commitWasRejected(record, errors);
256
- } else {
257
- response = store.cache.put(error);
258
- return maybeUpdateUiObjects(store, context.request, options, response);
259
- }
260
- }
261
- function handleFetchError(store, context, options, error) {
262
- store.requestManager._pending.delete(context.id);
263
- if (context.request.signal?.aborted) {
264
- throw error;
265
- }
266
- store._enableAsyncFlush = true;
267
- let response;
268
- store._join(() => {
269
- response = updateCacheForError(store, context, options, error);
270
- });
271
- store._enableAsyncFlush = null;
272
- if (options.identifier && store.lifetimes?.didRequest) {
273
- store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
274
- }
275
- if (isMutation(context.request)) {
276
- throw error;
277
- }
278
- const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
279
- if (finalPriority.blocking) {
280
- const newError = cloneError(error);
281
- newError.content = response;
282
- throw newError;
283
- } else {
284
- store.notifications._flush();
285
- }
286
- }
287
- function fetchContentAndHydrate(next, context, identifier, priority) {
288
- const {
289
- store
290
- } = context.request;
291
- const shouldHydrate = context.request[EnableHydration] || false;
292
- const options = {
293
- shouldHydrate,
294
- identifier,
295
- priority
296
- };
297
- let isMut = false;
298
- if (isMutation(context.request)) {
299
- isMut = true;
300
- // TODO should we handle multiple records in request.records by iteratively calling willCommit for each
301
- const record = context.request.data?.record || context.request.records?.[0];
302
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
303
- if (!test) {
304
- throw new Error(`Expected to receive a list of records included in the ${context.request.op} request`);
305
- }
306
- })(record || !shouldHydrate) : {};
307
- if (record) {
308
- store.cache.willCommit(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
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
323
- if (!test) {
324
- throw new Error(`Expected a mutation`);
325
- }
326
- })(isMutation(context.request)) : {};
327
-
328
- // for mutations we need to enqueue the promise with the requestStateService
329
- // TODO should we enque a request per record in records?
330
- const record = context.request.data?.record || context.request.records?.[0];
331
- return store._requestCache._enqueue(promise, {
332
- data: [{
333
- op: 'saveRecord',
334
- recordIdentifier: record,
335
- options: undefined
336
- }]
337
- });
338
- }
339
- export { CacheHandler as C };