@vtj/materials 0.13.25 → 0.13.27
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/assets/antdv/index.umd.js +2 -2
- package/dist/assets/charts/index.umd.js +2 -2
- package/dist/assets/element/index.umd.js +2 -2
- package/dist/assets/icons/index.umd.js +2 -2
- package/dist/assets/ui/index.umd.js +2 -2
- package/dist/assets/uni-h5/index.umd.js +2 -2
- package/dist/assets/uni-ui/index.umd.js +2 -2
- package/dist/assets/vant/index.umd.js +2 -2
- package/dist/deps/@vtj/charts/index.umd.js +2 -2
- package/dist/deps/@vtj/icons/index.umd.js +2 -2
- package/dist/deps/@vtj/icons/style.css +1 -1
- package/dist/deps/@vtj/ui/index.umd.js +3 -10
- package/dist/deps/@vtj/utils/index.umd.js +4 -39
- package/dist/deps/element-plus/index.css +1 -1
- package/dist/deps/element-plus/index.full.min.js +22 -22
- package/dist/deps/element-plus/zh-cn.js +1 -1
- package/dist/deps/pinia/pinia.iife.js +1940 -2
- package/dist/deps/pinia/pinia.iife.prod.js +7 -0
- package/dist/deps/uni-h5-vue/uni-h5-vue.js +11719 -0
- package/dist/deps/uni-h5-vue/uni-h5-vue.prod.js +8 -0
- package/package.json +10 -8
- package/src/uni-ui/index.ts +1 -0
- package/src/version.ts +2 -2
@@ -3,5 +3,1943 @@
|
|
3
3
|
* (c) 2025 Eduardo San Martin Morote
|
4
4
|
* @license MIT
|
5
5
|
*/
|
6
|
-
var Pinia
|
7
|
-
|
6
|
+
var Pinia = (function (exports, vue, devtoolsApi) {
|
7
|
+
'use strict';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* setActivePinia must be called to handle SSR at the top of functions like
|
11
|
+
* `fetch`, `setup`, `serverPrefetch` and others
|
12
|
+
*/
|
13
|
+
let activePinia;
|
14
|
+
/**
|
15
|
+
* Sets or unsets the active pinia. Used in SSR and internally when calling
|
16
|
+
* actions and getters
|
17
|
+
*
|
18
|
+
* @param pinia - Pinia instance
|
19
|
+
*/
|
20
|
+
// @ts-expect-error: cannot constrain the type of the return
|
21
|
+
const setActivePinia = (pinia) => (activePinia = pinia);
|
22
|
+
/**
|
23
|
+
* Get the currently active pinia if there is any.
|
24
|
+
*/
|
25
|
+
const getActivePinia = () => (vue.hasInjectionContext() && vue.inject(piniaSymbol)) || activePinia;
|
26
|
+
const piniaSymbol = (Symbol('pinia') );
|
27
|
+
|
28
|
+
function isPlainObject(
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
30
|
+
o) {
|
31
|
+
return (o &&
|
32
|
+
typeof o === 'object' &&
|
33
|
+
Object.prototype.toString.call(o) === '[object Object]' &&
|
34
|
+
typeof o.toJSON !== 'function');
|
35
|
+
}
|
36
|
+
// type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> }
|
37
|
+
// TODO: can we change these to numbers?
|
38
|
+
/**
|
39
|
+
* Possible types for SubscriptionCallback
|
40
|
+
*/
|
41
|
+
exports.MutationType = void 0;
|
42
|
+
(function (MutationType) {
|
43
|
+
/**
|
44
|
+
* Direct mutation of the state:
|
45
|
+
*
|
46
|
+
* - `store.name = 'new name'`
|
47
|
+
* - `store.$state.name = 'new name'`
|
48
|
+
* - `store.list.push('new item')`
|
49
|
+
*/
|
50
|
+
MutationType["direct"] = "direct";
|
51
|
+
/**
|
52
|
+
* Mutated the state with `$patch` and an object
|
53
|
+
*
|
54
|
+
* - `store.$patch({ name: 'newName' })`
|
55
|
+
*/
|
56
|
+
MutationType["patchObject"] = "patch object";
|
57
|
+
/**
|
58
|
+
* Mutated the state with `$patch` and a function
|
59
|
+
*
|
60
|
+
* - `store.$patch(state => state.name = 'newName')`
|
61
|
+
*/
|
62
|
+
MutationType["patchFunction"] = "patch function";
|
63
|
+
// maybe reset? for $state = {} and $reset
|
64
|
+
})(exports.MutationType || (exports.MutationType = {}));
|
65
|
+
|
66
|
+
const IS_CLIENT = typeof window !== 'undefined';
|
67
|
+
|
68
|
+
/*
|
69
|
+
* FileSaver.js A saveAs() FileSaver implementation.
|
70
|
+
*
|
71
|
+
* Originally by Eli Grey, adapted as an ESM module by Eduardo San Martin
|
72
|
+
* Morote.
|
73
|
+
*
|
74
|
+
* License : MIT
|
75
|
+
*/
|
76
|
+
// The one and only way of getting global scope in all environments
|
77
|
+
// https://stackoverflow.com/q/3277182/1008999
|
78
|
+
const _global = /*#__PURE__*/ (() => typeof window === 'object' && window.window === window
|
79
|
+
? window
|
80
|
+
: typeof self === 'object' && self.self === self
|
81
|
+
? self
|
82
|
+
: typeof global === 'object' && global.global === global
|
83
|
+
? global
|
84
|
+
: typeof globalThis === 'object'
|
85
|
+
? globalThis
|
86
|
+
: { HTMLElement: null })();
|
87
|
+
function bom(blob, { autoBom = false } = {}) {
|
88
|
+
// prepend BOM for UTF-8 XML and text/* types (including HTML)
|
89
|
+
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
|
90
|
+
if (autoBom &&
|
91
|
+
/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
|
92
|
+
return new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type });
|
93
|
+
}
|
94
|
+
return blob;
|
95
|
+
}
|
96
|
+
function download(url, name, opts) {
|
97
|
+
const xhr = new XMLHttpRequest();
|
98
|
+
xhr.open('GET', url);
|
99
|
+
xhr.responseType = 'blob';
|
100
|
+
xhr.onload = function () {
|
101
|
+
saveAs(xhr.response, name, opts);
|
102
|
+
};
|
103
|
+
xhr.onerror = function () {
|
104
|
+
console.error('could not download file');
|
105
|
+
};
|
106
|
+
xhr.send();
|
107
|
+
}
|
108
|
+
function corsEnabled(url) {
|
109
|
+
const xhr = new XMLHttpRequest();
|
110
|
+
// use sync to avoid popup blocker
|
111
|
+
xhr.open('HEAD', url, false);
|
112
|
+
try {
|
113
|
+
xhr.send();
|
114
|
+
}
|
115
|
+
catch (e) { }
|
116
|
+
return xhr.status >= 200 && xhr.status <= 299;
|
117
|
+
}
|
118
|
+
// `a.click()` doesn't work for all browsers (#465)
|
119
|
+
function click(node) {
|
120
|
+
try {
|
121
|
+
node.dispatchEvent(new MouseEvent('click'));
|
122
|
+
}
|
123
|
+
catch (e) {
|
124
|
+
const evt = new MouseEvent('click', {
|
125
|
+
bubbles: true,
|
126
|
+
cancelable: true,
|
127
|
+
view: window,
|
128
|
+
detail: 0,
|
129
|
+
screenX: 80,
|
130
|
+
screenY: 20,
|
131
|
+
clientX: 80,
|
132
|
+
clientY: 20,
|
133
|
+
ctrlKey: false,
|
134
|
+
altKey: false,
|
135
|
+
shiftKey: false,
|
136
|
+
metaKey: false,
|
137
|
+
button: 0,
|
138
|
+
relatedTarget: null,
|
139
|
+
});
|
140
|
+
node.dispatchEvent(evt);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
const _navigator = typeof navigator === 'object' ? navigator : { userAgent: '' };
|
144
|
+
// Detect WebView inside a native macOS app by ruling out all browsers
|
145
|
+
// We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
|
146
|
+
// https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
|
147
|
+
const isMacOSWebView = /*#__PURE__*/ (() => /Macintosh/.test(_navigator.userAgent) &&
|
148
|
+
/AppleWebKit/.test(_navigator.userAgent) &&
|
149
|
+
!/Safari/.test(_navigator.userAgent))();
|
150
|
+
const saveAs = !IS_CLIENT
|
151
|
+
? () => { } // noop
|
152
|
+
: // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView or mini program
|
153
|
+
typeof HTMLAnchorElement !== 'undefined' &&
|
154
|
+
'download' in HTMLAnchorElement.prototype &&
|
155
|
+
!isMacOSWebView
|
156
|
+
? downloadSaveAs
|
157
|
+
: // Use msSaveOrOpenBlob as a second approach
|
158
|
+
'msSaveOrOpenBlob' in _navigator
|
159
|
+
? msSaveAs
|
160
|
+
: // Fallback to using FileReader and a popup
|
161
|
+
fileSaverSaveAs;
|
162
|
+
function downloadSaveAs(blob, name = 'download', opts) {
|
163
|
+
const a = document.createElement('a');
|
164
|
+
a.download = name;
|
165
|
+
a.rel = 'noopener'; // tabnabbing
|
166
|
+
// TODO: detect chrome extensions & packaged apps
|
167
|
+
// a.target = '_blank'
|
168
|
+
if (typeof blob === 'string') {
|
169
|
+
// Support regular links
|
170
|
+
a.href = blob;
|
171
|
+
if (a.origin !== location.origin) {
|
172
|
+
if (corsEnabled(a.href)) {
|
173
|
+
download(blob, name, opts);
|
174
|
+
}
|
175
|
+
else {
|
176
|
+
a.target = '_blank';
|
177
|
+
click(a);
|
178
|
+
}
|
179
|
+
}
|
180
|
+
else {
|
181
|
+
click(a);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
else {
|
185
|
+
// Support blobs
|
186
|
+
a.href = URL.createObjectURL(blob);
|
187
|
+
setTimeout(function () {
|
188
|
+
URL.revokeObjectURL(a.href);
|
189
|
+
}, 4e4); // 40s
|
190
|
+
setTimeout(function () {
|
191
|
+
click(a);
|
192
|
+
}, 0);
|
193
|
+
}
|
194
|
+
}
|
195
|
+
function msSaveAs(blob, name = 'download', opts) {
|
196
|
+
if (typeof blob === 'string') {
|
197
|
+
if (corsEnabled(blob)) {
|
198
|
+
download(blob, name, opts);
|
199
|
+
}
|
200
|
+
else {
|
201
|
+
const a = document.createElement('a');
|
202
|
+
a.href = blob;
|
203
|
+
a.target = '_blank';
|
204
|
+
setTimeout(function () {
|
205
|
+
click(a);
|
206
|
+
});
|
207
|
+
}
|
208
|
+
}
|
209
|
+
else {
|
210
|
+
// @ts-ignore: works on windows
|
211
|
+
navigator.msSaveOrOpenBlob(bom(blob, opts), name);
|
212
|
+
}
|
213
|
+
}
|
214
|
+
function fileSaverSaveAs(blob, name, opts, popup) {
|
215
|
+
// Open a popup immediately do go around popup blocker
|
216
|
+
// Mostly only available on user interaction and the fileReader is async so...
|
217
|
+
popup = popup || open('', '_blank');
|
218
|
+
if (popup) {
|
219
|
+
popup.document.title = popup.document.body.innerText = 'downloading...';
|
220
|
+
}
|
221
|
+
if (typeof blob === 'string')
|
222
|
+
return download(blob, name, opts);
|
223
|
+
const force = blob.type === 'application/octet-stream';
|
224
|
+
const isSafari = /constructor/i.test(String(_global.HTMLElement)) || 'safari' in _global;
|
225
|
+
const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
|
226
|
+
if ((isChromeIOS || (force && isSafari) || isMacOSWebView) &&
|
227
|
+
typeof FileReader !== 'undefined') {
|
228
|
+
// Safari doesn't allow downloading of blob URLs
|
229
|
+
const reader = new FileReader();
|
230
|
+
reader.onloadend = function () {
|
231
|
+
let url = reader.result;
|
232
|
+
if (typeof url !== 'string') {
|
233
|
+
popup = null;
|
234
|
+
throw new Error('Wrong reader.result type');
|
235
|
+
}
|
236
|
+
url = isChromeIOS
|
237
|
+
? url
|
238
|
+
: url.replace(/^data:[^;]*;/, 'data:attachment/file;');
|
239
|
+
if (popup) {
|
240
|
+
popup.location.href = url;
|
241
|
+
}
|
242
|
+
else {
|
243
|
+
location.assign(url);
|
244
|
+
}
|
245
|
+
popup = null; // reverse-tabnabbing #460
|
246
|
+
};
|
247
|
+
reader.readAsDataURL(blob);
|
248
|
+
}
|
249
|
+
else {
|
250
|
+
const url = URL.createObjectURL(blob);
|
251
|
+
if (popup)
|
252
|
+
popup.location.assign(url);
|
253
|
+
else
|
254
|
+
location.href = url;
|
255
|
+
popup = null; // reverse-tabnabbing #460
|
256
|
+
setTimeout(function () {
|
257
|
+
URL.revokeObjectURL(url);
|
258
|
+
}, 4e4); // 40s
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
/**
|
263
|
+
* Shows a toast or console.log
|
264
|
+
*
|
265
|
+
* @param message - message to log
|
266
|
+
* @param type - different color of the tooltip
|
267
|
+
*/
|
268
|
+
function toastMessage(message, type) {
|
269
|
+
const piniaMessage = '🍍 ' + message;
|
270
|
+
if (typeof __VUE_DEVTOOLS_TOAST__ === 'function') {
|
271
|
+
// No longer available :(
|
272
|
+
__VUE_DEVTOOLS_TOAST__(piniaMessage, type);
|
273
|
+
}
|
274
|
+
else if (type === 'error') {
|
275
|
+
console.error(piniaMessage);
|
276
|
+
}
|
277
|
+
else if (type === 'warn') {
|
278
|
+
console.warn(piniaMessage);
|
279
|
+
}
|
280
|
+
else {
|
281
|
+
console.log(piniaMessage);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
function isPinia(o) {
|
285
|
+
return '_a' in o && 'install' in o;
|
286
|
+
}
|
287
|
+
|
288
|
+
/**
|
289
|
+
* This file contain devtools actions, they are not Pinia actions.
|
290
|
+
*/
|
291
|
+
// ---
|
292
|
+
function checkClipboardAccess() {
|
293
|
+
if (!('clipboard' in navigator)) {
|
294
|
+
toastMessage(`Your browser doesn't support the Clipboard API`, 'error');
|
295
|
+
return true;
|
296
|
+
}
|
297
|
+
}
|
298
|
+
function checkNotFocusedError(error) {
|
299
|
+
if (error instanceof Error &&
|
300
|
+
error.message.toLowerCase().includes('document is not focused')) {
|
301
|
+
toastMessage('You need to activate the "Emulate a focused page" setting in the "Rendering" panel of devtools.', 'warn');
|
302
|
+
return true;
|
303
|
+
}
|
304
|
+
return false;
|
305
|
+
}
|
306
|
+
async function actionGlobalCopyState(pinia) {
|
307
|
+
if (checkClipboardAccess())
|
308
|
+
return;
|
309
|
+
try {
|
310
|
+
await navigator.clipboard.writeText(JSON.stringify(pinia.state.value));
|
311
|
+
toastMessage('Global state copied to clipboard.');
|
312
|
+
}
|
313
|
+
catch (error) {
|
314
|
+
if (checkNotFocusedError(error))
|
315
|
+
return;
|
316
|
+
toastMessage(`Failed to serialize the state. Check the console for more details.`, 'error');
|
317
|
+
console.error(error);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
async function actionGlobalPasteState(pinia) {
|
321
|
+
if (checkClipboardAccess())
|
322
|
+
return;
|
323
|
+
try {
|
324
|
+
loadStoresState(pinia, JSON.parse(await navigator.clipboard.readText()));
|
325
|
+
toastMessage('Global state pasted from clipboard.');
|
326
|
+
}
|
327
|
+
catch (error) {
|
328
|
+
if (checkNotFocusedError(error))
|
329
|
+
return;
|
330
|
+
toastMessage(`Failed to deserialize the state from clipboard. Check the console for more details.`, 'error');
|
331
|
+
console.error(error);
|
332
|
+
}
|
333
|
+
}
|
334
|
+
async function actionGlobalSaveState(pinia) {
|
335
|
+
try {
|
336
|
+
saveAs(new Blob([JSON.stringify(pinia.state.value)], {
|
337
|
+
type: 'text/plain;charset=utf-8',
|
338
|
+
}), 'pinia-state.json');
|
339
|
+
}
|
340
|
+
catch (error) {
|
341
|
+
toastMessage(`Failed to export the state as JSON. Check the console for more details.`, 'error');
|
342
|
+
console.error(error);
|
343
|
+
}
|
344
|
+
}
|
345
|
+
let fileInput;
|
346
|
+
function getFileOpener() {
|
347
|
+
if (!fileInput) {
|
348
|
+
fileInput = document.createElement('input');
|
349
|
+
fileInput.type = 'file';
|
350
|
+
fileInput.accept = '.json';
|
351
|
+
}
|
352
|
+
function openFile() {
|
353
|
+
return new Promise((resolve, reject) => {
|
354
|
+
fileInput.onchange = async () => {
|
355
|
+
const files = fileInput.files;
|
356
|
+
if (!files)
|
357
|
+
return resolve(null);
|
358
|
+
const file = files.item(0);
|
359
|
+
if (!file)
|
360
|
+
return resolve(null);
|
361
|
+
return resolve({ text: await file.text(), file });
|
362
|
+
};
|
363
|
+
// @ts-ignore: TODO: changed from 4.3 to 4.4
|
364
|
+
fileInput.oncancel = () => resolve(null);
|
365
|
+
fileInput.onerror = reject;
|
366
|
+
fileInput.click();
|
367
|
+
});
|
368
|
+
}
|
369
|
+
return openFile;
|
370
|
+
}
|
371
|
+
async function actionGlobalOpenStateFile(pinia) {
|
372
|
+
try {
|
373
|
+
const open = getFileOpener();
|
374
|
+
const result = await open();
|
375
|
+
if (!result)
|
376
|
+
return;
|
377
|
+
const { text, file } = result;
|
378
|
+
loadStoresState(pinia, JSON.parse(text));
|
379
|
+
toastMessage(`Global state imported from "${file.name}".`);
|
380
|
+
}
|
381
|
+
catch (error) {
|
382
|
+
toastMessage(`Failed to import the state from JSON. Check the console for more details.`, 'error');
|
383
|
+
console.error(error);
|
384
|
+
}
|
385
|
+
}
|
386
|
+
function loadStoresState(pinia, state) {
|
387
|
+
for (const key in state) {
|
388
|
+
const storeState = pinia.state.value[key];
|
389
|
+
// store is already instantiated, patch it
|
390
|
+
if (storeState) {
|
391
|
+
Object.assign(storeState, state[key]);
|
392
|
+
}
|
393
|
+
else {
|
394
|
+
// store is not instantiated, set the initial state
|
395
|
+
pinia.state.value[key] = state[key];
|
396
|
+
}
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
function formatDisplay(display) {
|
401
|
+
return {
|
402
|
+
_custom: {
|
403
|
+
display,
|
404
|
+
},
|
405
|
+
};
|
406
|
+
}
|
407
|
+
const PINIA_ROOT_LABEL = '🍍 Pinia (root)';
|
408
|
+
const PINIA_ROOT_ID = '_root';
|
409
|
+
function formatStoreForInspectorTree(store) {
|
410
|
+
return isPinia(store)
|
411
|
+
? {
|
412
|
+
id: PINIA_ROOT_ID,
|
413
|
+
label: PINIA_ROOT_LABEL,
|
414
|
+
}
|
415
|
+
: {
|
416
|
+
id: store.$id,
|
417
|
+
label: store.$id,
|
418
|
+
};
|
419
|
+
}
|
420
|
+
function formatStoreForInspectorState(store) {
|
421
|
+
if (isPinia(store)) {
|
422
|
+
const storeNames = Array.from(store._s.keys());
|
423
|
+
const storeMap = store._s;
|
424
|
+
const state = {
|
425
|
+
state: storeNames.map((storeId) => ({
|
426
|
+
editable: true,
|
427
|
+
key: storeId,
|
428
|
+
value: store.state.value[storeId],
|
429
|
+
})),
|
430
|
+
getters: storeNames
|
431
|
+
.filter((id) => storeMap.get(id)._getters)
|
432
|
+
.map((id) => {
|
433
|
+
const store = storeMap.get(id);
|
434
|
+
return {
|
435
|
+
editable: false,
|
436
|
+
key: id,
|
437
|
+
value: store._getters.reduce((getters, key) => {
|
438
|
+
getters[key] = store[key];
|
439
|
+
return getters;
|
440
|
+
}, {}),
|
441
|
+
};
|
442
|
+
}),
|
443
|
+
};
|
444
|
+
return state;
|
445
|
+
}
|
446
|
+
const state = {
|
447
|
+
state: Object.keys(store.$state).map((key) => ({
|
448
|
+
editable: true,
|
449
|
+
key,
|
450
|
+
value: store.$state[key],
|
451
|
+
})),
|
452
|
+
};
|
453
|
+
// avoid adding empty getters
|
454
|
+
if (store._getters && store._getters.length) {
|
455
|
+
state.getters = store._getters.map((getterName) => ({
|
456
|
+
editable: false,
|
457
|
+
key: getterName,
|
458
|
+
value: store[getterName],
|
459
|
+
}));
|
460
|
+
}
|
461
|
+
if (store._customProperties.size) {
|
462
|
+
state.customProperties = Array.from(store._customProperties).map((key) => ({
|
463
|
+
editable: true,
|
464
|
+
key,
|
465
|
+
value: store[key],
|
466
|
+
}));
|
467
|
+
}
|
468
|
+
return state;
|
469
|
+
}
|
470
|
+
function formatEventData(events) {
|
471
|
+
if (!events)
|
472
|
+
return {};
|
473
|
+
if (Array.isArray(events)) {
|
474
|
+
// TODO: handle add and delete for arrays and objects
|
475
|
+
return events.reduce((data, event) => {
|
476
|
+
data.keys.push(event.key);
|
477
|
+
data.operations.push(event.type);
|
478
|
+
data.oldValue[event.key] = event.oldValue;
|
479
|
+
data.newValue[event.key] = event.newValue;
|
480
|
+
return data;
|
481
|
+
}, {
|
482
|
+
oldValue: {},
|
483
|
+
keys: [],
|
484
|
+
operations: [],
|
485
|
+
newValue: {},
|
486
|
+
});
|
487
|
+
}
|
488
|
+
else {
|
489
|
+
return {
|
490
|
+
operation: formatDisplay(events.type),
|
491
|
+
key: formatDisplay(events.key),
|
492
|
+
oldValue: events.oldValue,
|
493
|
+
newValue: events.newValue,
|
494
|
+
};
|
495
|
+
}
|
496
|
+
}
|
497
|
+
function formatMutationType(type) {
|
498
|
+
switch (type) {
|
499
|
+
case exports.MutationType.direct:
|
500
|
+
return 'mutation';
|
501
|
+
case exports.MutationType.patchFunction:
|
502
|
+
return '$patch';
|
503
|
+
case exports.MutationType.patchObject:
|
504
|
+
return '$patch';
|
505
|
+
default:
|
506
|
+
return 'unknown';
|
507
|
+
}
|
508
|
+
}
|
509
|
+
|
510
|
+
// timeline can be paused when directly changing the state
|
511
|
+
let isTimelineActive = true;
|
512
|
+
const componentStateTypes = [];
|
513
|
+
const MUTATIONS_LAYER_ID = 'pinia:mutations';
|
514
|
+
const INSPECTOR_ID = 'pinia';
|
515
|
+
const { assign: assign$1 } = Object;
|
516
|
+
/**
|
517
|
+
* Gets the displayed name of a store in devtools
|
518
|
+
*
|
519
|
+
* @param id - id of the store
|
520
|
+
* @returns a formatted string
|
521
|
+
*/
|
522
|
+
const getStoreType = (id) => '🍍 ' + id;
|
523
|
+
/**
|
524
|
+
* Add the pinia plugin without any store. Allows displaying a Pinia plugin tab
|
525
|
+
* as soon as it is added to the application.
|
526
|
+
*
|
527
|
+
* @param app - Vue application
|
528
|
+
* @param pinia - pinia instance
|
529
|
+
*/
|
530
|
+
function registerPiniaDevtools(app, pinia) {
|
531
|
+
devtoolsApi.setupDevtoolsPlugin({
|
532
|
+
id: 'dev.esm.pinia',
|
533
|
+
label: 'Pinia 🍍',
|
534
|
+
logo: 'https://pinia.vuejs.org/logo.svg',
|
535
|
+
packageName: 'pinia',
|
536
|
+
homepage: 'https://pinia.vuejs.org',
|
537
|
+
componentStateTypes,
|
538
|
+
app,
|
539
|
+
}, (api) => {
|
540
|
+
if (typeof api.now !== 'function') {
|
541
|
+
toastMessage('You seem to be using an outdated version of Vue Devtools. Are you still using the Beta release instead of the stable one? You can find the links at https://devtools.vuejs.org/guide/installation.html.');
|
542
|
+
}
|
543
|
+
api.addTimelineLayer({
|
544
|
+
id: MUTATIONS_LAYER_ID,
|
545
|
+
label: `Pinia 🍍`,
|
546
|
+
color: 0xe5df88,
|
547
|
+
});
|
548
|
+
api.addInspector({
|
549
|
+
id: INSPECTOR_ID,
|
550
|
+
label: 'Pinia 🍍',
|
551
|
+
icon: 'storage',
|
552
|
+
treeFilterPlaceholder: 'Search stores',
|
553
|
+
actions: [
|
554
|
+
{
|
555
|
+
icon: 'content_copy',
|
556
|
+
action: () => {
|
557
|
+
actionGlobalCopyState(pinia);
|
558
|
+
},
|
559
|
+
tooltip: 'Serialize and copy the state',
|
560
|
+
},
|
561
|
+
{
|
562
|
+
icon: 'content_paste',
|
563
|
+
action: async () => {
|
564
|
+
await actionGlobalPasteState(pinia);
|
565
|
+
api.sendInspectorTree(INSPECTOR_ID);
|
566
|
+
api.sendInspectorState(INSPECTOR_ID);
|
567
|
+
},
|
568
|
+
tooltip: 'Replace the state with the content of your clipboard',
|
569
|
+
},
|
570
|
+
{
|
571
|
+
icon: 'save',
|
572
|
+
action: () => {
|
573
|
+
actionGlobalSaveState(pinia);
|
574
|
+
},
|
575
|
+
tooltip: 'Save the state as a JSON file',
|
576
|
+
},
|
577
|
+
{
|
578
|
+
icon: 'folder_open',
|
579
|
+
action: async () => {
|
580
|
+
await actionGlobalOpenStateFile(pinia);
|
581
|
+
api.sendInspectorTree(INSPECTOR_ID);
|
582
|
+
api.sendInspectorState(INSPECTOR_ID);
|
583
|
+
},
|
584
|
+
tooltip: 'Import the state from a JSON file',
|
585
|
+
},
|
586
|
+
],
|
587
|
+
nodeActions: [
|
588
|
+
{
|
589
|
+
icon: 'restore',
|
590
|
+
tooltip: 'Reset the state (with "$reset")',
|
591
|
+
action: (nodeId) => {
|
592
|
+
const store = pinia._s.get(nodeId);
|
593
|
+
if (!store) {
|
594
|
+
toastMessage(`Cannot reset "${nodeId}" store because it wasn't found.`, 'warn');
|
595
|
+
}
|
596
|
+
else if (typeof store.$reset !== 'function') {
|
597
|
+
toastMessage(`Cannot reset "${nodeId}" store because it doesn't have a "$reset" method implemented.`, 'warn');
|
598
|
+
}
|
599
|
+
else {
|
600
|
+
store.$reset();
|
601
|
+
toastMessage(`Store "${nodeId}" reset.`);
|
602
|
+
}
|
603
|
+
},
|
604
|
+
},
|
605
|
+
],
|
606
|
+
});
|
607
|
+
api.on.inspectComponent((payload) => {
|
608
|
+
const proxy = (payload.componentInstance &&
|
609
|
+
payload.componentInstance.proxy);
|
610
|
+
if (proxy && proxy._pStores) {
|
611
|
+
const piniaStores = payload.componentInstance.proxy._pStores;
|
612
|
+
Object.values(piniaStores).forEach((store) => {
|
613
|
+
payload.instanceData.state.push({
|
614
|
+
type: getStoreType(store.$id),
|
615
|
+
key: 'state',
|
616
|
+
editable: true,
|
617
|
+
value: store._isOptionsAPI
|
618
|
+
? {
|
619
|
+
_custom: {
|
620
|
+
value: vue.toRaw(store.$state),
|
621
|
+
actions: [
|
622
|
+
{
|
623
|
+
icon: 'restore',
|
624
|
+
tooltip: 'Reset the state of this store',
|
625
|
+
action: () => store.$reset(),
|
626
|
+
},
|
627
|
+
],
|
628
|
+
},
|
629
|
+
}
|
630
|
+
: // NOTE: workaround to unwrap transferred refs
|
631
|
+
Object.keys(store.$state).reduce((state, key) => {
|
632
|
+
state[key] = store.$state[key];
|
633
|
+
return state;
|
634
|
+
}, {}),
|
635
|
+
});
|
636
|
+
if (store._getters && store._getters.length) {
|
637
|
+
payload.instanceData.state.push({
|
638
|
+
type: getStoreType(store.$id),
|
639
|
+
key: 'getters',
|
640
|
+
editable: false,
|
641
|
+
value: store._getters.reduce((getters, key) => {
|
642
|
+
try {
|
643
|
+
getters[key] = store[key];
|
644
|
+
}
|
645
|
+
catch (error) {
|
646
|
+
// @ts-expect-error: we just want to show it in devtools
|
647
|
+
getters[key] = error;
|
648
|
+
}
|
649
|
+
return getters;
|
650
|
+
}, {}),
|
651
|
+
});
|
652
|
+
}
|
653
|
+
});
|
654
|
+
}
|
655
|
+
});
|
656
|
+
api.on.getInspectorTree((payload) => {
|
657
|
+
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
|
658
|
+
let stores = [pinia];
|
659
|
+
stores = stores.concat(Array.from(pinia._s.values()));
|
660
|
+
payload.rootNodes = (payload.filter
|
661
|
+
? stores.filter((store) => '$id' in store
|
662
|
+
? store.$id
|
663
|
+
.toLowerCase()
|
664
|
+
.includes(payload.filter.toLowerCase())
|
665
|
+
: PINIA_ROOT_LABEL.toLowerCase().includes(payload.filter.toLowerCase()))
|
666
|
+
: stores).map(formatStoreForInspectorTree);
|
667
|
+
}
|
668
|
+
});
|
669
|
+
// Expose pinia instance as $pinia to window
|
670
|
+
globalThis.$pinia = pinia;
|
671
|
+
api.on.getInspectorState((payload) => {
|
672
|
+
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
|
673
|
+
const inspectedStore = payload.nodeId === PINIA_ROOT_ID
|
674
|
+
? pinia
|
675
|
+
: pinia._s.get(payload.nodeId);
|
676
|
+
if (!inspectedStore) {
|
677
|
+
// this could be the selected store restored for a different project
|
678
|
+
// so it's better not to say anything here
|
679
|
+
return;
|
680
|
+
}
|
681
|
+
if (inspectedStore) {
|
682
|
+
// Expose selected store as $store to window
|
683
|
+
if (payload.nodeId !== PINIA_ROOT_ID)
|
684
|
+
globalThis.$store = vue.toRaw(inspectedStore);
|
685
|
+
payload.state = formatStoreForInspectorState(inspectedStore);
|
686
|
+
}
|
687
|
+
}
|
688
|
+
});
|
689
|
+
api.on.editInspectorState((payload) => {
|
690
|
+
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) {
|
691
|
+
const inspectedStore = payload.nodeId === PINIA_ROOT_ID
|
692
|
+
? pinia
|
693
|
+
: pinia._s.get(payload.nodeId);
|
694
|
+
if (!inspectedStore) {
|
695
|
+
return toastMessage(`store "${payload.nodeId}" not found`, 'error');
|
696
|
+
}
|
697
|
+
const { path } = payload;
|
698
|
+
if (!isPinia(inspectedStore)) {
|
699
|
+
// access only the state
|
700
|
+
if (path.length !== 1 ||
|
701
|
+
!inspectedStore._customProperties.has(path[0]) ||
|
702
|
+
path[0] in inspectedStore.$state) {
|
703
|
+
path.unshift('$state');
|
704
|
+
}
|
705
|
+
}
|
706
|
+
else {
|
707
|
+
// Root access, we can omit the `.value` because the devtools API does it for us
|
708
|
+
path.unshift('state');
|
709
|
+
}
|
710
|
+
isTimelineActive = false;
|
711
|
+
payload.set(inspectedStore, path, payload.state.value);
|
712
|
+
isTimelineActive = true;
|
713
|
+
}
|
714
|
+
});
|
715
|
+
api.on.editComponentState((payload) => {
|
716
|
+
if (payload.type.startsWith('🍍')) {
|
717
|
+
const storeId = payload.type.replace(/^🍍\s*/, '');
|
718
|
+
const store = pinia._s.get(storeId);
|
719
|
+
if (!store) {
|
720
|
+
return toastMessage(`store "${storeId}" not found`, 'error');
|
721
|
+
}
|
722
|
+
const { path } = payload;
|
723
|
+
if (path[0] !== 'state') {
|
724
|
+
return toastMessage(`Invalid path for store "${storeId}":\n${path}\nOnly state can be modified.`);
|
725
|
+
}
|
726
|
+
// rewrite the first entry to be able to directly set the state as
|
727
|
+
// well as any other path
|
728
|
+
path[0] = '$state';
|
729
|
+
isTimelineActive = false;
|
730
|
+
payload.set(store, path, payload.state.value);
|
731
|
+
isTimelineActive = true;
|
732
|
+
}
|
733
|
+
});
|
734
|
+
});
|
735
|
+
}
|
736
|
+
function addStoreToDevtools(app, store) {
|
737
|
+
if (!componentStateTypes.includes(getStoreType(store.$id))) {
|
738
|
+
componentStateTypes.push(getStoreType(store.$id));
|
739
|
+
}
|
740
|
+
devtoolsApi.setupDevtoolsPlugin({
|
741
|
+
id: 'dev.esm.pinia',
|
742
|
+
label: 'Pinia 🍍',
|
743
|
+
logo: 'https://pinia.vuejs.org/logo.svg',
|
744
|
+
packageName: 'pinia',
|
745
|
+
homepage: 'https://pinia.vuejs.org',
|
746
|
+
componentStateTypes,
|
747
|
+
app,
|
748
|
+
settings: {
|
749
|
+
logStoreChanges: {
|
750
|
+
label: 'Notify about new/deleted stores',
|
751
|
+
type: 'boolean',
|
752
|
+
defaultValue: true,
|
753
|
+
},
|
754
|
+
// useEmojis: {
|
755
|
+
// label: 'Use emojis in messages ⚡️',
|
756
|
+
// type: 'boolean',
|
757
|
+
// defaultValue: true,
|
758
|
+
// },
|
759
|
+
},
|
760
|
+
}, (api) => {
|
761
|
+
// gracefully handle errors
|
762
|
+
const now = typeof api.now === 'function' ? api.now.bind(api) : Date.now;
|
763
|
+
store.$onAction(({ after, onError, name, args }) => {
|
764
|
+
const groupId = runningActionId++;
|
765
|
+
api.addTimelineEvent({
|
766
|
+
layerId: MUTATIONS_LAYER_ID,
|
767
|
+
event: {
|
768
|
+
time: now(),
|
769
|
+
title: '🛫 ' + name,
|
770
|
+
subtitle: 'start',
|
771
|
+
data: {
|
772
|
+
store: formatDisplay(store.$id),
|
773
|
+
action: formatDisplay(name),
|
774
|
+
args,
|
775
|
+
},
|
776
|
+
groupId,
|
777
|
+
},
|
778
|
+
});
|
779
|
+
after((result) => {
|
780
|
+
activeAction = undefined;
|
781
|
+
api.addTimelineEvent({
|
782
|
+
layerId: MUTATIONS_LAYER_ID,
|
783
|
+
event: {
|
784
|
+
time: now(),
|
785
|
+
title: '🛬 ' + name,
|
786
|
+
subtitle: 'end',
|
787
|
+
data: {
|
788
|
+
store: formatDisplay(store.$id),
|
789
|
+
action: formatDisplay(name),
|
790
|
+
args,
|
791
|
+
result,
|
792
|
+
},
|
793
|
+
groupId,
|
794
|
+
},
|
795
|
+
});
|
796
|
+
});
|
797
|
+
onError((error) => {
|
798
|
+
activeAction = undefined;
|
799
|
+
api.addTimelineEvent({
|
800
|
+
layerId: MUTATIONS_LAYER_ID,
|
801
|
+
event: {
|
802
|
+
time: now(),
|
803
|
+
logType: 'error',
|
804
|
+
title: '💥 ' + name,
|
805
|
+
subtitle: 'end',
|
806
|
+
data: {
|
807
|
+
store: formatDisplay(store.$id),
|
808
|
+
action: formatDisplay(name),
|
809
|
+
args,
|
810
|
+
error,
|
811
|
+
},
|
812
|
+
groupId,
|
813
|
+
},
|
814
|
+
});
|
815
|
+
});
|
816
|
+
}, true);
|
817
|
+
store._customProperties.forEach((name) => {
|
818
|
+
vue.watch(() => vue.unref(store[name]), (newValue, oldValue) => {
|
819
|
+
api.notifyComponentUpdate();
|
820
|
+
api.sendInspectorState(INSPECTOR_ID);
|
821
|
+
if (isTimelineActive) {
|
822
|
+
api.addTimelineEvent({
|
823
|
+
layerId: MUTATIONS_LAYER_ID,
|
824
|
+
event: {
|
825
|
+
time: now(),
|
826
|
+
title: 'Change',
|
827
|
+
subtitle: name,
|
828
|
+
data: {
|
829
|
+
newValue,
|
830
|
+
oldValue,
|
831
|
+
},
|
832
|
+
groupId: activeAction,
|
833
|
+
},
|
834
|
+
});
|
835
|
+
}
|
836
|
+
}, { deep: true });
|
837
|
+
});
|
838
|
+
store.$subscribe(({ events, type }, state) => {
|
839
|
+
api.notifyComponentUpdate();
|
840
|
+
api.sendInspectorState(INSPECTOR_ID);
|
841
|
+
if (!isTimelineActive)
|
842
|
+
return;
|
843
|
+
// rootStore.state[store.id] = state
|
844
|
+
const eventData = {
|
845
|
+
time: now(),
|
846
|
+
title: formatMutationType(type),
|
847
|
+
data: assign$1({ store: formatDisplay(store.$id) }, formatEventData(events)),
|
848
|
+
groupId: activeAction,
|
849
|
+
};
|
850
|
+
if (type === exports.MutationType.patchFunction) {
|
851
|
+
eventData.subtitle = '⤵️';
|
852
|
+
}
|
853
|
+
else if (type === exports.MutationType.patchObject) {
|
854
|
+
eventData.subtitle = '🧩';
|
855
|
+
}
|
856
|
+
else if (events && !Array.isArray(events)) {
|
857
|
+
eventData.subtitle = events.type;
|
858
|
+
}
|
859
|
+
if (events) {
|
860
|
+
eventData.data['rawEvent(s)'] = {
|
861
|
+
_custom: {
|
862
|
+
display: 'DebuggerEvent',
|
863
|
+
type: 'object',
|
864
|
+
tooltip: 'raw DebuggerEvent[]',
|
865
|
+
value: events,
|
866
|
+
},
|
867
|
+
};
|
868
|
+
}
|
869
|
+
api.addTimelineEvent({
|
870
|
+
layerId: MUTATIONS_LAYER_ID,
|
871
|
+
event: eventData,
|
872
|
+
});
|
873
|
+
}, { detached: true, flush: 'sync' });
|
874
|
+
const hotUpdate = store._hotUpdate;
|
875
|
+
store._hotUpdate = vue.markRaw((newStore) => {
|
876
|
+
hotUpdate(newStore);
|
877
|
+
api.addTimelineEvent({
|
878
|
+
layerId: MUTATIONS_LAYER_ID,
|
879
|
+
event: {
|
880
|
+
time: now(),
|
881
|
+
title: '🔥 ' + store.$id,
|
882
|
+
subtitle: 'HMR update',
|
883
|
+
data: {
|
884
|
+
store: formatDisplay(store.$id),
|
885
|
+
info: formatDisplay(`HMR update`),
|
886
|
+
},
|
887
|
+
},
|
888
|
+
});
|
889
|
+
// update the devtools too
|
890
|
+
api.notifyComponentUpdate();
|
891
|
+
api.sendInspectorTree(INSPECTOR_ID);
|
892
|
+
api.sendInspectorState(INSPECTOR_ID);
|
893
|
+
});
|
894
|
+
const { $dispose } = store;
|
895
|
+
store.$dispose = () => {
|
896
|
+
$dispose();
|
897
|
+
api.notifyComponentUpdate();
|
898
|
+
api.sendInspectorTree(INSPECTOR_ID);
|
899
|
+
api.sendInspectorState(INSPECTOR_ID);
|
900
|
+
api.getSettings().logStoreChanges &&
|
901
|
+
toastMessage(`Disposed "${store.$id}" store 🗑`);
|
902
|
+
};
|
903
|
+
// trigger an update so it can display new registered stores
|
904
|
+
api.notifyComponentUpdate();
|
905
|
+
api.sendInspectorTree(INSPECTOR_ID);
|
906
|
+
api.sendInspectorState(INSPECTOR_ID);
|
907
|
+
api.getSettings().logStoreChanges &&
|
908
|
+
toastMessage(`"${store.$id}" store installed 🆕`);
|
909
|
+
});
|
910
|
+
}
|
911
|
+
let runningActionId = 0;
|
912
|
+
let activeAction;
|
913
|
+
/**
|
914
|
+
* Patches a store to enable action grouping in devtools by wrapping the store with a Proxy that is passed as the
|
915
|
+
* context of all actions, allowing us to set `runningAction` on each access and effectively associating any state
|
916
|
+
* mutation to the action.
|
917
|
+
*
|
918
|
+
* @param store - store to patch
|
919
|
+
* @param actionNames - list of actionst to patch
|
920
|
+
*/
|
921
|
+
function patchActionForGrouping(store, actionNames, wrapWithProxy) {
|
922
|
+
// original actions of the store as they are given by pinia. We are going to override them
|
923
|
+
const actions = actionNames.reduce((storeActions, actionName) => {
|
924
|
+
// use toRaw to avoid tracking #541
|
925
|
+
storeActions[actionName] = vue.toRaw(store)[actionName];
|
926
|
+
return storeActions;
|
927
|
+
}, {});
|
928
|
+
for (const actionName in actions) {
|
929
|
+
store[actionName] = function () {
|
930
|
+
// the running action id is incremented in a before action hook
|
931
|
+
const _actionId = runningActionId;
|
932
|
+
const trackedStore = wrapWithProxy
|
933
|
+
? new Proxy(store, {
|
934
|
+
get(...args) {
|
935
|
+
activeAction = _actionId;
|
936
|
+
return Reflect.get(...args);
|
937
|
+
},
|
938
|
+
set(...args) {
|
939
|
+
activeAction = _actionId;
|
940
|
+
return Reflect.set(...args);
|
941
|
+
},
|
942
|
+
})
|
943
|
+
: store;
|
944
|
+
// For Setup Stores we need https://github.com/tc39/proposal-async-context
|
945
|
+
activeAction = _actionId;
|
946
|
+
const retValue = actions[actionName].apply(trackedStore, arguments);
|
947
|
+
// this is safer as async actions in Setup Stores would associate mutations done outside of the action
|
948
|
+
activeAction = undefined;
|
949
|
+
return retValue;
|
950
|
+
};
|
951
|
+
}
|
952
|
+
}
|
953
|
+
/**
|
954
|
+
* pinia.use(devtoolsPlugin)
|
955
|
+
*/
|
956
|
+
function devtoolsPlugin({ app, store, options }) {
|
957
|
+
// HMR module
|
958
|
+
if (store.$id.startsWith('__hot:')) {
|
959
|
+
return;
|
960
|
+
}
|
961
|
+
// detect option api vs setup api
|
962
|
+
store._isOptionsAPI = !!options.state;
|
963
|
+
// Do not overwrite actions mocked by @pinia/testing (#2298)
|
964
|
+
if (!store._p._testing) {
|
965
|
+
patchActionForGrouping(store, Object.keys(options.actions), store._isOptionsAPI);
|
966
|
+
// Upgrade the HMR to also update the new actions
|
967
|
+
const originalHotUpdate = store._hotUpdate;
|
968
|
+
vue.toRaw(store)._hotUpdate = function (newStore) {
|
969
|
+
originalHotUpdate.apply(this, arguments);
|
970
|
+
patchActionForGrouping(store, Object.keys(newStore._hmrPayload.actions), !!store._isOptionsAPI);
|
971
|
+
};
|
972
|
+
}
|
973
|
+
addStoreToDevtools(app,
|
974
|
+
// FIXME: is there a way to allow the assignment from Store<Id, S, G, A> to StoreGeneric?
|
975
|
+
store);
|
976
|
+
}
|
977
|
+
|
978
|
+
/**
|
979
|
+
* Creates a Pinia instance to be used by the application
|
980
|
+
*/
|
981
|
+
function createPinia() {
|
982
|
+
const scope = vue.effectScope(true);
|
983
|
+
// NOTE: here we could check the window object for a state and directly set it
|
984
|
+
// if there is anything like it with Vue 3 SSR
|
985
|
+
const state = scope.run(() => vue.ref({}));
|
986
|
+
let _p = [];
|
987
|
+
// plugins added before calling app.use(pinia)
|
988
|
+
let toBeInstalled = [];
|
989
|
+
const pinia = vue.markRaw({
|
990
|
+
install(app) {
|
991
|
+
// this allows calling useStore() outside of a component setup after
|
992
|
+
// installing pinia's plugin
|
993
|
+
setActivePinia(pinia);
|
994
|
+
pinia._a = app;
|
995
|
+
app.provide(piniaSymbol, pinia);
|
996
|
+
app.config.globalProperties.$pinia = pinia;
|
997
|
+
/* istanbul ignore else */
|
998
|
+
if (IS_CLIENT) {
|
999
|
+
registerPiniaDevtools(app, pinia);
|
1000
|
+
}
|
1001
|
+
toBeInstalled.forEach((plugin) => _p.push(plugin));
|
1002
|
+
toBeInstalled = [];
|
1003
|
+
},
|
1004
|
+
use(plugin) {
|
1005
|
+
if (!this._a) {
|
1006
|
+
toBeInstalled.push(plugin);
|
1007
|
+
}
|
1008
|
+
else {
|
1009
|
+
_p.push(plugin);
|
1010
|
+
}
|
1011
|
+
return this;
|
1012
|
+
},
|
1013
|
+
_p,
|
1014
|
+
// it's actually undefined here
|
1015
|
+
// @ts-expect-error
|
1016
|
+
_a: null,
|
1017
|
+
_e: scope,
|
1018
|
+
_s: new Map(),
|
1019
|
+
state,
|
1020
|
+
});
|
1021
|
+
// pinia devtools rely on dev only features so they cannot be forced unless
|
1022
|
+
// the dev build of Vue is used. Avoid old browsers like IE11.
|
1023
|
+
if (IS_CLIENT && typeof Proxy !== 'undefined') {
|
1024
|
+
pinia.use(devtoolsPlugin);
|
1025
|
+
}
|
1026
|
+
return pinia;
|
1027
|
+
}
|
1028
|
+
/**
|
1029
|
+
* Dispose a Pinia instance by stopping its effectScope and removing the state, plugins and stores. This is mostly
|
1030
|
+
* useful in tests, with both a testing pinia or a regular pinia and in applications that use multiple pinia instances.
|
1031
|
+
* Once disposed, the pinia instance cannot be used anymore.
|
1032
|
+
*
|
1033
|
+
* @param pinia - pinia instance
|
1034
|
+
*/
|
1035
|
+
function disposePinia(pinia) {
|
1036
|
+
pinia._e.stop();
|
1037
|
+
pinia._s.clear();
|
1038
|
+
pinia._p.splice(0);
|
1039
|
+
pinia.state.value = {};
|
1040
|
+
// @ts-expect-error: non valid
|
1041
|
+
pinia._a = null;
|
1042
|
+
}
|
1043
|
+
|
1044
|
+
/**
|
1045
|
+
* Checks if a function is a `StoreDefinition`.
|
1046
|
+
*
|
1047
|
+
* @param fn - object to test
|
1048
|
+
* @returns true if `fn` is a StoreDefinition
|
1049
|
+
*/
|
1050
|
+
const isUseStore = (fn) => {
|
1051
|
+
return typeof fn === 'function' && typeof fn.$id === 'string';
|
1052
|
+
};
|
1053
|
+
/**
|
1054
|
+
* Mutates in place `newState` with `oldState` to _hot update_ it. It will
|
1055
|
+
* remove any key not existing in `newState` and recursively merge plain
|
1056
|
+
* objects.
|
1057
|
+
*
|
1058
|
+
* @param newState - new state object to be patched
|
1059
|
+
* @param oldState - old state that should be used to patch newState
|
1060
|
+
* @returns - newState
|
1061
|
+
*/
|
1062
|
+
function patchObject(newState, oldState) {
|
1063
|
+
// no need to go through symbols because they cannot be serialized anyway
|
1064
|
+
for (const key in oldState) {
|
1065
|
+
const subPatch = oldState[key];
|
1066
|
+
// skip the whole sub tree
|
1067
|
+
if (!(key in newState)) {
|
1068
|
+
continue;
|
1069
|
+
}
|
1070
|
+
const targetValue = newState[key];
|
1071
|
+
if (isPlainObject(targetValue) &&
|
1072
|
+
isPlainObject(subPatch) &&
|
1073
|
+
!vue.isRef(subPatch) &&
|
1074
|
+
!vue.isReactive(subPatch)) {
|
1075
|
+
newState[key] = patchObject(targetValue, subPatch);
|
1076
|
+
}
|
1077
|
+
else {
|
1078
|
+
// objects are either a bit more complex (e.g. refs) or primitives, so we
|
1079
|
+
// just set the whole thing
|
1080
|
+
newState[key] = subPatch;
|
1081
|
+
}
|
1082
|
+
}
|
1083
|
+
return newState;
|
1084
|
+
}
|
1085
|
+
/**
|
1086
|
+
* Creates an _accept_ function to pass to `import.meta.hot` in Vite applications.
|
1087
|
+
*
|
1088
|
+
* @example
|
1089
|
+
* ```js
|
1090
|
+
* const useUser = defineStore(...)
|
1091
|
+
* if (import.meta.hot) {
|
1092
|
+
* import.meta.hot.accept(acceptHMRUpdate(useUser, import.meta.hot))
|
1093
|
+
* }
|
1094
|
+
* ```
|
1095
|
+
*
|
1096
|
+
* @param initialUseStore - return of the defineStore to hot update
|
1097
|
+
* @param hot - `import.meta.hot`
|
1098
|
+
*/
|
1099
|
+
function acceptHMRUpdate(initialUseStore, hot) {
|
1100
|
+
return (newModule) => {
|
1101
|
+
const pinia = hot.data.pinia || initialUseStore._pinia;
|
1102
|
+
if (!pinia) {
|
1103
|
+
// this store is still not used
|
1104
|
+
return;
|
1105
|
+
}
|
1106
|
+
// preserve the pinia instance across loads
|
1107
|
+
hot.data.pinia = pinia;
|
1108
|
+
// console.log('got data', newStore)
|
1109
|
+
for (const exportName in newModule) {
|
1110
|
+
const useStore = newModule[exportName];
|
1111
|
+
// console.log('checking for', exportName)
|
1112
|
+
if (isUseStore(useStore) && pinia._s.has(useStore.$id)) {
|
1113
|
+
// console.log('Accepting update for', useStore.$id)
|
1114
|
+
const id = useStore.$id;
|
1115
|
+
if (id !== initialUseStore.$id) {
|
1116
|
+
console.warn(`The id of the store changed from "${initialUseStore.$id}" to "${id}". Reloading.`);
|
1117
|
+
// return import.meta.hot.invalidate()
|
1118
|
+
return hot.invalidate();
|
1119
|
+
}
|
1120
|
+
const existingStore = pinia._s.get(id);
|
1121
|
+
if (!existingStore) {
|
1122
|
+
console.log(`[Pinia]: skipping hmr because store doesn't exist yet`);
|
1123
|
+
return;
|
1124
|
+
}
|
1125
|
+
useStore(pinia, existingStore);
|
1126
|
+
}
|
1127
|
+
}
|
1128
|
+
};
|
1129
|
+
}
|
1130
|
+
|
1131
|
+
const noop = () => { };
|
1132
|
+
function addSubscription(subscriptions, callback, detached, onCleanup = noop) {
|
1133
|
+
subscriptions.push(callback);
|
1134
|
+
const removeSubscription = () => {
|
1135
|
+
const idx = subscriptions.indexOf(callback);
|
1136
|
+
if (idx > -1) {
|
1137
|
+
subscriptions.splice(idx, 1);
|
1138
|
+
onCleanup();
|
1139
|
+
}
|
1140
|
+
};
|
1141
|
+
if (!detached && vue.getCurrentScope()) {
|
1142
|
+
vue.onScopeDispose(removeSubscription);
|
1143
|
+
}
|
1144
|
+
return removeSubscription;
|
1145
|
+
}
|
1146
|
+
function triggerSubscriptions(subscriptions, ...args) {
|
1147
|
+
subscriptions.slice().forEach((callback) => {
|
1148
|
+
callback(...args);
|
1149
|
+
});
|
1150
|
+
}
|
1151
|
+
|
1152
|
+
const fallbackRunWithContext = (fn) => fn();
|
1153
|
+
/**
|
1154
|
+
* Marks a function as an action for `$onAction`
|
1155
|
+
* @internal
|
1156
|
+
*/
|
1157
|
+
const ACTION_MARKER = Symbol();
|
1158
|
+
/**
|
1159
|
+
* Action name symbol. Allows to add a name to an action after defining it
|
1160
|
+
* @internal
|
1161
|
+
*/
|
1162
|
+
const ACTION_NAME = Symbol();
|
1163
|
+
function mergeReactiveObjects(target, patchToApply) {
|
1164
|
+
// Handle Map instances
|
1165
|
+
if (target instanceof Map && patchToApply instanceof Map) {
|
1166
|
+
patchToApply.forEach((value, key) => target.set(key, value));
|
1167
|
+
}
|
1168
|
+
else if (target instanceof Set && patchToApply instanceof Set) {
|
1169
|
+
// Handle Set instances
|
1170
|
+
patchToApply.forEach(target.add, target);
|
1171
|
+
}
|
1172
|
+
// no need to go through symbols because they cannot be serialized anyway
|
1173
|
+
for (const key in patchToApply) {
|
1174
|
+
if (!patchToApply.hasOwnProperty(key))
|
1175
|
+
continue;
|
1176
|
+
const subPatch = patchToApply[key];
|
1177
|
+
const targetValue = target[key];
|
1178
|
+
if (isPlainObject(targetValue) &&
|
1179
|
+
isPlainObject(subPatch) &&
|
1180
|
+
target.hasOwnProperty(key) &&
|
1181
|
+
!vue.isRef(subPatch) &&
|
1182
|
+
!vue.isReactive(subPatch)) {
|
1183
|
+
// NOTE: here I wanted to warn about inconsistent types but it's not possible because in setup stores one might
|
1184
|
+
// start the value of a property as a certain type e.g. a Map, and then for some reason, during SSR, change that
|
1185
|
+
// to `undefined`. When trying to hydrate, we want to override the Map with `undefined`.
|
1186
|
+
target[key] = mergeReactiveObjects(targetValue, subPatch);
|
1187
|
+
}
|
1188
|
+
else {
|
1189
|
+
// @ts-expect-error: subPatch is a valid value
|
1190
|
+
target[key] = subPatch;
|
1191
|
+
}
|
1192
|
+
}
|
1193
|
+
return target;
|
1194
|
+
}
|
1195
|
+
const skipHydrateSymbol = Symbol('pinia:skipHydration')
|
1196
|
+
;
|
1197
|
+
/**
|
1198
|
+
* Tells Pinia to skip the hydration process of a given object. This is useful in setup stores (only) when you return a
|
1199
|
+
* stateful object in the store but it isn't really state. e.g. returning a router instance in a setup store.
|
1200
|
+
*
|
1201
|
+
* @param obj - target object
|
1202
|
+
* @returns obj
|
1203
|
+
*/
|
1204
|
+
function skipHydrate(obj) {
|
1205
|
+
return Object.defineProperty(obj, skipHydrateSymbol, {});
|
1206
|
+
}
|
1207
|
+
/**
|
1208
|
+
* Returns whether a value should be hydrated
|
1209
|
+
*
|
1210
|
+
* @param obj - target variable
|
1211
|
+
* @returns true if `obj` should be hydrated
|
1212
|
+
*/
|
1213
|
+
function shouldHydrate(obj) {
|
1214
|
+
return (!isPlainObject(obj) ||
|
1215
|
+
!Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol));
|
1216
|
+
}
|
1217
|
+
const { assign } = Object;
|
1218
|
+
function isComputed(o) {
|
1219
|
+
return !!(vue.isRef(o) && o.effect);
|
1220
|
+
}
|
1221
|
+
function createOptionsStore(id, options, pinia, hot) {
|
1222
|
+
const { state, actions, getters } = options;
|
1223
|
+
const initialState = pinia.state.value[id];
|
1224
|
+
let store;
|
1225
|
+
function setup() {
|
1226
|
+
if (!initialState && (!hot)) {
|
1227
|
+
/* istanbul ignore if */
|
1228
|
+
pinia.state.value[id] = state ? state() : {};
|
1229
|
+
}
|
1230
|
+
// avoid creating a state in pinia.state.value
|
1231
|
+
const localState = hot
|
1232
|
+
? // use ref() to unwrap refs inside state TODO: check if this is still necessary
|
1233
|
+
vue.toRefs(vue.ref(state ? state() : {}).value)
|
1234
|
+
: vue.toRefs(pinia.state.value[id]);
|
1235
|
+
return assign(localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => {
|
1236
|
+
if (name in localState) {
|
1237
|
+
console.warn(`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".`);
|
1238
|
+
}
|
1239
|
+
computedGetters[name] = vue.markRaw(vue.computed(() => {
|
1240
|
+
setActivePinia(pinia);
|
1241
|
+
// it was created just before
|
1242
|
+
const store = pinia._s.get(id);
|
1243
|
+
// allow cross using stores
|
1244
|
+
// @ts-expect-error
|
1245
|
+
// return getters![name].call(context, context)
|
1246
|
+
// TODO: avoid reading the getter while assigning with a global variable
|
1247
|
+
return getters[name].call(store, store);
|
1248
|
+
}));
|
1249
|
+
return computedGetters;
|
1250
|
+
}, {}));
|
1251
|
+
}
|
1252
|
+
store = createSetupStore(id, setup, options, pinia, hot, true);
|
1253
|
+
return store;
|
1254
|
+
}
|
1255
|
+
function createSetupStore($id, setup, options = {}, pinia, hot, isOptionsStore) {
|
1256
|
+
let scope;
|
1257
|
+
const optionsForPlugin = assign({ actions: {} }, options);
|
1258
|
+
/* istanbul ignore if */
|
1259
|
+
if (!pinia._e.active) {
|
1260
|
+
throw new Error('Pinia destroyed');
|
1261
|
+
}
|
1262
|
+
// watcher options for $subscribe
|
1263
|
+
const $subscribeOptions = { deep: true };
|
1264
|
+
/* istanbul ignore else */
|
1265
|
+
{
|
1266
|
+
$subscribeOptions.onTrigger = (event) => {
|
1267
|
+
/* istanbul ignore else */
|
1268
|
+
if (isListening) {
|
1269
|
+
debuggerEvents = event;
|
1270
|
+
// avoid triggering this while the store is being built and the state is being set in pinia
|
1271
|
+
}
|
1272
|
+
else if (isListening == false && !store._hotUpdating) {
|
1273
|
+
// let patch send all the events together later
|
1274
|
+
/* istanbul ignore else */
|
1275
|
+
if (Array.isArray(debuggerEvents)) {
|
1276
|
+
debuggerEvents.push(event);
|
1277
|
+
}
|
1278
|
+
else {
|
1279
|
+
console.error('🍍 debuggerEvents should be an array. This is most likely an internal Pinia bug.');
|
1280
|
+
}
|
1281
|
+
}
|
1282
|
+
};
|
1283
|
+
}
|
1284
|
+
// internal state
|
1285
|
+
let isListening; // set to true at the end
|
1286
|
+
let isSyncListening; // set to true at the end
|
1287
|
+
let subscriptions = [];
|
1288
|
+
let actionSubscriptions = [];
|
1289
|
+
let debuggerEvents;
|
1290
|
+
const initialState = pinia.state.value[$id];
|
1291
|
+
// avoid setting the state for option stores if it is set
|
1292
|
+
// by the setup
|
1293
|
+
if (!isOptionsStore && !initialState && (!hot)) {
|
1294
|
+
/* istanbul ignore if */
|
1295
|
+
pinia.state.value[$id] = {};
|
1296
|
+
}
|
1297
|
+
const hotState = vue.ref({});
|
1298
|
+
// avoid triggering too many listeners
|
1299
|
+
// https://github.com/vuejs/pinia/issues/1129
|
1300
|
+
let activeListener;
|
1301
|
+
function $patch(partialStateOrMutator) {
|
1302
|
+
let subscriptionMutation;
|
1303
|
+
isListening = isSyncListening = false;
|
1304
|
+
// reset the debugger events since patches are sync
|
1305
|
+
/* istanbul ignore else */
|
1306
|
+
{
|
1307
|
+
debuggerEvents = [];
|
1308
|
+
}
|
1309
|
+
if (typeof partialStateOrMutator === 'function') {
|
1310
|
+
partialStateOrMutator(pinia.state.value[$id]);
|
1311
|
+
subscriptionMutation = {
|
1312
|
+
type: exports.MutationType.patchFunction,
|
1313
|
+
storeId: $id,
|
1314
|
+
events: debuggerEvents,
|
1315
|
+
};
|
1316
|
+
}
|
1317
|
+
else {
|
1318
|
+
mergeReactiveObjects(pinia.state.value[$id], partialStateOrMutator);
|
1319
|
+
subscriptionMutation = {
|
1320
|
+
type: exports.MutationType.patchObject,
|
1321
|
+
payload: partialStateOrMutator,
|
1322
|
+
storeId: $id,
|
1323
|
+
events: debuggerEvents,
|
1324
|
+
};
|
1325
|
+
}
|
1326
|
+
const myListenerId = (activeListener = Symbol());
|
1327
|
+
vue.nextTick().then(() => {
|
1328
|
+
if (activeListener === myListenerId) {
|
1329
|
+
isListening = true;
|
1330
|
+
}
|
1331
|
+
});
|
1332
|
+
isSyncListening = true;
|
1333
|
+
// because we paused the watcher, we need to manually call the subscriptions
|
1334
|
+
triggerSubscriptions(subscriptions, subscriptionMutation, pinia.state.value[$id]);
|
1335
|
+
}
|
1336
|
+
const $reset = isOptionsStore
|
1337
|
+
? function $reset() {
|
1338
|
+
const { state } = options;
|
1339
|
+
const newState = state ? state() : {};
|
1340
|
+
// we use a patch to group all changes into one single subscription
|
1341
|
+
this.$patch(($state) => {
|
1342
|
+
// @ts-expect-error: FIXME: shouldn't error?
|
1343
|
+
assign($state, newState);
|
1344
|
+
});
|
1345
|
+
}
|
1346
|
+
: /* istanbul ignore next */
|
1347
|
+
() => {
|
1348
|
+
throw new Error(`🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`);
|
1349
|
+
}
|
1350
|
+
;
|
1351
|
+
function $dispose() {
|
1352
|
+
scope.stop();
|
1353
|
+
subscriptions = [];
|
1354
|
+
actionSubscriptions = [];
|
1355
|
+
pinia._s.delete($id);
|
1356
|
+
}
|
1357
|
+
/**
|
1358
|
+
* Helper that wraps function so it can be tracked with $onAction
|
1359
|
+
* @param fn - action to wrap
|
1360
|
+
* @param name - name of the action
|
1361
|
+
*/
|
1362
|
+
const action = (fn, name = '') => {
|
1363
|
+
if (ACTION_MARKER in fn) {
|
1364
|
+
fn[ACTION_NAME] = name;
|
1365
|
+
return fn;
|
1366
|
+
}
|
1367
|
+
const wrappedAction = function () {
|
1368
|
+
setActivePinia(pinia);
|
1369
|
+
const args = Array.from(arguments);
|
1370
|
+
const afterCallbackList = [];
|
1371
|
+
const onErrorCallbackList = [];
|
1372
|
+
function after(callback) {
|
1373
|
+
afterCallbackList.push(callback);
|
1374
|
+
}
|
1375
|
+
function onError(callback) {
|
1376
|
+
onErrorCallbackList.push(callback);
|
1377
|
+
}
|
1378
|
+
// @ts-expect-error
|
1379
|
+
triggerSubscriptions(actionSubscriptions, {
|
1380
|
+
args,
|
1381
|
+
name: wrappedAction[ACTION_NAME],
|
1382
|
+
store,
|
1383
|
+
after,
|
1384
|
+
onError,
|
1385
|
+
});
|
1386
|
+
let ret;
|
1387
|
+
try {
|
1388
|
+
ret = fn.apply(this && this.$id === $id ? this : store, args);
|
1389
|
+
// handle sync errors
|
1390
|
+
}
|
1391
|
+
catch (error) {
|
1392
|
+
triggerSubscriptions(onErrorCallbackList, error);
|
1393
|
+
throw error;
|
1394
|
+
}
|
1395
|
+
if (ret instanceof Promise) {
|
1396
|
+
return ret
|
1397
|
+
.then((value) => {
|
1398
|
+
triggerSubscriptions(afterCallbackList, value);
|
1399
|
+
return value;
|
1400
|
+
})
|
1401
|
+
.catch((error) => {
|
1402
|
+
triggerSubscriptions(onErrorCallbackList, error);
|
1403
|
+
return Promise.reject(error);
|
1404
|
+
});
|
1405
|
+
}
|
1406
|
+
// trigger after callbacks
|
1407
|
+
triggerSubscriptions(afterCallbackList, ret);
|
1408
|
+
return ret;
|
1409
|
+
};
|
1410
|
+
wrappedAction[ACTION_MARKER] = true;
|
1411
|
+
wrappedAction[ACTION_NAME] = name; // will be set later
|
1412
|
+
// @ts-expect-error: we are intentionally limiting the returned type to just Fn
|
1413
|
+
// because all the added properties are internals that are exposed through `$onAction()` only
|
1414
|
+
return wrappedAction;
|
1415
|
+
};
|
1416
|
+
const _hmrPayload = /*#__PURE__*/ vue.markRaw({
|
1417
|
+
actions: {},
|
1418
|
+
getters: {},
|
1419
|
+
state: [],
|
1420
|
+
hotState,
|
1421
|
+
});
|
1422
|
+
const partialStore = {
|
1423
|
+
_p: pinia,
|
1424
|
+
// _s: scope,
|
1425
|
+
$id,
|
1426
|
+
$onAction: addSubscription.bind(null, actionSubscriptions),
|
1427
|
+
$patch,
|
1428
|
+
$reset,
|
1429
|
+
$subscribe(callback, options = {}) {
|
1430
|
+
const removeSubscription = addSubscription(subscriptions, callback, options.detached, () => stopWatcher());
|
1431
|
+
const stopWatcher = scope.run(() => vue.watch(() => pinia.state.value[$id], (state) => {
|
1432
|
+
if (options.flush === 'sync' ? isSyncListening : isListening) {
|
1433
|
+
callback({
|
1434
|
+
storeId: $id,
|
1435
|
+
type: exports.MutationType.direct,
|
1436
|
+
events: debuggerEvents,
|
1437
|
+
}, state);
|
1438
|
+
}
|
1439
|
+
}, assign({}, $subscribeOptions, options)));
|
1440
|
+
return removeSubscription;
|
1441
|
+
},
|
1442
|
+
$dispose,
|
1443
|
+
};
|
1444
|
+
const store = vue.reactive(assign({
|
1445
|
+
_hmrPayload,
|
1446
|
+
_customProperties: vue.markRaw(new Set()), // devtools custom properties
|
1447
|
+
}, partialStore
|
1448
|
+
// must be added later
|
1449
|
+
// setupStore
|
1450
|
+
)
|
1451
|
+
);
|
1452
|
+
// store the partial store now so the setup of stores can instantiate each other before they are finished without
|
1453
|
+
// creating infinite loops.
|
1454
|
+
pinia._s.set($id, store);
|
1455
|
+
const runWithContext = (pinia._a && pinia._a.runWithContext) || fallbackRunWithContext;
|
1456
|
+
// TODO: idea create skipSerialize that marks properties as non serializable and they are skipped
|
1457
|
+
const setupStore = runWithContext(() => pinia._e.run(() => (scope = vue.effectScope()).run(() => setup({ action }))));
|
1458
|
+
// overwrite existing actions to support $onAction
|
1459
|
+
for (const key in setupStore) {
|
1460
|
+
const prop = setupStore[key];
|
1461
|
+
if ((vue.isRef(prop) && !isComputed(prop)) || vue.isReactive(prop)) {
|
1462
|
+
// mark it as a piece of state to be serialized
|
1463
|
+
if (hot) {
|
1464
|
+
hotState.value[key] = vue.toRef(setupStore, key);
|
1465
|
+
// createOptionStore directly sets the state in pinia.state.value so we
|
1466
|
+
// can just skip that
|
1467
|
+
}
|
1468
|
+
else if (!isOptionsStore) {
|
1469
|
+
// in setup stores we must hydrate the state and sync pinia state tree with the refs the user just created
|
1470
|
+
if (initialState && shouldHydrate(prop)) {
|
1471
|
+
if (vue.isRef(prop)) {
|
1472
|
+
prop.value = initialState[key];
|
1473
|
+
}
|
1474
|
+
else {
|
1475
|
+
// probably a reactive object, lets recursively assign
|
1476
|
+
// @ts-expect-error: prop is unknown
|
1477
|
+
mergeReactiveObjects(prop, initialState[key]);
|
1478
|
+
}
|
1479
|
+
}
|
1480
|
+
// transfer the ref to the pinia state to keep everything in sync
|
1481
|
+
pinia.state.value[$id][key] = prop;
|
1482
|
+
}
|
1483
|
+
/* istanbul ignore else */
|
1484
|
+
{
|
1485
|
+
_hmrPayload.state.push(key);
|
1486
|
+
}
|
1487
|
+
// action
|
1488
|
+
}
|
1489
|
+
else if (typeof prop === 'function') {
|
1490
|
+
const actionValue = hot ? prop : action(prop, key);
|
1491
|
+
// this a hot module replacement store because the hotUpdate method needs
|
1492
|
+
// to do it with the right context
|
1493
|
+
// @ts-expect-error
|
1494
|
+
setupStore[key] = actionValue;
|
1495
|
+
/* istanbul ignore else */
|
1496
|
+
{
|
1497
|
+
_hmrPayload.actions[key] = prop;
|
1498
|
+
}
|
1499
|
+
// list actions so they can be used in plugins
|
1500
|
+
// @ts-expect-error
|
1501
|
+
optionsForPlugin.actions[key] = prop;
|
1502
|
+
}
|
1503
|
+
else {
|
1504
|
+
// add getters for devtools
|
1505
|
+
if (isComputed(prop)) {
|
1506
|
+
_hmrPayload.getters[key] = isOptionsStore
|
1507
|
+
? // @ts-expect-error
|
1508
|
+
options.getters[key]
|
1509
|
+
: prop;
|
1510
|
+
if (IS_CLIENT) {
|
1511
|
+
const getters = setupStore._getters ||
|
1512
|
+
// @ts-expect-error: same
|
1513
|
+
(setupStore._getters = vue.markRaw([]));
|
1514
|
+
getters.push(key);
|
1515
|
+
}
|
1516
|
+
}
|
1517
|
+
}
|
1518
|
+
}
|
1519
|
+
// add the state, getters, and action properties
|
1520
|
+
/* istanbul ignore if */
|
1521
|
+
assign(store, setupStore);
|
1522
|
+
// allows retrieving reactive objects with `storeToRefs()`. Must be called after assigning to the reactive object.
|
1523
|
+
// Make `storeToRefs()` work with `reactive()` #799
|
1524
|
+
assign(vue.toRaw(store), setupStore);
|
1525
|
+
// use this instead of a computed with setter to be able to create it anywhere
|
1526
|
+
// without linking the computed lifespan to wherever the store is first
|
1527
|
+
// created.
|
1528
|
+
Object.defineProperty(store, '$state', {
|
1529
|
+
get: () => (hot ? hotState.value : pinia.state.value[$id]),
|
1530
|
+
set: (state) => {
|
1531
|
+
/* istanbul ignore if */
|
1532
|
+
if (hot) {
|
1533
|
+
throw new Error('cannot set hotState');
|
1534
|
+
}
|
1535
|
+
$patch(($state) => {
|
1536
|
+
// @ts-expect-error: FIXME: shouldn't error?
|
1537
|
+
assign($state, state);
|
1538
|
+
});
|
1539
|
+
},
|
1540
|
+
});
|
1541
|
+
// add the hotUpdate before plugins to allow them to override it
|
1542
|
+
/* istanbul ignore else */
|
1543
|
+
{
|
1544
|
+
store._hotUpdate = vue.markRaw((newStore) => {
|
1545
|
+
store._hotUpdating = true;
|
1546
|
+
newStore._hmrPayload.state.forEach((stateKey) => {
|
1547
|
+
if (stateKey in store.$state) {
|
1548
|
+
const newStateTarget = newStore.$state[stateKey];
|
1549
|
+
const oldStateSource = store.$state[stateKey];
|
1550
|
+
if (typeof newStateTarget === 'object' &&
|
1551
|
+
isPlainObject(newStateTarget) &&
|
1552
|
+
isPlainObject(oldStateSource)) {
|
1553
|
+
patchObject(newStateTarget, oldStateSource);
|
1554
|
+
}
|
1555
|
+
else {
|
1556
|
+
// transfer the ref
|
1557
|
+
newStore.$state[stateKey] = oldStateSource;
|
1558
|
+
}
|
1559
|
+
}
|
1560
|
+
// patch direct access properties to allow store.stateProperty to work as
|
1561
|
+
// store.$state.stateProperty
|
1562
|
+
// @ts-expect-error: any type
|
1563
|
+
store[stateKey] = vue.toRef(newStore.$state, stateKey);
|
1564
|
+
});
|
1565
|
+
// remove deleted state properties
|
1566
|
+
Object.keys(store.$state).forEach((stateKey) => {
|
1567
|
+
if (!(stateKey in newStore.$state)) {
|
1568
|
+
// @ts-expect-error: noop if doesn't exist
|
1569
|
+
delete store[stateKey];
|
1570
|
+
}
|
1571
|
+
});
|
1572
|
+
// avoid devtools logging this as a mutation
|
1573
|
+
isListening = false;
|
1574
|
+
isSyncListening = false;
|
1575
|
+
pinia.state.value[$id] = vue.toRef(newStore._hmrPayload, 'hotState');
|
1576
|
+
isSyncListening = true;
|
1577
|
+
vue.nextTick().then(() => {
|
1578
|
+
isListening = true;
|
1579
|
+
});
|
1580
|
+
for (const actionName in newStore._hmrPayload.actions) {
|
1581
|
+
const actionFn = newStore[actionName];
|
1582
|
+
// @ts-expect-error: actionName is a string
|
1583
|
+
store[actionName] =
|
1584
|
+
//
|
1585
|
+
action(actionFn, actionName);
|
1586
|
+
}
|
1587
|
+
// TODO: does this work in both setup and option store?
|
1588
|
+
for (const getterName in newStore._hmrPayload.getters) {
|
1589
|
+
const getter = newStore._hmrPayload.getters[getterName];
|
1590
|
+
const getterValue = isOptionsStore
|
1591
|
+
? // special handling of options api
|
1592
|
+
vue.computed(() => {
|
1593
|
+
setActivePinia(pinia);
|
1594
|
+
return getter.call(store, store);
|
1595
|
+
})
|
1596
|
+
: getter;
|
1597
|
+
// @ts-expect-error: getterName is a string
|
1598
|
+
store[getterName] =
|
1599
|
+
//
|
1600
|
+
getterValue;
|
1601
|
+
}
|
1602
|
+
// remove deleted getters
|
1603
|
+
Object.keys(store._hmrPayload.getters).forEach((key) => {
|
1604
|
+
if (!(key in newStore._hmrPayload.getters)) {
|
1605
|
+
// @ts-expect-error: noop if doesn't exist
|
1606
|
+
delete store[key];
|
1607
|
+
}
|
1608
|
+
});
|
1609
|
+
// remove old actions
|
1610
|
+
Object.keys(store._hmrPayload.actions).forEach((key) => {
|
1611
|
+
if (!(key in newStore._hmrPayload.actions)) {
|
1612
|
+
// @ts-expect-error: noop if doesn't exist
|
1613
|
+
delete store[key];
|
1614
|
+
}
|
1615
|
+
});
|
1616
|
+
// update the values used in devtools and to allow deleting new properties later on
|
1617
|
+
store._hmrPayload = newStore._hmrPayload;
|
1618
|
+
store._getters = newStore._getters;
|
1619
|
+
store._hotUpdating = false;
|
1620
|
+
});
|
1621
|
+
}
|
1622
|
+
if (IS_CLIENT) {
|
1623
|
+
const nonEnumerable = {
|
1624
|
+
writable: true,
|
1625
|
+
configurable: true,
|
1626
|
+
// avoid warning on devtools trying to display this property
|
1627
|
+
enumerable: false,
|
1628
|
+
};
|
1629
|
+
['_p', '_hmrPayload', '_getters', '_customProperties'].forEach((p) => {
|
1630
|
+
Object.defineProperty(store, p, assign({ value: store[p] }, nonEnumerable));
|
1631
|
+
});
|
1632
|
+
}
|
1633
|
+
// apply all plugins
|
1634
|
+
pinia._p.forEach((extender) => {
|
1635
|
+
/* istanbul ignore else */
|
1636
|
+
if (IS_CLIENT) {
|
1637
|
+
const extensions = scope.run(() => extender({
|
1638
|
+
store: store,
|
1639
|
+
app: pinia._a,
|
1640
|
+
pinia,
|
1641
|
+
options: optionsForPlugin,
|
1642
|
+
}));
|
1643
|
+
Object.keys(extensions || {}).forEach((key) => store._customProperties.add(key));
|
1644
|
+
assign(store, extensions);
|
1645
|
+
}
|
1646
|
+
else {
|
1647
|
+
assign(store, scope.run(() => extender({
|
1648
|
+
store: store,
|
1649
|
+
app: pinia._a,
|
1650
|
+
pinia,
|
1651
|
+
options: optionsForPlugin,
|
1652
|
+
})));
|
1653
|
+
}
|
1654
|
+
});
|
1655
|
+
if (store.$state &&
|
1656
|
+
typeof store.$state === 'object' &&
|
1657
|
+
typeof store.$state.constructor === 'function' &&
|
1658
|
+
!store.$state.constructor.toString().includes('[native code]')) {
|
1659
|
+
console.warn(`[🍍]: The "state" must be a plain object. It cannot be\n` +
|
1660
|
+
`\tstate: () => new MyClass()\n` +
|
1661
|
+
`Found in store "${store.$id}".`);
|
1662
|
+
}
|
1663
|
+
// only apply hydrate to option stores with an initial state in pinia
|
1664
|
+
if (initialState &&
|
1665
|
+
isOptionsStore &&
|
1666
|
+
options.hydrate) {
|
1667
|
+
options.hydrate(store.$state, initialState);
|
1668
|
+
}
|
1669
|
+
isListening = true;
|
1670
|
+
isSyncListening = true;
|
1671
|
+
return store;
|
1672
|
+
}
|
1673
|
+
// allows unused stores to be tree shaken
|
1674
|
+
/*! #__NO_SIDE_EFFECTS__ */
|
1675
|
+
function defineStore(
|
1676
|
+
// TODO: add proper types from above
|
1677
|
+
id, setup, setupOptions) {
|
1678
|
+
let options;
|
1679
|
+
const isSetupStore = typeof setup === 'function';
|
1680
|
+
// the option store setup will contain the actual options in this case
|
1681
|
+
options = isSetupStore ? setupOptions : setup;
|
1682
|
+
function useStore(pinia, hot) {
|
1683
|
+
const hasContext = vue.hasInjectionContext();
|
1684
|
+
pinia =
|
1685
|
+
// in test mode, ignore the argument provided as we can always retrieve a
|
1686
|
+
// pinia instance with getActivePinia()
|
1687
|
+
(pinia) ||
|
1688
|
+
(hasContext ? vue.inject(piniaSymbol, null) : null);
|
1689
|
+
if (pinia)
|
1690
|
+
setActivePinia(pinia);
|
1691
|
+
if (!activePinia) {
|
1692
|
+
throw new Error(`[🍍]: "getActivePinia()" was called but there was no active Pinia. Are you trying to use a store before calling "app.use(pinia)"?\n` +
|
1693
|
+
`See https://pinia.vuejs.org/core-concepts/outside-component-usage.html for help.\n` +
|
1694
|
+
`This will fail in production.`);
|
1695
|
+
}
|
1696
|
+
pinia = activePinia;
|
1697
|
+
if (!pinia._s.has(id)) {
|
1698
|
+
// creating the store registers it in `pinia._s`
|
1699
|
+
if (isSetupStore) {
|
1700
|
+
createSetupStore(id, setup, options, pinia);
|
1701
|
+
}
|
1702
|
+
else {
|
1703
|
+
createOptionsStore(id, options, pinia);
|
1704
|
+
}
|
1705
|
+
/* istanbul ignore else */
|
1706
|
+
{
|
1707
|
+
// @ts-expect-error: not the right inferred type
|
1708
|
+
useStore._pinia = pinia;
|
1709
|
+
}
|
1710
|
+
}
|
1711
|
+
const store = pinia._s.get(id);
|
1712
|
+
if (hot) {
|
1713
|
+
const hotId = '__hot:' + id;
|
1714
|
+
const newStore = isSetupStore
|
1715
|
+
? createSetupStore(hotId, setup, options, pinia, true)
|
1716
|
+
: createOptionsStore(hotId, assign({}, options), pinia, true);
|
1717
|
+
hot._hotUpdate(newStore);
|
1718
|
+
// cleanup the state properties and the store from the cache
|
1719
|
+
delete pinia.state.value[hotId];
|
1720
|
+
pinia._s.delete(hotId);
|
1721
|
+
}
|
1722
|
+
if (IS_CLIENT) {
|
1723
|
+
const currentInstance = vue.getCurrentInstance();
|
1724
|
+
// save stores in instances to access them devtools
|
1725
|
+
if (currentInstance &&
|
1726
|
+
currentInstance.proxy &&
|
1727
|
+
// avoid adding stores that are just built for hot module replacement
|
1728
|
+
!hot) {
|
1729
|
+
const vm = currentInstance.proxy;
|
1730
|
+
const cache = '_pStores' in vm ? vm._pStores : (vm._pStores = {});
|
1731
|
+
cache[id] = store;
|
1732
|
+
}
|
1733
|
+
}
|
1734
|
+
// StoreGeneric cannot be casted towards Store
|
1735
|
+
return store;
|
1736
|
+
}
|
1737
|
+
useStore.$id = id;
|
1738
|
+
return useStore;
|
1739
|
+
}
|
1740
|
+
|
1741
|
+
let mapStoreSuffix = 'Store';
|
1742
|
+
/**
|
1743
|
+
* Changes the suffix added by `mapStores()`. Can be set to an empty string.
|
1744
|
+
* Defaults to `"Store"`. Make sure to extend the MapStoresCustomization
|
1745
|
+
* interface if you are using TypeScript.
|
1746
|
+
*
|
1747
|
+
* @param suffix - new suffix
|
1748
|
+
*/
|
1749
|
+
function setMapStoreSuffix(suffix // could be 'Store' but that would be annoying for JS
|
1750
|
+
) {
|
1751
|
+
mapStoreSuffix = suffix;
|
1752
|
+
}
|
1753
|
+
/**
|
1754
|
+
* Allows using stores without the composition API (`setup()`) by generating an
|
1755
|
+
* object to be spread in the `computed` field of a component. It accepts a list
|
1756
|
+
* of store definitions.
|
1757
|
+
*
|
1758
|
+
* @example
|
1759
|
+
* ```js
|
1760
|
+
* export default {
|
1761
|
+
* computed: {
|
1762
|
+
* // other computed properties
|
1763
|
+
* ...mapStores(useUserStore, useCartStore)
|
1764
|
+
* },
|
1765
|
+
*
|
1766
|
+
* created() {
|
1767
|
+
* this.userStore // store with id "user"
|
1768
|
+
* this.cartStore // store with id "cart"
|
1769
|
+
* }
|
1770
|
+
* }
|
1771
|
+
* ```
|
1772
|
+
*
|
1773
|
+
* @param stores - list of stores to map to an object
|
1774
|
+
*/
|
1775
|
+
function mapStores(...stores) {
|
1776
|
+
if (Array.isArray(stores[0])) {
|
1777
|
+
console.warn(`[🍍]: Directly pass all stores to "mapStores()" without putting them in an array:\n` +
|
1778
|
+
`Replace\n` +
|
1779
|
+
`\tmapStores([useAuthStore, useCartStore])\n` +
|
1780
|
+
`with\n` +
|
1781
|
+
`\tmapStores(useAuthStore, useCartStore)\n` +
|
1782
|
+
`This will fail in production if not fixed.`);
|
1783
|
+
stores = stores[0];
|
1784
|
+
}
|
1785
|
+
return stores.reduce((reduced, useStore) => {
|
1786
|
+
// @ts-expect-error: $id is added by defineStore
|
1787
|
+
reduced[useStore.$id + mapStoreSuffix] = function () {
|
1788
|
+
return useStore(this.$pinia);
|
1789
|
+
};
|
1790
|
+
return reduced;
|
1791
|
+
}, {});
|
1792
|
+
}
|
1793
|
+
/**
|
1794
|
+
* Allows using state and getters from one store without using the composition
|
1795
|
+
* API (`setup()`) by generating an object to be spread in the `computed` field
|
1796
|
+
* of a component.
|
1797
|
+
*
|
1798
|
+
* @param useStore - store to map from
|
1799
|
+
* @param keysOrMapper - array or object
|
1800
|
+
*/
|
1801
|
+
function mapState(useStore, keysOrMapper) {
|
1802
|
+
return Array.isArray(keysOrMapper)
|
1803
|
+
? keysOrMapper.reduce((reduced, key) => {
|
1804
|
+
reduced[key] = function () {
|
1805
|
+
// @ts-expect-error: FIXME: should work?
|
1806
|
+
return useStore(this.$pinia)[key];
|
1807
|
+
};
|
1808
|
+
return reduced;
|
1809
|
+
}, {})
|
1810
|
+
: Object.keys(keysOrMapper).reduce((reduced, key) => {
|
1811
|
+
// @ts-expect-error
|
1812
|
+
reduced[key] = function () {
|
1813
|
+
const store = useStore(this.$pinia);
|
1814
|
+
const storeKey = keysOrMapper[key];
|
1815
|
+
// for some reason TS is unable to infer the type of storeKey to be a
|
1816
|
+
// function
|
1817
|
+
return typeof storeKey === 'function'
|
1818
|
+
? storeKey.call(this, store)
|
1819
|
+
: // @ts-expect-error: FIXME: should work?
|
1820
|
+
store[storeKey];
|
1821
|
+
};
|
1822
|
+
return reduced;
|
1823
|
+
}, {});
|
1824
|
+
}
|
1825
|
+
/**
|
1826
|
+
* Alias for `mapState()`. You should use `mapState()` instead.
|
1827
|
+
* @deprecated use `mapState()` instead.
|
1828
|
+
*/
|
1829
|
+
const mapGetters = mapState;
|
1830
|
+
/**
|
1831
|
+
* Allows directly using actions from your store without using the composition
|
1832
|
+
* API (`setup()`) by generating an object to be spread in the `methods` field
|
1833
|
+
* of a component.
|
1834
|
+
*
|
1835
|
+
* @param useStore - store to map from
|
1836
|
+
* @param keysOrMapper - array or object
|
1837
|
+
*/
|
1838
|
+
function mapActions(useStore, keysOrMapper) {
|
1839
|
+
return Array.isArray(keysOrMapper)
|
1840
|
+
? keysOrMapper.reduce((reduced, key) => {
|
1841
|
+
// @ts-expect-error
|
1842
|
+
reduced[key] = function (...args) {
|
1843
|
+
// @ts-expect-error: FIXME: should work?
|
1844
|
+
return useStore(this.$pinia)[key](...args);
|
1845
|
+
};
|
1846
|
+
return reduced;
|
1847
|
+
}, {})
|
1848
|
+
: Object.keys(keysOrMapper).reduce((reduced, key) => {
|
1849
|
+
// @ts-expect-error
|
1850
|
+
reduced[key] = function (...args) {
|
1851
|
+
// @ts-expect-error: FIXME: should work?
|
1852
|
+
return useStore(this.$pinia)[keysOrMapper[key]](...args);
|
1853
|
+
};
|
1854
|
+
return reduced;
|
1855
|
+
}, {});
|
1856
|
+
}
|
1857
|
+
/**
|
1858
|
+
* Allows using state and getters from one store without using the composition
|
1859
|
+
* API (`setup()`) by generating an object to be spread in the `computed` field
|
1860
|
+
* of a component.
|
1861
|
+
*
|
1862
|
+
* @param useStore - store to map from
|
1863
|
+
* @param keysOrMapper - array or object
|
1864
|
+
*/
|
1865
|
+
function mapWritableState(useStore, keysOrMapper) {
|
1866
|
+
return Array.isArray(keysOrMapper)
|
1867
|
+
? keysOrMapper.reduce((reduced, key) => {
|
1868
|
+
reduced[key] = {
|
1869
|
+
get() {
|
1870
|
+
return useStore(this.$pinia)[key];
|
1871
|
+
},
|
1872
|
+
set(value) {
|
1873
|
+
return (useStore(this.$pinia)[key] = value);
|
1874
|
+
},
|
1875
|
+
};
|
1876
|
+
return reduced;
|
1877
|
+
}, {})
|
1878
|
+
: Object.keys(keysOrMapper).reduce((reduced, key) => {
|
1879
|
+
reduced[key] = {
|
1880
|
+
get() {
|
1881
|
+
return useStore(this.$pinia)[keysOrMapper[key]];
|
1882
|
+
},
|
1883
|
+
set(value) {
|
1884
|
+
return (useStore(this.$pinia)[keysOrMapper[key]] = value);
|
1885
|
+
},
|
1886
|
+
};
|
1887
|
+
return reduced;
|
1888
|
+
}, {});
|
1889
|
+
}
|
1890
|
+
|
1891
|
+
/**
|
1892
|
+
* Creates an object of references with all the state, getters, and plugin-added
|
1893
|
+
* state properties of the store. Similar to `toRefs()` but specifically
|
1894
|
+
* designed for Pinia stores so methods and non reactive properties are
|
1895
|
+
* completely ignored.
|
1896
|
+
*
|
1897
|
+
* @param store - store to extract the refs from
|
1898
|
+
*/
|
1899
|
+
function storeToRefs(store) {
|
1900
|
+
const rawStore = vue.toRaw(store);
|
1901
|
+
const refs = {};
|
1902
|
+
for (const key in rawStore) {
|
1903
|
+
const value = rawStore[key];
|
1904
|
+
// There is no native method to check for a computed
|
1905
|
+
// https://github.com/vuejs/core/pull/4165
|
1906
|
+
if (value.effect) {
|
1907
|
+
// @ts-expect-error: too hard to type correctly
|
1908
|
+
refs[key] =
|
1909
|
+
// ...
|
1910
|
+
vue.computed({
|
1911
|
+
get: () => store[key],
|
1912
|
+
set(value) {
|
1913
|
+
store[key] = value;
|
1914
|
+
},
|
1915
|
+
});
|
1916
|
+
}
|
1917
|
+
else if (vue.isRef(value) || vue.isReactive(value)) {
|
1918
|
+
// @ts-expect-error: the key is state or getter
|
1919
|
+
refs[key] =
|
1920
|
+
// ---
|
1921
|
+
vue.toRef(store, key);
|
1922
|
+
}
|
1923
|
+
}
|
1924
|
+
return refs;
|
1925
|
+
}
|
1926
|
+
|
1927
|
+
exports.acceptHMRUpdate = acceptHMRUpdate;
|
1928
|
+
exports.createPinia = createPinia;
|
1929
|
+
exports.defineStore = defineStore;
|
1930
|
+
exports.disposePinia = disposePinia;
|
1931
|
+
exports.getActivePinia = getActivePinia;
|
1932
|
+
exports.mapActions = mapActions;
|
1933
|
+
exports.mapGetters = mapGetters;
|
1934
|
+
exports.mapState = mapState;
|
1935
|
+
exports.mapStores = mapStores;
|
1936
|
+
exports.mapWritableState = mapWritableState;
|
1937
|
+
exports.setActivePinia = setActivePinia;
|
1938
|
+
exports.setMapStoreSuffix = setMapStoreSuffix;
|
1939
|
+
exports.shouldHydrate = shouldHydrate;
|
1940
|
+
exports.skipHydrate = skipHydrate;
|
1941
|
+
exports.storeToRefs = storeToRefs;
|
1942
|
+
|
1943
|
+
return exports;
|
1944
|
+
|
1945
|
+
})({}, Vue, devtoolsApi);
|