zova-module-a-model 5.0.37 → 5.1.1

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.
@@ -0,0 +1,9 @@
1
+ import type { IDecoratorModelOptions } from 'zova-module-a-model';
2
+ import { BeanModelBase, Model } from 'zova-module-a-model';
3
+
4
+ export interface IModelOptions<%=argv.beanNameCapitalize%> extends IDecoratorModelOptions {}
5
+
6
+ @Model<IModelOptions<%=argv.beanNameCapitalize%>>()
7
+ export class Model<%=argv.beanNameCapitalize%> extends BeanModelBase {
8
+ protected async __init__() {}
9
+ }
package/dist/index.js ADDED
@@ -0,0 +1,684 @@
1
+ import { BeanBase, BeanInfo, BeanScopeBase, BeanSimple, SymbolBeanFullName, Virtual, cast, createBeanDecorator, deepExtend, useCustomRef } from "zova";
2
+ import { Bean, Scope, Service } from "zova-module-a-bean";
3
+ import { QueryCache, QueryClient, VueQueryPlugin, defaultShouldDehydrateQuery, dehydrate, hashKey, hydrate, useMutation, useQuery, useQueryClient } from "@tanstack/vue-query";
4
+ import localforage from "localforage";
5
+ import { experimental_createQueryPersister } from "@tanstack/query-persist-client-core";
6
+ import { markRaw } from "vue";
7
+ //#region src/types/query.ts
8
+ function resolveStaleTime(staleTime, query) {
9
+ return typeof staleTime === "function" ? staleTime(query) : staleTime;
10
+ }
11
+ function resolveMaxAgeTime(maxAge, query) {
12
+ return typeof maxAge === "function" ? maxAge(query) : maxAge;
13
+ }
14
+ //#endregion
15
+ //#region src/.metadata/this.ts
16
+ var __ThisModule__ = "a-model";
17
+ //#endregion
18
+ //#region src/common/cookieWrapper.ts
19
+ var CookieWrapper = class extends BeanSimple {
20
+ constructor(...args) {
21
+ super(...args);
22
+ this.options = void 0;
23
+ this.query = void 0;
24
+ }
25
+ __init__(options, query) {
26
+ this.options = options;
27
+ this.query = query;
28
+ }
29
+ getItem(key) {
30
+ return this.app.meta.cookie.getItem(key);
31
+ }
32
+ setItem(key, value) {
33
+ const opts = { ...this.bean.scope(__ThisModule__).config.persister.cookie.options };
34
+ let maxAge = resolveMaxAgeTime(this.options.maxAge, this.query);
35
+ if (maxAge !== void 0) {
36
+ if (maxAge === Infinity) maxAge = 1e3 * 60 * 60 * 24 * 365;
37
+ opts.expires = new Date(Date.now() + maxAge);
38
+ }
39
+ this.app.meta.cookie.setItem(key, value, opts);
40
+ }
41
+ removeItem(key) {
42
+ this.app.meta.cookie.removeItem(key);
43
+ }
44
+ };
45
+ //#endregion
46
+ //#region src/bean/bean.model/bean.model.last.ts
47
+ var BeanModelLast = class extends BeanBase {
48
+ constructor(selector) {
49
+ super();
50
+ this.selector = void 0;
51
+ if (this.$onionOptions?.enableSelector) this.selector = selector ?? "";
52
+ }
53
+ async __init__(_selector) {}
54
+ get $onionOptions() {
55
+ return super.$onionOptions;
56
+ }
57
+ get self() {
58
+ return cast(this);
59
+ }
60
+ get scopeSelf() {
61
+ return this.bean.scope(__ThisModule__);
62
+ }
63
+ };
64
+ //#endregion
65
+ //#region src/bean/bean.model/bean.model.persister.ts
66
+ var BeanModelPersister = class extends BeanModelLast {
67
+ _persisterLoad_inner(storage, storageKey, storedData, query, options) {
68
+ if (!storedData) return void 0;
69
+ const persistedQuery = options.deserialize ? options.deserialize(storedData, options.deserializeDefault) : options.deserializeDefault(storedData);
70
+ if (persistedQuery.state.dataUpdatedAt) {
71
+ const expired = Date.now() - persistedQuery.state.dataUpdatedAt > (resolveMaxAgeTime(options.maxAge, query) ?? Infinity);
72
+ const busted = persistedQuery.buster !== options.buster;
73
+ if (expired || busted) storage.removeItem(storageKey);
74
+ else {
75
+ query.setState({
76
+ dataUpdatedAt: persistedQuery.state.dataUpdatedAt,
77
+ errorUpdatedAt: persistedQuery.state.errorUpdatedAt
78
+ });
79
+ return persistedQuery.state.data;
80
+ }
81
+ } else storage.removeItem(storageKey);
82
+ }
83
+ $persisterLoad(queryKey) {
84
+ const query = this.self.$queryFind({ queryKey });
85
+ if (!query) return void 0;
86
+ const options = this._adjustPersisterOptions(query.meta?.persister);
87
+ if (!options) return void 0;
88
+ const storage = this._getPersisterStorage(options, query);
89
+ if (!storage) return void 0;
90
+ const storageKey = this._getPersisterStorageKey(options, query);
91
+ try {
92
+ const storedData = storage.getItem(storageKey);
93
+ if (options.sync) return this._persisterLoad_inner(storage, storageKey, storedData, query, options);
94
+ else return storedData.then((storedData) => {
95
+ return this._persisterLoad_inner(storage, storageKey, storedData, query, options);
96
+ });
97
+ } catch (err) {
98
+ if (process.env.DEV) {
99
+ console.error(err);
100
+ console.warn("Encountered an error attempting to restore query cache from persisted location.");
101
+ }
102
+ storage.removeItem(storageKey);
103
+ }
104
+ }
105
+ $persisterSave(queryKey) {
106
+ const query = this.self.$queryFind({ queryKey });
107
+ if (!query) return;
108
+ const options = this._adjustPersisterOptions(query.meta?.persister);
109
+ if (!options) return;
110
+ const storage = this._getPersisterStorage(options, query);
111
+ if (!storage) return;
112
+ const storageKey = this._getPersisterStorageKey(options, query);
113
+ const params = {
114
+ state: query.state,
115
+ queryKey: query.queryKey,
116
+ queryHash: query.queryHash,
117
+ buster: options.buster
118
+ };
119
+ const data = options.serialize ? options.serialize(params, options.serializeDefault) : options.serializeDefault(params);
120
+ if (options.sync === true) storage.setItem(storageKey, data);
121
+ else setTimeout(() => {
122
+ storage.setItem(storageKey, data);
123
+ }, 0);
124
+ }
125
+ $persisterRemove(queryKey) {
126
+ const query = this.self.$queryFind({ queryKey });
127
+ if (!query) return;
128
+ const options = this._adjustPersisterOptions(query.meta?.persister);
129
+ if (!options) return;
130
+ const storage = this._getPersisterStorage(options, query);
131
+ if (!storage) return;
132
+ const storageKey = this._getPersisterStorageKey(options, query);
133
+ if (options.sync === true) storage.removeItem(storageKey);
134
+ else setTimeout(() => {
135
+ storage.removeItem(storageKey);
136
+ }, 0);
137
+ }
138
+ _createPersister(options) {
139
+ options = this._adjustPersisterOptions(options);
140
+ if (!options) return void 0;
141
+ return experimental_createQueryPersister({
142
+ storage: this._getPersisterStorage(options),
143
+ maxAge: options.maxAge,
144
+ refetchOnRestore: options.refetchOnRestore,
145
+ prefix: options.prefix,
146
+ buster: options.buster
147
+ }).persisterFn;
148
+ }
149
+ _adjustPersisterOptions(options) {
150
+ if (options === false) return void 0;
151
+ if (options === void 0 || options === true) options = {};
152
+ else options = { ...options };
153
+ options.storage = options.storage ?? (options.sync ? "local" : "db");
154
+ options.maxAge = options.maxAge ?? this.scopeSelf.config.persister.maxAge[options.storage];
155
+ options.refetchOnRestore = options.refetchOnRestore ?? this.scopeSelf.config.persister.refetchOnRestore;
156
+ options.prefix = options.prefix ?? this._getPersisterPrefix();
157
+ options.buster = options.buster ?? this._getPersisterBuster();
158
+ options.serializeDefault = options.serializeDefault ?? JSON.stringify;
159
+ options.deserializeDefault = options.deserializeDefault ?? JSON.parse;
160
+ return options;
161
+ }
162
+ _getPersisterStorageKey(options, query) {
163
+ if (options.storageKeySimplify) return String(query.queryKey[query.queryKey.length - 1]);
164
+ return `${options.prefix}-${query.queryHash}`;
165
+ }
166
+ _getPersisterStorage(options, query) {
167
+ options = this._adjustPersisterOptions(options);
168
+ if (!options) return void 0;
169
+ if (options.storage === "cookie") return this.bean._newBeanSimple(CookieWrapper, false, options, query);
170
+ if (process.env.SERVER) return void 0;
171
+ if (options.storage === "local") return localStorage;
172
+ if (options.storage === "db") return localforage;
173
+ }
174
+ _getPersisterPrefix() {
175
+ return `${this.sys.env.APP_NAME}-query`;
176
+ }
177
+ _getPersisterBuster() {
178
+ return this.sys.env.APP_VERSION;
179
+ }
180
+ _forceQueryKeyPrefix(queryKey) {
181
+ if (!queryKey) queryKey = [];
182
+ if (!this._prefixIsBeanFullName(queryKey[0])) {
183
+ const prefixes = [this[SymbolBeanFullName]];
184
+ if (this.$onionOptions?.enableSelector) prefixes.push(this.selector);
185
+ queryKey = prefixes.concat(queryKey);
186
+ }
187
+ return queryKey;
188
+ }
189
+ _prefixIsBeanFullName(prefix) {
190
+ return prefix === this[SymbolBeanFullName];
191
+ }
192
+ };
193
+ //#endregion
194
+ //#region src/bean/bean.model/bean.model.local.ts
195
+ var BeanModelLocal = class extends BeanModelPersister {
196
+ $serializeLocal(obj) {
197
+ return JSON.stringify(obj?.state?.data);
198
+ }
199
+ $deserializeLocal(value) {
200
+ return {
201
+ state: {
202
+ data: value ? JSON.parse(value) : void 0,
203
+ dataUpdateCount: 0,
204
+ dataUpdatedAt: Date.now(),
205
+ error: null,
206
+ errorUpdateCount: 0,
207
+ errorUpdatedAt: 0,
208
+ fetchFailureCount: 0,
209
+ fetchFailureReason: null,
210
+ fetchMeta: null,
211
+ isInvalidated: false,
212
+ status: "success",
213
+ fetchStatus: "idle"
214
+ },
215
+ queryKey: void 0,
216
+ queryHash: void 0,
217
+ buster: this._getPersisterBuster()
218
+ };
219
+ }
220
+ };
221
+ //#endregion
222
+ //#region src/bean/bean.model/bean.model.cookie.ts
223
+ var BeanModelCookie = class extends BeanModelLocal {
224
+ $serializeCookie(obj) {
225
+ return String(obj?.state?.data ?? "");
226
+ }
227
+ $deserializeCookie(value) {
228
+ return {
229
+ state: {
230
+ data: value,
231
+ dataUpdateCount: 0,
232
+ dataUpdatedAt: Date.now(),
233
+ error: null,
234
+ errorUpdateCount: 0,
235
+ errorUpdatedAt: 0,
236
+ fetchFailureCount: 0,
237
+ fetchFailureReason: null,
238
+ fetchMeta: null,
239
+ isInvalidated: false,
240
+ status: "success",
241
+ fetchStatus: "idle"
242
+ },
243
+ queryKey: void 0,
244
+ queryHash: void 0,
245
+ buster: this._getPersisterBuster()
246
+ };
247
+ }
248
+ _cookieCoerce(value, cookieType) {
249
+ if (value === void 0 || value === "") return void 0;
250
+ if (!cookieType || cookieType === "auto") return value === "true" ? true : value === "false" ? false : value;
251
+ else if (cookieType === "boolean") return value === "true" ? true : value === "false" ? false : Boolean(Number(value));
252
+ else if (cookieType === "number") return Number(value);
253
+ else if (cookieType === "date") return new Date(value);
254
+ else if (cookieType === "string") return value;
255
+ return value;
256
+ }
257
+ };
258
+ //#endregion
259
+ //#region src/bean/bean.model/bean.model.query.ts
260
+ var BeanModelQuery = class extends BeanModelCookie {
261
+ $setQueryData(queryKey, updater, persisterSave, options) {
262
+ queryKey = this._forceQueryKeyPrefix(queryKey);
263
+ const data = this.$queryClient.setQueryData(queryKey, updater, options);
264
+ if (data === void 0) {
265
+ if (persisterSave) this.$persisterRemove(queryKey);
266
+ this.$setQueryDataDirect(queryKey, data);
267
+ } else if (persisterSave) this.$persisterSave(queryKey);
268
+ return data;
269
+ }
270
+ $queryFind(filters) {
271
+ filters = this.$normalizeFilters(filters);
272
+ return this.$queryClient.getQueryCache().find(filters);
273
+ }
274
+ $invalidateQueries(filters, options) {
275
+ filters = this.$normalizeFilters(filters);
276
+ return this.$queryClient.invalidateQueries(filters, options);
277
+ }
278
+ $refetchQueries(filters, options) {
279
+ filters = this.$normalizeFilters(filters);
280
+ return this.$queryClient.refetchQueries(filters, options);
281
+ }
282
+ $setQueryDataDirect(queryKey, value) {
283
+ this.$queryFind({
284
+ queryKey,
285
+ exact: true
286
+ })?.setData(value);
287
+ }
288
+ async $clear() {
289
+ const queries = this.$queryClient.getQueryCache().getAll();
290
+ for (const query of queries) query?.setData(void 0);
291
+ await localforage.clear();
292
+ }
293
+ $normalizeFilters(filters) {
294
+ if (!filters) filters = {};
295
+ const queryKey = this._forceQueryKeyPrefix(cast(filters).queryKey);
296
+ return {
297
+ ...filters,
298
+ queryKey
299
+ };
300
+ }
301
+ };
302
+ //#endregion
303
+ //#region src/bean/bean.model/bean.model.useQuery.ts
304
+ var BeanModelUseQuery = class extends BeanModelQuery {
305
+ $useQuery(options, queryClient) {
306
+ const queryKey = this.self._forceQueryKeyPrefix(options.queryKey);
307
+ const persister = this._createPersister(options.meta?.persister);
308
+ const optionsDefault = {};
309
+ if (!cast(options).meta?.disableErrorEffect) optionsDefault.throwOnError = (error, query) => {
310
+ let errorInfo = cast(options).meta?.errorInfo;
311
+ if (typeof errorInfo === "function") errorInfo = errorInfo(error, query);
312
+ if (!error.message.includes("useQuery:")) error.message = `useQuery: [${queryKey.join(", ")}]: ${error.message}`;
313
+ this.$errorHandler(error, errorInfo ?? "useQuery");
314
+ return false;
315
+ };
316
+ options = Object.assign(optionsDefault, options, {
317
+ queryKey,
318
+ persister
319
+ });
320
+ if ((typeof options.meta?.persister === "object" && options.meta?.persister?.sync) !== true) {
321
+ const staleTime = options.staleTime ?? this.scopeSelf.config.query.staleTime.async;
322
+ const queryCacheExists = this.$queryFind({ queryKey })?.state.data !== void 0;
323
+ options.staleTime = (query) => {
324
+ if (process.env.CLIENT && this.ctx.meta.$ssr.isRuntimeSsrPreHydration && queryCacheExists) return resolveStaleTime(this.scopeSelf.config.query.staleTime.ssr, query);
325
+ return resolveStaleTime(staleTime, query);
326
+ };
327
+ }
328
+ return this.ctx.util.instanceScope(() => {
329
+ return useQuery(options, queryClient);
330
+ });
331
+ }
332
+ };
333
+ //#endregion
334
+ //#region src/bean/bean.model/bean.model.useState.ts
335
+ var SymbolUseQueries = Symbol("SymbolUseQueries");
336
+ var SymbolUseComputeds = Symbol("SymbolUseComputeds");
337
+ var BeanModelUseState = class extends BeanModelUseQuery {
338
+ constructor(...args) {
339
+ super(...args);
340
+ this[SymbolUseQueries] = {};
341
+ this[SymbolUseComputeds] = {};
342
+ }
343
+ async $loadStateDb(dbData) {
344
+ return await dbData;
345
+ }
346
+ $useStateDb(options, queryClient) {
347
+ options = deepExtend({ meta: { maxAge: this.scopeSelf.config.persister.maxAge.local } }, options, {
348
+ enabled: false,
349
+ staleTime: Infinity,
350
+ meta: {
351
+ ssr: { dehydrate: false },
352
+ persister: {
353
+ storage: "db",
354
+ sync: false
355
+ }
356
+ }
357
+ });
358
+ const self = this;
359
+ return useCustomRef(() => {
360
+ return {
361
+ get() {
362
+ return self._handleAsyncDataGet(options, queryClient, true);
363
+ },
364
+ set(value) {
365
+ self._handleSyncDataSet(options, queryClient, true, value);
366
+ }
367
+ };
368
+ });
369
+ }
370
+ $useStateLocal(options, queryClient) {
371
+ options = deepExtend({ meta: { persister: {
372
+ serializeDefault: (obj) => {
373
+ return this.$serializeLocal(obj);
374
+ },
375
+ deserializeDefault: (value) => {
376
+ return this.$deserializeLocal(value);
377
+ },
378
+ storageKeySimplify: true
379
+ } } }, options, {
380
+ enabled: false,
381
+ staleTime: Infinity,
382
+ meta: { persister: {
383
+ storage: "local",
384
+ sync: true
385
+ } }
386
+ });
387
+ const self = this;
388
+ return useCustomRef(() => {
389
+ return {
390
+ get() {
391
+ return self._handleSyncDataGet(options, queryClient, true);
392
+ },
393
+ set(value) {
394
+ self._handleSyncDataSet(options, queryClient, true, value);
395
+ }
396
+ };
397
+ });
398
+ }
399
+ $useStateCookie(options, queryClient) {
400
+ options = deepExtend({ meta: { persister: {
401
+ serializeDefault: (obj) => {
402
+ return this.$serializeCookie(obj);
403
+ },
404
+ deserializeDefault: (value) => {
405
+ const cookieType = options.meta.persister.cookieType;
406
+ return this.$deserializeCookie(this._cookieCoerce(value, cookieType));
407
+ },
408
+ storageKeySimplify: true
409
+ } } }, options, {
410
+ enabled: false,
411
+ staleTime: Infinity,
412
+ meta: { persister: {
413
+ storage: "cookie",
414
+ sync: true
415
+ } }
416
+ });
417
+ const self = this;
418
+ return useCustomRef(() => {
419
+ return {
420
+ get() {
421
+ return self._handleSyncDataGet(options, queryClient, true);
422
+ },
423
+ set(value) {
424
+ self._handleSyncDataSet(options, queryClient, true, value);
425
+ }
426
+ };
427
+ });
428
+ }
429
+ $useStateMem(options, queryClient) {
430
+ options = deepExtend({}, options, {
431
+ enabled: false,
432
+ staleTime: Infinity,
433
+ meta: { persister: false }
434
+ });
435
+ const self = this;
436
+ return useCustomRef(() => {
437
+ return {
438
+ get() {
439
+ return self._handleSyncDataGet(options, queryClient, false);
440
+ },
441
+ set(value) {
442
+ self._handleSyncDataSet(options, queryClient, false, value);
443
+ }
444
+ };
445
+ });
446
+ }
447
+ $useStateComputed(options) {
448
+ const queryHash = hashKey(this.self._forceQueryKeyPrefix(options.queryKey));
449
+ if (!this[SymbolUseComputeds][queryHash]) this[SymbolUseComputeds][queryHash] = this.$useComputed(options.queryFn, options.debugOptions);
450
+ return this[SymbolUseComputeds][queryHash];
451
+ }
452
+ $useStateData(options, queryClient) {
453
+ const queryHash = hashKey(this.self._forceQueryKeyPrefix(options.queryKey));
454
+ if (!this[SymbolUseQueries][queryHash]) {
455
+ const useQuery = this.$useQuery(options, queryClient);
456
+ this[SymbolUseQueries][queryHash] = useQuery;
457
+ if (!options.meta?.disableSuspenseOnInit) useQuery.suspense();
458
+ }
459
+ return this[SymbolUseQueries][queryHash];
460
+ }
461
+ _handleAsyncDataGet(options, queryClient, persister) {
462
+ const query = this.$useStateData(options, queryClient);
463
+ if (query.data !== void 0) return query.data;
464
+ return this._handleAsyncDataGet_inner(options, queryClient, persister);
465
+ }
466
+ async _handleAsyncDataGet_inner(options, queryClient, persister) {
467
+ const queryKey = options.queryKey;
468
+ const query = this.$useStateData(options, queryClient);
469
+ if (persister) {
470
+ const data = await this.$persisterLoad(queryKey);
471
+ if (data !== void 0) this.$setQueryData(queryKey, data, false);
472
+ }
473
+ if (query.data === void 0) this._handleSyncDataGet_defaultData(queryKey, options);
474
+ return query.data;
475
+ }
476
+ _handleSyncDataGet(options, queryClient, persister) {
477
+ const queryKey = options.queryKey;
478
+ const query = this.$useStateData(options, queryClient);
479
+ if (query.data !== void 0) return query.data;
480
+ if (persister) {
481
+ const data = this.$persisterLoad(queryKey);
482
+ if (data !== void 0) this.$setQueryData(queryKey, data, false);
483
+ }
484
+ if (query.data === void 0) this._handleSyncDataGet_defaultData(queryKey, options);
485
+ return query.data;
486
+ }
487
+ _handleSyncDataGet_defaultData(queryKey, options) {
488
+ let defaultData = options.meta?.defaultData;
489
+ if (typeof defaultData === "function") defaultData = defaultData();
490
+ if (defaultData !== void 0) this.$setQueryData(queryKey, defaultData, false);
491
+ }
492
+ _handleSyncDataSet(options, queryClient, persister, value) {
493
+ const queryKey = options.queryKey;
494
+ const query = this.$useStateData(options, queryClient);
495
+ this.$setQueryData(queryKey, value, persister);
496
+ return query;
497
+ }
498
+ };
499
+ //#endregion
500
+ //#region src/bean/bean.model/bean.model.useStateGeneral.ts
501
+ var BeanModelUseStateGeneral = class extends BeanModelUseState {
502
+ $useState(stateType, options, queryClient) {
503
+ switch (stateType) {
504
+ case "db": return this.$useStateDb(options, queryClient);
505
+ case "local": return this.$useStateLocal(options, queryClient);
506
+ case "cookie": return this.$useStateCookie(options, queryClient);
507
+ case "mem": return this.$useStateMem(options, queryClient);
508
+ case "data": return this.$useStateData(options, queryClient);
509
+ }
510
+ }
511
+ };
512
+ //#endregion
513
+ //#region src/bean/bean.model/bean.model.useMutation.ts
514
+ var SymbolUseMutations = Symbol("SymbolUseMutations");
515
+ var BeanModelUseMutation = class extends BeanModelUseStateGeneral {
516
+ constructor(...args) {
517
+ super(...args);
518
+ this[SymbolUseMutations] = {};
519
+ }
520
+ $useMutation(mutationOptions, queryClient) {
521
+ return this.ctx.util.instanceScope(() => {
522
+ return useMutation(mutationOptions, queryClient);
523
+ });
524
+ }
525
+ $useMutationData(mutationOptions, queryClient) {
526
+ let mutationKey = cast(mutationOptions).mutationKey;
527
+ if (!mutationKey || mutationKey.length === 0) throw new Error("should specify mutationKey");
528
+ mutationKey = this.self._forceQueryKeyPrefix(mutationKey);
529
+ const mutationHash = hashKey(mutationKey);
530
+ if (!this[SymbolUseMutations][mutationHash]) {
531
+ const optionsDefault = {};
532
+ if (!cast(mutationOptions).meta?.disableErrorEffect) optionsDefault.onError = (error, variables, context) => {
533
+ let errorInfo = cast(mutationOptions).meta?.errorInfo;
534
+ if (typeof errorInfo === "function") errorInfo = errorInfo(error, variables, context);
535
+ this.$errorHandler(error, errorInfo ?? "useMutationData");
536
+ };
537
+ mutationOptions = Object.assign(optionsDefault, mutationOptions, { mutationKey });
538
+ this[SymbolUseMutations][mutationHash] = this.$useMutation(mutationOptions, queryClient);
539
+ }
540
+ return this[SymbolUseMutations][mutationHash];
541
+ }
542
+ };
543
+ //#endregion
544
+ //#region src/bean/bean.model/bean.model.first.ts
545
+ var BeanModelFirst = class extends BeanModelUseMutation {};
546
+ //#endregion
547
+ //#region src/bean/bean.modelBase.ts
548
+ var _dec$2, _dec2$2, _dec3, _class$2;
549
+ var BeanModelBase = (_dec$2 = Bean(), _dec2$2 = Virtual(), _dec3 = BeanInfo({ module: "a-model" }), _dec$2(_class$2 = _dec2$2(_class$2 = _dec3(_class$2 = class BeanModelBase extends BeanModelFirst {}) || _class$2) || _class$2) || _class$2);
550
+ //#endregion
551
+ //#region src/service/storage.ts
552
+ var _dec$1, _dec2$1, _class$1;
553
+ var ServiceStorage = (_dec$1 = Service(), _dec2$1 = BeanInfo({ module: "a-model" }), _dec$1(_class$1 = _dec2$1(_class$1 = class ServiceStorage extends BeanBase {
554
+ constructor(...args) {
555
+ super(...args);
556
+ this._queryClient = void 0;
557
+ }
558
+ async moduleLoaded() {
559
+ let options = this.scope.config.queryClientConfig.defaultOptions;
560
+ if (process.env.SERVER) options = deepExtend({}, options, { queries: { gcTime: Infinity } });
561
+ const queryCache = new QueryCache();
562
+ const vueQueryPluginOptions = { queryClient: this._queryClient = new QueryClient({
563
+ defaultOptions: options,
564
+ queryCache
565
+ }) };
566
+ this.app.vue.use(VueQueryPlugin, vueQueryPluginOptions);
567
+ }
568
+ async appInitialize() {
569
+ if (process.env.SERVER) this.ctx.meta.$ssr.context.onRendered((err) => {
570
+ if (!err) this.ctx.meta.$ssr.stateDefer.query = dehydrate(this._queryClient, { shouldDehydrateMutation: () => {
571
+ return false;
572
+ } });
573
+ this._queryClient.clear();
574
+ });
575
+ if (process.env.CLIENT && this.ctx.meta.$ssr.isRuntimeSsrPreHydration) hydrate(this._queryClient, this.ctx.meta.$ssr.stateDefer.query);
576
+ }
577
+ }) || _class$1) || _class$1);
578
+ //#endregion
579
+ //#region src/config/config.ts
580
+ var defaultOptions = {
581
+ queries: {
582
+ retry: false,
583
+ refetchOnWindowFocus: false,
584
+ refetchOnMount: false,
585
+ refetchOnReconnect: true,
586
+ gcTime: 1e3 * 60 * 5
587
+ },
588
+ dehydrate: {
589
+ shouldDehydrateQuery(query) {
590
+ if (query.meta?.ssr?.dehydrate === false) return false;
591
+ if (typeof query.meta?.persister === "object" && query.meta?.persister?.sync) return false;
592
+ return defaultShouldDehydrateQuery(query);
593
+ },
594
+ shouldDehydrateMutation(_mutation) {
595
+ return false;
596
+ }
597
+ }
598
+ };
599
+ var config = (_sys) => {
600
+ return {
601
+ persister: {
602
+ maxAge: {
603
+ cookie: void 0,
604
+ local: Infinity,
605
+ db: 1e3 * 60 * 60 * 24
606
+ },
607
+ cookie: { options: {} },
608
+ refetchOnRestore: true
609
+ },
610
+ query: { staleTime: {
611
+ async: 0,
612
+ ssr: Infinity
613
+ } },
614
+ queryClientConfig: { defaultOptions }
615
+ };
616
+ };
617
+ //#endregion
618
+ //#region src/monkey.ts
619
+ var Monkey = class extends BeanSimple {
620
+ constructor(moduleSelf) {
621
+ super();
622
+ this._moduleSelf = void 0;
623
+ this._storage = void 0;
624
+ this._moduleSelf = moduleSelf;
625
+ }
626
+ async getStorage() {
627
+ if (!this._storage) this._storage = await this.bean._newBean(ServiceStorage, false);
628
+ return this._storage;
629
+ }
630
+ async appInitialize() {
631
+ await (await this.getStorage()).appInitialize();
632
+ }
633
+ async moduleLoading(_module) {}
634
+ async moduleLoaded(module) {
635
+ if (this._moduleSelf === module) await (await this.getStorage()).moduleLoaded();
636
+ }
637
+ async beanInit(bean, beanInstance) {
638
+ bean.defineProperty(beanInstance, "$queryClient", {
639
+ enumerable: false,
640
+ configurable: true,
641
+ get() {
642
+ return markRaw(useQueryClient());
643
+ }
644
+ });
645
+ }
646
+ };
647
+ //#endregion
648
+ //#region src/.metadata/index.ts
649
+ /** monkey: end */
650
+ /** scope: begin */
651
+ var _dec, _dec2, _class;
652
+ var ScopeModuleAModel = (_dec = Scope(), _dec2 = BeanInfo({ module: "a-model" }), _dec(_class = _dec2(_class = class ScopeModuleAModel extends BeanScopeBase {}) || _class) || _class);
653
+ /** scope: end */
654
+ //#endregion
655
+ //#region src/lib/model.ts
656
+ function Model(options) {
657
+ return createBeanDecorator("model", "ctx", true, options);
658
+ }
659
+ //#endregion
660
+ //#region src/lib/utils.ts
661
+ async function $QueryAutoLoad(fn) {
662
+ return _QueryAutoLoadInner(fn);
663
+ }
664
+ async function $QueriesAutoLoad(fn1, fn2, fn3, fn4, fn5, ...fns) {
665
+ let promises = [
666
+ _QueryAutoLoadInner(fn1),
667
+ _QueryAutoLoadInner(fn2),
668
+ _QueryAutoLoadInner(fn3),
669
+ _QueryAutoLoadInner(fn4),
670
+ _QueryAutoLoadInner(fn5)
671
+ ];
672
+ if (fns.length > 0) promises = promises.concat(fns.map((fn) => _QueryAutoLoadInner(fn)));
673
+ return await Promise.all(promises);
674
+ }
675
+ async function _QueryAutoLoadInner(fn) {
676
+ if (!fn) return;
677
+ const query = fn();
678
+ if (query && query.data === void 0) await query.suspense();
679
+ return query;
680
+ }
681
+ //#endregion
682
+ export { $QueriesAutoLoad, $QueryAutoLoad, BeanModelBase, Model, Monkey, ScopeModuleAModel, ServiceStorage, config, resolveMaxAgeTime, resolveStaleTime };
683
+
684
+ //# sourceMappingURL=index.js.map