zova-module-a-model 5.0.9 → 5.0.11
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zova-module-a-model",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.11",
|
|
4
4
|
"title": "a-model",
|
|
5
5
|
"zovaModule": {
|
|
6
6
|
"capabilities": {
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"js-cookie": "^3.0.5",
|
|
64
64
|
"localforage": "^1.10.0"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "d7ecefeb8b5f566e4b4831d7b3a02e9e7a765c15"
|
|
67
67
|
}
|
|
@@ -70,6 +70,24 @@ export class BeanModelPersister<TScopeModule = unknown> extends BeanModelLast<TS
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
$persisterRemove(queryKey: QueryKey) {
|
|
74
|
+
const query = this.self.$queryFind({ queryKey });
|
|
75
|
+
if (!query) return;
|
|
76
|
+
const options = this._adjustPersisterOptions(query.meta?.persister);
|
|
77
|
+
if (!options) return;
|
|
78
|
+
const storage = this._getPersisterStorage(options);
|
|
79
|
+
if (!storage) return;
|
|
80
|
+
const storageKey = this._getPersisterStorageKey(options, query);
|
|
81
|
+
if (options.sync === true) {
|
|
82
|
+
storage.removeItem(storageKey);
|
|
83
|
+
} else {
|
|
84
|
+
// Persist if we have storage defined, we use timeout to get proper state to be persisted
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
storage.removeItem(storageKey);
|
|
87
|
+
}, 0);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
protected _createPersister(options?: QueryMetaPersister | boolean) {
|
|
74
92
|
options = this._adjustPersisterOptions(options);
|
|
75
93
|
if (!options) return undefined;
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
SetDataOptions,
|
|
10
10
|
Updater,
|
|
11
11
|
} from '@tanstack/vue-query';
|
|
12
|
+
import localforage from 'localforage';
|
|
12
13
|
import { MaybeRefDeep, NoUnknown } from '../../common/types.js';
|
|
13
14
|
import { Cast } from 'zova';
|
|
14
15
|
import { BeanModelCookie } from './bean.model.cookie.js';
|
|
@@ -33,8 +34,15 @@ export class BeanModelQuery<TScopeModule = unknown> extends BeanModelCookie<TSco
|
|
|
33
34
|
$setQueryData(queryKey, updater, persisterSave, options) {
|
|
34
35
|
queryKey = this._forceQueryKeyPrefix(queryKey);
|
|
35
36
|
const data = this.$queryClient.setQueryData(queryKey, updater, options);
|
|
36
|
-
if (
|
|
37
|
-
|
|
37
|
+
if (data === undefined) {
|
|
38
|
+
if (persisterSave) {
|
|
39
|
+
this.$persisterRemove(queryKey);
|
|
40
|
+
}
|
|
41
|
+
this.$setQueryDataDirect(queryKey, data);
|
|
42
|
+
} else {
|
|
43
|
+
if (persisterSave) {
|
|
44
|
+
this.$persisterSave(queryKey);
|
|
45
|
+
}
|
|
38
46
|
}
|
|
39
47
|
return data;
|
|
40
48
|
}
|
|
@@ -56,4 +64,18 @@ export class BeanModelQuery<TScopeModule = unknown> extends BeanModelCookie<TSco
|
|
|
56
64
|
filters = { ...filters, queryKey };
|
|
57
65
|
return this.$queryClient.invalidateQueries(filters, options);
|
|
58
66
|
}
|
|
67
|
+
|
|
68
|
+
$setQueryDataDirect(queryKey: QueryKey, value: any) {
|
|
69
|
+
const query = this.$queryFind({ queryKey, exact: true });
|
|
70
|
+
query?.setData(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async $clear() {
|
|
74
|
+
const queries = this.$queryClient.getQueryCache().getAll();
|
|
75
|
+
for (const query of queries) {
|
|
76
|
+
query?.setData(undefined);
|
|
77
|
+
}
|
|
78
|
+
// remove all db cache
|
|
79
|
+
await localforage.clear();
|
|
80
|
+
}
|
|
59
81
|
}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DefaultError,
|
|
3
|
+
MutationKey,
|
|
3
4
|
MutationObserverOptions,
|
|
4
5
|
QueryClient,
|
|
5
6
|
UseMutationReturnType,
|
|
7
|
+
hashKey,
|
|
6
8
|
useMutation,
|
|
7
9
|
} from '@tanstack/vue-query';
|
|
8
|
-
import { BeanModelUseQuery } from './bean.model.useQuery.js';
|
|
9
10
|
import { MaybeRefDeep } from '../../common/types.js';
|
|
10
11
|
import { UnwrapNestedRefs } from 'vue';
|
|
12
|
+
import { Cast } from 'zova';
|
|
13
|
+
import { BeanModelUseQuery } from './bean.model.useQuery.js';
|
|
14
|
+
|
|
15
|
+
const SymbolUseMutations = Symbol('SymbolUseMutations');
|
|
11
16
|
|
|
12
17
|
export class BeanModelUseMutation<TScopeModule = unknown> extends BeanModelUseQuery<TScopeModule> {
|
|
18
|
+
private [SymbolUseMutations]: Record<string, unknown> = {};
|
|
19
|
+
|
|
13
20
|
$useMutation<TData = unknown, TVariables = void, TContext = unknown>(
|
|
14
21
|
mutationOptions: MaybeRefDeep<MutationObserverOptions<TData, DefaultError, TVariables, TContext>>,
|
|
15
22
|
queryClient?: QueryClient,
|
|
@@ -18,4 +25,21 @@ export class BeanModelUseMutation<TScopeModule = unknown> extends BeanModelUseQu
|
|
|
18
25
|
return useMutation(mutationOptions, queryClient) as any;
|
|
19
26
|
});
|
|
20
27
|
}
|
|
28
|
+
|
|
29
|
+
$useMutationExisting<TData = unknown, TVariables = void, TContext = unknown>(
|
|
30
|
+
mutationOptions: MaybeRefDeep<MutationObserverOptions<TData, DefaultError, TVariables, TContext>>,
|
|
31
|
+
queryClient?: QueryClient,
|
|
32
|
+
): UnwrapNestedRefs<UseMutationReturnType<TData, DefaultError, TVariables, TContext>> {
|
|
33
|
+
let mutationKey: MutationKey = Cast(mutationOptions).mutationKey;
|
|
34
|
+
if (!mutationKey || mutationKey.length === 0) throw new Error('should specify mutationKey');
|
|
35
|
+
mutationKey = this.self._forceQueryKeyPrefix(mutationKey);
|
|
36
|
+
const mutationHash = hashKey(mutationKey);
|
|
37
|
+
if (!this[SymbolUseMutations][mutationHash]) {
|
|
38
|
+
mutationOptions = { ...mutationOptions, mutationKey };
|
|
39
|
+
this[SymbolUseMutations][mutationHash] = this.$useMutation(mutationOptions, queryClient);
|
|
40
|
+
}
|
|
41
|
+
return this[SymbolUseMutations][mutationHash] as UnwrapNestedRefs<
|
|
42
|
+
UseMutationReturnType<TData, DefaultError, TVariables, TContext>
|
|
43
|
+
>;
|
|
44
|
+
}
|
|
21
45
|
}
|
|
@@ -17,7 +17,7 @@ import { BeanModelQuery } from './bean.model.query.js';
|
|
|
17
17
|
const SymbolUseQueries = Symbol('SymbolUseQueries');
|
|
18
18
|
|
|
19
19
|
export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TScopeModule> {
|
|
20
|
-
[SymbolUseQueries]: Record<string, unknown> = {};
|
|
20
|
+
private [SymbolUseQueries]: Record<string, unknown> = {};
|
|
21
21
|
|
|
22
22
|
$useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(
|
|
23
23
|
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
@@ -40,42 +40,6 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
$useQueryExisting<
|
|
44
|
-
TQueryFnData = unknown,
|
|
45
|
-
TError = DefaultError,
|
|
46
|
-
TData = TQueryFnData,
|
|
47
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
48
|
-
>(
|
|
49
|
-
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
50
|
-
queryClient?: QueryClient,
|
|
51
|
-
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
52
|
-
$useQueryExisting<
|
|
53
|
-
TQueryFnData = unknown,
|
|
54
|
-
TError = DefaultError,
|
|
55
|
-
TData = TQueryFnData,
|
|
56
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
57
|
-
>(
|
|
58
|
-
options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
59
|
-
queryClient?: QueryClient,
|
|
60
|
-
): UnwrapNestedRefs<UseQueryDefinedReturnType<TData, TError>>;
|
|
61
|
-
$useQueryExisting<
|
|
62
|
-
TQueryFnData = unknown,
|
|
63
|
-
TError = DefaultError,
|
|
64
|
-
TData = TQueryFnData,
|
|
65
|
-
TQueryKey extends QueryKey = QueryKey,
|
|
66
|
-
>(
|
|
67
|
-
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
68
|
-
queryClient?: QueryClient,
|
|
69
|
-
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
70
|
-
$useQueryExisting(options, queryClient) {
|
|
71
|
-
const queryKey = this.self._forceQueryKeyPrefix(options.queryKey);
|
|
72
|
-
const queryHash = hashKey(queryKey);
|
|
73
|
-
if (!this[SymbolUseQueries][queryHash]) {
|
|
74
|
-
this[SymbolUseQueries][queryHash] = this.$useQuery(options, queryClient);
|
|
75
|
-
}
|
|
76
|
-
return this[SymbolUseQueries][queryHash];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
43
|
$useQueryLocal<
|
|
80
44
|
TQueryFnData = unknown,
|
|
81
45
|
TError = DefaultError,
|
|
@@ -102,21 +66,13 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
102
66
|
persister: { storage: 'local', sync: true },
|
|
103
67
|
},
|
|
104
68
|
});
|
|
105
|
-
const queryKey = options.queryKey;
|
|
106
69
|
const self = this;
|
|
107
70
|
return useComputed({
|
|
108
71
|
get() {
|
|
109
|
-
|
|
110
|
-
if (query.data.value === undefined) {
|
|
111
|
-
const data = self.$persisterLoad(queryKey);
|
|
112
|
-
if (data !== undefined) {
|
|
113
|
-
self.$setQueryData(queryKey, data);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
return query.data;
|
|
72
|
+
return self._handleSyncDataGet(options, queryClient, true);
|
|
117
73
|
},
|
|
118
74
|
set(value) {
|
|
119
|
-
self
|
|
75
|
+
self._handleSyncDataSet(options, queryClient, true, value);
|
|
120
76
|
},
|
|
121
77
|
});
|
|
122
78
|
}
|
|
@@ -162,21 +118,13 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
162
118
|
},
|
|
163
119
|
},
|
|
164
120
|
);
|
|
165
|
-
const queryKey = options.queryKey;
|
|
166
121
|
const self = this;
|
|
167
122
|
return useComputed({
|
|
168
123
|
get() {
|
|
169
|
-
|
|
170
|
-
if (query.data.value === undefined) {
|
|
171
|
-
const data = self.$persisterLoad(queryKey);
|
|
172
|
-
if (data !== undefined) {
|
|
173
|
-
self.$setQueryData(queryKey, data);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return query.data;
|
|
124
|
+
return self._handleSyncDataGet(options, queryClient, true);
|
|
177
125
|
},
|
|
178
126
|
set(value) {
|
|
179
|
-
self
|
|
127
|
+
self._handleSyncDataSet(options, queryClient, true, value);
|
|
180
128
|
},
|
|
181
129
|
});
|
|
182
130
|
}
|
|
@@ -207,16 +155,81 @@ export class BeanModelUseQuery<TScopeModule = unknown> extends BeanModelQuery<TS
|
|
|
207
155
|
persister: false,
|
|
208
156
|
},
|
|
209
157
|
});
|
|
210
|
-
const queryKey = options.queryKey;
|
|
211
158
|
const self = this;
|
|
212
159
|
return useComputed({
|
|
213
160
|
get() {
|
|
214
|
-
|
|
215
|
-
return query.data;
|
|
161
|
+
return self._handleSyncDataGet(options, queryClient, false);
|
|
216
162
|
},
|
|
217
163
|
set(value) {
|
|
218
|
-
self
|
|
164
|
+
self._handleSyncDataSet(options, queryClient, false, value);
|
|
219
165
|
},
|
|
220
166
|
});
|
|
221
167
|
}
|
|
168
|
+
|
|
169
|
+
$useQueryExisting<
|
|
170
|
+
TQueryFnData = unknown,
|
|
171
|
+
TError = DefaultError,
|
|
172
|
+
TData = TQueryFnData,
|
|
173
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
174
|
+
>(
|
|
175
|
+
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
176
|
+
queryClient?: QueryClient,
|
|
177
|
+
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
178
|
+
$useQueryExisting<
|
|
179
|
+
TQueryFnData = unknown,
|
|
180
|
+
TError = DefaultError,
|
|
181
|
+
TData = TQueryFnData,
|
|
182
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
183
|
+
>(
|
|
184
|
+
options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
185
|
+
queryClient?: QueryClient,
|
|
186
|
+
): UnwrapNestedRefs<UseQueryDefinedReturnType<TData, TError>>;
|
|
187
|
+
$useQueryExisting<
|
|
188
|
+
TQueryFnData = unknown,
|
|
189
|
+
TError = DefaultError,
|
|
190
|
+
TData = TQueryFnData,
|
|
191
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
192
|
+
>(
|
|
193
|
+
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,
|
|
194
|
+
queryClient?: QueryClient,
|
|
195
|
+
): UnwrapNestedRefs<UseQueryReturnType<TData, TError>>;
|
|
196
|
+
$useQueryExisting(options, queryClient) {
|
|
197
|
+
const queryKey = this.self._forceQueryKeyPrefix(options.queryKey);
|
|
198
|
+
const queryHash = hashKey(queryKey);
|
|
199
|
+
if (!this[SymbolUseQueries][queryHash]) {
|
|
200
|
+
this[SymbolUseQueries][queryHash] = this.$useQuery(options, queryClient);
|
|
201
|
+
}
|
|
202
|
+
return this[SymbolUseQueries][queryHash];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private _handleSyncDataGet(options, queryClient, persister) {
|
|
206
|
+
const queryKey = options.queryKey;
|
|
207
|
+
const query = this.$useQueryExisting(options, queryClient);
|
|
208
|
+
if (query.data === undefined) {
|
|
209
|
+
if (persister) {
|
|
210
|
+
const data = this.$persisterLoad(queryKey);
|
|
211
|
+
if (data !== undefined) {
|
|
212
|
+
this.$setQueryData(queryKey, data, false);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (query.data === undefined) {
|
|
216
|
+
let defaultData = options.meta?.defaultData;
|
|
217
|
+
if (typeof defaultData === 'function') {
|
|
218
|
+
defaultData = defaultData();
|
|
219
|
+
}
|
|
220
|
+
if (defaultData !== undefined) {
|
|
221
|
+
// need not persister save
|
|
222
|
+
this.$setQueryData(queryKey, defaultData, false);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return query.data;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private _handleSyncDataSet(options, queryClient, persister, value) {
|
|
230
|
+
const queryKey = options.queryKey;
|
|
231
|
+
const query = this.$useQueryExisting(options, queryClient);
|
|
232
|
+
this.$setQueryData(queryKey, value, persister);
|
|
233
|
+
return query;
|
|
234
|
+
}
|
|
222
235
|
}
|