@mobx-query/core 0.2.2 → 0.2.4

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,94 +0,0 @@
1
- import {
2
- type DefaultError,
3
- type MutationFunctionContext,
4
- useMutation,
5
- } from "@tanstack/react-query";
6
- import type { EntityConstructorAny } from "../entity";
7
- import type { UseEntityMutationHookOptions } from "./types";
8
- import { EntityMutationBase } from "./EntityMutationBase";
9
- import { action } from "mobx";
10
-
11
- export class DeleteMutation<
12
- TEntityConstructor extends EntityConstructorAny,
13
- TError = DefaultError,
14
- TMutateResult = unknown,
15
- > extends EntityMutationBase<void, TEntityConstructor, TError, TMutateResult> {
16
- static readonly mutationPrefix = "__mutation__delete__";
17
-
18
- constructor(
19
- options: UseEntityMutationHookOptions<
20
- TEntityConstructor,
21
- TError,
22
- TMutateResult
23
- >,
24
- ) {
25
- super(DeleteMutation.mutationPrefix, options);
26
- }
27
-
28
- useMutation() {
29
- const mutation = useMutation({
30
- mutationFn: () => this.options.mutationFn(void 0, this.context),
31
- onMutate: (_, context) => this.onMutate(context),
32
- onSuccess: (_, __, result, context) => this.onSuccess(result, context),
33
- onError: (error, _, result, context) =>
34
- this.onError(error, result, context),
35
- onSettled: (_, error, __, result, context) =>
36
- this.onSettled(error, result, context),
37
- gcTime: this.options.gcTime,
38
- meta: this.options.meta,
39
- networkMode: this.options.networkMode,
40
- retry: this.options.retry,
41
- retryDelay: this.options.retryDelay,
42
- scope: this.options.scope,
43
- throwOnError: this.options.throwOnError,
44
- mutationKey: this.entityMutationKey,
45
- });
46
-
47
- return () => mutation.mutate();
48
- }
49
-
50
- @action private async onMutate(
51
- context: MutationFunctionContext,
52
- ): Promise<TMutateResult | undefined> {
53
- const result = await this.onMutateBase(context);
54
- this.collection.deletedRecordIds.add(this.options.instance.id);
55
- return result;
56
- }
57
-
58
- @action private async onSuccess(
59
- result: TMutateResult | undefined,
60
- ctx: MutationFunctionContext,
61
- ) {
62
- await this.onSuccessBase(result, ctx);
63
- this.collection.deletedRecordIds.delete(this.options.instance.id);
64
- this.collection.deleteEntity(this.options.instance.id);
65
- }
66
-
67
- @action private async onError(
68
- error: TError,
69
- result: TMutateResult | undefined,
70
- ctx: MutationFunctionContext,
71
- ) {
72
- await this.onErrorBase(error, result, ctx);
73
- this.collection.deletedRecordIds.delete(this.options.instance.id);
74
- }
75
-
76
- private async onSettled(
77
- error: TError | null,
78
- result: TMutateResult | undefined,
79
- ctx: MutationFunctionContext,
80
- ) {
81
- await this.onSettledBase(error, result, ctx);
82
- }
83
-
84
- mutate(input: void): void {
85
- this.mutateBase(input, {
86
- onMutate: (_, context) => this.onMutate(context),
87
- onSuccess: (_, result, context) => this.onSuccess(result, context),
88
- onError: (error, _, result, context) =>
89
- this.onError(error, result, context),
90
- onSettled: (_, error, result, context) =>
91
- this.onSettled(error, result, context),
92
- });
93
- }
94
- }
@@ -1,110 +0,0 @@
1
- import type { EntityConstructorAny, EntityAny } from "../entity";
2
- import { OptimisticMutationStrategy } from "./OptimisticMutationStrategy";
3
- import type {
4
- EntityMutationCallbacks,
5
- UseEntityMutationHookOptions,
6
- } from "./types";
7
- import type {
8
- DefaultError,
9
- MutationFunctionContext,
10
- } from "@tanstack/react-query";
11
- import { MutationBase } from "./MutationBase";
12
-
13
- /**
14
- * Base class for mutations that operate ON an existing entity.
15
- * Use this for update/delete mutations where the entity already exists.
16
- * For entity-creating mutations, use CollectionMutationBase instead.
17
- */
18
- export abstract class EntityMutationBase<
19
- TInput,
20
- TEntityConstructor extends EntityConstructorAny,
21
- TError = DefaultError,
22
- TMutateResult = unknown,
23
- > extends MutationBase<TEntityConstructor> {
24
- protected readonly mutationStrategy: OptimisticMutationStrategy;
25
-
26
- constructor(
27
- mutationPrefix: string,
28
- protected readonly options: UseEntityMutationHookOptions<
29
- TEntityConstructor,
30
- TError,
31
- TMutateResult
32
- >,
33
- ) {
34
- super(mutationPrefix, options.entity);
35
- this.mutationStrategy = this.createMutationStrategy([options.instance], {
36
- invalidationStrategy: options.invalidationStrategy,
37
- errorStrategy: options.errorStrategy,
38
- invalidateOnError: options.invalidateOnError,
39
- });
40
- }
41
-
42
- get entityMutationKey() {
43
- return [...this.baseMutationKey, this.options.instance.id];
44
- }
45
-
46
- protected async onMutateBase(
47
- context: MutationFunctionContext,
48
- ): Promise<TMutateResult | undefined> {
49
- this.mutationStrategy.onMutate();
50
- const result = await this.options.onMutate?.(
51
- this.options.instance,
52
- context,
53
- );
54
- return result;
55
- }
56
-
57
- protected async onSuccessBase(
58
- result: TMutateResult | undefined,
59
- ctx: MutationFunctionContext,
60
- ) {
61
- this.mutationStrategy.onSuccess();
62
- await this.options.onSuccess?.(this.options.instance, result, ctx);
63
- }
64
-
65
- protected async onErrorBase(
66
- error: TError,
67
- result: TMutateResult | undefined,
68
- ctx: MutationFunctionContext,
69
- ) {
70
- this.mutationStrategy.onError();
71
- await this.options.onError?.(error, this.options.instance, result, ctx);
72
- }
73
-
74
- protected async onSettledBase(
75
- error: TError | null,
76
- result: TMutateResult | undefined,
77
- ctx: MutationFunctionContext,
78
- ) {
79
- await this.options.onSettled?.(this.options.instance, error, result, ctx);
80
- }
81
-
82
- protected mutateBase(
83
- input: TInput,
84
- callbacks: EntityMutationCallbacks<EntityAny, TError, TMutateResult>,
85
- ) {
86
- this.runSyncMutation(
87
- input,
88
- (_, context) => this.options.mutationFn(void 0, context),
89
- {
90
- onMutate: async (_, context) =>
91
- callbacks.onMutate?.(this.options.instance, context),
92
- onSuccess: (_, __, result, context) =>
93
- callbacks.onSuccess?.(this.options.instance, result!, context),
94
- onError: (error, _, result, context) =>
95
- callbacks.onError?.(error, this.options.instance, result, context),
96
- onSettled: (_, error, __, result, context) =>
97
- callbacks.onSettled?.(this.options.instance, error, result, context),
98
- gcTime: this.options.gcTime,
99
- meta: this.options.meta,
100
- networkMode: this.options.networkMode,
101
- retry: this.options.retry,
102
- retryDelay: this.options.retryDelay,
103
- scope: this.options.scope,
104
- mutationKey: this.entityMutationKey,
105
- },
106
- );
107
- }
108
-
109
- abstract mutate(input: TInput): void;
110
- }
@@ -1,40 +0,0 @@
1
- import type { EntityAny, EntityConstructorAny } from "../entity";
2
- import { EntityCollection } from "../entity/EntityCollection";
3
- import { MQClientAccessor } from "../client";
4
- import { OptimisticMutationStrategy } from "./OptimisticMutationStrategy";
5
- import type { OptimisticMutationStrategyOptions } from "./types";
6
-
7
- export class MutationBase<
8
- TEntityConstructor extends EntityConstructorAny,
9
- > extends MQClientAccessor {
10
- protected readonly collection: EntityCollection<TEntityConstructor>;
11
-
12
- constructor(
13
- protected readonly mutationPrefix: string,
14
- protected readonly entity: TEntityConstructor,
15
- ) {
16
- super();
17
-
18
- this.collection = this.getEntityCollection(entity);
19
- }
20
-
21
- get baseMutationKey() {
22
- return [this.entity.name, this.mutationPrefix];
23
- }
24
-
25
- protected createMutationStrategy(
26
- entities: EntityAny[],
27
- options: OptimisticMutationStrategyOptions,
28
- ) {
29
- const mutationStrategy = new OptimisticMutationStrategy(
30
- this.entity,
31
- entities,
32
- {
33
- invalidationStrategy: options.invalidationStrategy,
34
- errorStrategy: options.errorStrategy,
35
- invalidateOnError: options.invalidateOnError,
36
- },
37
- );
38
- return mutationStrategy;
39
- }
40
- }
@@ -1,122 +0,0 @@
1
- import { __MOBX_QUERY__ } from "../client/MQClient";
2
- import {
3
- type EntityConstructorAny,
4
- type EntityAny,
5
- EntityState,
6
- } from "../entity";
7
- import type { OptimisticMutationStrategyOptions } from "./types";
8
- import { action } from "mobx";
9
- import { invalidateQueryByHash } from "../utils";
10
- import { MQClientAccessor } from "../client";
11
- import {
12
- OptimisticMutationErrorStrategy,
13
- OptimisticMutationInvalidationStrategy,
14
- } from "./constants";
15
-
16
- export class OptimisticMutationStrategy extends MQClientAccessor {
17
- constructor(
18
- private readonly entityConstructor: EntityConstructorAny,
19
- private readonly entities: EntityAny[],
20
- private readonly options?: OptimisticMutationStrategyOptions,
21
- ) {
22
- super();
23
- }
24
-
25
- get invalidationStrategy() {
26
- return (
27
- this.options?.invalidationStrategy ??
28
- __MOBX_QUERY__.options.invalidationStrategy
29
- );
30
- }
31
-
32
- get mutationErrorStrategy() {
33
- return this.options?.errorStrategy ?? __MOBX_QUERY__.options.errorStrategy;
34
- }
35
-
36
- get invalidateOnError() {
37
- return (
38
- this.options?.invalidateOnError ??
39
- __MOBX_QUERY__.options.invalidateOnError
40
- );
41
- }
42
-
43
- @action onMutate() {
44
- this.queryClient.cancelQueries({ queryKey: [this.entityConstructor.name] });
45
- for (const entity of this.entities) {
46
- entity.state = EntityState.PENDING;
47
- }
48
- }
49
-
50
- @action onSuccess() {
51
- for (const entity of this.entities) {
52
- entity.state = EntityState.CONFIRMED;
53
- }
54
- this.runInvalidationStrategy();
55
- }
56
-
57
- @action onError(ignoreReset: boolean = false) {
58
- for (const entity of this.entities) {
59
- entity.state = EntityState.FAILED;
60
-
61
- if (
62
- this.mutationErrorStrategy ===
63
- OptimisticMutationErrorStrategy.ROLLBACK &&
64
- !ignoreReset
65
- ) {
66
- entity.reset();
67
- }
68
- }
69
-
70
- if (this.invalidateOnError) {
71
- this.runInvalidationStrategy();
72
- }
73
- }
74
-
75
- private runInvalidationStrategy() {
76
- const strategy = this.invalidationStrategy;
77
-
78
- switch (strategy) {
79
- case OptimisticMutationInvalidationStrategy.ALL_QUERIES:
80
- this.queryClient.invalidateQueries();
81
- break;
82
- case OptimisticMutationInvalidationStrategy.ALL_ENTITY_QUERIES:
83
- this.invalidateCollectionRelatedQueries();
84
- break;
85
- case OptimisticMutationInvalidationStrategy.REFERENCED_QUERIES:
86
- this.invalidateEntityRelatedQueries();
87
- break;
88
- default:
89
- break;
90
- }
91
- }
92
-
93
- private async invalidateCollectionRelatedQueries() {
94
- await this.queryClient
95
- .invalidateQueries({ queryKey: [this.entityConstructor.name] })
96
- .catch((err) => console.error(err));
97
- }
98
-
99
- private invalidateEntityRelatedQueries() {
100
- const cache = this.queryClient.getQueryCache();
101
-
102
- const entitiesByHash: Record<string, EntityAny[]> = {};
103
-
104
- for (const entity of this.entities) {
105
- for (const hash of entity.queryHashes) {
106
- if (!entitiesByHash[hash]) {
107
- entitiesByHash[hash] = [];
108
- }
109
-
110
- entitiesByHash[hash].push(entity);
111
- }
112
- }
113
-
114
- for (const [hash, entities] of Object.entries(entitiesByHash)) {
115
- invalidateQueryByHash(hash, cache, () => {
116
- for (const entity of entities) {
117
- entity._removeQueryHashes([hash]);
118
- }
119
- });
120
- }
121
- }
122
- }
@@ -1,76 +0,0 @@
1
- import { type DefaultError, useMutation } from "@tanstack/react-query";
2
- import type { EntityConstructorAny } from "../entity";
3
- import type { UseEntityMutationHookOptions } from "./types";
4
- import { EntityMutationBase } from "./EntityMutationBase";
5
-
6
- export class UpdateMutation<
7
- TEntityConstructor extends EntityConstructorAny,
8
- TError = DefaultError,
9
- TMutateResult = unknown,
10
- > extends EntityMutationBase<void, TEntityConstructor, TError, TMutateResult> {
11
- static readonly mutationPrefix = "__mutation__update__";
12
-
13
- constructor(
14
- options: UseEntityMutationHookOptions<
15
- TEntityConstructor,
16
- TError,
17
- TMutateResult
18
- >,
19
- ) {
20
- super(UpdateMutation.mutationPrefix, options);
21
- }
22
-
23
- useMutation() {
24
- const mutation = useMutation({
25
- mutationFn: () => this.options.mutationFn(void 0, this.context),
26
- onMutate: async (_, context) => this.onMutateBase(context),
27
- onSuccess: (_, __, result, context) =>
28
- this.onSuccessBase(result, context),
29
- onError: (error, _, result, context) =>
30
- this.onErrorBase(error, result, context),
31
- onSettled: (_, error, __, result, context) =>
32
- this.onSettledBase(error, result, context),
33
- gcTime: this.options.gcTime,
34
- meta: this.options.meta,
35
- networkMode: this.options.networkMode,
36
- retry: this.options.retry,
37
- retryDelay: this.options.retryDelay,
38
- scope: this.options.scope,
39
- throwOnError: this.options.throwOnError,
40
- mutationKey: this.entityMutationKey,
41
- });
42
-
43
- return () => {
44
- if (!this.options.instance.isDirty) {
45
- return console.warn(
46
- "Entity values has not been changed, mutation is skipped.",
47
- );
48
- }
49
-
50
- // if (this.entity.state === EntityState.PENDING) {
51
- // return console.warn(
52
- // 'Entity update mutation is already in progress, new mutation is skipped.',
53
- // )
54
- // }
55
-
56
- mutation.mutate();
57
- };
58
- }
59
-
60
- mutate(input: void): void {
61
- if (!this.options.instance.isDirty) {
62
- return console.warn(
63
- "Entity values has not been changed, mutation is skipped.",
64
- );
65
- }
66
-
67
- this.mutateBase(input, {
68
- onMutate: (_, context) => this.onMutateBase(context),
69
- onSuccess: (_, result, context) => this.onSuccessBase(result, context),
70
- onError: (error, _, result, context) =>
71
- this.onErrorBase(error, result, context),
72
- onSettled: (_, error, result, context) =>
73
- this.onSettledBase(error, result, context),
74
- });
75
- }
76
- }
@@ -1,16 +0,0 @@
1
- export const OptimisticMutationInvalidationStrategy = {
2
- ALL_QUERIES: "all-queries",
3
- ALL_ENTITY_QUERIES: "all-entity-queries",
4
- REFERENCED_QUERIES: "referenced-queries",
5
- NONE: "none",
6
- } as const;
7
-
8
- export const OptimisticMutationErrorStrategy = {
9
- ROLLBACK: "rollback",
10
- KEEP: "keep",
11
- } as const;
12
-
13
- export type OptimisticMutationInvalidationStrategy =
14
- (typeof OptimisticMutationInvalidationStrategy)[keyof typeof OptimisticMutationInvalidationStrategy];
15
- export type OptimisticMutationErrorStrategy =
16
- (typeof OptimisticMutationErrorStrategy)[keyof typeof OptimisticMutationErrorStrategy];
@@ -1,44 +0,0 @@
1
- import type {
2
- MutationFn,
3
- BatchMutationFn,
4
- CreateMutationFn,
5
- OptimisticMutationStrategyOptions,
6
- UseCreateMutationHookOptions,
7
- UseEntityMutationHookOptions,
8
- UseBatchMutationHookOptions,
9
- } from "./types";
10
-
11
- import { BatchMutationBase } from "./BatchMutationBase";
12
- import { BatchUpdateMutation } from "./BatchUpdateMutation";
13
- import { CreateMutation } from "./CreateMutation";
14
- import { DeleteMutation } from "./DeleteMutation";
15
- import { EntityMutationBase } from "./EntityMutationBase";
16
- import { UpdateMutation } from "./UpdateMutation";
17
- import { MutationBase } from "./MutationBase";
18
-
19
- import {
20
- OptimisticMutationErrorStrategy,
21
- OptimisticMutationInvalidationStrategy,
22
- } from "./constants";
23
-
24
- export {
25
- MutationBase,
26
- BatchMutationBase,
27
- EntityMutationBase,
28
- BatchUpdateMutation,
29
- CreateMutation,
30
- DeleteMutation,
31
- UpdateMutation,
32
- OptimisticMutationErrorStrategy,
33
- OptimisticMutationInvalidationStrategy,
34
- };
35
-
36
- export type {
37
- MutationFn,
38
- BatchMutationFn,
39
- CreateMutationFn,
40
- OptimisticMutationStrategyOptions,
41
- UseCreateMutationHookOptions,
42
- UseEntityMutationHookOptions,
43
- UseBatchMutationHookOptions,
44
- };