| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var Pinia = (function (exports, vueDemi) { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
let activePinia; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
const setActivePinia = (pinia) => (activePinia = pinia); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
const getActivePinia = () => (vueDemi.getCurrentInstance() && vueDemi.inject(piniaSymbol)) || activePinia; |
| 25 |
const piniaSymbol = (Symbol('pinia') ); |
| 26 |
|
| 27 |
function getDevtoolsGlobalHook() { |
| 28 |
return getTarget().__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 29 |
} |
| 30 |
function getTarget() { |
| 31 |
|
| 32 |
return (typeof navigator !== 'undefined' && typeof window !== 'undefined') |
| 33 |
? window |
| 34 |
: typeof global !== 'undefined' |
| 35 |
? global |
| 36 |
: {}; |
| 37 |
} |
| 38 |
const isProxyAvailable = typeof Proxy === 'function'; |
| 39 |
|
| 40 |
const HOOK_SETUP = 'devtools-plugin:setup'; |
| 41 |
const HOOK_PLUGIN_SETTINGS_SET = 'plugin:settings:set'; |
| 42 |
|
| 43 |
let supported; |
| 44 |
let perf; |
| 45 |
function isPerformanceSupported() { |
| 46 |
var _a; |
| 47 |
if (supported !== undefined) { |
| 48 |
return supported; |
| 49 |
} |
| 50 |
if (typeof window !== 'undefined' && window.performance) { |
| 51 |
supported = true; |
| 52 |
perf = window.performance; |
| 53 |
} |
| 54 |
else if (typeof global !== 'undefined' && ((_a = global.perf_hooks) === null || _a === void 0 ? void 0 : _a.performance)) { |
| 55 |
supported = true; |
| 56 |
perf = global.perf_hooks.performance; |
| 57 |
} |
| 58 |
else { |
| 59 |
supported = false; |
| 60 |
} |
| 61 |
return supported; |
| 62 |
} |
| 63 |
function now() { |
| 64 |
return isPerformanceSupported() ? perf.now() : Date.now(); |
| 65 |
} |
| 66 |
|
| 67 |
class ApiProxy { |
| 68 |
constructor(plugin, hook) { |
| 69 |
this.target = null; |
| 70 |
this.targetQueue = []; |
| 71 |
this.onQueue = []; |
| 72 |
this.plugin = plugin; |
| 73 |
this.hook = hook; |
| 74 |
const defaultSettings = {}; |
| 75 |
if (plugin.settings) { |
| 76 |
for (const id in plugin.settings) { |
| 77 |
const item = plugin.settings[id]; |
| 78 |
defaultSettings[id] = item.defaultValue; |
| 79 |
} |
| 80 |
} |
| 81 |
const localSettingsSaveId = `__vue-devtools-plugin-settings__${plugin.id}`; |
| 82 |
let currentSettings = Object.assign({}, defaultSettings); |
| 83 |
try { |
| 84 |
const raw = localStorage.getItem(localSettingsSaveId); |
| 85 |
const data = JSON.parse(raw); |
| 86 |
Object.assign(currentSettings, data); |
| 87 |
} |
| 88 |
catch (e) { |
| 89 |
|
| 90 |
} |
| 91 |
this.fallbacks = { |
| 92 |
getSettings() { |
| 93 |
return currentSettings; |
| 94 |
}, |
| 95 |
setSettings(value) { |
| 96 |
try { |
| 97 |
localStorage.setItem(localSettingsSaveId, JSON.stringify(value)); |
| 98 |
} |
| 99 |
catch (e) { |
| 100 |
|
| 101 |
} |
| 102 |
currentSettings = value; |
| 103 |
}, |
| 104 |
now() { |
| 105 |
return now(); |
| 106 |
}, |
| 107 |
}; |
| 108 |
if (hook) { |
| 109 |
hook.on(HOOK_PLUGIN_SETTINGS_SET, (pluginId, value) => { |
| 110 |
if (pluginId === this.plugin.id) { |
| 111 |
this.fallbacks.setSettings(value); |
| 112 |
} |
| 113 |
}); |
| 114 |
} |
| 115 |
this.proxiedOn = new Proxy({}, { |
| 116 |
get: (_target, prop) => { |
| 117 |
if (this.target) { |
| 118 |
return this.target.on[prop]; |
| 119 |
} |
| 120 |
else { |
| 121 |
return (...args) => { |
| 122 |
this.onQueue.push({ |
| 123 |
method: prop, |
| 124 |
args, |
| 125 |
}); |
| 126 |
}; |
| 127 |
} |
| 128 |
}, |
| 129 |
}); |
| 130 |
this.proxiedTarget = new Proxy({}, { |
| 131 |
get: (_target, prop) => { |
| 132 |
if (this.target) { |
| 133 |
return this.target[prop]; |
| 134 |
} |
| 135 |
else if (prop === 'on') { |
| 136 |
return this.proxiedOn; |
| 137 |
} |
| 138 |
else if (Object.keys(this.fallbacks).includes(prop)) { |
| 139 |
return (...args) => { |
| 140 |
this.targetQueue.push({ |
| 141 |
method: prop, |
| 142 |
args, |
| 143 |
resolve: () => { }, |
| 144 |
}); |
| 145 |
return this.fallbacks[prop](...args); |
| 146 |
}; |
| 147 |
} |
| 148 |
else { |
| 149 |
return (...args) => { |
| 150 |
return new Promise(resolve => { |
| 151 |
this.targetQueue.push({ |
| 152 |
method: prop, |
| 153 |
args, |
| 154 |
resolve, |
| 155 |
}); |
| 156 |
}); |
| 157 |
}; |
| 158 |
} |
| 159 |
}, |
| 160 |
}); |
| 161 |
} |
| 162 |
async setRealTarget(target) { |
| 163 |
this.target = target; |
| 164 |
for (const item of this.onQueue) { |
| 165 |
this.target.on[item.method](...item.args); |
| 166 |
} |
| 167 |
for (const item of this.targetQueue) { |
| 168 |
item.resolve(await this.target[item.method](...item.args)); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
function setupDevtoolsPlugin(pluginDescriptor, setupFn) { |
| 174 |
const descriptor = pluginDescriptor; |
| 175 |
const target = getTarget(); |
| 176 |
const hook = getDevtoolsGlobalHook(); |
| 177 |
const enableProxy = isProxyAvailable && descriptor.enableEarlyProxy; |
| 178 |
if (hook && (target.__VUE_DEVTOOLS_PLUGIN_API_AVAILABLE__ || !enableProxy)) { |
| 179 |
hook.emit(HOOK_SETUP, pluginDescriptor, setupFn); |
| 180 |
} |
| 181 |
else { |
| 182 |
const proxy = enableProxy ? new ApiProxy(descriptor, hook) : null; |
| 183 |
const list = target.__VUE_DEVTOOLS_PLUGINS__ = target.__VUE_DEVTOOLS_PLUGINS__ || []; |
| 184 |
list.push({ |
| 185 |
pluginDescriptor: descriptor, |
| 186 |
setupFn, |
| 187 |
proxy, |
| 188 |
}); |
| 189 |
if (proxy) |
| 190 |
setupFn(proxy.proxiedTarget); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
function isPlainObject( |
| 195 |
|
| 196 |
o) { |
| 197 |
return (o && |
| 198 |
typeof o === 'object' && |
| 199 |
Object.prototype.toString.call(o) === '[object Object]' && |
| 200 |
typeof o.toJSON !== 'function'); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
exports.MutationType = void 0; |
| 208 |
(function (MutationType) { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
MutationType["direct"] = "direct"; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
MutationType["patchObject"] = "patch object"; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
MutationType["patchFunction"] = "patch function"; |
| 229 |
|
| 230 |
})(exports.MutationType || (exports.MutationType = {})); |
| 231 |
|
| 232 |
const IS_CLIENT = typeof window !== 'undefined'; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
const _global = (() => typeof window === 'object' && window.window === window |
| 245 |
? window |
| 246 |
: typeof self === 'object' && self.self === self |
| 247 |
? self |
| 248 |
: typeof global === 'object' && global.global === global |
| 249 |
? global |
| 250 |
: typeof globalThis === 'object' |
| 251 |
? globalThis |
| 252 |
: { HTMLElement: null })(); |
| 253 |
function bom(blob, { autoBom = false } = {}) { |
| 254 |
|
| 255 |
|
| 256 |
if (autoBom && |
| 257 |
/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { |
| 258 |
return new Blob([String.fromCharCode(0xfeff), blob], { type: blob.type }); |
| 259 |
} |
| 260 |
return blob; |
| 261 |
} |
| 262 |
function download(url, name, opts) { |
| 263 |
const xhr = new XMLHttpRequest(); |
| 264 |
xhr.open('GET', url); |
| 265 |
xhr.responseType = 'blob'; |
| 266 |
xhr.onload = function () { |
| 267 |
saveAs(xhr.response, name, opts); |
| 268 |
}; |
| 269 |
xhr.onerror = function () { |
| 270 |
console.error('could not download file'); |
| 271 |
}; |
| 272 |
xhr.send(); |
| 273 |
} |
| 274 |
function corsEnabled(url) { |
| 275 |
const xhr = new XMLHttpRequest(); |
| 276 |
|
| 277 |
xhr.open('HEAD', url, false); |
| 278 |
try { |
| 279 |
xhr.send(); |
| 280 |
} |
| 281 |
catch (e) { } |
| 282 |
return xhr.status >= 200 && xhr.status <= 299; |
| 283 |
} |
| 284 |
|
| 285 |
function click(node) { |
| 286 |
try { |
| 287 |
node.dispatchEvent(new MouseEvent('click')); |
| 288 |
} |
| 289 |
catch (e) { |
| 290 |
const evt = document.createEvent('MouseEvents'); |
| 291 |
evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null); |
| 292 |
node.dispatchEvent(evt); |
| 293 |
} |
| 294 |
} |
| 295 |
const _navigator = |
| 296 |
typeof navigator === 'object' ? navigator : { userAgent: '' }; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
const isMacOSWebView = (() => /Macintosh/.test(_navigator.userAgent) && |
| 301 |
/AppleWebKit/.test(_navigator.userAgent) && |
| 302 |
!/Safari/.test(_navigator.userAgent))(); |
| 303 |
const saveAs = !IS_CLIENT |
| 304 |
? () => { } |
| 305 |
: |
| 306 |
typeof HTMLAnchorElement !== 'undefined' && |
| 307 |
'download' in HTMLAnchorElement.prototype && |
| 308 |
!isMacOSWebView |
| 309 |
? downloadSaveAs |
| 310 |
: |
| 311 |
'msSaveOrOpenBlob' in _navigator |
| 312 |
? msSaveAs |
| 313 |
: |
| 314 |
fileSaverSaveAs; |
| 315 |
function downloadSaveAs(blob, name = 'download', opts) { |
| 316 |
const a = document.createElement('a'); |
| 317 |
a.download = name; |
| 318 |
a.rel = 'noopener'; |
| 319 |
|
| 320 |
|
| 321 |
if (typeof blob === 'string') { |
| 322 |
|
| 323 |
a.href = blob; |
| 324 |
if (a.origin !== location.origin) { |
| 325 |
if (corsEnabled(a.href)) { |
| 326 |
download(blob, name, opts); |
| 327 |
} |
| 328 |
else { |
| 329 |
a.target = '_blank'; |
| 330 |
click(a); |
| 331 |
} |
| 332 |
} |
| 333 |
else { |
| 334 |
click(a); |
| 335 |
} |
| 336 |
} |
| 337 |
else { |
| 338 |
|
| 339 |
a.href = URL.createObjectURL(blob); |
| 340 |
setTimeout(function () { |
| 341 |
URL.revokeObjectURL(a.href); |
| 342 |
}, 4e4); |
| 343 |
setTimeout(function () { |
| 344 |
click(a); |
| 345 |
}, 0); |
| 346 |
} |
| 347 |
} |
| 348 |
function msSaveAs(blob, name = 'download', opts) { |
| 349 |
if (typeof blob === 'string') { |
| 350 |
if (corsEnabled(blob)) { |
| 351 |
download(blob, name, opts); |
| 352 |
} |
| 353 |
else { |
| 354 |
const a = document.createElement('a'); |
| 355 |
a.href = blob; |
| 356 |
a.target = '_blank'; |
| 357 |
setTimeout(function () { |
| 358 |
click(a); |
| 359 |
}); |
| 360 |
} |
| 361 |
} |
| 362 |
else { |
| 363 |
|
| 364 |
navigator.msSaveOrOpenBlob(bom(blob, opts), name); |
| 365 |
} |
| 366 |
} |
| 367 |
function fileSaverSaveAs(blob, name, opts, popup) { |
| 368 |
|
| 369 |
|
| 370 |
popup = popup || open('', '_blank'); |
| 371 |
if (popup) { |
| 372 |
popup.document.title = popup.document.body.innerText = 'downloading...'; |
| 373 |
} |
| 374 |
if (typeof blob === 'string') |
| 375 |
return download(blob, name, opts); |
| 376 |
const force = blob.type === 'application/octet-stream'; |
| 377 |
const isSafari = /constructor/i.test(String(_global.HTMLElement)) || 'safari' in _global; |
| 378 |
const isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent); |
| 379 |
if ((isChromeIOS || (force && isSafari) || isMacOSWebView) && |
| 380 |
typeof FileReader !== 'undefined') { |
| 381 |
|
| 382 |
const reader = new FileReader(); |
| 383 |
reader.onloadend = function () { |
| 384 |
let url = reader.result; |
| 385 |
if (typeof url !== 'string') { |
| 386 |
popup = null; |
| 387 |
throw new Error('Wrong reader.result type'); |
| 388 |
} |
| 389 |
url = isChromeIOS |
| 390 |
? url |
| 391 |
: url.replace(/^data:[^;]*;/, 'data:attachment/file;'); |
| 392 |
if (popup) { |
| 393 |
popup.location.href = url; |
| 394 |
} |
| 395 |
else { |
| 396 |
location.assign(url); |
| 397 |
} |
| 398 |
popup = null; |
| 399 |
}; |
| 400 |
reader.readAsDataURL(blob); |
| 401 |
} |
| 402 |
else { |
| 403 |
const url = URL.createObjectURL(blob); |
| 404 |
if (popup) |
| 405 |
popup.location.assign(url); |
| 406 |
else |
| 407 |
location.href = url; |
| 408 |
popup = null; |
| 409 |
setTimeout(function () { |
| 410 |
URL.revokeObjectURL(url); |
| 411 |
}, 4e4); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
function toastMessage(message, type) { |
| 422 |
const piniaMessage = '🍍 ' + message; |
| 423 |
if (typeof __VUE_DEVTOOLS_TOAST__ === 'function') { |
| 424 |
__VUE_DEVTOOLS_TOAST__(piniaMessage, type); |
| 425 |
} |
| 426 |
else if (type === 'error') { |
| 427 |
console.error(piniaMessage); |
| 428 |
} |
| 429 |
else if (type === 'warn') { |
| 430 |
console.warn(piniaMessage); |
| 431 |
} |
| 432 |
else { |
| 433 |
console.log(piniaMessage); |
| 434 |
} |
| 435 |
} |
| 436 |
function isPinia(o) { |
| 437 |
return '_a' in o && 'install' in o; |
| 438 |
} |
| 439 |
|
| 440 |
function checkClipboardAccess() { |
| 441 |
if (!('clipboard' in navigator)) { |
| 442 |
toastMessage(`Your browser doesn't support the Clipboard API`, 'error'); |
| 443 |
return true; |
| 444 |
} |
| 445 |
} |
| 446 |
function checkNotFocusedError(error) { |
| 447 |
if (error instanceof Error && |
| 448 |
error.message.toLowerCase().includes('document is not focused')) { |
| 449 |
toastMessage('You need to activate the "Emulate a focused page" setting in the "Rendering" panel of devtools.', 'warn'); |
| 450 |
return true; |
| 451 |
} |
| 452 |
return false; |
| 453 |
} |
| 454 |
async function actionGlobalCopyState(pinia) { |
| 455 |
if (checkClipboardAccess()) |
| 456 |
return; |
| 457 |
try { |
| 458 |
await navigator.clipboard.writeText(JSON.stringify(pinia.state.value)); |
| 459 |
toastMessage('Global state copied to clipboard.'); |
| 460 |
} |
| 461 |
catch (error) { |
| 462 |
if (checkNotFocusedError(error)) |
| 463 |
return; |
| 464 |
toastMessage(`Failed to serialize the state. Check the console for more details.`, 'error'); |
| 465 |
console.error(error); |
| 466 |
} |
| 467 |
} |
| 468 |
async function actionGlobalPasteState(pinia) { |
| 469 |
if (checkClipboardAccess()) |
| 470 |
return; |
| 471 |
try { |
| 472 |
pinia.state.value = JSON.parse(await navigator.clipboard.readText()); |
| 473 |
toastMessage('Global state pasted from clipboard.'); |
| 474 |
} |
| 475 |
catch (error) { |
| 476 |
if (checkNotFocusedError(error)) |
| 477 |
return; |
| 478 |
toastMessage(`Failed to deserialize the state from clipboard. Check the console for more details.`, 'error'); |
| 479 |
console.error(error); |
| 480 |
} |
| 481 |
} |
| 482 |
async function actionGlobalSaveState(pinia) { |
| 483 |
try { |
| 484 |
saveAs(new Blob([JSON.stringify(pinia.state.value)], { |
| 485 |
type: 'text/plain;charset=utf-8', |
| 486 |
}), 'pinia-state.json'); |
| 487 |
} |
| 488 |
catch (error) { |
| 489 |
toastMessage(`Failed to export the state as JSON. Check the console for more details.`, 'error'); |
| 490 |
console.error(error); |
| 491 |
} |
| 492 |
} |
| 493 |
let fileInput; |
| 494 |
function getFileOpener() { |
| 495 |
if (!fileInput) { |
| 496 |
fileInput = document.createElement('input'); |
| 497 |
fileInput.type = 'file'; |
| 498 |
fileInput.accept = '.json'; |
| 499 |
} |
| 500 |
function openFile() { |
| 501 |
return new Promise((resolve, reject) => { |
| 502 |
fileInput.onchange = async () => { |
| 503 |
const files = fileInput.files; |
| 504 |
if (!files) |
| 505 |
return resolve(null); |
| 506 |
const file = files.item(0); |
| 507 |
if (!file) |
| 508 |
return resolve(null); |
| 509 |
return resolve({ text: await file.text(), file }); |
| 510 |
}; |
| 511 |
|
| 512 |
fileInput.oncancel = () => resolve(null); |
| 513 |
fileInput.onerror = reject; |
| 514 |
fileInput.click(); |
| 515 |
}); |
| 516 |
} |
| 517 |
return openFile; |
| 518 |
} |
| 519 |
async function actionGlobalOpenStateFile(pinia) { |
| 520 |
try { |
| 521 |
const open = await getFileOpener(); |
| 522 |
const result = await open(); |
| 523 |
if (!result) |
| 524 |
return; |
| 525 |
const { text, file } = result; |
| 526 |
pinia.state.value = JSON.parse(text); |
| 527 |
toastMessage(`Global state imported from "${file.name}".`); |
| 528 |
} |
| 529 |
catch (error) { |
| 530 |
toastMessage(`Failed to export the state as JSON. Check the console for more details.`, 'error'); |
| 531 |
console.error(error); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
function formatDisplay(display) { |
| 536 |
return { |
| 537 |
_custom: { |
| 538 |
display, |
| 539 |
}, |
| 540 |
}; |
| 541 |
} |
| 542 |
const PINIA_ROOT_LABEL = '🍍 Pinia (root)'; |
| 543 |
const PINIA_ROOT_ID = '_root'; |
| 544 |
function formatStoreForInspectorTree(store) { |
| 545 |
return isPinia(store) |
| 546 |
? { |
| 547 |
id: PINIA_ROOT_ID, |
| 548 |
label: PINIA_ROOT_LABEL, |
| 549 |
} |
| 550 |
: { |
| 551 |
id: store.$id, |
| 552 |
label: store.$id, |
| 553 |
}; |
| 554 |
} |
| 555 |
function formatStoreForInspectorState(store) { |
| 556 |
if (isPinia(store)) { |
| 557 |
const storeNames = Array.from(store._s.keys()); |
| 558 |
const storeMap = store._s; |
| 559 |
const state = { |
| 560 |
state: storeNames.map((storeId) => ({ |
| 561 |
editable: true, |
| 562 |
key: storeId, |
| 563 |
value: store.state.value[storeId], |
| 564 |
})), |
| 565 |
getters: storeNames |
| 566 |
.filter((id) => storeMap.get(id)._getters) |
| 567 |
.map((id) => { |
| 568 |
const store = storeMap.get(id); |
| 569 |
return { |
| 570 |
editable: false, |
| 571 |
key: id, |
| 572 |
value: store._getters.reduce((getters, key) => { |
| 573 |
getters[key] = store[key]; |
| 574 |
return getters; |
| 575 |
}, {}), |
| 576 |
}; |
| 577 |
}), |
| 578 |
}; |
| 579 |
return state; |
| 580 |
} |
| 581 |
const state = { |
| 582 |
state: Object.keys(store.$state).map((key) => ({ |
| 583 |
editable: true, |
| 584 |
key, |
| 585 |
value: store.$state[key], |
| 586 |
})), |
| 587 |
}; |
| 588 |
|
| 589 |
if (store._getters && store._getters.length) { |
| 590 |
state.getters = store._getters.map((getterName) => ({ |
| 591 |
editable: false, |
| 592 |
key: getterName, |
| 593 |
value: store[getterName], |
| 594 |
})); |
| 595 |
} |
| 596 |
if (store._customProperties.size) { |
| 597 |
state.customProperties = Array.from(store._customProperties).map((key) => ({ |
| 598 |
editable: true, |
| 599 |
key, |
| 600 |
value: store[key], |
| 601 |
})); |
| 602 |
} |
| 603 |
return state; |
| 604 |
} |
| 605 |
function formatEventData(events) { |
| 606 |
if (!events) |
| 607 |
return {}; |
| 608 |
if (Array.isArray(events)) { |
| 609 |
|
| 610 |
return events.reduce((data, event) => { |
| 611 |
data.keys.push(event.key); |
| 612 |
data.operations.push(event.type); |
| 613 |
data.oldValue[event.key] = event.oldValue; |
| 614 |
data.newValue[event.key] = event.newValue; |
| 615 |
return data; |
| 616 |
}, { |
| 617 |
oldValue: {}, |
| 618 |
keys: [], |
| 619 |
operations: [], |
| 620 |
newValue: {}, |
| 621 |
}); |
| 622 |
} |
| 623 |
else { |
| 624 |
return { |
| 625 |
operation: formatDisplay(events.type), |
| 626 |
key: formatDisplay(events.key), |
| 627 |
oldValue: events.oldValue, |
| 628 |
newValue: events.newValue, |
| 629 |
}; |
| 630 |
} |
| 631 |
} |
| 632 |
function formatMutationType(type) { |
| 633 |
switch (type) { |
| 634 |
case exports.MutationType.direct: |
| 635 |
return 'mutation'; |
| 636 |
case exports.MutationType.patchFunction: |
| 637 |
return '$patch'; |
| 638 |
case exports.MutationType.patchObject: |
| 639 |
return '$patch'; |
| 640 |
default: |
| 641 |
return 'unknown'; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
let isTimelineActive = true; |
| 647 |
const componentStateTypes = []; |
| 648 |
const MUTATIONS_LAYER_ID = 'pinia:mutations'; |
| 649 |
const INSPECTOR_ID = 'pinia'; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
const getStoreType = (id) => '🍍 ' + id; |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
function registerPiniaDevtools(app, pinia) { |
| 665 |
setupDevtoolsPlugin({ |
| 666 |
id: 'dev.esm.pinia', |
| 667 |
label: 'Pinia 🍍', |
| 668 |
logo: 'https://pinia.vuejs.org/logo.svg', |
| 669 |
packageName: 'pinia', |
| 670 |
homepage: 'https://pinia.vuejs.org', |
| 671 |
componentStateTypes, |
| 672 |
app, |
| 673 |
}, (api) => { |
| 674 |
if (typeof api.now !== 'function') { |
| 675 |
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.'); |
| 676 |
} |
| 677 |
api.addTimelineLayer({ |
| 678 |
id: MUTATIONS_LAYER_ID, |
| 679 |
label: `Pinia 🍍`, |
| 680 |
color: 0xe5df88, |
| 681 |
}); |
| 682 |
api.addInspector({ |
| 683 |
id: INSPECTOR_ID, |
| 684 |
label: 'Pinia 🍍', |
| 685 |
icon: 'storage', |
| 686 |
treeFilterPlaceholder: 'Search stores', |
| 687 |
actions: [ |
| 688 |
{ |
| 689 |
icon: 'content_copy', |
| 690 |
action: () => { |
| 691 |
actionGlobalCopyState(pinia); |
| 692 |
}, |
| 693 |
tooltip: 'Serialize and copy the state', |
| 694 |
}, |
| 695 |
{ |
| 696 |
icon: 'content_paste', |
| 697 |
action: async () => { |
| 698 |
await actionGlobalPasteState(pinia); |
| 699 |
api.sendInspectorTree(INSPECTOR_ID); |
| 700 |
api.sendInspectorState(INSPECTOR_ID); |
| 701 |
}, |
| 702 |
tooltip: 'Replace the state with the content of your clipboard', |
| 703 |
}, |
| 704 |
{ |
| 705 |
icon: 'save', |
| 706 |
action: () => { |
| 707 |
actionGlobalSaveState(pinia); |
| 708 |
}, |
| 709 |
tooltip: 'Save the state as a JSON file', |
| 710 |
}, |
| 711 |
{ |
| 712 |
icon: 'folder_open', |
| 713 |
action: async () => { |
| 714 |
await actionGlobalOpenStateFile(pinia); |
| 715 |
api.sendInspectorTree(INSPECTOR_ID); |
| 716 |
api.sendInspectorState(INSPECTOR_ID); |
| 717 |
}, |
| 718 |
tooltip: 'Import the state from a JSON file', |
| 719 |
}, |
| 720 |
], |
| 721 |
}); |
| 722 |
api.on.inspectComponent((payload, ctx) => { |
| 723 |
const proxy = (payload.componentInstance && |
| 724 |
payload.componentInstance.proxy); |
| 725 |
if (proxy && proxy._pStores) { |
| 726 |
const piniaStores = payload.componentInstance.proxy._pStores; |
| 727 |
Object.values(piniaStores).forEach((store) => { |
| 728 |
payload.instanceData.state.push({ |
| 729 |
type: getStoreType(store.$id), |
| 730 |
key: 'state', |
| 731 |
editable: true, |
| 732 |
value: store._isOptionsAPI |
| 733 |
? { |
| 734 |
_custom: { |
| 735 |
value: store.$state, |
| 736 |
actions: [ |
| 737 |
{ |
| 738 |
icon: 'restore', |
| 739 |
tooltip: 'Reset the state of this store', |
| 740 |
action: () => store.$reset(), |
| 741 |
}, |
| 742 |
], |
| 743 |
}, |
| 744 |
} |
| 745 |
: store.$state, |
| 746 |
}); |
| 747 |
if (store._getters && store._getters.length) { |
| 748 |
payload.instanceData.state.push({ |
| 749 |
type: getStoreType(store.$id), |
| 750 |
key: 'getters', |
| 751 |
editable: false, |
| 752 |
value: store._getters.reduce((getters, key) => { |
| 753 |
try { |
| 754 |
getters[key] = store[key]; |
| 755 |
} |
| 756 |
catch (error) { |
| 757 |
|
| 758 |
getters[key] = error; |
| 759 |
} |
| 760 |
return getters; |
| 761 |
}, {}), |
| 762 |
}); |
| 763 |
} |
| 764 |
}); |
| 765 |
} |
| 766 |
}); |
| 767 |
api.on.getInspectorTree((payload) => { |
| 768 |
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { |
| 769 |
let stores = [pinia]; |
| 770 |
stores = stores.concat(Array.from(pinia._s.values())); |
| 771 |
payload.rootNodes = (payload.filter |
| 772 |
? stores.filter((store) => '$id' in store |
| 773 |
? store.$id |
| 774 |
.toLowerCase() |
| 775 |
.includes(payload.filter.toLowerCase()) |
| 776 |
: PINIA_ROOT_LABEL.toLowerCase().includes(payload.filter.toLowerCase())) |
| 777 |
: stores).map(formatStoreForInspectorTree); |
| 778 |
} |
| 779 |
}); |
| 780 |
api.on.getInspectorState((payload) => { |
| 781 |
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { |
| 782 |
const inspectedStore = payload.nodeId === PINIA_ROOT_ID |
| 783 |
? pinia |
| 784 |
: pinia._s.get(payload.nodeId); |
| 785 |
if (!inspectedStore) { |
| 786 |
|
| 787 |
|
| 788 |
return; |
| 789 |
} |
| 790 |
if (inspectedStore) { |
| 791 |
payload.state = formatStoreForInspectorState(inspectedStore); |
| 792 |
} |
| 793 |
} |
| 794 |
}); |
| 795 |
api.on.editInspectorState((payload, ctx) => { |
| 796 |
if (payload.app === app && payload.inspectorId === INSPECTOR_ID) { |
| 797 |
const inspectedStore = payload.nodeId === PINIA_ROOT_ID |
| 798 |
? pinia |
| 799 |
: pinia._s.get(payload.nodeId); |
| 800 |
if (!inspectedStore) { |
| 801 |
return toastMessage(`store "${payload.nodeId}" not found`, 'error'); |
| 802 |
} |
| 803 |
const { path } = payload; |
| 804 |
if (!isPinia(inspectedStore)) { |
| 805 |
|
| 806 |
if (path.length !== 1 || |
| 807 |
!inspectedStore._customProperties.has(path[0]) || |
| 808 |
path[0] in inspectedStore.$state) { |
| 809 |
path.unshift('$state'); |
| 810 |
} |
| 811 |
} |
| 812 |
else { |
| 813 |
|
| 814 |
path.unshift('state'); |
| 815 |
} |
| 816 |
isTimelineActive = false; |
| 817 |
payload.set(inspectedStore, path, payload.state.value); |
| 818 |
isTimelineActive = true; |
| 819 |
} |
| 820 |
}); |
| 821 |
api.on.editComponentState((payload) => { |
| 822 |
if (payload.type.startsWith('🍍')) { |
| 823 |
const storeId = payload.type.replace(/^🍍\s*/, ''); |
| 824 |
const store = pinia._s.get(storeId); |
| 825 |
if (!store) { |
| 826 |
return toastMessage(`store "${storeId}" not found`, 'error'); |
| 827 |
} |
| 828 |
const { path } = payload; |
| 829 |
if (path[0] !== 'state') { |
| 830 |
return toastMessage(`Invalid path for store "${storeId}":\n${path}\nOnly state can be modified.`); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
path[0] = '$state'; |
| 835 |
isTimelineActive = false; |
| 836 |
payload.set(store, path, payload.state.value); |
| 837 |
isTimelineActive = true; |
| 838 |
} |
| 839 |
}); |
| 840 |
}); |
| 841 |
} |
| 842 |
function addStoreToDevtools(app, store) { |
| 843 |
if (!componentStateTypes.includes(getStoreType(store.$id))) { |
| 844 |
componentStateTypes.push(getStoreType(store.$id)); |
| 845 |
} |
| 846 |
setupDevtoolsPlugin({ |
| 847 |
id: 'dev.esm.pinia', |
| 848 |
label: 'Pinia 🍍', |
| 849 |
logo: 'https://pinia.vuejs.org/logo.svg', |
| 850 |
packageName: 'pinia', |
| 851 |
homepage: 'https://pinia.vuejs.org', |
| 852 |
componentStateTypes, |
| 853 |
app, |
| 854 |
settings: { |
| 855 |
logStoreChanges: { |
| 856 |
label: 'Notify about new/deleted stores', |
| 857 |
type: 'boolean', |
| 858 |
defaultValue: true, |
| 859 |
}, |
| 860 |
|
| 861 |
️', |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
}, |
| 866 |
}, (api) => { |
| 867 |
|
| 868 |
const now = typeof api.now === 'function' ? api.now.bind(api) : Date.now; |
| 869 |
store.$onAction(({ after, onError, name, args }) => { |
| 870 |
const groupId = runningActionId++; |
| 871 |
api.addTimelineEvent({ |
| 872 |
layerId: MUTATIONS_LAYER_ID, |
| 873 |
event: { |
| 874 |
time: now(), |
| 875 |
title: '🛫 ' + name, |
| 876 |
subtitle: 'start', |
| 877 |
data: { |
| 878 |
store: formatDisplay(store.$id), |
| 879 |
action: formatDisplay(name), |
| 880 |
args, |
| 881 |
}, |
| 882 |
groupId, |
| 883 |
}, |
| 884 |
}); |
| 885 |
after((result) => { |
| 886 |
activeAction = undefined; |
| 887 |
api.addTimelineEvent({ |
| 888 |
layerId: MUTATIONS_LAYER_ID, |
| 889 |
event: { |
| 890 |
time: now(), |
| 891 |
title: '🛬 ' + name, |
| 892 |
subtitle: 'end', |
| 893 |
data: { |
| 894 |
store: formatDisplay(store.$id), |
| 895 |
action: formatDisplay(name), |
| 896 |
args, |
| 897 |
result, |
| 898 |
}, |
| 899 |
groupId, |
| 900 |
}, |
| 901 |
}); |
| 902 |
}); |
| 903 |
onError((error) => { |
| 904 |
activeAction = undefined; |
| 905 |
api.addTimelineEvent({ |
| 906 |
layerId: MUTATIONS_LAYER_ID, |
| 907 |
event: { |
| 908 |
time: now(), |
| 909 |
logType: 'error', |
| 910 |
title: '💥 ' + name, |
| 911 |
subtitle: 'end', |
| 912 |
data: { |
| 913 |
store: formatDisplay(store.$id), |
| 914 |
action: formatDisplay(name), |
| 915 |
args, |
| 916 |
error, |
| 917 |
}, |
| 918 |
groupId, |
| 919 |
}, |
| 920 |
}); |
| 921 |
}); |
| 922 |
}, true); |
| 923 |
store._customProperties.forEach((name) => { |
| 924 |
vueDemi.watch(() => vueDemi.unref(store[name]), (newValue, oldValue) => { |
| 925 |
api.notifyComponentUpdate(); |
| 926 |
api.sendInspectorState(INSPECTOR_ID); |
| 927 |
if (isTimelineActive) { |
| 928 |
api.addTimelineEvent({ |
| 929 |
layerId: MUTATIONS_LAYER_ID, |
| 930 |
event: { |
| 931 |
time: now(), |
| 932 |
title: 'Change', |
| 933 |
subtitle: name, |
| 934 |
data: { |
| 935 |
newValue, |
| 936 |
oldValue, |
| 937 |
}, |
| 938 |
groupId: activeAction, |
| 939 |
}, |
| 940 |
}); |
| 941 |
} |
| 942 |
}, { deep: true }); |
| 943 |
}); |
| 944 |
store.$subscribe(({ events, type }, state) => { |
| 945 |
api.notifyComponentUpdate(); |
| 946 |
api.sendInspectorState(INSPECTOR_ID); |
| 947 |
if (!isTimelineActive) |
| 948 |
return; |
| 949 |
|
| 950 |
const eventData = { |
| 951 |
time: now(), |
| 952 |
title: formatMutationType(type), |
| 953 |
data: { |
| 954 |
store: formatDisplay(store.$id), |
| 955 |
...formatEventData(events), |
| 956 |
}, |
| 957 |
groupId: activeAction, |
| 958 |
}; |
| 959 |
|
| 960 |
activeAction = undefined; |
| 961 |
if (type === exports.MutationType.patchFunction) { |
| 962 |
eventData.subtitle = '⤵️'; |
| 963 |
} |
| 964 |
else if (type === exports.MutationType.patchObject) { |
| 965 |
eventData.subtitle = '🧩'; |
| 966 |
} |
| 967 |
else if (events && !Array.isArray(events)) { |
| 968 |
eventData.subtitle = events.type; |
| 969 |
} |
| 970 |
if (events) { |
| 971 |
eventData.data['rawEvent(s)'] = { |
| 972 |
_custom: { |
| 973 |
display: 'DebuggerEvent', |
| 974 |
type: 'object', |
| 975 |
tooltip: 'raw DebuggerEvent[]', |
| 976 |
value: events, |
| 977 |
}, |
| 978 |
}; |
| 979 |
} |
| 980 |
api.addTimelineEvent({ |
| 981 |
layerId: MUTATIONS_LAYER_ID, |
| 982 |
event: eventData, |
| 983 |
}); |
| 984 |
}, { detached: true, flush: 'sync' }); |
| 985 |
const hotUpdate = store._hotUpdate; |
| 986 |
store._hotUpdate = vueDemi.markRaw((newStore) => { |
| 987 |
hotUpdate(newStore); |
| 988 |
api.addTimelineEvent({ |
| 989 |
layerId: MUTATIONS_LAYER_ID, |
| 990 |
event: { |
| 991 |
time: now(), |
| 992 |
title: '🔥 ' + store.$id, |
| 993 |
subtitle: 'HMR update', |
| 994 |
data: { |
| 995 |
store: formatDisplay(store.$id), |
| 996 |
info: formatDisplay(`HMR update`), |
| 997 |
}, |
| 998 |
}, |
| 999 |
}); |
| 1000 |
|
| 1001 |
api.notifyComponentUpdate(); |
| 1002 |
api.sendInspectorTree(INSPECTOR_ID); |
| 1003 |
api.sendInspectorState(INSPECTOR_ID); |
| 1004 |
}); |
| 1005 |
const { $dispose } = store; |
| 1006 |
store.$dispose = () => { |
| 1007 |
$dispose(); |
| 1008 |
api.notifyComponentUpdate(); |
| 1009 |
api.sendInspectorTree(INSPECTOR_ID); |
| 1010 |
api.sendInspectorState(INSPECTOR_ID); |
| 1011 |
api.getSettings().logStoreChanges && |
| 1012 |
toastMessage(`Disposed "${store.$id}" store 🗑`); |
| 1013 |
}; |
| 1014 |
|
| 1015 |
api.notifyComponentUpdate(); |
| 1016 |
api.sendInspectorTree(INSPECTOR_ID); |
| 1017 |
api.sendInspectorState(INSPECTOR_ID); |
| 1018 |
api.getSettings().logStoreChanges && |
| 1019 |
toastMessage(`"${store.$id}" store installed 🆕`); |
| 1020 |
}); |
| 1021 |
} |
| 1022 |
let runningActionId = 0; |
| 1023 |
let activeAction; |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
function patchActionForGrouping(store, actionNames) { |
| 1033 |
|
| 1034 |
const actions = actionNames.reduce((storeActions, actionName) => { |
| 1035 |
|
| 1036 |
storeActions[actionName] = vueDemi.toRaw(store)[actionName]; |
| 1037 |
return storeActions; |
| 1038 |
}, {}); |
| 1039 |
for (const actionName in actions) { |
| 1040 |
store[actionName] = function () { |
| 1041 |
|
| 1042 |
|
| 1043 |
const _actionId = runningActionId; |
| 1044 |
const trackedStore = new Proxy(store, { |
| 1045 |
get(...args) { |
| 1046 |
activeAction = _actionId; |
| 1047 |
return Reflect.get(...args); |
| 1048 |
}, |
| 1049 |
set(...args) { |
| 1050 |
activeAction = _actionId; |
| 1051 |
return Reflect.set(...args); |
| 1052 |
}, |
| 1053 |
}); |
| 1054 |
return actions[actionName].apply(trackedStore, arguments); |
| 1055 |
}; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
function devtoolsPlugin({ app, store, options }) { |
| 1062 |
|
| 1063 |
if (store.$id.startsWith('__hot:')) { |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if (options.state) { |
| 1068 |
store._isOptionsAPI = true; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
if (typeof options.state === 'function') { |
| 1073 |
patchActionForGrouping( |
| 1074 |
|
| 1075 |
store, Object.keys(options.actions)); |
| 1076 |
const originalHotUpdate = store._hotUpdate; |
| 1077 |
|
| 1078 |
vueDemi.toRaw(store)._hotUpdate = function (newStore) { |
| 1079 |
originalHotUpdate.apply(this, arguments); |
| 1080 |
patchActionForGrouping(store, Object.keys(newStore._hmrPayload.actions)); |
| 1081 |
}; |
| 1082 |
} |
| 1083 |
addStoreToDevtools(app, |
| 1084 |
|
| 1085 |
store); |
| 1086 |
} |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
function createPinia() { |
| 1092 |
const scope = vueDemi.effectScope(true); |
| 1093 |
|
| 1094 |
|
| 1095 |
const state = scope.run(() => vueDemi.ref({})); |
| 1096 |
let _p = []; |
| 1097 |
|
| 1098 |
let toBeInstalled = []; |
| 1099 |
const pinia = vueDemi.markRaw({ |
| 1100 |
install(app) { |
| 1101 |
|
| 1102 |
|
| 1103 |
setActivePinia(pinia); |
| 1104 |
if (!vueDemi.isVue2) { |
| 1105 |
pinia._a = app; |
| 1106 |
app.provide(piniaSymbol, pinia); |
| 1107 |
app.config.globalProperties.$pinia = pinia; |
| 1108 |
|
| 1109 |
if (IS_CLIENT) { |
| 1110 |
registerPiniaDevtools(app, pinia); |
| 1111 |
} |
| 1112 |
toBeInstalled.forEach((plugin) => _p.push(plugin)); |
| 1113 |
toBeInstalled = []; |
| 1114 |
} |
| 1115 |
}, |
| 1116 |
use(plugin) { |
| 1117 |
if (!this._a && !vueDemi.isVue2) { |
| 1118 |
toBeInstalled.push(plugin); |
| 1119 |
} |
| 1120 |
else { |
| 1121 |
_p.push(plugin); |
| 1122 |
} |
| 1123 |
return this; |
| 1124 |
}, |
| 1125 |
_p, |
| 1126 |
|
| 1127 |
|
| 1128 |
_a: null, |
| 1129 |
_e: scope, |
| 1130 |
_s: new Map(), |
| 1131 |
state, |
| 1132 |
}); |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
if (IS_CLIENT && !false) { |
| 1137 |
pinia.use(devtoolsPlugin); |
| 1138 |
} |
| 1139 |
return pinia; |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
const isUseStore = (fn) => { |
| 1149 |
return typeof fn === 'function' && typeof fn.$id === 'string'; |
| 1150 |
}; |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
function patchObject(newState, oldState) { |
| 1161 |
|
| 1162 |
for (const key in oldState) { |
| 1163 |
const subPatch = oldState[key]; |
| 1164 |
|
| 1165 |
if (!(key in newState)) { |
| 1166 |
continue; |
| 1167 |
} |
| 1168 |
const targetValue = newState[key]; |
| 1169 |
if (isPlainObject(targetValue) && |
| 1170 |
isPlainObject(subPatch) && |
| 1171 |
!vueDemi.isRef(subPatch) && |
| 1172 |
!vueDemi.isReactive(subPatch)) { |
| 1173 |
newState[key] = patchObject(targetValue, subPatch); |
| 1174 |
} |
| 1175 |
else { |
| 1176 |
|
| 1177 |
|
| 1178 |
if (vueDemi.isVue2) { |
| 1179 |
vueDemi.set(newState, key, subPatch); |
| 1180 |
} |
| 1181 |
else { |
| 1182 |
newState[key] = subPatch; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
return newState; |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
function acceptHMRUpdate(initialUseStore, hot) { |
| 1203 |
return (newModule) => { |
| 1204 |
const pinia = hot.data.pinia || initialUseStore._pinia; |
| 1205 |
if (!pinia) { |
| 1206 |
|
| 1207 |
return; |
| 1208 |
} |
| 1209 |
|
| 1210 |
hot.data.pinia = pinia; |
| 1211 |
|
| 1212 |
for (const exportName in newModule) { |
| 1213 |
const useStore = newModule[exportName]; |
| 1214 |
|
| 1215 |
if (isUseStore(useStore) && pinia._s.has(useStore.$id)) { |
| 1216 |
|
| 1217 |
const id = useStore.$id; |
| 1218 |
if (id !== initialUseStore.$id) { |
| 1219 |
console.warn(`The id of the store changed from "${initialUseStore.$id}" to "${id}". Reloading.`); |
| 1220 |
|
| 1221 |
return hot.invalidate(); |
| 1222 |
} |
| 1223 |
const existingStore = pinia._s.get(id); |
| 1224 |
if (!existingStore) { |
| 1225 |
console.log(`[Pinia]: skipping hmr because store doesn't exist yet`); |
| 1226 |
return; |
| 1227 |
} |
| 1228 |
useStore(pinia, existingStore); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
}; |
| 1232 |
} |
| 1233 |
|
| 1234 |
const noop = () => { }; |
| 1235 |
function addSubscription(subscriptions, callback, detached, onCleanup = noop) { |
| 1236 |
subscriptions.push(callback); |
| 1237 |
const removeSubscription = () => { |
| 1238 |
const idx = subscriptions.indexOf(callback); |
| 1239 |
if (idx > -1) { |
| 1240 |
subscriptions.splice(idx, 1); |
| 1241 |
onCleanup(); |
| 1242 |
} |
| 1243 |
}; |
| 1244 |
if (!detached && vueDemi.getCurrentInstance()) { |
| 1245 |
vueDemi.onUnmounted(removeSubscription); |
| 1246 |
} |
| 1247 |
return removeSubscription; |
| 1248 |
} |
| 1249 |
function triggerSubscriptions(subscriptions, ...args) { |
| 1250 |
subscriptions.slice().forEach((callback) => { |
| 1251 |
callback(...args); |
| 1252 |
}); |
| 1253 |
} |
| 1254 |
|
| 1255 |
function mergeReactiveObjects(target, patchToApply) { |
| 1256 |
|
| 1257 |
for (const key in patchToApply) { |
| 1258 |
if (!patchToApply.hasOwnProperty(key)) |
| 1259 |
continue; |
| 1260 |
const subPatch = patchToApply[key]; |
| 1261 |
const targetValue = target[key]; |
| 1262 |
if (isPlainObject(targetValue) && |
| 1263 |
isPlainObject(subPatch) && |
| 1264 |
target.hasOwnProperty(key) && |
| 1265 |
!vueDemi.isRef(subPatch) && |
| 1266 |
!vueDemi.isReactive(subPatch)) { |
| 1267 |
target[key] = mergeReactiveObjects(targetValue, subPatch); |
| 1268 |
} |
| 1269 |
else { |
| 1270 |
|
| 1271 |
target[key] = subPatch; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
return target; |
| 1275 |
} |
| 1276 |
const skipHydrateSymbol = Symbol('pinia:skipHydration') |
| 1277 |
; |
| 1278 |
const skipHydrateMap = new WeakMap(); |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
function skipHydrate(obj) { |
| 1287 |
return vueDemi.isVue2 |
| 1288 |
? |
| 1289 |
skipHydrateMap.set(obj, 1) && obj |
| 1290 |
: Object.defineProperty(obj, skipHydrateSymbol, {}); |
| 1291 |
} |
| 1292 |
function shouldHydrate(obj) { |
| 1293 |
return vueDemi.isVue2 |
| 1294 |
? !skipHydrateMap.has(obj) |
| 1295 |
: !isPlainObject(obj) || !obj.hasOwnProperty(skipHydrateSymbol); |
| 1296 |
} |
| 1297 |
const { assign } = Object; |
| 1298 |
function isComputed(o) { |
| 1299 |
return !!(vueDemi.isRef(o) && o.effect); |
| 1300 |
} |
| 1301 |
function createOptionsStore(id, options, pinia, hot) { |
| 1302 |
const { state, actions, getters } = options; |
| 1303 |
const initialState = pinia.state.value[id]; |
| 1304 |
let store; |
| 1305 |
function setup() { |
| 1306 |
if (!initialState && (!hot)) { |
| 1307 |
|
| 1308 |
if (vueDemi.isVue2) { |
| 1309 |
vueDemi.set(pinia.state.value, id, state ? state() : {}); |
| 1310 |
} |
| 1311 |
else { |
| 1312 |
pinia.state.value[id] = state ? state() : {}; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
const localState = hot |
| 1317 |
? |
| 1318 |
vueDemi.toRefs(vueDemi.ref(state ? state() : {}).value) |
| 1319 |
: vueDemi.toRefs(pinia.state.value[id]); |
| 1320 |
return assign(localState, actions, Object.keys(getters || {}).reduce((computedGetters, name) => { |
| 1321 |
if (name in localState) { |
| 1322 |
console.warn(`[🍍]: A getter cannot have the same name as another state property. Rename one of them. Found with "${name}" in store "${id}".`); |
| 1323 |
} |
| 1324 |
computedGetters[name] = vueDemi.markRaw(vueDemi.computed(() => { |
| 1325 |
setActivePinia(pinia); |
| 1326 |
|
| 1327 |
const store = pinia._s.get(id); |
| 1328 |
|
| 1329 |
|
| 1330 |
if (vueDemi.isVue2 && !store._r) |
| 1331 |
return; |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
return getters[name].call(store, store); |
| 1336 |
})); |
| 1337 |
return computedGetters; |
| 1338 |
}, {})); |
| 1339 |
} |
| 1340 |
store = createSetupStore(id, setup, options, pinia, hot, true); |
| 1341 |
store.$reset = function $reset() { |
| 1342 |
const newState = state ? state() : {}; |
| 1343 |
|
| 1344 |
this.$patch(($state) => { |
| 1345 |
assign($state, newState); |
| 1346 |
}); |
| 1347 |
}; |
| 1348 |
return store; |
| 1349 |
} |
| 1350 |
function createSetupStore($id, setup, options = {}, pinia, hot, isOptionsStore) { |
| 1351 |
let scope; |
| 1352 |
const optionsForPlugin = assign({ actions: {} }, options); |
| 1353 |
|
| 1354 |
|
| 1355 |
if (!pinia._e.active) { |
| 1356 |
throw new Error('Pinia destroyed'); |
| 1357 |
} |
| 1358 |
|
| 1359 |
const $subscribeOptions = { |
| 1360 |
deep: true, |
| 1361 |
|
| 1362 |
}; |
| 1363 |
|
| 1364 |
if (!vueDemi.isVue2) { |
| 1365 |
$subscribeOptions.onTrigger = (event) => { |
| 1366 |
|
| 1367 |
if (isListening) { |
| 1368 |
debuggerEvents = event; |
| 1369 |
|
| 1370 |
} |
| 1371 |
else if (isListening == false && !store._hotUpdating) { |
| 1372 |
|
| 1373 |
|
| 1374 |
if (Array.isArray(debuggerEvents)) { |
| 1375 |
debuggerEvents.push(event); |
| 1376 |
} |
| 1377 |
else { |
| 1378 |
console.error('🍍 debuggerEvents should be an array. This is most likely an internal Pinia bug.'); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
}; |
| 1382 |
} |
| 1383 |
|
| 1384 |
let isListening; |
| 1385 |
let isSyncListening; |
| 1386 |
let subscriptions = vueDemi.markRaw([]); |
| 1387 |
let actionSubscriptions = vueDemi.markRaw([]); |
| 1388 |
let debuggerEvents; |
| 1389 |
const initialState = pinia.state.value[$id]; |
| 1390 |
|
| 1391 |
|
| 1392 |
if (!isOptionsStore && !initialState && (!hot)) { |
| 1393 |
|
| 1394 |
if (vueDemi.isVue2) { |
| 1395 |
vueDemi.set(pinia.state.value, $id, {}); |
| 1396 |
} |
| 1397 |
else { |
| 1398 |
pinia.state.value[$id] = {}; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
const hotState = vueDemi.ref({}); |
| 1402 |
|
| 1403 |
|
| 1404 |
let activeListener; |
| 1405 |
function $patch(partialStateOrMutator) { |
| 1406 |
let subscriptionMutation; |
| 1407 |
isListening = isSyncListening = false; |
| 1408 |
|
| 1409 |
|
| 1410 |
{ |
| 1411 |
debuggerEvents = []; |
| 1412 |
} |
| 1413 |
if (typeof partialStateOrMutator === 'function') { |
| 1414 |
partialStateOrMutator(pinia.state.value[$id]); |
| 1415 |
subscriptionMutation = { |
| 1416 |
type: exports.MutationType.patchFunction, |
| 1417 |
storeId: $id, |
| 1418 |
events: debuggerEvents, |
| 1419 |
}; |
| 1420 |
} |
| 1421 |
else { |
| 1422 |
mergeReactiveObjects(pinia.state.value[$id], partialStateOrMutator); |
| 1423 |
subscriptionMutation = { |
| 1424 |
type: exports.MutationType.patchObject, |
| 1425 |
payload: partialStateOrMutator, |
| 1426 |
storeId: $id, |
| 1427 |
events: debuggerEvents, |
| 1428 |
}; |
| 1429 |
} |
| 1430 |
const myListenerId = (activeListener = Symbol()); |
| 1431 |
vueDemi.nextTick().then(() => { |
| 1432 |
if (activeListener === myListenerId) { |
| 1433 |
isListening = true; |
| 1434 |
} |
| 1435 |
}); |
| 1436 |
isSyncListening = true; |
| 1437 |
|
| 1438 |
triggerSubscriptions(subscriptions, subscriptionMutation, pinia.state.value[$id]); |
| 1439 |
} |
| 1440 |
|
| 1441 |
const $reset = () => { |
| 1442 |
throw new Error(`🍍: Store "${$id}" is built using the setup syntax and does not implement $reset().`); |
| 1443 |
} |
| 1444 |
; |
| 1445 |
function $dispose() { |
| 1446 |
scope.stop(); |
| 1447 |
subscriptions = []; |
| 1448 |
actionSubscriptions = []; |
| 1449 |
pinia._s.delete($id); |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
function wrapAction(name, action) { |
| 1459 |
return function () { |
| 1460 |
setActivePinia(pinia); |
| 1461 |
const args = Array.from(arguments); |
| 1462 |
const afterCallbackList = []; |
| 1463 |
const onErrorCallbackList = []; |
| 1464 |
function after(callback) { |
| 1465 |
afterCallbackList.push(callback); |
| 1466 |
} |
| 1467 |
function onError(callback) { |
| 1468 |
onErrorCallbackList.push(callback); |
| 1469 |
} |
| 1470 |
|
| 1471 |
triggerSubscriptions(actionSubscriptions, { |
| 1472 |
args, |
| 1473 |
name, |
| 1474 |
store, |
| 1475 |
after, |
| 1476 |
onError, |
| 1477 |
}); |
| 1478 |
let ret; |
| 1479 |
try { |
| 1480 |
ret = action.apply(this && this.$id === $id ? this : store, args); |
| 1481 |
|
| 1482 |
} |
| 1483 |
catch (error) { |
| 1484 |
triggerSubscriptions(onErrorCallbackList, error); |
| 1485 |
throw error; |
| 1486 |
} |
| 1487 |
if (ret instanceof Promise) { |
| 1488 |
return ret |
| 1489 |
.then((value) => { |
| 1490 |
triggerSubscriptions(afterCallbackList, value); |
| 1491 |
return value; |
| 1492 |
}) |
| 1493 |
.catch((error) => { |
| 1494 |
triggerSubscriptions(onErrorCallbackList, error); |
| 1495 |
return Promise.reject(error); |
| 1496 |
}); |
| 1497 |
} |
| 1498 |
|
| 1499 |
triggerSubscriptions(afterCallbackList, ret); |
| 1500 |
return ret; |
| 1501 |
}; |
| 1502 |
} |
| 1503 |
const _hmrPayload = vueDemi.markRaw({ |
| 1504 |
actions: {}, |
| 1505 |
getters: {}, |
| 1506 |
state: [], |
| 1507 |
hotState, |
| 1508 |
}); |
| 1509 |
const partialStore = { |
| 1510 |
_p: pinia, |
| 1511 |
|
| 1512 |
$id, |
| 1513 |
$onAction: addSubscription.bind(null, actionSubscriptions), |
| 1514 |
$patch, |
| 1515 |
$reset, |
| 1516 |
$subscribe(callback, options = {}) { |
| 1517 |
const removeSubscription = addSubscription(subscriptions, callback, options.detached, () => stopWatcher()); |
| 1518 |
const stopWatcher = scope.run(() => vueDemi.watch(() => pinia.state.value[$id], (state) => { |
| 1519 |
if (options.flush === 'sync' ? isSyncListening : isListening) { |
| 1520 |
callback({ |
| 1521 |
storeId: $id, |
| 1522 |
type: exports.MutationType.direct, |
| 1523 |
events: debuggerEvents, |
| 1524 |
}, state); |
| 1525 |
} |
| 1526 |
}, assign({}, $subscribeOptions, options))); |
| 1527 |
return removeSubscription; |
| 1528 |
}, |
| 1529 |
$dispose, |
| 1530 |
}; |
| 1531 |
|
| 1532 |
if (vueDemi.isVue2) { |
| 1533 |
|
| 1534 |
partialStore._r = false; |
| 1535 |
} |
| 1536 |
const store = vueDemi.reactive(assign(IS_CLIENT |
| 1537 |
? |
| 1538 |
{ |
| 1539 |
_customProperties: vueDemi.markRaw(new Set()), |
| 1540 |
_hmrPayload, |
| 1541 |
} |
| 1542 |
: {}, partialStore |
| 1543 |
|
| 1544 |
|
| 1545 |
)); |
| 1546 |
|
| 1547 |
|
| 1548 |
pinia._s.set($id, store); |
| 1549 |
|
| 1550 |
const setupStore = pinia._e.run(() => { |
| 1551 |
scope = vueDemi.effectScope(); |
| 1552 |
return scope.run(() => setup()); |
| 1553 |
}); |
| 1554 |
|
| 1555 |
for (const key in setupStore) { |
| 1556 |
const prop = setupStore[key]; |
| 1557 |
if ((vueDemi.isRef(prop) && !isComputed(prop)) || vueDemi.isReactive(prop)) { |
| 1558 |
|
| 1559 |
if (hot) { |
| 1560 |
vueDemi.set(hotState.value, key, vueDemi.toRef(setupStore, key)); |
| 1561 |
|
| 1562 |
|
| 1563 |
} |
| 1564 |
else if (!isOptionsStore) { |
| 1565 |
|
| 1566 |
if (initialState && shouldHydrate(prop)) { |
| 1567 |
if (vueDemi.isRef(prop)) { |
| 1568 |
prop.value = initialState[key]; |
| 1569 |
} |
| 1570 |
else { |
| 1571 |
|
| 1572 |
mergeReactiveObjects(prop, initialState[key]); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
if (vueDemi.isVue2) { |
| 1578 |
vueDemi.set(pinia.state.value[$id], key, prop); |
| 1579 |
} |
| 1580 |
else { |
| 1581 |
pinia.state.value[$id][key] = prop; |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
{ |
| 1586 |
_hmrPayload.state.push(key); |
| 1587 |
} |
| 1588 |
|
| 1589 |
} |
| 1590 |
else if (typeof prop === 'function') { |
| 1591 |
|
| 1592 |
const actionValue = hot ? prop : wrapAction(key, prop); |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
if (vueDemi.isVue2) { |
| 1597 |
vueDemi.set(setupStore, key, actionValue); |
| 1598 |
} |
| 1599 |
else { |
| 1600 |
|
| 1601 |
setupStore[key] = actionValue; |
| 1602 |
} |
| 1603 |
|
| 1604 |
{ |
| 1605 |
_hmrPayload.actions[key] = prop; |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
optionsForPlugin.actions[key] = prop; |
| 1610 |
} |
| 1611 |
else { |
| 1612 |
|
| 1613 |
if (isComputed(prop)) { |
| 1614 |
_hmrPayload.getters[key] = isOptionsStore |
| 1615 |
? |
| 1616 |
options.getters[key] |
| 1617 |
: prop; |
| 1618 |
if (IS_CLIENT) { |
| 1619 |
const getters = |
| 1620 |
|
| 1621 |
setupStore._getters || (setupStore._getters = vueDemi.markRaw([])); |
| 1622 |
getters.push(key); |
| 1623 |
} |
| 1624 |
} |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
|
| 1629 |
if (vueDemi.isVue2) { |
| 1630 |
Object.keys(setupStore).forEach((key) => { |
| 1631 |
vueDemi.set(store, key, |
| 1632 |
|
| 1633 |
setupStore[key]); |
| 1634 |
}); |
| 1635 |
} |
| 1636 |
else { |
| 1637 |
assign(store, setupStore); |
| 1638 |
|
| 1639 |
|
| 1640 |
assign(vueDemi.toRaw(store), setupStore); |
| 1641 |
} |
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
Object.defineProperty(store, '$state', { |
| 1646 |
get: () => (hot ? hotState.value : pinia.state.value[$id]), |
| 1647 |
set: (state) => { |
| 1648 |
|
| 1649 |
if (hot) { |
| 1650 |
throw new Error('cannot set hotState'); |
| 1651 |
} |
| 1652 |
$patch(($state) => { |
| 1653 |
assign($state, state); |
| 1654 |
}); |
| 1655 |
}, |
| 1656 |
}); |
| 1657 |
|
| 1658 |
|
| 1659 |
{ |
| 1660 |
store._hotUpdate = vueDemi.markRaw((newStore) => { |
| 1661 |
store._hotUpdating = true; |
| 1662 |
newStore._hmrPayload.state.forEach((stateKey) => { |
| 1663 |
if (stateKey in store.$state) { |
| 1664 |
const newStateTarget = newStore.$state[stateKey]; |
| 1665 |
const oldStateSource = store.$state[stateKey]; |
| 1666 |
if (typeof newStateTarget === 'object' && |
| 1667 |
isPlainObject(newStateTarget) && |
| 1668 |
isPlainObject(oldStateSource)) { |
| 1669 |
patchObject(newStateTarget, oldStateSource); |
| 1670 |
} |
| 1671 |
else { |
| 1672 |
|
| 1673 |
newStore.$state[stateKey] = oldStateSource; |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
|
| 1678 |
vueDemi.set(store, stateKey, vueDemi.toRef(newStore.$state, stateKey)); |
| 1679 |
}); |
| 1680 |
|
| 1681 |
Object.keys(store.$state).forEach((stateKey) => { |
| 1682 |
if (!(stateKey in newStore.$state)) { |
| 1683 |
vueDemi.del(store, stateKey); |
| 1684 |
} |
| 1685 |
}); |
| 1686 |
|
| 1687 |
isListening = false; |
| 1688 |
isSyncListening = false; |
| 1689 |
pinia.state.value[$id] = vueDemi.toRef(newStore._hmrPayload, 'hotState'); |
| 1690 |
isSyncListening = true; |
| 1691 |
vueDemi.nextTick().then(() => { |
| 1692 |
isListening = true; |
| 1693 |
}); |
| 1694 |
for (const actionName in newStore._hmrPayload.actions) { |
| 1695 |
const action = newStore[actionName]; |
| 1696 |
vueDemi.set(store, actionName, wrapAction(actionName, action)); |
| 1697 |
} |
| 1698 |
|
| 1699 |
for (const getterName in newStore._hmrPayload.getters) { |
| 1700 |
const getter = newStore._hmrPayload.getters[getterName]; |
| 1701 |
const getterValue = isOptionsStore |
| 1702 |
? |
| 1703 |
vueDemi.computed(() => { |
| 1704 |
setActivePinia(pinia); |
| 1705 |
return getter.call(store, store); |
| 1706 |
}) |
| 1707 |
: getter; |
| 1708 |
vueDemi.set(store, getterName, getterValue); |
| 1709 |
} |
| 1710 |
|
| 1711 |
Object.keys(store._hmrPayload.getters).forEach((key) => { |
| 1712 |
if (!(key in newStore._hmrPayload.getters)) { |
| 1713 |
vueDemi.del(store, key); |
| 1714 |
} |
| 1715 |
}); |
| 1716 |
|
| 1717 |
Object.keys(store._hmrPayload.actions).forEach((key) => { |
| 1718 |
if (!(key in newStore._hmrPayload.actions)) { |
| 1719 |
vueDemi.del(store, key); |
| 1720 |
} |
| 1721 |
}); |
| 1722 |
|
| 1723 |
store._hmrPayload = newStore._hmrPayload; |
| 1724 |
store._getters = newStore._getters; |
| 1725 |
store._hotUpdating = false; |
| 1726 |
}); |
| 1727 |
const nonEnumerable = { |
| 1728 |
writable: true, |
| 1729 |
configurable: true, |
| 1730 |
|
| 1731 |
enumerable: false, |
| 1732 |
}; |
| 1733 |
if (IS_CLIENT) { |
| 1734 |
['_p', '_hmrPayload', '_getters', '_customProperties'].forEach((p) => { |
| 1735 |
Object.defineProperty(store, p, { |
| 1736 |
value: store[p], |
| 1737 |
...nonEnumerable, |
| 1738 |
}); |
| 1739 |
}); |
| 1740 |
} |
| 1741 |
} |
| 1742 |
|
| 1743 |
if (vueDemi.isVue2) { |
| 1744 |
|
| 1745 |
store._r = true; |
| 1746 |
} |
| 1747 |
|
| 1748 |
pinia._p.forEach((extender) => { |
| 1749 |
|
| 1750 |
if (IS_CLIENT) { |
| 1751 |
const extensions = scope.run(() => extender({ |
| 1752 |
store, |
| 1753 |
app: pinia._a, |
| 1754 |
pinia, |
| 1755 |
options: optionsForPlugin, |
| 1756 |
})); |
| 1757 |
Object.keys(extensions || {}).forEach((key) => store._customProperties.add(key)); |
| 1758 |
assign(store, extensions); |
| 1759 |
} |
| 1760 |
else { |
| 1761 |
assign(store, scope.run(() => extender({ |
| 1762 |
store, |
| 1763 |
app: pinia._a, |
| 1764 |
pinia, |
| 1765 |
options: optionsForPlugin, |
| 1766 |
}))); |
| 1767 |
} |
| 1768 |
}); |
| 1769 |
if (store.$state && |
| 1770 |
typeof store.$state === 'object' && |
| 1771 |
typeof store.$state.constructor === 'function' && |
| 1772 |
!store.$state.constructor.toString().includes('[native code]')) { |
| 1773 |
console.warn(`[🍍]: The "state" must be a plain object. It cannot be\n` + |
| 1774 |
`\tstate: () => new MyClass()\n` + |
| 1775 |
`Found in store "${store.$id}".`); |
| 1776 |
} |
| 1777 |
|
| 1778 |
if (initialState && |
| 1779 |
isOptionsStore && |
| 1780 |
options.hydrate) { |
| 1781 |
options.hydrate(store.$state, initialState); |
| 1782 |
} |
| 1783 |
isListening = true; |
| 1784 |
isSyncListening = true; |
| 1785 |
return store; |
| 1786 |
} |
| 1787 |
function defineStore( |
| 1788 |
|
| 1789 |
idOrOptions, setup, setupOptions) { |
| 1790 |
let id; |
| 1791 |
let options; |
| 1792 |
const isSetupStore = typeof setup === 'function'; |
| 1793 |
if (typeof idOrOptions === 'string') { |
| 1794 |
id = idOrOptions; |
| 1795 |
|
| 1796 |
options = isSetupStore ? setupOptions : setup; |
| 1797 |
} |
| 1798 |
else { |
| 1799 |
options = idOrOptions; |
| 1800 |
id = idOrOptions.id; |
| 1801 |
} |
| 1802 |
function useStore(pinia, hot) { |
| 1803 |
const currentInstance = vueDemi.getCurrentInstance(); |
| 1804 |
pinia = |
| 1805 |
|
| 1806 |
|
| 1807 |
(pinia) || |
| 1808 |
(currentInstance && vueDemi.inject(piniaSymbol)); |
| 1809 |
if (pinia) |
| 1810 |
setActivePinia(pinia); |
| 1811 |
if (!activePinia) { |
| 1812 |
throw new Error(`[🍍]: getActivePinia was called with no active Pinia. Did you forget to install pinia?\n` + |
| 1813 |
`\tconst pinia = createPinia()\n` + |
| 1814 |
`\tapp.use(pinia)\n` + |
| 1815 |
`This will fail in production.`); |
| 1816 |
} |
| 1817 |
pinia = activePinia; |
| 1818 |
if (!pinia._s.has(id)) { |
| 1819 |
|
| 1820 |
if (isSetupStore) { |
| 1821 |
createSetupStore(id, setup, options, pinia); |
| 1822 |
} |
| 1823 |
else { |
| 1824 |
createOptionsStore(id, options, pinia); |
| 1825 |
} |
| 1826 |
|
| 1827 |
{ |
| 1828 |
|
| 1829 |
useStore._pinia = pinia; |
| 1830 |
} |
| 1831 |
} |
| 1832 |
const store = pinia._s.get(id); |
| 1833 |
if (hot) { |
| 1834 |
const hotId = '__hot:' + id; |
| 1835 |
const newStore = isSetupStore |
| 1836 |
? createSetupStore(hotId, setup, options, pinia, true) |
| 1837 |
: createOptionsStore(hotId, assign({}, options), pinia, true); |
| 1838 |
hot._hotUpdate(newStore); |
| 1839 |
|
| 1840 |
delete pinia.state.value[hotId]; |
| 1841 |
pinia._s.delete(hotId); |
| 1842 |
} |
| 1843 |
|
| 1844 |
if (IS_CLIENT && |
| 1845 |
currentInstance && |
| 1846 |
currentInstance.proxy && |
| 1847 |
|
| 1848 |
!hot) { |
| 1849 |
const vm = currentInstance.proxy; |
| 1850 |
const cache = '_pStores' in vm ? vm._pStores : (vm._pStores = {}); |
| 1851 |
cache[id] = store; |
| 1852 |
} |
| 1853 |
|
| 1854 |
return store; |
| 1855 |
} |
| 1856 |
useStore.$id = id; |
| 1857 |
return useStore; |
| 1858 |
} |
| 1859 |
|
| 1860 |
let mapStoreSuffix = 'Store'; |
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
function setMapStoreSuffix(suffix |
| 1869 |
) { |
| 1870 |
mapStoreSuffix = suffix; |
| 1871 |
} |
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
function mapStores(...stores) { |
| 1895 |
if (Array.isArray(stores[0])) { |
| 1896 |
console.warn(`[🍍]: Directly pass all stores to "mapStores()" without putting them in an array:\n` + |
| 1897 |
`Replace\n` + |
| 1898 |
`\tmapStores([useAuthStore, useCartStore])\n` + |
| 1899 |
`with\n` + |
| 1900 |
`\tmapStores(useAuthStore, useCartStore)\n` + |
| 1901 |
`This will fail in production if not fixed.`); |
| 1902 |
stores = stores[0]; |
| 1903 |
} |
| 1904 |
return stores.reduce((reduced, useStore) => { |
| 1905 |
|
| 1906 |
reduced[useStore.$id + mapStoreSuffix] = function () { |
| 1907 |
return useStore(this.$pinia); |
| 1908 |
}; |
| 1909 |
return reduced; |
| 1910 |
}, {}); |
| 1911 |
} |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
function mapState(useStore, keysOrMapper) { |
| 1921 |
return Array.isArray(keysOrMapper) |
| 1922 |
? keysOrMapper.reduce((reduced, key) => { |
| 1923 |
reduced[key] = function () { |
| 1924 |
return useStore(this.$pinia)[key]; |
| 1925 |
}; |
| 1926 |
return reduced; |
| 1927 |
}, {}) |
| 1928 |
: Object.keys(keysOrMapper).reduce((reduced, key) => { |
| 1929 |
|
| 1930 |
reduced[key] = function () { |
| 1931 |
const store = useStore(this.$pinia); |
| 1932 |
const storeKey = keysOrMapper[key]; |
| 1933 |
|
| 1934 |
|
| 1935 |
return typeof storeKey === 'function' |
| 1936 |
? storeKey.call(this, store) |
| 1937 |
: store[storeKey]; |
| 1938 |
}; |
| 1939 |
return reduced; |
| 1940 |
}, {}); |
| 1941 |
} |
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
const mapGetters = mapState; |
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
function mapActions(useStore, keysOrMapper) { |
| 1956 |
return Array.isArray(keysOrMapper) |
| 1957 |
? keysOrMapper.reduce((reduced, key) => { |
| 1958 |
|
| 1959 |
reduced[key] = function (...args) { |
| 1960 |
return useStore(this.$pinia)[key](...args); |
| 1961 |
}; |
| 1962 |
return reduced; |
| 1963 |
}, {}) |
| 1964 |
: Object.keys(keysOrMapper).reduce((reduced, key) => { |
| 1965 |
|
| 1966 |
reduced[key] = function (...args) { |
| 1967 |
return useStore(this.$pinia)[keysOrMapper[key]](...args); |
| 1968 |
}; |
| 1969 |
return reduced; |
| 1970 |
}, {}); |
| 1971 |
} |
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
function mapWritableState(useStore, keysOrMapper) { |
| 1981 |
return Array.isArray(keysOrMapper) |
| 1982 |
? keysOrMapper.reduce((reduced, key) => { |
| 1983 |
|
| 1984 |
reduced[key] = { |
| 1985 |
get() { |
| 1986 |
return useStore(this.$pinia)[key]; |
| 1987 |
}, |
| 1988 |
set(value) { |
| 1989 |
|
| 1990 |
return (useStore(this.$pinia)[key] = value); |
| 1991 |
}, |
| 1992 |
}; |
| 1993 |
return reduced; |
| 1994 |
}, {}) |
| 1995 |
: Object.keys(keysOrMapper).reduce((reduced, key) => { |
| 1996 |
|
| 1997 |
reduced[key] = { |
| 1998 |
get() { |
| 1999 |
return useStore(this.$pinia)[keysOrMapper[key]]; |
| 2000 |
}, |
| 2001 |
set(value) { |
| 2002 |
|
| 2003 |
return (useStore(this.$pinia)[keysOrMapper[key]] = value); |
| 2004 |
}, |
| 2005 |
}; |
| 2006 |
return reduced; |
| 2007 |
}, {}); |
| 2008 |
} |
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
function storeToRefs(store) { |
| 2019 |
|
| 2020 |
|
| 2021 |
if (vueDemi.isVue2) { |
| 2022 |
|
| 2023 |
return vueDemi.toRefs(store); |
| 2024 |
} |
| 2025 |
else { |
| 2026 |
store = vueDemi.toRaw(store); |
| 2027 |
const refs = {}; |
| 2028 |
for (const key in store) { |
| 2029 |
const value = store[key]; |
| 2030 |
if (vueDemi.isRef(value) || vueDemi.isReactive(value)) { |
| 2031 |
|
| 2032 |
refs[key] = |
| 2033 |
|
| 2034 |
vueDemi.toRef(store, key); |
| 2035 |
} |
| 2036 |
} |
| 2037 |
return refs; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
const PiniaVuePlugin = function (_Vue) { |
| 2064 |
|
| 2065 |
|
| 2066 |
_Vue.mixin({ |
| 2067 |
beforeCreate() { |
| 2068 |
const options = this.$options; |
| 2069 |
if (options.pinia) { |
| 2070 |
const pinia = options.pinia; |
| 2071 |
|
| 2072 |
|
| 2073 |
if (!this._provided) { |
| 2074 |
const provideCache = {}; |
| 2075 |
Object.defineProperty(this, '_provided', { |
| 2076 |
get: () => provideCache, |
| 2077 |
set: (v) => Object.assign(provideCache, v), |
| 2078 |
}); |
| 2079 |
} |
| 2080 |
this._provided[piniaSymbol] = pinia; |
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
if (!this.$pinia) { |
| 2085 |
this.$pinia = pinia; |
| 2086 |
} |
| 2087 |
pinia._a = this; |
| 2088 |
if (IS_CLIENT) { |
| 2089 |
|
| 2090 |
|
| 2091 |
setActivePinia(pinia); |
| 2092 |
{ |
| 2093 |
registerPiniaDevtools(pinia._a, pinia); |
| 2094 |
} |
| 2095 |
} |
| 2096 |
} |
| 2097 |
else if (!this.$pinia && options.parent && options.parent.$pinia) { |
| 2098 |
this.$pinia = options.parent.$pinia; |
| 2099 |
} |
| 2100 |
}, |
| 2101 |
destroyed() { |
| 2102 |
delete this._pStores; |
| 2103 |
}, |
| 2104 |
}); |
| 2105 |
}; |
| 2106 |
|
| 2107 |
exports.PiniaVuePlugin = PiniaVuePlugin; |
| 2108 |
exports.acceptHMRUpdate = acceptHMRUpdate; |
| 2109 |
exports.createPinia = createPinia; |
| 2110 |
exports.defineStore = defineStore; |
| 2111 |
exports.getActivePinia = getActivePinia; |
| 2112 |
exports.mapActions = mapActions; |
| 2113 |
exports.mapGetters = mapGetters; |
| 2114 |
exports.mapState = mapState; |
| 2115 |
exports.mapStores = mapStores; |
| 2116 |
exports.mapWritableState = mapWritableState; |
| 2117 |
exports.setActivePinia = setActivePinia; |
| 2118 |
exports.setMapStoreSuffix = setMapStoreSuffix; |
| 2119 |
exports.skipHydrate = skipHydrate; |
| 2120 |
exports.storeToRefs = storeToRefs; |
| 2121 |
|
| 2122 |
Object.defineProperty(exports, '__esModule', { value: true }); |
| 2123 |
|
| 2124 |
return exports; |
| 2125 |
|
| 2126 |
})({}, VueDemi); |