@mobx-query/core 0.2.1 → 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.
- package/dist/client/MQClient.d.ts +23 -0
- package/dist/client/MQClientAccessor.d.ts +12 -0
- package/dist/client/index.d.ts +5 -0
- package/{src/libs/mobx-query/client/types.ts → dist/client/types.d.ts} +31 -37
- package/dist/core.js +1830 -0
- package/dist/entity/Entity.d.ts +31 -0
- package/dist/entity/EntityCollection.d.ts +29 -0
- package/dist/entity/constants.d.ts +6 -0
- package/dist/entity/index.d.ts +6 -0
- package/{src/libs/mobx-query/entity/types.ts → dist/entity/types.d.ts} +8 -11
- package/dist/index.d.ts +6 -0
- package/dist/mutations/BatchMutationBase.d.ts +14 -0
- package/dist/mutations/BatchUpdateMutation.d.ts +9 -0
- package/dist/mutations/CreateMutation.d.ts +15 -0
- package/dist/mutations/DeleteMutation.d.ts +14 -0
- package/dist/mutations/EntityMutationBase.d.ts +22 -0
- package/dist/mutations/MutationBase.d.ts +13 -0
- package/dist/mutations/OptimisticMutationStrategy.d.ts +19 -0
- package/dist/mutations/UpdateMutation.d.ts +10 -0
- package/dist/mutations/constants.d.ts +12 -0
- package/dist/mutations/index.d.ts +11 -0
- package/dist/mutations/types.d.ts +55 -0
- package/dist/queries/QueryBase.d.ts +14 -0
- package/dist/queries/QueryFragmentMany.d.ts +9 -0
- package/dist/queries/QueryFragmentOne.d.ts +9 -0
- package/dist/queries/QueryMany.d.ts +11 -0
- package/dist/queries/QueryManyBase.d.ts +34 -0
- package/dist/queries/QueryOne.d.ts +11 -0
- package/dist/queries/QueryOneBase.d.ts +19 -0
- package/dist/queries/index.d.ts +10 -0
- package/dist/queries/types.d.ts +23 -0
- package/dist/react/createReactContext.d.ts +8 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/utils/generateEntityId.d.ts +2 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/invalidateQueryByHash.d.ts +2 -0
- package/dist/utils/types.d.ts +7 -0
- package/package.json +8 -18
- package/.gitattributes +0 -2
- package/eslint.config.js +0 -29
- package/index.html +0 -13
- package/src/App.css +0 -0
- package/src/App.tsx +0 -5
- package/src/api/constants.ts +0 -1
- package/src/api/fetch.ts +0 -29
- package/src/api/todos.ts +0 -14
- package/src/api/types.ts +0 -30
- package/src/api/users.ts +0 -19
- package/src/assets/react.svg +0 -1
- package/src/index.css +0 -60
- package/src/libs/mobx-query/client/MQClient.ts +0 -75
- package/src/libs/mobx-query/client/MQClientAccessor.ts +0 -74
- package/src/libs/mobx-query/client/index.ts +0 -8
- package/src/libs/mobx-query/entity/Entity.ts +0 -232
- package/src/libs/mobx-query/entity/EntityCollection.ts +0 -285
- package/src/libs/mobx-query/entity/constants.ts +0 -7
- package/src/libs/mobx-query/entity/index.ts +0 -15
- package/src/libs/mobx-query/index.ts +0 -6
- package/src/libs/mobx-query/mutations/BatchMutationBase.ts +0 -105
- package/src/libs/mobx-query/mutations/BatchUpdateMutation.ts +0 -48
- package/src/libs/mobx-query/mutations/CreateMutation.ts +0 -172
- package/src/libs/mobx-query/mutations/DeleteMutation.ts +0 -94
- package/src/libs/mobx-query/mutations/EntityMutationBase.ts +0 -110
- package/src/libs/mobx-query/mutations/MutationBase.ts +0 -40
- package/src/libs/mobx-query/mutations/OptimisticMutationStrategy.ts +0 -122
- package/src/libs/mobx-query/mutations/UpdateMutation.ts +0 -76
- package/src/libs/mobx-query/mutations/constants.ts +0 -16
- package/src/libs/mobx-query/mutations/index.ts +0 -44
- package/src/libs/mobx-query/mutations/types.ts +0 -205
- package/src/libs/mobx-query/queries/QueryBase.ts +0 -65
- package/src/libs/mobx-query/queries/QueryFragmentMany.ts +0 -31
- package/src/libs/mobx-query/queries/QueryFragmentOne.ts +0 -35
- package/src/libs/mobx-query/queries/QueryMany.ts +0 -80
- package/src/libs/mobx-query/queries/QueryManyBase.ts +0 -135
- package/src/libs/mobx-query/queries/QueryOne.ts +0 -84
- package/src/libs/mobx-query/queries/QueryOneBase.ts +0 -93
- package/src/libs/mobx-query/queries/index.ts +0 -33
- package/src/libs/mobx-query/queries/types.ts +0 -60
- package/src/libs/mobx-query/react/createReactContext.tsx +0 -23
- package/src/libs/mobx-query/react/index.ts +0 -3
- package/src/libs/mobx-query/utils/generateEntityId.ts +0 -12
- package/src/libs/mobx-query/utils/index.ts +0 -8
- package/src/libs/mobx-query/utils/invalidateQueryByHash.ts +0 -18
- package/src/libs/mobx-query/utils/types.ts +0 -18
- package/src/libs/react-query.ts +0 -11
- package/src/main.tsx +0 -16
- package/src/utils.ts +0 -3
- package/src/vite-env.d.ts +0 -1
- package/tsconfig.app.json +0 -27
- package/tsconfig.json +0 -7
- package/tsconfig.node.json +0 -25
- package/vite.config.ts +0 -52
- /package/{public → dist}/vite.svg +0 -0
package/src/api/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const API_URL = "https://jsonplaceholder.typicode.com";
|
package/src/api/fetch.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { API_URL } from "./constants";
|
|
2
|
-
|
|
3
|
-
export interface ApiFetchOptions {
|
|
4
|
-
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
5
|
-
params?: Map<string, string>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export async function apiFetch<R = unknown>(
|
|
9
|
-
path: string,
|
|
10
|
-
options?: ApiFetchOptions
|
|
11
|
-
) {
|
|
12
|
-
const url = new URL(API_URL);
|
|
13
|
-
|
|
14
|
-
url.pathname = path;
|
|
15
|
-
|
|
16
|
-
if (options?.params && options.params.size > 0) {
|
|
17
|
-
for (const [key, value] of options.params.entries()) {
|
|
18
|
-
url.searchParams.append(key, value);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const res = await fetch(url.toString(), {
|
|
23
|
-
method: options?.method || "GET",
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const json = await res.json();
|
|
27
|
-
|
|
28
|
-
return json as R;
|
|
29
|
-
}
|
package/src/api/todos.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { apiFetch } from "./fetch";
|
|
2
|
-
import type { TodoDTO } from "./types";
|
|
3
|
-
|
|
4
|
-
export function getTodosByUserId(userId: number) {
|
|
5
|
-
return apiFetch<TodoDTO[]>("/todos", {
|
|
6
|
-
params: new Map([["userId", userId.toString()]]),
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export async function deleteTodo(todoId: number) {
|
|
11
|
-
return apiFetch<void>(`/todos/${todoId}`, {
|
|
12
|
-
method: "DELETE",
|
|
13
|
-
});
|
|
14
|
-
}
|
package/src/api/types.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export interface UserDTO {
|
|
2
|
-
id: number;
|
|
3
|
-
name: string;
|
|
4
|
-
username: string;
|
|
5
|
-
email: string;
|
|
6
|
-
address: {
|
|
7
|
-
street: string;
|
|
8
|
-
suite: string;
|
|
9
|
-
city: string;
|
|
10
|
-
zipcode: string;
|
|
11
|
-
geo: {
|
|
12
|
-
lat: string;
|
|
13
|
-
lng: string;
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
phone: string;
|
|
17
|
-
website: string;
|
|
18
|
-
company: {
|
|
19
|
-
name: string;
|
|
20
|
-
catchPhrase: string;
|
|
21
|
-
bs: string;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface TodoDTO {
|
|
26
|
-
id: number;
|
|
27
|
-
title: string;
|
|
28
|
-
completed: boolean;
|
|
29
|
-
userId: number;
|
|
30
|
-
}
|
package/src/api/users.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { wait } from "../utils";
|
|
2
|
-
import { apiFetch } from "./fetch";
|
|
3
|
-
import type { UserDTO } from "./types";
|
|
4
|
-
|
|
5
|
-
export function getAllUsers() {
|
|
6
|
-
return apiFetch<UserDTO[]>("/users");
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function getUserById(userId: number) {
|
|
10
|
-
return apiFetch<UserDTO>(`/users/${userId}`);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export async function deleteUser(userId: number) {
|
|
14
|
-
await wait(1500);
|
|
15
|
-
|
|
16
|
-
return apiFetch<void>(`/users/${userId}`, {
|
|
17
|
-
method: "DELETE",
|
|
18
|
-
});
|
|
19
|
-
}
|
package/src/assets/react.svg
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
package/src/index.css
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
:root {
|
|
2
|
-
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
|
|
3
|
-
line-height: 1.5;
|
|
4
|
-
font-weight: 400;
|
|
5
|
-
|
|
6
|
-
color-scheme: light dark;
|
|
7
|
-
color: rgba(255, 255, 255, 0.87);
|
|
8
|
-
background-color: #242424;
|
|
9
|
-
|
|
10
|
-
font-synthesis: none;
|
|
11
|
-
text-rendering: optimizeLegibility;
|
|
12
|
-
-webkit-font-smoothing: antialiased;
|
|
13
|
-
-moz-osx-font-smoothing: grayscale;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
a {
|
|
17
|
-
font-weight: 500;
|
|
18
|
-
color: #646cff;
|
|
19
|
-
text-decoration: inherit;
|
|
20
|
-
}
|
|
21
|
-
a:hover {
|
|
22
|
-
color: #535bf2;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
h1 {
|
|
26
|
-
font-size: 3.2em;
|
|
27
|
-
line-height: 1.1;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
button {
|
|
31
|
-
border-radius: 8px;
|
|
32
|
-
border: 1px solid transparent;
|
|
33
|
-
padding: 0.6em 1.2em;
|
|
34
|
-
font-size: 1em;
|
|
35
|
-
font-weight: 500;
|
|
36
|
-
font-family: inherit;
|
|
37
|
-
background-color: #1a1a1a;
|
|
38
|
-
cursor: pointer;
|
|
39
|
-
transition: border-color 0.25s;
|
|
40
|
-
}
|
|
41
|
-
button:hover {
|
|
42
|
-
border-color: #646cff;
|
|
43
|
-
}
|
|
44
|
-
button:focus,
|
|
45
|
-
button:focus-visible {
|
|
46
|
-
outline: 4px auto -webkit-focus-ring-color;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
@media (prefers-color-scheme: light) {
|
|
50
|
-
:root {
|
|
51
|
-
color: #213547;
|
|
52
|
-
background-color: #ffffff;
|
|
53
|
-
}
|
|
54
|
-
a:hover {
|
|
55
|
-
color: #747bff;
|
|
56
|
-
}
|
|
57
|
-
button {
|
|
58
|
-
background-color: #f9f9f9;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { type EntityConstructorAny, EntityCollection } from "../entity";
|
|
2
|
-
import type { MQClientContextRegistered } from "./types";
|
|
3
|
-
import {
|
|
4
|
-
OptimisticMutationErrorStrategy,
|
|
5
|
-
OptimisticMutationInvalidationStrategy,
|
|
6
|
-
} from "../mutations";
|
|
7
|
-
|
|
8
|
-
export interface MQClientOptions {
|
|
9
|
-
invalidationStrategy?: OptimisticMutationInvalidationStrategy;
|
|
10
|
-
errorStrategy?: OptimisticMutationErrorStrategy;
|
|
11
|
-
invalidateOnError?: boolean;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface MQClientProps<T> extends MQClientOptions {
|
|
15
|
-
context: MQClientContextRegistered;
|
|
16
|
-
entities: EntityConstructorAny[];
|
|
17
|
-
rootStore: () => T;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const __MOBX_QUERY__: {
|
|
21
|
-
state: Map<string, EntityCollection>;
|
|
22
|
-
context: MQClientContextRegistered | null;
|
|
23
|
-
options: Required<MQClientOptions>;
|
|
24
|
-
} = {
|
|
25
|
-
state: new Map(),
|
|
26
|
-
context: null,
|
|
27
|
-
options: {
|
|
28
|
-
invalidationStrategy:
|
|
29
|
-
OptimisticMutationInvalidationStrategy.REFERENCED_QUERIES,
|
|
30
|
-
errorStrategy: OptimisticMutationErrorStrategy.ROLLBACK,
|
|
31
|
-
invalidateOnError: true,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export class MQClient<T> {
|
|
36
|
-
readonly rootStore: T;
|
|
37
|
-
|
|
38
|
-
constructor(props: MQClientProps<T>) {
|
|
39
|
-
__MOBX_QUERY__.context = props.context;
|
|
40
|
-
|
|
41
|
-
for (const entity of props.entities) {
|
|
42
|
-
__MOBX_QUERY__.state.set(
|
|
43
|
-
entity.name,
|
|
44
|
-
new EntityCollection(entity, props.context.queryClient),
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// if (process.env.NODE_ENV === 'development') {
|
|
49
|
-
// setupMobxDevtoolsV2()
|
|
50
|
-
// }
|
|
51
|
-
|
|
52
|
-
__MOBX_QUERY__.options.invalidationStrategy =
|
|
53
|
-
props.invalidationStrategy ??
|
|
54
|
-
OptimisticMutationInvalidationStrategy.REFERENCED_QUERIES;
|
|
55
|
-
__MOBX_QUERY__.options.errorStrategy =
|
|
56
|
-
props.errorStrategy ?? OptimisticMutationErrorStrategy.ROLLBACK;
|
|
57
|
-
__MOBX_QUERY__.options.invalidateOnError = props.invalidateOnError ?? true;
|
|
58
|
-
|
|
59
|
-
this.rootStore = props.rootStore();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
getEntityCollection<TEntityConstructor extends EntityConstructorAny>(
|
|
63
|
-
entity: TEntityConstructor,
|
|
64
|
-
) {
|
|
65
|
-
const collection = __MOBX_QUERY__.state.get(
|
|
66
|
-
entity.name,
|
|
67
|
-
) as EntityCollection<TEntityConstructor>;
|
|
68
|
-
|
|
69
|
-
if (!collection) {
|
|
70
|
-
throw new Error(`Entity collection not found for entity ${entity.name}`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return collection;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { __MOBX_QUERY__ } from "./MQClient";
|
|
2
|
-
import type {
|
|
3
|
-
DefaultError,
|
|
4
|
-
MutationOptions,
|
|
5
|
-
QueryClient,
|
|
6
|
-
} from "@tanstack/react-query";
|
|
7
|
-
import type { MQClientContextRegistered } from "./types";
|
|
8
|
-
import type { EntityCollection, EntityConstructorAny } from "../entity";
|
|
9
|
-
import type { MutationFn } from "../mutations";
|
|
10
|
-
|
|
11
|
-
export abstract class MQClientAccessor {
|
|
12
|
-
protected readonly queryClient: QueryClient;
|
|
13
|
-
protected readonly context: MQClientContextRegistered;
|
|
14
|
-
private readonly globalState: Map<string, EntityCollection>;
|
|
15
|
-
|
|
16
|
-
constructor() {
|
|
17
|
-
if (!__MOBX_QUERY__) {
|
|
18
|
-
throw new Error("Client not found");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (!__MOBX_QUERY__.context) {
|
|
22
|
-
throw new Error("MQClientContext is required");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this.queryClient = __MOBX_QUERY__.context.queryClient;
|
|
26
|
-
this.globalState = __MOBX_QUERY__.state;
|
|
27
|
-
this.context = __MOBX_QUERY__.context;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected getEntityCollection<
|
|
31
|
-
TEntityConstructor extends EntityConstructorAny = EntityConstructorAny,
|
|
32
|
-
>(
|
|
33
|
-
entityConstructor: TEntityConstructor,
|
|
34
|
-
): EntityCollection<TEntityConstructor> {
|
|
35
|
-
const collection = this.globalState.get(entityConstructor.name);
|
|
36
|
-
|
|
37
|
-
if (!collection) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
`Entity collection not found for entity ${entityConstructor.name}`,
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return collection as unknown as EntityCollection<TEntityConstructor>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
protected runSyncMutation<
|
|
47
|
-
TInput,
|
|
48
|
-
TError = DefaultError,
|
|
49
|
-
TMutateResult = unknown,
|
|
50
|
-
>(
|
|
51
|
-
input: TInput,
|
|
52
|
-
mutationFn: MutationFn<TInput>,
|
|
53
|
-
options?: MutationOptions<void, TError, TInput, TMutateResult>,
|
|
54
|
-
) {
|
|
55
|
-
this.queryClient
|
|
56
|
-
.getMutationCache()
|
|
57
|
-
.build<void, TError, TInput, TMutateResult>(this.queryClient, {
|
|
58
|
-
mutationFn: (input) => mutationFn(input, this.context),
|
|
59
|
-
onError: options?.onError,
|
|
60
|
-
onMutate: options?.onMutate,
|
|
61
|
-
onSuccess: options?.onSuccess,
|
|
62
|
-
onSettled: options?.onSettled,
|
|
63
|
-
gcTime: options?.gcTime,
|
|
64
|
-
meta: options?.meta,
|
|
65
|
-
networkMode: options?.networkMode,
|
|
66
|
-
retry: options?.retry,
|
|
67
|
-
retryDelay: options?.retryDelay,
|
|
68
|
-
scope: options?.scope,
|
|
69
|
-
mutationKey: options?.mutationKey,
|
|
70
|
-
})
|
|
71
|
-
.execute(input)
|
|
72
|
-
.catch((error) => console.log("runSyncMutation error", error));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { MQClient } from "./MQClient";
|
|
2
|
-
import { MQClientAccessor } from "./MQClientAccessor";
|
|
3
|
-
|
|
4
|
-
import type { MQClientContext, MQClientContextRegistered } from "./types";
|
|
5
|
-
|
|
6
|
-
export { MQClient, MQClientAccessor };
|
|
7
|
-
|
|
8
|
-
export type { MQClientContext, MQClientContextRegistered };
|
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
action,
|
|
3
|
-
type IObjectDidChange,
|
|
4
|
-
isObservableArray,
|
|
5
|
-
isObservableMap,
|
|
6
|
-
observable,
|
|
7
|
-
observe,
|
|
8
|
-
toJS,
|
|
9
|
-
} from "mobx";
|
|
10
|
-
import type { EntityData, EntityEvents, EntityId } from "./types";
|
|
11
|
-
import { MQClientAccessor } from "../client";
|
|
12
|
-
import { invalidateQueryByHash } from "../utils";
|
|
13
|
-
import { EntityState } from "./constants";
|
|
14
|
-
|
|
15
|
-
export type EntityConstructor<
|
|
16
|
-
TData = unknown,
|
|
17
|
-
TEntityId extends EntityId = string,
|
|
18
|
-
E extends Entity<TData, TEntityId> = Entity<TData, TEntityId>,
|
|
19
|
-
> = new () => E;
|
|
20
|
-
|
|
21
|
-
export type EntityConstructorAny = EntityConstructor<
|
|
22
|
-
EntityData,
|
|
23
|
-
any,
|
|
24
|
-
EntityAny
|
|
25
|
-
>;
|
|
26
|
-
|
|
27
|
-
export type EntityAny = Entity<any, any>;
|
|
28
|
-
|
|
29
|
-
const IGNORE_FIELDS: PropertyKey[] = ["isDirty", "state"] as const;
|
|
30
|
-
|
|
31
|
-
export type EntityValueKeys<T> = {
|
|
32
|
-
[K in keyof T]: T[K] extends Function
|
|
33
|
-
? never
|
|
34
|
-
: K extends "isDirty" | "state" | "queryHashes"
|
|
35
|
-
? never
|
|
36
|
-
: K;
|
|
37
|
-
}[keyof T] &
|
|
38
|
-
string;
|
|
39
|
-
|
|
40
|
-
export abstract class Entity<
|
|
41
|
-
TData = unknown,
|
|
42
|
-
TEntityId extends EntityId = string,
|
|
43
|
-
> extends MQClientAccessor {
|
|
44
|
-
abstract id: TEntityId;
|
|
45
|
-
|
|
46
|
-
abstract hydrate(data: TData): void;
|
|
47
|
-
|
|
48
|
-
readonly queryHashes = new Set<string>();
|
|
49
|
-
|
|
50
|
-
private events!: EntityEvents<TEntityId>;
|
|
51
|
-
|
|
52
|
-
@observable accessor state: EntityState = EntityState.CONFIRMED;
|
|
53
|
-
@observable accessor isDirty = false;
|
|
54
|
-
|
|
55
|
-
private readonly initValuesSnapshot: Map<string | number | symbol, unknown> =
|
|
56
|
-
new Map();
|
|
57
|
-
|
|
58
|
-
private isHydrated = false;
|
|
59
|
-
|
|
60
|
-
_init(queryHashes: string[], events: EntityEvents<TEntityId>) {
|
|
61
|
-
this.events = events;
|
|
62
|
-
|
|
63
|
-
for (const hash of queryHashes) {
|
|
64
|
-
this.queryHashes.add(hash);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
observe(this, (change) => this.onObservableChange(change));
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// @computed get isDirty() {
|
|
71
|
-
// return this._isDirty
|
|
72
|
-
// }
|
|
73
|
-
|
|
74
|
-
_removeQueryHashes(hashes: string[]) {
|
|
75
|
-
for (const hash of hashes) {
|
|
76
|
-
this.queryHashes.delete(hash);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (this.queryHashes.size === 0) {
|
|
80
|
-
this.events.onAllQueryHashesRemoved(this.id);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
_markAsHydrated() {
|
|
85
|
-
this.isHydrated = true;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
@action private onObservableChange(change: IObjectDidChange) {
|
|
89
|
-
if (change.type !== "update") {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (!this.isHydrated) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (IGNORE_FIELDS.includes(change.name as any)) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Store the initial value (deep cloned for complex data structures)
|
|
102
|
-
if (!this.initValuesSnapshot.has(change.name)) {
|
|
103
|
-
// Deep clone complex data structures (objects, arrays, nested observables)
|
|
104
|
-
// toJS converts observables to plain JS, effectively creating a deep clone
|
|
105
|
-
this.initValuesSnapshot.set(
|
|
106
|
-
change.name,
|
|
107
|
-
this.deepCloneValue(change.oldValue),
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (this.isDirty === false) {
|
|
112
|
-
this.isDirty = true;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Deep clones a value to ensure we store a snapshot, not a reference.
|
|
118
|
-
* Handles primitives, objects, arrays, and MobX observables.
|
|
119
|
-
*/
|
|
120
|
-
private deepCloneValue(value: unknown): unknown {
|
|
121
|
-
// Handle null and undefined
|
|
122
|
-
if (value === null || value === undefined) {
|
|
123
|
-
return value;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Handle primitives (string, number, boolean, symbol, bigint)
|
|
127
|
-
const valueType = typeof value;
|
|
128
|
-
if (
|
|
129
|
-
valueType === "string" ||
|
|
130
|
-
valueType === "number" ||
|
|
131
|
-
valueType === "boolean" ||
|
|
132
|
-
valueType === "symbol" ||
|
|
133
|
-
valueType === "bigint"
|
|
134
|
-
) {
|
|
135
|
-
return value;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Handle functions - store as-is (though they shouldn't typically be observable)
|
|
139
|
-
if (valueType === "function") {
|
|
140
|
-
return value;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
if (value instanceof Date) {
|
|
144
|
-
return new Date(value.getTime());
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (value instanceof RegExp) {
|
|
148
|
-
return new RegExp(value);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (value instanceof Set) {
|
|
152
|
-
return new Set(
|
|
153
|
-
Array.from(value).map((item) => this.deepCloneValue(item)),
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (value instanceof Map) {
|
|
158
|
-
const clonedMap = new Map();
|
|
159
|
-
for (const [key, val] of value.entries()) {
|
|
160
|
-
clonedMap.set(this.deepCloneValue(key), this.deepCloneValue(val));
|
|
161
|
-
}
|
|
162
|
-
return clonedMap;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// For objects and arrays (including MobX observables), use toJS to convert
|
|
166
|
-
// observables to plain JS and create a deep clone
|
|
167
|
-
try {
|
|
168
|
-
return toJS(value);
|
|
169
|
-
} catch (error) {
|
|
170
|
-
// Fallback: if toJS fails (e.g., circular reference), return as-is
|
|
171
|
-
// This is a safety measure, though it may not fully solve the problem
|
|
172
|
-
console.warn(
|
|
173
|
-
`Failed to deep clone value for property, using reference:`,
|
|
174
|
-
error,
|
|
175
|
-
);
|
|
176
|
-
return value;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
@action reset() {
|
|
181
|
-
for (const [key, value] of this.initValuesSnapshot.entries()) {
|
|
182
|
-
// Deep clone the stored value before restoring to ensure we're not
|
|
183
|
-
// assigning a reference that might have been mutated
|
|
184
|
-
const clonedValue = this.deepCloneValue(value);
|
|
185
|
-
|
|
186
|
-
// @ts-expect-error: unknown field
|
|
187
|
-
const currentValue = this[key];
|
|
188
|
-
|
|
189
|
-
// Handle observable arrays and maps specially, similar to createViewModel
|
|
190
|
-
// This ensures proper restoration of MobX observable collections
|
|
191
|
-
if (isObservableArray(currentValue)) {
|
|
192
|
-
// For observable arrays, use replace() to restore the original array
|
|
193
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
194
|
-
currentValue.replace(clonedValue as any);
|
|
195
|
-
} else if (isObservableMap(currentValue)) {
|
|
196
|
-
// For observable maps, clear and merge to restore the original map
|
|
197
|
-
currentValue.clear();
|
|
198
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
199
|
-
currentValue.merge(clonedValue as any);
|
|
200
|
-
} else {
|
|
201
|
-
// For other types, assign directly
|
|
202
|
-
// @ts-expect-error: unknown field
|
|
203
|
-
this[key] = clonedValue;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this._clearDirty();
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
@action _clearDirty() {
|
|
211
|
-
this.isDirty = false;
|
|
212
|
-
this.initValuesSnapshot.clear();
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// isDirtyField(field: EntityValueKeys<this>) {
|
|
216
|
-
// return this.initValuesSnapshot.has(field)
|
|
217
|
-
// }
|
|
218
|
-
|
|
219
|
-
// getOriginalValue<K extends EntityValueKeys<this>>(
|
|
220
|
-
// field: K,
|
|
221
|
-
// ): this[K] | undefined {
|
|
222
|
-
// return this.initValuesSnapshot.get(field) as this[K] | undefined
|
|
223
|
-
// }
|
|
224
|
-
|
|
225
|
-
invalidateRelatedQueries() {
|
|
226
|
-
const cache = this.queryClient.getQueryCache();
|
|
227
|
-
|
|
228
|
-
for (const hash of this.queryHashes) {
|
|
229
|
-
invalidateQueryByHash(hash, cache, () => this._removeQueryHashes([hash]));
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|