You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
35 lines
1.1 KiB
import { toRef, isRef } from 'vue';
|
|
import { u as useNuxtApp } from './server.mjs';
|
|
|
|
const useStateKeyPrefix = "$s";
|
|
function useState(...args) {
|
|
const autoKey = typeof args[args.length - 1] === "string" ? args.pop() : void 0;
|
|
if (typeof args[0] !== "string") {
|
|
args.unshift(autoKey);
|
|
}
|
|
const [_key, init] = args;
|
|
if (!_key || typeof _key !== "string") {
|
|
throw new TypeError("[nuxt] [useState] key must be a string: " + _key);
|
|
}
|
|
if (init !== void 0 && typeof init !== "function") {
|
|
throw new Error("[nuxt] [useState] init must be a function: " + init);
|
|
}
|
|
const key = useStateKeyPrefix + _key;
|
|
const nuxtApp = useNuxtApp();
|
|
const state = toRef(nuxtApp.payload.state, key);
|
|
if (init) {
|
|
nuxtApp._state[key] ??= { _default: init };
|
|
}
|
|
if (state.value === void 0 && init) {
|
|
const initialValue = init();
|
|
if (isRef(initialValue)) {
|
|
nuxtApp.payload.state[key] = initialValue;
|
|
return initialValue;
|
|
}
|
|
state.value = initialValue;
|
|
}
|
|
return state;
|
|
}
|
|
|
|
export { useState as u };
|
|
//# sourceMappingURL=state-DQnwRar-.mjs.map
|
|
|