Spaces:
Paused
Paused
File size: 1,233 Bytes
8d1819a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
// Track all created stores
const stores = new Map();
/**
* Creates a store that can be used to share state between components.
* Uses initial state object and returns a proxy to it that uses Alpine when initialized
* @template T
* @param {string} name
* @param {T} initialState
* @returns {T}
*/
export function createStore(name, initialState) {
const proxy = new Proxy(initialState, {
set(target, prop, value) {
const store = globalThis.Alpine?.store(name);
if (store) store[prop] = value;
else target[prop] = value;
return true;
},
get(target, prop) {
const store = globalThis.Alpine?.store(name);
if (store) return store[prop];
return target[prop];
}
});
if (globalThis.Alpine) {
globalThis.Alpine.store(name, initialState);
} else {
document.addEventListener("alpine:init", () => Alpine.store(name, initialState));
}
// Store the proxy
stores.set(name, proxy);
return /** @type {T} */ (proxy); // explicitly cast for linter support
}
/**
* Get an existing store by name
* @template T
* @param {string} name
* @returns {T | undefined}
*/
export function getStore(name) {
return /** @type {T | undefined} */ (stores.get(name));
} |