Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 111x 111x 111x 58x 58x 58x 58x 58x 58x 58x 58x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 3x 182x 3x 3x 3x 3x 3x 2x 2x 3x 1x 1x 1x 3x 3x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 182x 58x | import { ProxyHandlerFactory, IS_REACTIVE_PROXY, PROXY_REF_SYMBOL } from './proxyHandler.js';
export { toRaw, isReactive, markRaw } from './proxyHandler.js';
/**
* Factory for creating reactive state objects.
*/
export class StateFactory {
/**
* @param {Function} [handlerFactoryClass] - The factory class to create proxy handlers.
*/
constructor(handlerFactoryClass = ProxyHandlerFactory) {
/** @type {Function} */
this.handlerFactoryClass = handlerFactoryClass;
}
/**
* Creates a reactive proxy for the given initial state.
* @param {object} [initialState] - The initial state object.
* @param {object} [options] - Configuration options for the proxy handler.
* @returns {Proxy} The reactive state proxy.
*/
create(initialState = {}, options = {}) {
if (initialState && initialState[IS_REACTIVE_PROXY]) {
return initialState;
}
if (initialState && initialState[PROXY_REF_SYMBOL]) {
return initialState[PROXY_REF_SYMBOL];
}
const persistKeys = new Set(options.persist || []);
if (
persistKeys.size > 0 &&
typeof localStorage !== 'undefined' &&
initialState &&
typeof initialState === 'object'
) {
for (const key of persistKeys) {
try {
const storedValue = localStorage.getItem(key);
if (storedValue !== null) {
initialState[key] = JSON.parse(storedValue);
}
} catch {
// Ignore corrupted or unavailable persisted values.
// The original initial state will be used instead.
}
}
}
const handlerFactory = new this.handlerFactoryClass(options);
const proxy = new Proxy(initialState, handlerFactory.create());
if (Object.isExtensible(initialState)) {
Object.defineProperty(initialState, PROXY_REF_SYMBOL, {
value: proxy,
writable: false,
enumerable: false,
configurable: false,
});
}
return proxy;
}
}
|