@mobx-query/core 0.2.2 → 0.2.3

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 (93) hide show
  1. package/dist/client/MQClient.d.ts +23 -0
  2. package/dist/client/MQClientAccessor.d.ts +12 -0
  3. package/dist/client/index.d.ts +5 -0
  4. package/{src/libs/mobx-query/client/types.ts → dist/client/types.d.ts} +31 -37
  5. package/dist/core.js +1830 -0
  6. package/dist/entity/Entity.d.ts +31 -0
  7. package/dist/entity/EntityCollection.d.ts +29 -0
  8. package/dist/entity/constants.d.ts +6 -0
  9. package/dist/entity/index.d.ts +6 -0
  10. package/{src/libs/mobx-query/entity/types.ts → dist/entity/types.d.ts} +8 -11
  11. package/dist/index.d.ts +6 -0
  12. package/dist/mutations/BatchMutationBase.d.ts +14 -0
  13. package/dist/mutations/BatchUpdateMutation.d.ts +9 -0
  14. package/dist/mutations/CreateMutation.d.ts +15 -0
  15. package/dist/mutations/DeleteMutation.d.ts +14 -0
  16. package/dist/mutations/EntityMutationBase.d.ts +22 -0
  17. package/dist/mutations/MutationBase.d.ts +13 -0
  18. package/dist/mutations/OptimisticMutationStrategy.d.ts +19 -0
  19. package/dist/mutations/UpdateMutation.d.ts +10 -0
  20. package/dist/mutations/constants.d.ts +12 -0
  21. package/dist/mutations/index.d.ts +11 -0
  22. package/dist/mutations/types.d.ts +55 -0
  23. package/dist/queries/QueryBase.d.ts +14 -0
  24. package/dist/queries/QueryFragmentMany.d.ts +9 -0
  25. package/dist/queries/QueryFragmentOne.d.ts +9 -0
  26. package/dist/queries/QueryMany.d.ts +11 -0
  27. package/dist/queries/QueryManyBase.d.ts +34 -0
  28. package/dist/queries/QueryOne.d.ts +11 -0
  29. package/dist/queries/QueryOneBase.d.ts +19 -0
  30. package/dist/queries/index.d.ts +10 -0
  31. package/dist/queries/types.d.ts +23 -0
  32. package/dist/react/createReactContext.d.ts +8 -0
  33. package/dist/react/index.d.ts +2 -0
  34. package/dist/utils/generateEntityId.d.ts +2 -0
  35. package/dist/utils/index.d.ts +5 -0
  36. package/dist/utils/invalidateQueryByHash.d.ts +2 -0
  37. package/dist/utils/types.d.ts +7 -0
  38. package/package.json +6 -1
  39. package/.gitattributes +0 -2
  40. package/eslint.config.js +0 -29
  41. package/index.html +0 -13
  42. package/src/App.css +0 -0
  43. package/src/App.tsx +0 -5
  44. package/src/api/constants.ts +0 -1
  45. package/src/api/fetch.ts +0 -29
  46. package/src/api/todos.ts +0 -14
  47. package/src/api/types.ts +0 -30
  48. package/src/api/users.ts +0 -19
  49. package/src/assets/react.svg +0 -1
  50. package/src/index.css +0 -60
  51. package/src/libs/mobx-query/client/MQClient.ts +0 -75
  52. package/src/libs/mobx-query/client/MQClientAccessor.ts +0 -74
  53. package/src/libs/mobx-query/client/index.ts +0 -8
  54. package/src/libs/mobx-query/entity/Entity.ts +0 -232
  55. package/src/libs/mobx-query/entity/EntityCollection.ts +0 -285
  56. package/src/libs/mobx-query/entity/constants.ts +0 -7
  57. package/src/libs/mobx-query/entity/index.ts +0 -15
  58. package/src/libs/mobx-query/index.ts +0 -6
  59. package/src/libs/mobx-query/mutations/BatchMutationBase.ts +0 -105
  60. package/src/libs/mobx-query/mutations/BatchUpdateMutation.ts +0 -48
  61. package/src/libs/mobx-query/mutations/CreateMutation.ts +0 -172
  62. package/src/libs/mobx-query/mutations/DeleteMutation.ts +0 -94
  63. package/src/libs/mobx-query/mutations/EntityMutationBase.ts +0 -110
  64. package/src/libs/mobx-query/mutations/MutationBase.ts +0 -40
  65. package/src/libs/mobx-query/mutations/OptimisticMutationStrategy.ts +0 -122
  66. package/src/libs/mobx-query/mutations/UpdateMutation.ts +0 -76
  67. package/src/libs/mobx-query/mutations/constants.ts +0 -16
  68. package/src/libs/mobx-query/mutations/index.ts +0 -44
  69. package/src/libs/mobx-query/mutations/types.ts +0 -205
  70. package/src/libs/mobx-query/queries/QueryBase.ts +0 -65
  71. package/src/libs/mobx-query/queries/QueryFragmentMany.ts +0 -31
  72. package/src/libs/mobx-query/queries/QueryFragmentOne.ts +0 -35
  73. package/src/libs/mobx-query/queries/QueryMany.ts +0 -80
  74. package/src/libs/mobx-query/queries/QueryManyBase.ts +0 -135
  75. package/src/libs/mobx-query/queries/QueryOne.ts +0 -84
  76. package/src/libs/mobx-query/queries/QueryOneBase.ts +0 -93
  77. package/src/libs/mobx-query/queries/index.ts +0 -33
  78. package/src/libs/mobx-query/queries/types.ts +0 -60
  79. package/src/libs/mobx-query/react/createReactContext.tsx +0 -23
  80. package/src/libs/mobx-query/react/index.ts +0 -3
  81. package/src/libs/mobx-query/utils/generateEntityId.ts +0 -12
  82. package/src/libs/mobx-query/utils/index.ts +0 -8
  83. package/src/libs/mobx-query/utils/invalidateQueryByHash.ts +0 -18
  84. package/src/libs/mobx-query/utils/types.ts +0 -18
  85. package/src/libs/react-query.ts +0 -11
  86. package/src/main.tsx +0 -16
  87. package/src/utils.ts +0 -3
  88. package/src/vite-env.d.ts +0 -1
  89. package/tsconfig.app.json +0 -27
  90. package/tsconfig.json +0 -7
  91. package/tsconfig.node.json +0 -25
  92. package/vite.config.ts +0 -52
  93. /package/{public → dist}/vite.svg +0 -0
@@ -1,285 +0,0 @@
1
- import { action, computed, observable } from "mobx";
2
- import type { EntityConstructor, EntityConstructorAny } from "./Entity";
3
- import { QueryClient } from "@tanstack/react-query";
4
- import type { EntityDataAny, EntityId } from "./types";
5
-
6
- export class EntityCollection<
7
- TEntityConstructor extends EntityConstructorAny = EntityConstructorAny,
8
- > {
9
- @observable accessor collection = new Map<
10
- EntityId,
11
- InstanceType<TEntityConstructor>
12
- >();
13
- @observable accessor deletedRecordIds = new Set<EntityId>();
14
- @observable accessor clientOnlyEntityIds = new Set<EntityId>();
15
-
16
- constructor(
17
- private readonly entityConstructor: EntityConstructor<
18
- any,
19
- any,
20
- InstanceType<TEntityConstructor>
21
- >,
22
- private readonly queryClient: QueryClient,
23
- ) {
24
- this.initQueryClientCacheListener();
25
- }
26
-
27
- @computed get size() {
28
- return this.collection.size;
29
- }
30
-
31
- @computed get entities() {
32
- const entities: InstanceType<TEntityConstructor>[] = [];
33
-
34
- for (const entity of this.collection.values()) {
35
- if (this.deletedRecordIds.has(entity.id)) continue;
36
- entities.push(entity);
37
- }
38
-
39
- return entities;
40
- }
41
-
42
- @computed get clientOnlyEntitiesMap() {
43
- const entities = new Map<EntityId, InstanceType<TEntityConstructor>>();
44
-
45
- for (const entityId of this.clientOnlyEntityIds.values()) {
46
- const entity = this.collection.get(entityId);
47
-
48
- if (!entity) {
49
- continue;
50
- }
51
-
52
- entities.set(entityId, entity);
53
- }
54
-
55
- return entities;
56
- }
57
-
58
- @computed get clientOnlyEntities() {
59
- const entities: InstanceType<TEntityConstructor>[] = [];
60
-
61
- for (const entityId of this.clientOnlyEntityIds.values()) {
62
- const entity = this.collection.get(entityId);
63
-
64
- if (!entity) {
65
- continue;
66
- }
67
-
68
- entities.push(entity);
69
- }
70
-
71
- return entities;
72
- }
73
-
74
- @action setEntity(data: EntityDataAny, queryHashes?: string[]) {
75
- // if (clearQueryHashes && queryHashes) {
76
- // console.log('clearQueryHashes', queryHashes)
77
- // this.removeQueryHashesFromAllEntities(queryHashes)
78
- // }
79
-
80
- const entity =
81
- new this.entityConstructor() as InstanceType<TEntityConstructor>;
82
- entity.hydrate(data);
83
- entity._markAsHydrated();
84
-
85
- const prevEntity = this.collection.get(entity.id) as
86
- | InstanceType<TEntityConstructor>
87
- | undefined;
88
-
89
- const updatedQueryHashes = prevEntity
90
- ? [...prevEntity.queryHashes, ...(queryHashes || [])]
91
- : [...(queryHashes || [])];
92
-
93
- if (prevEntity) {
94
- if (queryHashes) {
95
- for (const hash of queryHashes) {
96
- prevEntity.queryHashes.add(hash);
97
- }
98
- }
99
-
100
- prevEntity.hydrate(data);
101
- prevEntity._clearDirty();
102
-
103
- return prevEntity;
104
- }
105
-
106
- entity._init(updatedQueryHashes, {
107
- onAllQueryHashesRemoved: (entityId: EntityId) =>
108
- this.deleteEntity(entityId),
109
- });
110
-
111
- this.collection.set(entity.id, entity);
112
- return entity;
113
- }
114
-
115
- @action setEntities(entityData: EntityDataAny[], queryHashes: string[]) {
116
- const entities: InstanceType<TEntityConstructor>[] = [];
117
- const ids: EntityId[] = [];
118
-
119
- if (!Array.isArray(entityData)) {
120
- throw new Error("setEntities: entityData must be an array");
121
- }
122
-
123
- if (queryHashes) {
124
- this.removeQueryHashesFromAllEntities(queryHashes);
125
- }
126
-
127
- for (const record of entityData) {
128
- const entity = this.setEntity(record, queryHashes);
129
- entities.push(entity);
130
- ids.push(entity.id);
131
- }
132
-
133
- return [entities, ids] as const;
134
- }
135
-
136
- @action deleteEntity(entityId: EntityId) {
137
- this.collection.delete(entityId);
138
- this.clientOnlyEntityIds.delete(entityId);
139
- this.deletedRecordIds.delete(entityId);
140
- }
141
-
142
- getEntityById(entityId: EntityId) {
143
- return this.collection.get(entityId);
144
- }
145
-
146
- filter(
147
- predicate: (
148
- value: InstanceType<TEntityConstructor>,
149
- index: number,
150
- ) => boolean,
151
- ): InstanceType<TEntityConstructor>[] {
152
- const result: InstanceType<TEntityConstructor>[] = [];
153
- let i = 0;
154
- for (const entity of this.collection.values()) {
155
- if (this.deletedRecordIds.has(entity.id)) continue;
156
- if (predicate(entity, i++)) {
157
- result.push(entity);
158
- }
159
- }
160
- return result;
161
- }
162
-
163
- find(
164
- predicate: (
165
- value: InstanceType<TEntityConstructor>,
166
- index: number,
167
- ) => boolean,
168
- ): InstanceType<TEntityConstructor> | undefined {
169
- let i = 0;
170
- for (const entity of this.collection.values()) {
171
- if (this.deletedRecordIds.has(entity.id)) continue;
172
- if (predicate(entity, i++)) {
173
- return entity;
174
- }
175
- }
176
- return undefined;
177
- }
178
-
179
- findIndex(
180
- predicate: (
181
- value: InstanceType<TEntityConstructor>,
182
- index: number,
183
- ) => boolean,
184
- ): number {
185
- let i = 0;
186
- for (const entity of this.collection.values()) {
187
- if (this.deletedRecordIds.has(entity.id)) continue;
188
- if (predicate(entity, i)) {
189
- return i;
190
- }
191
- i++;
192
- }
193
- return -1;
194
- }
195
-
196
- findLast(
197
- predicate: (
198
- value: InstanceType<TEntityConstructor>,
199
- index: number,
200
- ) => boolean,
201
- ): InstanceType<TEntityConstructor> | undefined {
202
- const entities = this.entities;
203
- for (let i = entities.length - 1; i >= 0; i--) {
204
- const entity = entities[i];
205
- if (predicate(entity, i)) {
206
- return entity;
207
- }
208
- }
209
- return undefined;
210
- }
211
-
212
- findLastIndex(
213
- predicate: (
214
- value: InstanceType<TEntityConstructor>,
215
- index: number,
216
- ) => boolean,
217
- ): number {
218
- const entities = this.entities;
219
- for (let i = entities.length - 1; i >= 0; i--) {
220
- const entity = entities[i];
221
- if (predicate(entity, i)) {
222
- return i;
223
- }
224
- }
225
- return -1;
226
- }
227
-
228
- some(
229
- predicate: (
230
- value: InstanceType<TEntityConstructor>,
231
- index: number,
232
- ) => boolean,
233
- ): boolean {
234
- let i = 0;
235
- for (const entity of this.collection.values()) {
236
- if (this.deletedRecordIds.has(entity.id)) continue;
237
- if (predicate(entity, i++)) {
238
- return true;
239
- }
240
- }
241
- return false;
242
- }
243
-
244
- every(
245
- predicate: (
246
- value: InstanceType<TEntityConstructor>,
247
- index: number,
248
- ) => boolean,
249
- ): boolean {
250
- let i = 0;
251
- for (const entity of this.collection.values()) {
252
- if (this.deletedRecordIds.has(entity.id)) continue;
253
- if (!predicate(entity, i++)) {
254
- return false;
255
- }
256
- }
257
- return true;
258
- }
259
-
260
- map<T>(
261
- predicate: (value: InstanceType<TEntityConstructor>, index: number) => T,
262
- ): T[] {
263
- const result: T[] = [];
264
- let i = 0;
265
- for (const entity of this.collection.values()) {
266
- if (this.deletedRecordIds.has(entity.id)) continue;
267
- result.push(predicate(entity, i++));
268
- }
269
- return observable.array(result);
270
- }
271
-
272
- private initQueryClientCacheListener() {
273
- this.queryClient.getQueryCache().subscribe((event) => {
274
- if (event.type === "removed") {
275
- this.removeQueryHashesFromAllEntities([event.query.queryHash]);
276
- }
277
- });
278
- }
279
-
280
- private removeQueryHashesFromAllEntities(hashes: string[]) {
281
- for (const entity of this.collection.values()) {
282
- entity._removeQueryHashes(hashes);
283
- }
284
- }
285
- }
@@ -1,7 +0,0 @@
1
- export const EntityState = {
2
- PENDING: "pending",
3
- CONFIRMED: "confirmed",
4
- FAILED: "failed",
5
- } as const;
6
-
7
- export type EntityState = (typeof EntityState)[keyof typeof EntityState];
@@ -1,15 +0,0 @@
1
- import { Entity, type EntityConstructorAny, type EntityAny } from "./Entity";
2
- import { EntityCollection } from "./EntityCollection";
3
- import { EntityState } from "./constants";
4
-
5
- import type { EntityId, EntityData, EntityDataAny } from "./types";
6
-
7
- export { Entity, EntityState, EntityCollection };
8
-
9
- export type {
10
- EntityConstructorAny,
11
- EntityAny,
12
- EntityData,
13
- EntityDataAny,
14
- EntityId,
15
- };
@@ -1,6 +0,0 @@
1
- export * from "./client";
2
- export * from "./entity";
3
- export * from "./mutations";
4
- export * from "./queries";
5
- export * from "./react";
6
- export * from "./utils";
@@ -1,105 +0,0 @@
1
- import type {
2
- DefaultError,
3
- MutationFunctionContext,
4
- } from "@tanstack/react-query";
5
- import type { EntityConstructorAny, EntityCollection } from "../entity";
6
- import type {
7
- BatchMutationInputInternal,
8
- UseBatchMutationHookOptions,
9
- } from "./types";
10
- import { MutationBase } from "./MutationBase";
11
-
12
- export class BatchMutationBase<
13
- TEntityConstructor extends EntityConstructorAny,
14
- TError = DefaultError,
15
- TMutateResult = unknown,
16
- > extends MutationBase<TEntityConstructor> {
17
- protected readonly collection: EntityCollection<TEntityConstructor>;
18
-
19
- constructor(
20
- mutationPrefix: string,
21
- protected readonly options: UseBatchMutationHookOptions<
22
- TEntityConstructor,
23
- TError,
24
- TMutateResult
25
- >,
26
- ) {
27
- super(mutationPrefix, options.entity);
28
-
29
- this.collection = this.getEntityCollection(options.entity);
30
- }
31
-
32
- protected async onMutateBase(
33
- input: BatchMutationInputInternal<TEntityConstructor>,
34
- context: MutationFunctionContext,
35
- ): Promise<TMutateResult> {
36
- input.strategy.onMutate();
37
- const result = await this.options.onMutate?.(input.entities, context);
38
- return result ?? ({} as TMutateResult);
39
- }
40
-
41
- protected async onSuccessBase(
42
- input: BatchMutationInputInternal<TEntityConstructor>,
43
- onMutateResult: TMutateResult | undefined,
44
- context: MutationFunctionContext,
45
- ): Promise<void> {
46
- input.strategy.onSuccess();
47
- await this.options.onSuccess?.(input.entities, context, onMutateResult);
48
- }
49
-
50
- protected async onErrorBase(
51
- error: TError,
52
- input: BatchMutationInputInternal<TEntityConstructor>,
53
- onMutateResult: TMutateResult | undefined,
54
- context: MutationFunctionContext,
55
- ): Promise<void> {
56
- input.strategy.onError();
57
- await this.options.onError?.(
58
- error,
59
- input.entities,
60
- context,
61
- onMutateResult,
62
- );
63
- }
64
-
65
- protected async onSettledBase(
66
- error: TError | null,
67
- input: BatchMutationInputInternal<TEntityConstructor>,
68
- onMutateResult: TMutateResult | undefined,
69
- context: MutationFunctionContext,
70
- ): Promise<void> {
71
- await this.options.onSettled?.(
72
- input.entities,
73
- context,
74
- onMutateResult,
75
- error,
76
- );
77
- }
78
-
79
- protected createInternalInput(
80
- entities: InstanceType<TEntityConstructor>[],
81
- ): BatchMutationInputInternal<TEntityConstructor> {
82
- const dirtyEntities: InstanceType<TEntityConstructor>[] = [];
83
-
84
- for (const entity of entities) {
85
- if (!entity.isDirty) {
86
- console.warn(
87
- "Entity values has not been changed, mutation is skipped.",
88
- );
89
-
90
- continue;
91
- }
92
-
93
- dirtyEntities.push(entity);
94
- }
95
-
96
- return {
97
- entities: dirtyEntities,
98
- strategy: this.createMutationStrategy(dirtyEntities, {
99
- invalidationStrategy: this.options.invalidationStrategy,
100
- errorStrategy: this.options.errorStrategy,
101
- invalidateOnError: this.options.invalidateOnError,
102
- }),
103
- };
104
- }
105
- }
@@ -1,48 +0,0 @@
1
- import { type DefaultError, useMutation } from "@tanstack/react-query";
2
- import type { EntityConstructorAny } from "../entity";
3
- import { BatchMutationBase } from "./BatchMutationBase";
4
- import type {
5
- BatchMutationInputInternal,
6
- UseBatchMutationHookOptions,
7
- } from "./types";
8
-
9
- export class BatchUpdateMutation<
10
- TEntityConstructor extends EntityConstructorAny,
11
- TError = DefaultError,
12
- TMutateResult = unknown,
13
- > extends BatchMutationBase<TEntityConstructor, TError, TMutateResult> {
14
- static readonly mutationPrefix = "__mutation__batch__update__";
15
-
16
- constructor(
17
- options: UseBatchMutationHookOptions<
18
- TEntityConstructor,
19
- TError,
20
- TMutateResult
21
- >,
22
- ) {
23
- super(BatchUpdateMutation.mutationPrefix, options);
24
- }
25
-
26
- useMutation() {
27
- const mutation = useMutation<
28
- void,
29
- TError,
30
- BatchMutationInputInternal<TEntityConstructor>,
31
- TMutateResult
32
- >({
33
- mutationFn: (input) =>
34
- this.options.mutationFn(input.entities, this.context),
35
- onMutate: (input, context) => this.onMutateBase(input, context),
36
- onSuccess: (_, input, onMutateResult, context) =>
37
- this.onSuccessBase(input, onMutateResult, context),
38
- onError: (error, input, onMutateResult, context) =>
39
- this.onErrorBase(error, input, onMutateResult, context),
40
- onSettled: (_, error, input, onMutateResult, context) =>
41
- this.onSettledBase(error, input, onMutateResult, context),
42
- });
43
-
44
- return (entities: InstanceType<TEntityConstructor>[]) => {
45
- mutation.mutate(this.createInternalInput(entities));
46
- };
47
- }
48
- }
@@ -1,172 +0,0 @@
1
- import {
2
- type DefaultError,
3
- type MutationFunctionContext,
4
- useMutation,
5
- } from "@tanstack/react-query";
6
- import type { EntityAny, EntityConstructorAny } from "../entity";
7
- import type {
8
- CreateMutationInputInternal,
9
- UseCreateMutationHookOptions,
10
- } from "./types";
11
- import { action } from "mobx";
12
- import {
13
- OptimisticMutationErrorStrategy,
14
- OptimisticMutationInvalidationStrategy,
15
- } from "./constants";
16
- import { MutationBase } from "./MutationBase";
17
-
18
- export class CreateMutation<
19
- TInput,
20
- TEntityConstructor extends EntityConstructorAny = EntityConstructorAny,
21
- TError = DefaultError,
22
- TMutateResult = unknown,
23
- > extends MutationBase<TEntityConstructor> {
24
- static readonly mutationPrefix = "__mutation__create__";
25
-
26
- constructor(
27
- private readonly options: UseCreateMutationHookOptions<
28
- TInput,
29
- TEntityConstructor,
30
- TError,
31
- TMutateResult
32
- >,
33
- ) {
34
- super(CreateMutation.mutationPrefix, options.entity);
35
- }
36
-
37
- useMutation() {
38
- const mutation = useMutation<
39
- void,
40
- TError,
41
- CreateMutationInputInternal<TInput, TEntityConstructor>,
42
- TMutateResult
43
- >({
44
- mutationFn: (
45
- input: CreateMutationInputInternal<TInput, TEntityConstructor>,
46
- ) => this.options.mutationFn(input.input, input.entity, this.context),
47
- onMutate: (input, context) => this.onMutate(input, context),
48
- onSuccess: (_, input, onMutateResult, context) =>
49
- this.onSuccess(input, onMutateResult, context),
50
- onError: (error, input, onMutateResult, context) =>
51
- this.onError(error, input, onMutateResult, context),
52
- onSettled: (_, error, input, onMutateResult, context) =>
53
- this.onSettled(input, error, onMutateResult, context),
54
- gcTime: this.options.gcTime,
55
- meta: this.options.meta,
56
- networkMode: this.options.networkMode,
57
- retry: this.options.retry,
58
- retryDelay: this.options.retryDelay,
59
- scope: this.options.scope,
60
- throwOnError: this.options.throwOnError,
61
- mutationKey: this.baseMutationKey,
62
- });
63
-
64
- return (input: TInput) => {
65
- const entity =
66
- new this.options.entity() as InstanceType<TEntityConstructor>;
67
- entity.hydrate(input);
68
- entity._markAsHydrated();
69
-
70
- this.collection.setEntity(entity);
71
- this.collection.clientOnlyEntityIds.add(entity.id);
72
-
73
- mutation.mutate({
74
- input,
75
- entity,
76
- mutationStrategy: this.createMutationStrategy([entity]),
77
- });
78
- };
79
- }
80
-
81
- @action private async onMutate(
82
- input: CreateMutationInputInternal<TInput, TEntityConstructor>,
83
- context: MutationFunctionContext,
84
- ): Promise<TMutateResult> {
85
- input.mutationStrategy.onMutate();
86
-
87
- const onMutateResult = await this.options.onMutate?.(
88
- input.input,
89
- input.entity,
90
- context,
91
- );
92
- return onMutateResult ?? ({} as TMutateResult);
93
- }
94
-
95
- @action private onSuccess(
96
- input: CreateMutationInputInternal<TInput, TEntityConstructor>,
97
- onMutateResult: TMutateResult,
98
- context: MutationFunctionContext,
99
- ) {
100
- input.mutationStrategy.onSuccess();
101
-
102
- // if (this.pendingEntity) {
103
- // this.entityManager.clientOnlyEntityIds.delete(this.pendingEntity.id)
104
- // }
105
- this.options.onSuccess?.(
106
- input.input,
107
- input.entity,
108
- onMutateResult,
109
- context,
110
- );
111
- }
112
-
113
- @action private onError(
114
- error: TError,
115
- input: CreateMutationInputInternal<TInput, TEntityConstructor>,
116
- onMutateResult: TMutateResult | undefined,
117
- context: MutationFunctionContext,
118
- ) {
119
- const strategy =
120
- input.mutationStrategy?.mutationErrorStrategy ??
121
- OptimisticMutationErrorStrategy.ROLLBACK;
122
- input.mutationStrategy.onError(true);
123
-
124
- if (strategy === OptimisticMutationErrorStrategy.ROLLBACK) {
125
- this.collection.deleteEntity(input.entity.id);
126
- }
127
-
128
- this.options.onError?.(
129
- error,
130
- input.input,
131
- input.entity,
132
- onMutateResult,
133
- context,
134
- );
135
- }
136
-
137
- @action private onSettled(
138
- input: CreateMutationInputInternal<TInput, TEntityConstructor>,
139
- error: TError | null,
140
- onMutateResult: TMutateResult | undefined,
141
- context: MutationFunctionContext,
142
- ) {
143
- this.options.onSettled?.(
144
- input.input,
145
- input.entity,
146
- error,
147
- onMutateResult,
148
- context,
149
- );
150
- }
151
-
152
- protected createMutationStrategy(entities: EntityAny[]) {
153
- if (
154
- this.options.invalidationStrategy ===
155
- OptimisticMutationInvalidationStrategy.REFERENCED_QUERIES
156
- ) {
157
- throw new Error(
158
- "REFERENCED_QUERIES invalidation strategy is not supported for create mutation",
159
- );
160
- }
161
-
162
- const mutationStrategy = super.createMutationStrategy(entities, {
163
- invalidationStrategy:
164
- this.options.invalidationStrategy ??
165
- OptimisticMutationInvalidationStrategy.ALL_ENTITY_QUERIES,
166
- errorStrategy: this.options.errorStrategy,
167
- invalidateOnError: this.options.invalidateOnError,
168
- });
169
-
170
- return mutationStrategy;
171
- }
172
- }