-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (136 loc) · 5.03 KB
/
index.js
File metadata and controls
159 lines (136 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { VueReactivity } from "./integrations";
import TanStackStateManager from "./TanStackStateManager";
export class QueryManager {
constructor(config, Reactivity = VueReactivity) {
this.reactivity = new Reactivity();
this.stateManager = new TanStackStateManager(this.reactivity);
this.invalidateQueries = this.stateManager.invalidateQuery;
this.operations = new Map();
return this.#initializeFromConfig(config);
}
#initializeFromConfig(config) {
const instance = {};
const getExactQueryKey = (key) => this.stateManager.queryStates[key]?.queryKey;
Object.entries(config).forEach(([key, definition]) => {
if (definition.type === "query") {
instance[key] = this.#createQueryInterface(key, definition, getExactQueryKey);
} else if (definition.type === "mutation") {
instance[key] = this.#createMutationInterface(key, definition, getExactQueryKey);
}
});
return instance;
}
#createQueryInterface(key, definition, getExactQueryKey) {
return this.reactivity.create({
...this.stateManager.defineQuery(this.#define(definition, getExactQueryKey)),
fetch: (payload, options = {}) =>
this.#promisifyQuery({ key, definition, payload, options, getExactQueryKey }),
fetchAsync: (payload, options = {}) =>
this.#promisifyQuery({
key,
definition,
payload,
options: { ...options, throwError: true },
getExactQueryKey,
}),
subscribe: (fn) => this.stateManager.registerGlobalSubscriber(key, fn),
});
}
#createMutationInterface(key, definition, getExactQueryKey) {
return this.reactivity.create({
...this.stateManager.defineMutation(this.#define(definition, getExactQueryKey)),
mutate: (payload, options = {}) =>
this.#promisifyMutation({
key,
definition,
payload,
options,
getExactQueryKey,
}),
mutateAsync: (payload, options = {}) =>
this.#promisifyMutation({
key,
definition,
payload,
options: { ...options, throwError: true },
getExactQueryKey,
}),
subscribe: (fn) => this.stateManager.registerGlobalSubscriber(key, fn),
});
}
#promisifyQuery({ key, definition, payload, options = {}, getExactQueryKey }) {
const { refresh = false } = options;
if (refresh && getExactQueryKey(key)) this.stateManager.queryClient.resetQueries(getExactQueryKey(key));
const query = this.stateManager.defineQuery(definition.define(payload));
if (this.operations.has(query.hash)) {
return this.operations.get(query.hash);
}
const operation = this.#promisifyOperation(() => {
if (query.state.data && !query.state.isStale && !query.state.isFetching) {
return { immediate: true, data: query.state.data };
}
return {
type: "query",
operation: query,
throwError: options.throwError ?? false,
trigger: () => query.fetch(),
};
});
this.operations.set(query.hash, operation);
return operation;
}
#promisifyMutation({ definition, payload, options, getExactQueryKey }) {
const mutation = this.stateManager.defineMutation(this.#define(definition, getExactQueryKey));
if (this.operations.has(mutation.hash)) {
return this.operations.get(mutation.hash);
}
const operation = this.#promisifyOperation(() => {
return {
type: "mutation",
operation: mutation,
throwError: options.throwError ?? false,
trigger: () => mutation.mutate(payload),
};
});
this.operations.set(mutation.hash, operation);
return operation;
}
#promisifyOperation(operationFactory) {
return new Promise((resolve, reject) => {
const result = operationFactory();
if (result.immediate) return resolve(result.data);
const { operation, trigger, type, throwError } = result;
const resolveOperation = (state) => {
unsubscribe();
this.operations.delete(operation.hash);
if (throwError) {
if (state.status === "success") resolve(state.data);
else reject(state.error);
} else {
resolve(state.data ?? state.error);
}
};
const isQuery = type === "query";
const isMutation = type === "mutation";
const unsubscribe = operation.subscribe((state) => {
const isReadyQuery = isQuery && !state.isFetching && !state.isLoading;
const isReadyMutation = isMutation && !state.isPending;
if (isReadyQuery || isReadyMutation) resolveOperation(state);
});
trigger();
});
}
#define(definition, getExactQueryKey) {
const { options, ...rest } = definition.define();
return {
...rest,
options: this.#generateOptions(options, getExactQueryKey),
};
}
#generateOptions(options, getExactQueryKey) {
return typeof options === "function" ? options(this.stateManager.queryClient, getExactQueryKey) : options;
}
getExactQuery(key) {
return this.stateManager.queries[key]?.queryHash;
}
}