@tschk/moonshine-vue 0.4.4 → 0.4.80
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/README.md +279 -52
- package/package.json +13 -5
- package/src/app.ts +482 -16
- package/src/context.ts +231 -13
- package/src/error-context.ts +53 -3
- package/src/events.ts +30 -2
- package/src/expose.ts +123 -0
- package/src/lifecycle.ts +145 -12
- package/src/map.ts +196 -0
- package/src/model-equality.ts +43 -0
- package/src/model-ssr.ts +39 -0
- package/src/model.ts +222 -0
- package/src/reactive.ts +2419 -116
- package/src/router.ts +1562 -0
- package/src/select-ssr.ts +24 -0
- package/src/select.ts +159 -0
- package/src/server-renderer.ts +257 -0
- package/src/set.ts +145 -0
- package/src/slots.ts +26 -0
- package/src/store.ts +1042 -0
package/src/store.ts
ADDED
|
@@ -0,0 +1,1042 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-free Pinia-compatible store runtime on the Moonshine reactive graph.
|
|
3
|
+
* Supports both Setup Stores (`defineStore(id, () => ...)`) and Option Stores
|
|
4
|
+
* (`defineStore(id, { state, getters, actions })`), `$patch`, `$reset`,
|
|
5
|
+
* `$subscribe`, `$onAction`, `$dispose`, plugins, and `storeToRefs`.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
batch,
|
|
9
|
+
getOwner,
|
|
10
|
+
onCleanup,
|
|
11
|
+
untrack,
|
|
12
|
+
} from "@tschk/moonshine-dom/reactive";
|
|
13
|
+
import { peekComponentContext } from "@tschk/moonshine-dom/component";
|
|
14
|
+
import {
|
|
15
|
+
computed,
|
|
16
|
+
effectScope,
|
|
17
|
+
getCurrentScope,
|
|
18
|
+
isReactive,
|
|
19
|
+
isReadonly,
|
|
20
|
+
isRef,
|
|
21
|
+
onScopeDispose,
|
|
22
|
+
reactive,
|
|
23
|
+
shallowRef,
|
|
24
|
+
toRaw,
|
|
25
|
+
toRef,
|
|
26
|
+
watch,
|
|
27
|
+
type EffectScope,
|
|
28
|
+
type Ref,
|
|
29
|
+
type Unwrap,
|
|
30
|
+
} from "./reactive";
|
|
31
|
+
import {
|
|
32
|
+
hasInjectionContext,
|
|
33
|
+
inject,
|
|
34
|
+
provide,
|
|
35
|
+
type InjectionKey,
|
|
36
|
+
} from "./context";
|
|
37
|
+
|
|
38
|
+
export type StateTree = Record<string | number | symbol, any>;
|
|
39
|
+
|
|
40
|
+
export enum MutationType {
|
|
41
|
+
direct = "direct",
|
|
42
|
+
patchObject = "patch object",
|
|
43
|
+
patchFunction = "patch function",
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface SubscriptionCallbackMutation<S = StateTree> {
|
|
47
|
+
type: MutationType;
|
|
48
|
+
storeId: string;
|
|
49
|
+
payload?: DeepPartial<Unwrap<S>>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type SubscriptionCallback<S = StateTree> = (
|
|
53
|
+
mutation: SubscriptionCallbackMutation<S>,
|
|
54
|
+
state: Unwrap<S>,
|
|
55
|
+
) => void;
|
|
56
|
+
|
|
57
|
+
export interface StoreSubscriptionOptions {
|
|
58
|
+
detached?: boolean;
|
|
59
|
+
immediate?: boolean;
|
|
60
|
+
deep?: boolean;
|
|
61
|
+
flush?: "sync" | "pre" | "post";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface StoreOnActionListenerContext<
|
|
65
|
+
Id extends string = string,
|
|
66
|
+
S extends StateTree = StateTree,
|
|
67
|
+
G = Record<string, any>,
|
|
68
|
+
A = Record<string, any>,
|
|
69
|
+
> {
|
|
70
|
+
after: (callback: (resolvedReturn: any) => void) => void;
|
|
71
|
+
onError: (callback: (error: unknown) => void) => void;
|
|
72
|
+
name: string;
|
|
73
|
+
store: Store<Id, S, G, A>;
|
|
74
|
+
args: any[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type StoreOnActionListener<
|
|
78
|
+
Id extends string = string,
|
|
79
|
+
S extends StateTree = StateTree,
|
|
80
|
+
G = Record<string, any>,
|
|
81
|
+
A = Record<string, any>,
|
|
82
|
+
> = (context: StoreOnActionListenerContext<Id, S, G, A>) => void;
|
|
83
|
+
|
|
84
|
+
export type DeepPartial<T> = {
|
|
85
|
+
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export interface PiniaCustomProperties {}
|
|
89
|
+
|
|
90
|
+
export interface StoreProperties<
|
|
91
|
+
Id extends string = string,
|
|
92
|
+
> extends PiniaCustomProperties {
|
|
93
|
+
readonly $id: Id;
|
|
94
|
+
readonly _p: Pinia;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface Store<
|
|
98
|
+
Id extends string = string,
|
|
99
|
+
S extends StateTree = StateTree,
|
|
100
|
+
G = Record<string, any>,
|
|
101
|
+
A = Record<string, any>,
|
|
102
|
+
> extends StoreProperties<Id> {
|
|
103
|
+
$state: Unwrap<S>;
|
|
104
|
+
$patch(partialState: DeepPartial<Unwrap<S>>): void;
|
|
105
|
+
$patch<F extends (state: Unwrap<S>) => any>(
|
|
106
|
+
stateMutator: ReturnType<F> extends Promise<any> ? never : F,
|
|
107
|
+
): void;
|
|
108
|
+
$reset(): void;
|
|
109
|
+
$subscribe(
|
|
110
|
+
callback: SubscriptionCallback<S>,
|
|
111
|
+
options?: StoreSubscriptionOptions,
|
|
112
|
+
): () => void;
|
|
113
|
+
$onAction(
|
|
114
|
+
callback: StoreOnActionListener<Id, S, G, A>,
|
|
115
|
+
detached?: boolean,
|
|
116
|
+
): () => void;
|
|
117
|
+
$dispose(): void;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type StoreGeneric = Store<
|
|
121
|
+
string,
|
|
122
|
+
StateTree,
|
|
123
|
+
Record<string, any>,
|
|
124
|
+
Record<string, any>
|
|
125
|
+
>;
|
|
126
|
+
|
|
127
|
+
export interface PiniaPluginContext<
|
|
128
|
+
Id extends string = string,
|
|
129
|
+
S extends StateTree = StateTree,
|
|
130
|
+
G = Record<string, any>,
|
|
131
|
+
A = Record<string, any>,
|
|
132
|
+
> {
|
|
133
|
+
pinia: Pinia;
|
|
134
|
+
store: Store<Id, S, G, A>;
|
|
135
|
+
options: DefineStoreOptionsInPlugin<Id, S, G, A>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type PiniaPlugin = (
|
|
139
|
+
context: PiniaPluginContext<any, any, any, any>,
|
|
140
|
+
) => Partial<PiniaCustomProperties & Record<string, any>> | void;
|
|
141
|
+
|
|
142
|
+
export interface Pinia {
|
|
143
|
+
install: (app?: any) => void;
|
|
144
|
+
state: Ref<Record<string, StateTree>>;
|
|
145
|
+
use: (plugin: PiniaPlugin) => Pinia;
|
|
146
|
+
_p: PiniaPlugin[];
|
|
147
|
+
_s: Map<string, StoreGeneric>;
|
|
148
|
+
_e: EffectScope;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const piniaSymbol: InjectionKey<Pinia> = /* @__PURE__ */ Symbol(
|
|
152
|
+
"moonshine.vue.pinia",
|
|
153
|
+
) as InjectionKey<Pinia>;
|
|
154
|
+
|
|
155
|
+
let activePinia: Pinia | undefined;
|
|
156
|
+
|
|
157
|
+
export function setActivePinia(pinia: Pinia | undefined): Pinia | undefined {
|
|
158
|
+
return (activePinia = pinia);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function getActivePinia(): Pinia | undefined {
|
|
162
|
+
if (hasInjectionContext()) {
|
|
163
|
+
const injected = inject(piniaSymbol);
|
|
164
|
+
if (injected) {
|
|
165
|
+
setActivePinia(injected);
|
|
166
|
+
return injected;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return activePinia;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function createPinia(): Pinia {
|
|
173
|
+
const scope = effectScope(true);
|
|
174
|
+
const state = scope.run(() => shallowRef<Record<string, StateTree>>({}))!;
|
|
175
|
+
const _p: PiniaPlugin[] = [];
|
|
176
|
+
const pinia: Pinia = {
|
|
177
|
+
install(app?: any) {
|
|
178
|
+
setActivePinia(pinia);
|
|
179
|
+
if (app && typeof app === "object") {
|
|
180
|
+
if (typeof app.provide === "function") {
|
|
181
|
+
app.provide(piniaSymbol, pinia);
|
|
182
|
+
}
|
|
183
|
+
if (app.config && app.config.globalProperties) {
|
|
184
|
+
app.config.globalProperties.$pinia = pinia;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (peekComponentContext()) {
|
|
188
|
+
provide(piniaSymbol, pinia);
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
use(plugin: PiniaPlugin) {
|
|
192
|
+
_p.push(plugin);
|
|
193
|
+
return pinia;
|
|
194
|
+
},
|
|
195
|
+
_p,
|
|
196
|
+
_s: new Map<string, StoreGeneric>(),
|
|
197
|
+
_e: scope,
|
|
198
|
+
state,
|
|
199
|
+
};
|
|
200
|
+
setActivePinia(pinia);
|
|
201
|
+
return pinia;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export interface DefineStoreOptionsBase<_S extends StateTree, _Store> {
|
|
205
|
+
[key: string]: any;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface DefineStoreOptions<
|
|
209
|
+
Id extends string,
|
|
210
|
+
S extends StateTree,
|
|
211
|
+
G,
|
|
212
|
+
A,
|
|
213
|
+
> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
|
|
214
|
+
id: Id;
|
|
215
|
+
state?: () => S;
|
|
216
|
+
getters?: G &
|
|
217
|
+
ThisType<Unwrap<S> & StoreWithGetters<G> & PiniaCustomProperties> &
|
|
218
|
+
Record<string, (state: Unwrap<S>) => any>;
|
|
219
|
+
actions?: A &
|
|
220
|
+
ThisType<
|
|
221
|
+
A &
|
|
222
|
+
Unwrap<S> &
|
|
223
|
+
StoreWithState<Id, S, G, A> &
|
|
224
|
+
StoreWithGetters<G> &
|
|
225
|
+
PiniaCustomProperties
|
|
226
|
+
>;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface DefineSetupStoreOptions<
|
|
230
|
+
Id extends string,
|
|
231
|
+
S extends StateTree,
|
|
232
|
+
G,
|
|
233
|
+
A,
|
|
234
|
+
> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
|
|
235
|
+
actions?: A;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export interface DefineStoreOptionsInPlugin<
|
|
239
|
+
Id extends string,
|
|
240
|
+
S extends StateTree,
|
|
241
|
+
G,
|
|
242
|
+
A,
|
|
243
|
+
> extends Omit<DefineStoreOptions<Id, S, G, A>, "id" | "actions"> {
|
|
244
|
+
actions: A;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export type StoreWithState<
|
|
248
|
+
Id extends string,
|
|
249
|
+
S extends StateTree,
|
|
250
|
+
G,
|
|
251
|
+
A,
|
|
252
|
+
> = Store<Id, S, G, A> & Unwrap<S>;
|
|
253
|
+
|
|
254
|
+
export type StoreWithGetters<G> = {
|
|
255
|
+
readonly [K in keyof G]: G[K] extends (...args: any[]) => infer R
|
|
256
|
+
? R
|
|
257
|
+
: Unwrap<G[K]>;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export type StoreGetters<SS> = {
|
|
261
|
+
readonly [
|
|
262
|
+
K in keyof SS as SS[K] extends (...args: any[]) => any ? never : K
|
|
263
|
+
]: Unwrap<SS[K]>;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export type StoreActions<SS> = {
|
|
267
|
+
[K in keyof SS as SS[K] extends (...args: any[]) => any ? K : never]: SS[K];
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export type StoreState<SS> = {
|
|
271
|
+
[K in keyof SS as SS[K] extends (...args: any[]) => any ? never : K]: Unwrap<
|
|
272
|
+
SS[K]
|
|
273
|
+
>;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export type FullStore<
|
|
277
|
+
Id extends string = string,
|
|
278
|
+
S extends StateTree = StateTree,
|
|
279
|
+
G = Record<string, any>,
|
|
280
|
+
A = Record<string, any>,
|
|
281
|
+
> = Store<Id, S, G, A> & Unwrap<S> & StoreWithGetters<G> & A;
|
|
282
|
+
|
|
283
|
+
export interface StoreDefinition<
|
|
284
|
+
Id extends string = string,
|
|
285
|
+
S extends StateTree = StateTree,
|
|
286
|
+
G = Record<string, any>,
|
|
287
|
+
A = Record<string, any>,
|
|
288
|
+
> {
|
|
289
|
+
(pinia?: Pinia | null): FullStore<Id, S, G, A>;
|
|
290
|
+
$id: Id;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function isPlainObject(value: unknown): value is Record<string, any> {
|
|
294
|
+
if (value === null || typeof value !== "object") return false;
|
|
295
|
+
const proto = Object.getPrototypeOf(value);
|
|
296
|
+
return proto === Object.prototype || proto === null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function cloneInitialValue<T>(value: T): T {
|
|
300
|
+
const raw = toRaw(value);
|
|
301
|
+
if (Array.isArray(raw)) {
|
|
302
|
+
return raw.map((item) => cloneInitialValue(item)) as unknown as T;
|
|
303
|
+
}
|
|
304
|
+
if (raw instanceof Set) {
|
|
305
|
+
return new Set(
|
|
306
|
+
Array.from(raw, (item) => cloneInitialValue(item)),
|
|
307
|
+
) as unknown as T;
|
|
308
|
+
}
|
|
309
|
+
if (raw instanceof Map) {
|
|
310
|
+
return new Map(
|
|
311
|
+
Array.from(raw.entries(), ([k, v]) => [
|
|
312
|
+
cloneInitialValue(k),
|
|
313
|
+
cloneInitialValue(v),
|
|
314
|
+
]),
|
|
315
|
+
) as unknown as T;
|
|
316
|
+
}
|
|
317
|
+
if (isPlainObject(raw)) {
|
|
318
|
+
const out: Record<string, any> = {};
|
|
319
|
+
for (const key of Object.keys(raw)) {
|
|
320
|
+
out[key] = cloneInitialValue(raw[key]);
|
|
321
|
+
}
|
|
322
|
+
return out as T;
|
|
323
|
+
}
|
|
324
|
+
return raw;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function mergeReactiveObjects<T extends Record<string, any>>(
|
|
328
|
+
target: T,
|
|
329
|
+
patchToApply: DeepPartial<T>,
|
|
330
|
+
): T {
|
|
331
|
+
for (const key of Object.keys(patchToApply)) {
|
|
332
|
+
const subPatch = (patchToApply as Record<string, any>)[key];
|
|
333
|
+
const targetValue = target[key];
|
|
334
|
+
if (
|
|
335
|
+
isPlainObject(targetValue) &&
|
|
336
|
+
isPlainObject(subPatch) &&
|
|
337
|
+
Object.hasOwn(target, key) &&
|
|
338
|
+
!isRef(subPatch)
|
|
339
|
+
) {
|
|
340
|
+
(target as Record<string, any>)[key] = mergeReactiveObjects(
|
|
341
|
+
targetValue,
|
|
342
|
+
subPatch,
|
|
343
|
+
);
|
|
344
|
+
} else {
|
|
345
|
+
(target as Record<string, any>)[key] = subPatch;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return target;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const storeRawMappings = new WeakMap<
|
|
352
|
+
object,
|
|
353
|
+
{
|
|
354
|
+
rawSetup: Record<string, any>;
|
|
355
|
+
stateKeys: Set<string>;
|
|
356
|
+
getterKeys: Set<string>;
|
|
357
|
+
}
|
|
358
|
+
>();
|
|
359
|
+
|
|
360
|
+
function registerAutoCleanup(fn: () => void, detached?: boolean): void {
|
|
361
|
+
if (detached) return;
|
|
362
|
+
if (getCurrentScope()) {
|
|
363
|
+
onScopeDispose(fn);
|
|
364
|
+
} else if (getOwner()) {
|
|
365
|
+
onCleanup(fn);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function defineStore<
|
|
370
|
+
Id extends string,
|
|
371
|
+
S extends StateTree = {},
|
|
372
|
+
G = {},
|
|
373
|
+
A = {},
|
|
374
|
+
>(
|
|
375
|
+
id: Id,
|
|
376
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, "id">,
|
|
377
|
+
): StoreDefinition<Id, S, G, A>;
|
|
378
|
+
export function defineStore<
|
|
379
|
+
Id extends string,
|
|
380
|
+
S extends StateTree = {},
|
|
381
|
+
G = {},
|
|
382
|
+
A = {},
|
|
383
|
+
>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>;
|
|
384
|
+
export function defineStore<Id extends string, SS>(
|
|
385
|
+
id: Id,
|
|
386
|
+
storeSetup: () => SS,
|
|
387
|
+
options?: DefineSetupStoreOptions<
|
|
388
|
+
Id,
|
|
389
|
+
StoreState<SS>,
|
|
390
|
+
StoreGetters<SS>,
|
|
391
|
+
StoreActions<SS>
|
|
392
|
+
>,
|
|
393
|
+
): StoreDefinition<Id, StoreState<SS>, StoreGetters<SS>, StoreActions<SS>>;
|
|
394
|
+
export function defineStore(
|
|
395
|
+
idOrOptions: any,
|
|
396
|
+
setupOrOptions?: any,
|
|
397
|
+
setupOptions?: any,
|
|
398
|
+
): StoreDefinition<any, any, any, any> {
|
|
399
|
+
let id: string;
|
|
400
|
+
let options: Record<string, any>;
|
|
401
|
+
const isSetupStore = typeof setupOrOptions === "function";
|
|
402
|
+
|
|
403
|
+
if (typeof idOrOptions === "string") {
|
|
404
|
+
id = idOrOptions;
|
|
405
|
+
options = isSetupStore ? (setupOptions ?? {}) : (setupOrOptions ?? {});
|
|
406
|
+
} else if (
|
|
407
|
+
idOrOptions &&
|
|
408
|
+
typeof idOrOptions === "object" &&
|
|
409
|
+
typeof idOrOptions.id === "string"
|
|
410
|
+
) {
|
|
411
|
+
id = idOrOptions.id;
|
|
412
|
+
options = idOrOptions;
|
|
413
|
+
} else {
|
|
414
|
+
throw new TypeError("defineStore requires a string store id");
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function useStore(pinia?: Pinia | null): any {
|
|
418
|
+
const resolvedPinia = pinia ?? getActivePinia() ?? createPinia();
|
|
419
|
+
setActivePinia(resolvedPinia);
|
|
420
|
+
if (!resolvedPinia._s.has(id)) {
|
|
421
|
+
if (isSetupStore) {
|
|
422
|
+
createSetupStore(id, setupOrOptions, options, resolvedPinia, false);
|
|
423
|
+
} else {
|
|
424
|
+
createOptionsStore(id, options as any, resolvedPinia);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return resolvedPinia._s.get(id)!;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
useStore.$id = id;
|
|
431
|
+
return useStore as StoreDefinition<any, any, any, any>;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function createOptionsStore<
|
|
435
|
+
Id extends string,
|
|
436
|
+
S extends StateTree,
|
|
437
|
+
G extends Record<string, any>,
|
|
438
|
+
A extends Record<string, any>,
|
|
439
|
+
>(
|
|
440
|
+
id: Id,
|
|
441
|
+
options: DefineStoreOptions<Id, S, G, A>,
|
|
442
|
+
pinia: Pinia,
|
|
443
|
+
): FullStore<Id, S, G, A> {
|
|
444
|
+
const { state, actions, getters } = options;
|
|
445
|
+
|
|
446
|
+
function setup() {
|
|
447
|
+
if (!pinia.state.value[id]) {
|
|
448
|
+
pinia.state.value[id] = reactive(state ? state() : {});
|
|
449
|
+
}
|
|
450
|
+
const localState = pinia.state.value[id]!;
|
|
451
|
+
const out: Record<string, any> = {};
|
|
452
|
+
for (const key of Object.keys(localState)) {
|
|
453
|
+
out[key] = toRef(localState, key);
|
|
454
|
+
}
|
|
455
|
+
if (getters) {
|
|
456
|
+
for (const name of Object.keys(getters)) {
|
|
457
|
+
const getterFn = getters[name]!;
|
|
458
|
+
out[name] = computed(() => {
|
|
459
|
+
setActivePinia(pinia);
|
|
460
|
+
const store = pinia._s.get(id)!;
|
|
461
|
+
return getterFn.call(store, store as any);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (actions) {
|
|
466
|
+
for (const name of Object.keys(actions)) {
|
|
467
|
+
out[name] = actions[name];
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return out;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const store = createSetupStore<Id, Record<string, any>, S, G, A>(
|
|
474
|
+
id,
|
|
475
|
+
setup,
|
|
476
|
+
options,
|
|
477
|
+
pinia,
|
|
478
|
+
true,
|
|
479
|
+
);
|
|
480
|
+
return store;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function createSetupStore<
|
|
484
|
+
Id extends string,
|
|
485
|
+
SS extends Record<string, any>,
|
|
486
|
+
S extends StateTree,
|
|
487
|
+
G extends Record<string, any>,
|
|
488
|
+
A extends Record<string, any>,
|
|
489
|
+
>(
|
|
490
|
+
id: Id,
|
|
491
|
+
setup: () => SS,
|
|
492
|
+
options: Record<string, any>,
|
|
493
|
+
pinia: Pinia,
|
|
494
|
+
isOptionsStore: boolean,
|
|
495
|
+
): FullStore<Id, S, G, A> {
|
|
496
|
+
let scope!: EffectScope;
|
|
497
|
+
const subscriptions = new Set<SubscriptionCallback<S>>();
|
|
498
|
+
const actionSubscriptions = new Set<StoreOnActionListener<Id, S, G, A>>();
|
|
499
|
+
let isSyncListening = true;
|
|
500
|
+
|
|
501
|
+
if (!isOptionsStore && !pinia.state.value[id]) {
|
|
502
|
+
pinia.state.value[id] = {};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const initialSetupValues = new Map<string, any>();
|
|
506
|
+
const stateKeys = new Set<string>();
|
|
507
|
+
const getterKeys = new Set<string>();
|
|
508
|
+
const actionMap: Record<string, Function> = {};
|
|
509
|
+
const customProps: Record<string | symbol, any> = {};
|
|
510
|
+
let rawSetup: Record<string, any> = {};
|
|
511
|
+
|
|
512
|
+
function wrapAction(name: string, action: Function) {
|
|
513
|
+
return function (this: any, ...args: any[]) {
|
|
514
|
+
setActivePinia(pinia);
|
|
515
|
+
const afterCallbackList: Array<(resolvedReturn: any) => void> = [];
|
|
516
|
+
const onErrorCallbackList: Array<(error: unknown) => void> = [];
|
|
517
|
+
|
|
518
|
+
function after(callback: (resolvedReturn: any) => void) {
|
|
519
|
+
afterCallbackList.push(callback);
|
|
520
|
+
}
|
|
521
|
+
function onError(callback: (error: unknown) => void) {
|
|
522
|
+
onErrorCallbackList.push(callback);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
for (const listener of Array.from(actionSubscriptions)) {
|
|
526
|
+
listener({
|
|
527
|
+
args,
|
|
528
|
+
name,
|
|
529
|
+
store: storeProxy as any,
|
|
530
|
+
after,
|
|
531
|
+
onError,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
let ret: unknown;
|
|
536
|
+
try {
|
|
537
|
+
ret = action.apply(this && this.$id === id ? this : storeProxy, args);
|
|
538
|
+
} catch (error) {
|
|
539
|
+
for (const cb of onErrorCallbackList) {
|
|
540
|
+
cb(error);
|
|
541
|
+
}
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (ret instanceof Promise) {
|
|
546
|
+
return ret
|
|
547
|
+
.then((value) => {
|
|
548
|
+
for (const cb of afterCallbackList) {
|
|
549
|
+
cb(value);
|
|
550
|
+
}
|
|
551
|
+
return value;
|
|
552
|
+
})
|
|
553
|
+
.catch((error) => {
|
|
554
|
+
for (const cb of onErrorCallbackList) {
|
|
555
|
+
cb(error);
|
|
556
|
+
}
|
|
557
|
+
return Promise.reject(error);
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
for (const cb of afterCallbackList) {
|
|
562
|
+
cb(ret);
|
|
563
|
+
}
|
|
564
|
+
return ret;
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function $patch(
|
|
569
|
+
partialStateOrMutator:
|
|
570
|
+
DeepPartial<Unwrap<S>> | ((state: Unwrap<S>) => void),
|
|
571
|
+
): void {
|
|
572
|
+
let subscriptionMutation: SubscriptionCallbackMutation<S>;
|
|
573
|
+
isSyncListening = false;
|
|
574
|
+
try {
|
|
575
|
+
batch(() => {
|
|
576
|
+
if (typeof partialStateOrMutator === "function") {
|
|
577
|
+
partialStateOrMutator(pinia.state.value[id] as Unwrap<S>);
|
|
578
|
+
subscriptionMutation = {
|
|
579
|
+
type: MutationType.patchFunction,
|
|
580
|
+
storeId: id,
|
|
581
|
+
};
|
|
582
|
+
} else {
|
|
583
|
+
mergeReactiveObjects(pinia.state.value[id]!, partialStateOrMutator);
|
|
584
|
+
subscriptionMutation = {
|
|
585
|
+
type: MutationType.patchObject,
|
|
586
|
+
storeId: id,
|
|
587
|
+
payload: partialStateOrMutator,
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
} finally {
|
|
592
|
+
isSyncListening = true;
|
|
593
|
+
}
|
|
594
|
+
const currentState = pinia.state.value[id] as Unwrap<S>;
|
|
595
|
+
untrack(() => {
|
|
596
|
+
for (const callback of Array.from(subscriptions)) {
|
|
597
|
+
callback(subscriptionMutation!, currentState);
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function $reset(): void {
|
|
603
|
+
if (isOptionsStore) {
|
|
604
|
+
const { state } = options as DefineStoreOptions<Id, S, G, A>;
|
|
605
|
+
const newState = state ? state() : {};
|
|
606
|
+
$patch(($state) => {
|
|
607
|
+
Object.assign($state as Record<string, any>, newState);
|
|
608
|
+
});
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
$patch(($state) => {
|
|
612
|
+
for (const [key, initialVal] of initialSetupValues.entries()) {
|
|
613
|
+
const cloned = cloneInitialValue(initialVal);
|
|
614
|
+
const setupEntry = rawSetup[key];
|
|
615
|
+
if (isRef(setupEntry) && !isReadonly(setupEntry)) {
|
|
616
|
+
setupEntry.value = cloned;
|
|
617
|
+
} else if (
|
|
618
|
+
isReactive(setupEntry) &&
|
|
619
|
+
isPlainObject(setupEntry) &&
|
|
620
|
+
isPlainObject(cloned)
|
|
621
|
+
) {
|
|
622
|
+
for (const existingKey of Object.keys(setupEntry)) {
|
|
623
|
+
if (!(existingKey in cloned)) {
|
|
624
|
+
delete (setupEntry as Record<string, any>)[existingKey];
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
Object.assign(setupEntry, cloned);
|
|
628
|
+
}
|
|
629
|
+
($state as Record<string, any>)[key] = cloned;
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function $subscribe(
|
|
635
|
+
callback: SubscriptionCallback<S>,
|
|
636
|
+
subscribeOptions: StoreSubscriptionOptions = {},
|
|
637
|
+
): () => void {
|
|
638
|
+
subscriptions.add(callback);
|
|
639
|
+
const stopWatcher = scope.run(() =>
|
|
640
|
+
watch(
|
|
641
|
+
() => pinia.state.value[id] as Unwrap<S>,
|
|
642
|
+
(state) => {
|
|
643
|
+
if (isSyncListening) {
|
|
644
|
+
callback(
|
|
645
|
+
{
|
|
646
|
+
storeId: id,
|
|
647
|
+
type: MutationType.direct,
|
|
648
|
+
},
|
|
649
|
+
state,
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
flush: "sync",
|
|
655
|
+
deep: subscribeOptions.deep !== false,
|
|
656
|
+
immediate: subscribeOptions.immediate,
|
|
657
|
+
},
|
|
658
|
+
),
|
|
659
|
+
);
|
|
660
|
+
|
|
661
|
+
const removeSubscription = () => {
|
|
662
|
+
subscriptions.delete(callback);
|
|
663
|
+
stopWatcher?.();
|
|
664
|
+
};
|
|
665
|
+
registerAutoCleanup(removeSubscription, subscribeOptions.detached);
|
|
666
|
+
return removeSubscription;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function $onAction(
|
|
670
|
+
callback: StoreOnActionListener<Id, S, G, A>,
|
|
671
|
+
detached?: boolean,
|
|
672
|
+
): () => void {
|
|
673
|
+
actionSubscriptions.add(callback);
|
|
674
|
+
const removeListener = () => {
|
|
675
|
+
actionSubscriptions.delete(callback);
|
|
676
|
+
};
|
|
677
|
+
registerAutoCleanup(removeListener, detached);
|
|
678
|
+
return removeListener;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function $dispose(): void {
|
|
682
|
+
scope.stop();
|
|
683
|
+
subscriptions.clear();
|
|
684
|
+
actionSubscriptions.clear();
|
|
685
|
+
pinia._s.delete(id);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const baseProperties: Record<string | symbol, any> = {
|
|
689
|
+
_p: pinia,
|
|
690
|
+
$id: id,
|
|
691
|
+
$patch,
|
|
692
|
+
$reset,
|
|
693
|
+
$subscribe,
|
|
694
|
+
$onAction,
|
|
695
|
+
$dispose,
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
const storeTarget: Record<string | symbol, any> = Object.assign(
|
|
699
|
+
reactive({}),
|
|
700
|
+
baseProperties,
|
|
701
|
+
);
|
|
702
|
+
|
|
703
|
+
const storeProxy = new Proxy(storeTarget, {
|
|
704
|
+
get(target, prop, receiver) {
|
|
705
|
+
if (prop === "$state") {
|
|
706
|
+
return pinia.state.value[id];
|
|
707
|
+
}
|
|
708
|
+
if (prop in baseProperties) {
|
|
709
|
+
return baseProperties[prop];
|
|
710
|
+
}
|
|
711
|
+
if (prop in actionMap && typeof prop === "string") {
|
|
712
|
+
return actionMap[prop];
|
|
713
|
+
}
|
|
714
|
+
if (typeof prop === "string" && prop in rawSetup) {
|
|
715
|
+
const val = rawSetup[prop];
|
|
716
|
+
return isRef(val) ? val.value : val;
|
|
717
|
+
}
|
|
718
|
+
const stateObj = pinia.state.value[id];
|
|
719
|
+
if (stateObj && typeof prop === "string" && prop in stateObj) {
|
|
720
|
+
const val = stateObj[prop];
|
|
721
|
+
return isRef(val) ? val.value : val;
|
|
722
|
+
}
|
|
723
|
+
if (prop in customProps) {
|
|
724
|
+
const val = customProps[prop];
|
|
725
|
+
return isRef(val) ? val.value : val;
|
|
726
|
+
}
|
|
727
|
+
return Reflect.get(target, prop, receiver);
|
|
728
|
+
},
|
|
729
|
+
set(target, prop, value, receiver) {
|
|
730
|
+
if (prop === "$state") {
|
|
731
|
+
$patch(($state) => {
|
|
732
|
+
Object.assign($state as Record<string, any>, value);
|
|
733
|
+
});
|
|
734
|
+
return true;
|
|
735
|
+
}
|
|
736
|
+
if (typeof prop === "string" && prop in rawSetup) {
|
|
737
|
+
const current = rawSetup[prop];
|
|
738
|
+
if (isRef(current)) {
|
|
739
|
+
if (isReadonly(current)) {
|
|
740
|
+
throw new TypeError(`Cannot assign to readonly getter '${prop}'`);
|
|
741
|
+
}
|
|
742
|
+
current.value = value;
|
|
743
|
+
return true;
|
|
744
|
+
}
|
|
745
|
+
rawSetup[prop] = value;
|
|
746
|
+
if (stateKeys.has(prop)) {
|
|
747
|
+
pinia.state.value[id]![prop] = value;
|
|
748
|
+
}
|
|
749
|
+
return true;
|
|
750
|
+
}
|
|
751
|
+
const stateObj = pinia.state.value[id];
|
|
752
|
+
if (stateObj && typeof prop === "string" && prop in stateObj) {
|
|
753
|
+
stateObj[prop] = value;
|
|
754
|
+
return true;
|
|
755
|
+
}
|
|
756
|
+
if (prop in customProps) {
|
|
757
|
+
const current = customProps[prop];
|
|
758
|
+
if (isRef(current) && !isReadonly(current)) {
|
|
759
|
+
current.value = value;
|
|
760
|
+
return true;
|
|
761
|
+
}
|
|
762
|
+
customProps[prop] = value;
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
return Reflect.set(target, prop, value, receiver);
|
|
766
|
+
},
|
|
767
|
+
has(target, prop) {
|
|
768
|
+
return (
|
|
769
|
+
prop === "$state" ||
|
|
770
|
+
prop in baseProperties ||
|
|
771
|
+
(typeof prop === "string" &&
|
|
772
|
+
(prop in rawSetup ||
|
|
773
|
+
prop in actionMap ||
|
|
774
|
+
Boolean(
|
|
775
|
+
pinia.state.value[id] && prop in pinia.state.value[id]!,
|
|
776
|
+
))) ||
|
|
777
|
+
prop in customProps ||
|
|
778
|
+
Reflect.has(target, prop)
|
|
779
|
+
);
|
|
780
|
+
},
|
|
781
|
+
ownKeys(target) {
|
|
782
|
+
const keys = new Set<string | symbol>([
|
|
783
|
+
...Reflect.ownKeys(target),
|
|
784
|
+
...Object.keys(rawSetup),
|
|
785
|
+
...Object.keys(actionMap),
|
|
786
|
+
...Object.keys(pinia.state.value[id] ?? {}),
|
|
787
|
+
...Reflect.ownKeys(customProps),
|
|
788
|
+
]);
|
|
789
|
+
return Array.from(keys);
|
|
790
|
+
},
|
|
791
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
792
|
+
if (
|
|
793
|
+
prop === "$state" ||
|
|
794
|
+
prop in baseProperties ||
|
|
795
|
+
(typeof prop === "string" &&
|
|
796
|
+
(prop in rawSetup ||
|
|
797
|
+
prop in actionMap ||
|
|
798
|
+
Boolean(
|
|
799
|
+
pinia.state.value[id] && prop in pinia.state.value[id]!,
|
|
800
|
+
))) ||
|
|
801
|
+
prop in customProps
|
|
802
|
+
) {
|
|
803
|
+
return {
|
|
804
|
+
enumerable: true,
|
|
805
|
+
configurable: true,
|
|
806
|
+
writable: true,
|
|
807
|
+
value: (storeProxy as any)[prop],
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
811
|
+
},
|
|
812
|
+
}) as unknown as FullStore<Id, S, G, A>;
|
|
813
|
+
|
|
814
|
+
pinia._s.set(id, storeProxy);
|
|
815
|
+
|
|
816
|
+
const setupStore = pinia._e.run(() => {
|
|
817
|
+
scope = effectScope();
|
|
818
|
+
return scope.run(() => setup());
|
|
819
|
+
})!;
|
|
820
|
+
|
|
821
|
+
rawSetup = setupStore ?? {};
|
|
822
|
+
|
|
823
|
+
for (const key of Object.keys(rawSetup)) {
|
|
824
|
+
const prop = rawSetup[key];
|
|
825
|
+
if (isRef(prop)) {
|
|
826
|
+
if (isReadonly(prop)) {
|
|
827
|
+
getterKeys.add(key);
|
|
828
|
+
} else {
|
|
829
|
+
stateKeys.add(key);
|
|
830
|
+
if (!isOptionsStore) {
|
|
831
|
+
initialSetupValues.set(key, cloneInitialValue(prop.value));
|
|
832
|
+
const stateTarget = pinia.state.value[id]!;
|
|
833
|
+
Object.defineProperty(stateTarget, key, {
|
|
834
|
+
enumerable: true,
|
|
835
|
+
configurable: true,
|
|
836
|
+
get: () => prop.value,
|
|
837
|
+
set: (v) => {
|
|
838
|
+
prop.value = v;
|
|
839
|
+
},
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
} else if (isReactive(prop)) {
|
|
844
|
+
stateKeys.add(key);
|
|
845
|
+
if (!isOptionsStore) {
|
|
846
|
+
initialSetupValues.set(key, cloneInitialValue(prop));
|
|
847
|
+
pinia.state.value[id]![key] = prop;
|
|
848
|
+
}
|
|
849
|
+
} else if (typeof prop === "function") {
|
|
850
|
+
const wrapped = wrapAction(key, prop);
|
|
851
|
+
actionMap[key] = wrapped;
|
|
852
|
+
rawSetup[key] = wrapped;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
storeRawMappings.set(storeProxy, {
|
|
857
|
+
rawSetup,
|
|
858
|
+
stateKeys,
|
|
859
|
+
getterKeys,
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
const pluginOptions: DefineStoreOptionsInPlugin<Id, S, G, A> = {
|
|
863
|
+
...options,
|
|
864
|
+
actions: actionMap as unknown as A,
|
|
865
|
+
};
|
|
866
|
+
|
|
867
|
+
for (const plugin of pinia._p) {
|
|
868
|
+
const extension = scope.run(() =>
|
|
869
|
+
plugin({
|
|
870
|
+
store: storeProxy,
|
|
871
|
+
pinia,
|
|
872
|
+
options: pluginOptions,
|
|
873
|
+
}),
|
|
874
|
+
);
|
|
875
|
+
if (extension && typeof extension === "object") {
|
|
876
|
+
for (const key of Reflect.ownKeys(extension)) {
|
|
877
|
+
const val = (extension as Record<string | symbol, any>)[key];
|
|
878
|
+
customProps[key] = val;
|
|
879
|
+
if (
|
|
880
|
+
typeof key === "string" &&
|
|
881
|
+
(isRef(val) || isReactive(val)) &&
|
|
882
|
+
!isReadonly(val)
|
|
883
|
+
) {
|
|
884
|
+
stateKeys.add(key);
|
|
885
|
+
if (isRef(val)) {
|
|
886
|
+
Object.defineProperty(pinia.state.value[id]!, key, {
|
|
887
|
+
enumerable: true,
|
|
888
|
+
configurable: true,
|
|
889
|
+
get: () => val.value,
|
|
890
|
+
set: (v) => {
|
|
891
|
+
val.value = v;
|
|
892
|
+
},
|
|
893
|
+
});
|
|
894
|
+
} else {
|
|
895
|
+
pinia.state.value[id]![key] = val;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
return storeProxy;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export type StoreToRefs<SS> = {
|
|
906
|
+
[
|
|
907
|
+
K in keyof SS as SS[K] extends (...args: any[]) => any
|
|
908
|
+
? never
|
|
909
|
+
: K extends `$${string}` | "_p"
|
|
910
|
+
? never
|
|
911
|
+
: K
|
|
912
|
+
]: SS[K] extends Ref<infer V> ? Ref<V> : Ref<Unwrap<SS[K]>>;
|
|
913
|
+
};
|
|
914
|
+
|
|
915
|
+
export function storeToRefs<SS extends StoreGeneric>(
|
|
916
|
+
store: SS,
|
|
917
|
+
): StoreToRefs<SS> {
|
|
918
|
+
if (!store || typeof store !== "object") {
|
|
919
|
+
throw new TypeError("storeToRefs requires a store instance");
|
|
920
|
+
}
|
|
921
|
+
const refs = {} as Record<string, Ref<any>>;
|
|
922
|
+
const mapping = storeRawMappings.get(store);
|
|
923
|
+
if (mapping) {
|
|
924
|
+
for (const key of mapping.stateKeys) {
|
|
925
|
+
const rawVal = mapping.rawSetup[key];
|
|
926
|
+
refs[key] = isRef(rawVal) ? rawVal : toRef(store as any, key);
|
|
927
|
+
}
|
|
928
|
+
for (const key of mapping.getterKeys) {
|
|
929
|
+
const rawVal = mapping.rawSetup[key];
|
|
930
|
+
refs[key] = isRef(rawVal) ? rawVal : toRef(store as any, key);
|
|
931
|
+
}
|
|
932
|
+
return refs as StoreToRefs<SS>;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
for (const key of Object.keys(store)) {
|
|
936
|
+
if (key.startsWith("$") || key === "_p") continue;
|
|
937
|
+
const val = (store as Record<string, any>)[key];
|
|
938
|
+
if (typeof val === "function") continue;
|
|
939
|
+
refs[key] = toRef(store as any, key);
|
|
940
|
+
}
|
|
941
|
+
return refs as StoreToRefs<SS>;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
let mapStoreSuffix = "Store";
|
|
945
|
+
|
|
946
|
+
export function setMapStoreSuffix(suffix: string): void {
|
|
947
|
+
mapStoreSuffix = suffix;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
export function mapStores<
|
|
951
|
+
Stores extends Array<StoreDefinition<any, any, any, any>>,
|
|
952
|
+
>(...stores: Stores): Record<string, () => StoreGeneric> {
|
|
953
|
+
const res: Record<string, () => StoreGeneric> = {};
|
|
954
|
+
for (const useStore of stores) {
|
|
955
|
+
res[`${useStore.$id}${mapStoreSuffix}`] = function (this: any) {
|
|
956
|
+
return useStore(this?.$pinia);
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
return res;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
export function mapState<Id extends string, S extends StateTree, G, A>(
|
|
963
|
+
useStore: StoreDefinition<Id, S, G, A>,
|
|
964
|
+
keysOrMapper:
|
|
965
|
+
| ReadonlyArray<string>
|
|
966
|
+
| Record<string, string | ((store: FullStore<Id, S, G, A>) => any)>,
|
|
967
|
+
): Record<string, () => any> {
|
|
968
|
+
const res: Record<string, () => any> = {};
|
|
969
|
+
if (Array.isArray(keysOrMapper)) {
|
|
970
|
+
for (const key of keysOrMapper) {
|
|
971
|
+
res[key] = function (this: any) {
|
|
972
|
+
return (useStore(this?.$pinia) as any)[key];
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
} else {
|
|
976
|
+
for (const [alias, keyOrFn] of Object.entries(keysOrMapper)) {
|
|
977
|
+
res[alias] = function (this: any) {
|
|
978
|
+
const store = useStore(this?.$pinia);
|
|
979
|
+
return typeof keyOrFn === "function"
|
|
980
|
+
? keyOrFn.call(this, store)
|
|
981
|
+
: (store as any)[keyOrFn];
|
|
982
|
+
};
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
return res;
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
export const mapGetters = mapState;
|
|
989
|
+
|
|
990
|
+
export function mapWritableState<Id extends string, S extends StateTree, G, A>(
|
|
991
|
+
useStore: StoreDefinition<Id, S, G, A>,
|
|
992
|
+
keysOrMapper: ReadonlyArray<string> | Record<string, string>,
|
|
993
|
+
): Record<string, { get: () => any; set: (value: any) => void }> {
|
|
994
|
+
const res: Record<string, { get: () => any; set: (value: any) => void }> = {};
|
|
995
|
+
if (Array.isArray(keysOrMapper)) {
|
|
996
|
+
for (const key of keysOrMapper) {
|
|
997
|
+
res[key] = {
|
|
998
|
+
get(this: any) {
|
|
999
|
+
return (useStore(this?.$pinia) as any)[key];
|
|
1000
|
+
},
|
|
1001
|
+
set(this: any, value: any) {
|
|
1002
|
+
(useStore(this?.$pinia) as any)[key] = value;
|
|
1003
|
+
},
|
|
1004
|
+
};
|
|
1005
|
+
}
|
|
1006
|
+
} else {
|
|
1007
|
+
for (const [alias, key] of Object.entries(keysOrMapper)) {
|
|
1008
|
+
res[alias] = {
|
|
1009
|
+
get(this: any) {
|
|
1010
|
+
return (useStore(this?.$pinia) as any)[key];
|
|
1011
|
+
},
|
|
1012
|
+
set(this: any, value: any) {
|
|
1013
|
+
(useStore(this?.$pinia) as any)[key] = value;
|
|
1014
|
+
},
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
return res;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
export function mapActions<Id extends string, S extends StateTree, G, A>(
|
|
1022
|
+
useStore: StoreDefinition<Id, S, G, A>,
|
|
1023
|
+
keysOrMapper: ReadonlyArray<string> | Record<string, string>,
|
|
1024
|
+
): Record<string, (...args: any[]) => any> {
|
|
1025
|
+
const res: Record<string, (...args: any[]) => any> = {};
|
|
1026
|
+
if (Array.isArray(keysOrMapper)) {
|
|
1027
|
+
for (const key of keysOrMapper) {
|
|
1028
|
+
res[key] = function (this: any, ...args: any[]) {
|
|
1029
|
+
const store = useStore(this?.$pinia) as any;
|
|
1030
|
+
return store[key](...args);
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
} else {
|
|
1034
|
+
for (const [alias, key] of Object.entries(keysOrMapper)) {
|
|
1035
|
+
res[alias] = function (this: any, ...args: any[]) {
|
|
1036
|
+
const store = useStore(this?.$pinia) as any;
|
|
1037
|
+
return store[key](...args);
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
return res;
|
|
1042
|
+
}
|