{"version":3,"file":"state.mjs","sources":["state.js"],"sourcesContent":["const stateRegistry = new Map();\nconst channel = new BroadcastChannel('aegis:state_sync');\nconst sender = crypto.randomUUID();\nconst proxySymbol = Symbol('proxy');\nconst updateSymbol = Symbol('aegis:state:update');\nlet isChannelOpen = true;\n\nexport const EVENT_TARGET = new EventTarget();\nexport const stateKey = 'aegisStateKey';\nexport const stateAttr = 'aegisStateAttr';\nexport const stateStyle = 'aegisStateStyle';\nexport const stateProperty = 'aegisStateProperty';\nexport const stateKeyAttribute = 'data-aegis-state-key';\nexport const stateAttrAttribute = 'data-aegis-state-attr';\nexport const statePropertyAttr = 'data-aegis-state-property';\nexport const stateStyleAttribute = 'data-aegis-state-style';\nexport const changeEvent = 'change';\nexport const beforeChangeEvent = 'beforechange';\n\nconst _getState = (key, fallback = null) => history.state?.[key] ?? fallback;\n\nfunction $$(selector, base = document.documentElement) {\n\tconst results = base.querySelectorAll(selector);\n\treturn base.matches instanceof Function && base.matches(selector) ? [base, ...results] : Array.from(results);\n}\n\nasync function _updateElement({ state = history.state ?? {} } = {}) {\n\tconst key = this.dataset.aegisStateKey;\n\tconst val = state?.[key];\n\n\tawait scheduler?.yield();\n\n\tif (typeof this.dataset[stateAttr] === 'string') {\n\t\tconst attr = this.dataset[stateAttr];\n\t\tconst oldVal = this.getAttribute(attr);\n\n\t\tif (typeof oldVal === 'string' && oldVal.startsWith('blob:')) {\n\t\t\tURL.revokeObjectURL(oldVal);\n\t\t}\n\n\t\tif (typeof val === 'boolean') {\n\t\t\tthis.toggleAttribute(attr, val);\n\t\t} else if (val === null || val === undefined) {\n\t\t\tthis.removeAttribute(attr);\n\t\t} else if (val instanceof Blob) {\n\t\t\tthis.setAttribute(attr, URL.createObjectURL(val));\n\t\t} else {\n\t\t\tthis.setAttribute(attr, val);\n\t\t}\n\t} else if (typeof this.dataset[stateProperty] === 'string' && this.dataset[stateProperty] !== 'innerHTML') {\n\t\tthis[this.dataset[stateProperty]] = val;\n\t} else if (typeof this.dataset[stateStyle] === 'string') {\n\t\tif (typeof val === 'undefined' || val === null || val === false) {\n\t\t\tthis.style.removeProperty(this.dataset[stateStyle]);\n\t\t} else {\n\t\t\tthis.style.setProperty(this.dataset[stateStyle], val);\n\t\t}\n\t} else if (this instanceof HTMLInputElement || this instanceof HTMLSelectElement || this instanceof HTMLTextAreaElement) {\n\t\tthis.value = val;\n\t} else {\n\t\tthis.textContent = val;\n\t}\n}\n\nconst domObserver = new MutationObserver(mutations => {\n\tmutations.forEach(record => {\n\t\tswitch(record.type) {\n\t\t\tcase 'childList':\n\t\t\t\trecord.addedNodes.forEach(node => {\n\t\t\t\t\tif (node.nodeType === Node.ELEMENT_NODE) {\n\t\t\t\t\t\t$$(`[${stateKeyAttribute}]`, node.target).forEach(el => {\n\t\t\t\t\t\t\tel[updateSymbol] = _updateElement.bind(el);\n\t\t\t\t\t\t\tobserveStateChanges(el[updateSymbol], el.dataset[stateKey]);\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\trecord.removedNodes.forEach(node => {\n\t\t\t\t\tif (node.nodeType === Node.ELEMENT_NODE) {\n\t\t\t\t\t\t$$(`[${stateKeyAttribute}]`, node.target).forEach(el => {\n\t\t\t\t\t\t\tunobserveStateChanges(el[updateSymbol]);\n\t\t\t\t\t\t\tdelete el[updateSymbol];\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tbreak;\n\n\t\t\tcase 'attributes':\n\t\t\t\tif (typeof record.oldValue === 'string') {\n\t\t\t\t\tunobserveStateChanges(record.target[updateSymbol]);\n\t\t\t\t\tdelete record.target[updateSymbol];\n\t\t\t\t} else if (typeof record.target.dataset[stateKey] === 'string') {\n\t\t\t\t\trecord.target[updateSymbol] = _updateElement.bind(record.target);\n\t\t\t\t\tobserveStateChanges(record.target[updateSymbol], record.target.dataset[stateKey]);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t}\n\t});\n});\n\nfunction _getStateMessage(type, recipient, data = {}) {\n\treturn {\n\t\tsender, type, state: getStateObj(), msgId: crypto.randomUUID(),\n\t\trecipient, location: location.href, timestamp: Date.now(), ...data\n\t};\n}\n\nfunction _updateState(state = getStateObj(), url = location.href) {\n\tconst diff = diffState(state);\n\n\tif (diff.length !== 0) {\n\t\thistory.replaceState(state, '', url);\n\t\tnotifyStateChange(diff);\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n}\n\n/**\n * Closes the broadcast channel if it's currently open. This will stop syncing between browsing contexts (tabs/windows/iframes)\n */\nexport function closeChannel() {\n\tif (isChannelOpen) {\n\t\tchannel.close();\n\t\tisChannelOpen = false;\n\t}\n}\n\n/**\n * Calculates the difference between two state objects.\n *\n * @param {object} newState - The new state object.\n * @param {object} [oldState] - The old state object. Defaults to the current state object.\n * @returns {string[]} - An array of keys representing the added, removed, or changed properties.\n */\nexport function diffState(newState, oldState = getStateObj()) {\n\tif (oldState !== newState) {\n\t\tconst oldKeys = Object.keys(oldState);\n\t\tconst newKeys = Object.keys(newState);\n\t\tconst addedKeys = newKeys.filter(key => ! oldKeys.includes(key));\n\t\tconst removedKeys = oldKeys.filter(key => ! newKeys.includes(key));\n\t\tconst changedKeys = oldKeys.filter(key => key in newState && key in oldState && newState[key] !== oldState[key]);\n\n\t\treturn Object.freeze([...addedKeys, ...changedKeys, ...removedKeys]);\n\t} else {\n\t\treturn [];\n\t}\n}\n\n/**\n * Notifies registered state change callbacks about changes in the state object.\n *\n * @param {string[]} diff - An array of keys representing the added, removed, or changed properties.\n * @returns {Promise<object[]>} - A promise that resolves to an array of settlement objects, one for each callback invocation.\n */\nexport async function notifyStateChange(diff) {\n\tif (Array.isArray(diff) && diff.length !== 0) {\n\t\tconst currState = getStateObj();\n\t\tconst state = Object.fromEntries(diff.map(key => [key, currState[key]]));\n\n\t\tawait Promise.allSettled(Array.from(\n\t\t\tstateRegistry.entries(),\n\t\t\t([callback, observedStates]) => {\n\t\t\t\tif (observedStates.length === 0 || observedStates.some(state => diff.includes(state))) {\n\t\t\t\t\tcallback({ diff, state });\n\t\t\t\t}\n\t\t\t}\n\t\t));\n\t}\n}\n\n/**\n * Registers a callback function to be notified of state changes.\n *\n * @param {Function} target - The callback function to register.\n * @param {string[]} observedStates - An array of state keys to observe.\n * @returns {boolean} - True if the callback was successfully registered, false otherwise.\n */\nexport function observeStateChanges(target, ...observedStates) {\n\tif (target instanceof Function && ! stateRegistry.has(target)) {\n\t\tstateRegistry.set(target, observedStates);\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n};\n\n/**\n * Gets a state value associated with a given key, providing a proxy object for reactive access and modification.\n *\n * @param {string} key - The key of the state value to retrieve.\n * @param {*} [fallback=null] - The fallback value to return if the state value is undefined or null.\n * @returns {ProxyHandler} - A proxy object representing the state value.\n */\nexport function getState(key, fallback = null) {\n\treturn new Proxy({\n\t\ttoString() {\n\t\t\treturn _getState(key, fallback)?.toString() ?? '';\n\t\t},\n\t\tvalueOf() {\n\t\t\tconst val = _getState(key, fallback);\n\t\t\treturn val?.valueOf instanceof Function ? val.valueOf() : val;\n\t\t},\n\t\ttoJSON() {\n\t\t\treturn _getState(key, fallback);\n\t\t},\n\t\t[Symbol.toPrimitive](hint) {\n\t\t\tconst val = _getState(key, fallback);\n\n\t\t\tif (typeof val === hint) {\n\t\t\t\treturn val;\n\t\t\t} else if (hint === 'default' && typeof val !== 'object') {\n\t\t\t\treturn val;\n\t\t\t} else if (val?.[Symbol.toPrimitive] instanceof Function) {\n\t\t\t\treturn val?.[Symbol.toPrimitive] instanceof Function ? val[Symbol.toPrimitive](hint) : val;\n\t\t\t} else if (hint !== 'number' && val?.toString instanceof Function) {\n\t\t\t\treturn val.toString();\n\t\t\t} else if (hint === 'number') {\n\t\t\t\treturn parseFloat(val);\n\t\t\t} else {\n\t\t\t\treturn val;\n\t\t\t}\n\t\t},\n\t\t[proxySymbol]: true,\n\t\t[Symbol.toStringTag]: 'StateValue',\n\t\t[Symbol.iterator]() {\n\t\t\treturn _getState(key, fallback)?.[Symbol.iterator]();\n\t\t},\n\t}, {\n\t\tdefineProperty(target, prop, attributes) {\n\t\t\tconst val = _getState(key, fallback);\n\n\t\t\tif (Reflect.defineProperty(val, prop, attributes)) {\n\t\t\t\tsetState(key, val);\n\t\t\t\treturn val;\n\t\t\t} else {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t},\n\t\tdeleteProperty(target, prop) {\n\t\t\treturn Reflect.deleteProperty(_getState(key, fallback), prop);\n\t\t},\n\t\tget(target, prop) {\n\t\t\tconst val = _getState(key, fallback);\n\n\t\t\tif (prop in target) {\n\t\t\t\treturn target[prop];\n\t\t\t} else if (typeof val === 'object') {\n\t\t\t\tconst result = Reflect.get(val, prop, val);\n\t\t\t\treturn result instanceof Function ? result.bind(val) : result;\n\t\t\t} else {\n\t\t\t\treturn val[prop];\n\t\t\t}\n\t\t},\n\t\tgetOwnPropertyDescriptor(target, prop) {\n\t\t\treturn Reflect.getOwnPropertyDescriptor(_getState(key, fallback), prop);\n\t\t},\n\t\tgetPrototypeOf() {\n\t\t\tconst val = _getState(key, fallback);\n\t\t\treturn typeof val === 'object' ? Reflect.getPrototypeOf(val) : Object.getPrototypeOf(val);\n\t\t},\n\t\thas(target, prop) {\n\t\t\treturn Reflect.has(_getState(key, fallback), prop);\n\t\t},\n\t\tisExtensible() {\n\t\t\treturn Reflect.isExtensible(_getState(key, fallback));\n\t\t},\n\t\townKeys() {\n\t\t\treturn Reflect.ownKeys(_getState(key, fallback));\n\t\t},\n\t\tpreventExtensions() {\n\t\t\treturn Reflect.preventExtensions(_getState(key, fallback));\n\t\t},\n\t\tset(target, prop, newValue) {\n\t\t\tconst val = _getState(key, fallback);\n\n\t\t\tif (Reflect.set(val, prop, newValue, val)) {\n\t\t\t\tsetState(key, val);\n\t\t\t\treturn true;\n\t\t\t} else {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t});\n}\n\n/**\n * Unregisters a callback function from being notified of state changes.\n *\n * @param {Function} target - The callback function to unregister.\n * @returns {boolean} - True if the callback was successfully unregistered, false otherwise.\n */\nexport const unobserveStateChanges = target => stateRegistry.delete(target);\n\n/**\n * Gets the current state object from the history.\n *\n * @returns {object} - A frozen copy of the current state object.\n */\nexport const getStateObj = () => Object.freeze(history.state === null ? {} : structuredClone(history.state));\n\n/**\n * Checks if a state key exists in the current state object.\n *\n * @param {string} key - The key to check for.\n * @returns {boolean} - True if the key exists, false otherwise.\n */\nexport const hasState = key => key in getStateObj();\n\n/**\n * Sets a state value.\n *\n * @param {string} key - The property name to set.\n * @param {*} newValue - The new value for the property, or a function to call to update\n * @throws {TypeError} If state is not a string or has a length of 0\n */\nexport function setState(key, newValue) {\n\tconst state = getStateObj();\n\n\tif (typeof key !== 'string' || key.length === 0) {\n\t\tthrow new TypeError('Invalid key.');\n\t} else if (typeof newValue === 'function') {\n\t\tupdateState(key, newValue);\n\t} else if (state[key] !== newValue) {\n\t\tconst detail = { key, oldValue: state[key], newValue };\n\t\tconst event = new CustomEvent(beforeChangeEvent, { cancelable: true, detail });\n\n\t\tEVENT_TARGET.dispatchEvent(event);\n\n\t\tif (! event.defaultPrevented) {\n\t\t\treplaceState({ ...getStateObj(), [key]: newValue?.[proxySymbol] ? newValue.valueOf() : newValue }, location.href);\n\n\t\t\tEVENT_TARGET.dispatchEvent(new CustomEvent(changeEvent, { detail }));\n\t\t}\n\t}\n};\n\n/**\n * Updates a state value asynchronously.\n *\n * @param {string} key - The key of the state value to update.\n * @param {Function} cb - The callback function to update the value.\n * @returns {Promise<*>} - A promise that resolves to the updated state value.\n */\nexport const updateState = async (key, cb) => await Promise.try(() => cb(_getState(key), key)).then(val => {\n\tsetState(key, val);\n\treturn val;\n});\n\n/**\n * Manages a state value, providing a getter and setter functions.\n *\n * @param {string} key - The key of the state value.\n * @param {*} [initialValue=null] - The initial value for the state value.\n * @returns {[ProxyHandler, Function]} - An array containing the getter and setter functions. The first function returns a proxy object representing the state value, and the second function is used to update the value.\n */\nexport function manageState(key, initialValue = null) {\n\treturn [getState(key, initialValue), newVal => setState(key, newVal)];\n};\n\n/**\n * Deletes a state value.\n *\n * @param {string} key - The key of the state value to delete.\n */\nexport function deleteState(key) {\n\tconst state = { ...getStateObj() };\n\tdelete state[key];\n\treplaceState(state, location.href);\n};\n\n/**\n * Saves the current state object to local storage.\n *\n * @param {string} [key=\"aegis:state\"] - The key to use for storing the state in local storage.\n */\nexport const saveState = (key = 'aegis:state') => localStorage.setItem(key, JSON.stringify(getStateObj()));\n\n/**\n * Restores the state object from local storage.\n *\n * @param {string} [key=\"aegis:state\"] - The key used for storing the state in local storage.\n */\nexport const restoreState = (key = 'aegis:state') => _updateState(JSON.parse(localStorage.getItem(key)), location.href);\n\n/**\n * Clears the current state object.\n */\nexport const clearState = () => replaceState({}, location.href);\n\n/**\n * Replaces the current state object with the given state and updates the URL.\n *\n * @param {Object} state - The new state object.\n * @param {string} url - The new URL.\n * @returns {boolean} - True if the state was successfully replaced, false otherwise.\n */\nexport function replaceState(state = getStateObj(), url = location.href) {\n\tif (_updateState(state, url)) {\n\t\tif (isChannelOpen) {\n\t\t\tchannel.postMessage(_getStateMessage('update'));\n\t\t}\n\t}\n}\n\n/**\n * Watches for state updates broadcasted through the channel and applies them to the local state.\n *\n * @param {object} [options] - Optional options.\n * @param {AbortSignal} [options.signal] - An AbortSignal to cancel the watcher.\n */\nexport function watchState({ signal } = {}) {\n\tchannel.addEventListener('message', event => {\n\t\tif (\n\t\t\tevent.isTrusted\n\t\t\t&& typeof event.data.msgId === 'string'\n\t\t\t&& typeof event.data.sender === 'string'\n\t\t\t&& event.data.sender !== sender\n\t\t\t&& typeof event.data.state === 'object'\n\t\t\t&& (typeof event.data.recipient !== 'string' || event.data.recipient === sender)\n\t\t) {\n\t\t\tconst currentState = getStateObj();\n\t\t\tconst diff = diffState(event.data.state, currentState);\n\n\t\t\tif (diff.length !== 0) {\n\t\t\t\tswitch(event.data.type) {\n\t\t\t\t\tcase 'update':\n\t\t\t\t\t\t_updateState({ ...currentState, ...event.data.state }, location.href);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'sync':\n\t\t\t\t\t\tif (isChannelOpen) {\n\t\t\t\t\t\t\tchannel.postMessage(_getStateMessage('update', event.data.sender));\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 'clear':\n\t\t\t\t\t\t_updateState({}, location.href);\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\treportError(new Error(`Unhandled broadcast channel message type: ${event.data.type}`));\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}, { signal });\n\n\tchannel.postMessage(_getStateMessage('sync'));\n\n\tif (signal instanceof AbortSignal && signal.aborted) {\n\t\tcloseChannel();\n\t} else if (signal instanceof AbortSignal) {\n\t\tsignal.addEventListener('abort', closeChannel, { once: true });\n\t}\n};\n\n/**\n * Watches for DOM mutations (added/removed nodes and attribute changes) for elements matching `[data-aegis-state-key]`.\n * Matching elements register a callback to be updated on state changes\n *\n * @param {Element|ShadowRoot|string} [target=document.documentElement] Root element to observe from\n * @param {object} options\n * @param {AbortSignal} [options.signal] Optional signal to disconnect the observer on abort\n * @param {Element} [options.base=document.documentElement] Base element to query from when `target` is a selector\n * @throws {TypeError} If the `target` is not an Element, ShadowRoot, or a valid CSS selector.\n * @throws {Error} If the provided `signal` is aborted.\n */\nexport function observeDOMState(target = document.documentElement, { signal, base = document.documentElement } = {}) {\n\tif (signal instanceof AbortSignal && signal.aborted) {\n\t\tthrow signal.reason;\n\t} else if (typeof target === 'string') {\n\t\tobserveDOMState(base.querySelector(target), { signal });\n\t} else if (! (target instanceof Element || target instanceof ShadowRoot)) {\n\t\tthrow new TypeError('Target must be an element, selector, or shadow root.');\n\t} else {\n\t\tdomObserver.observe(target, {\n\t\t\tchildList: true,\n\t\t\tsubtree: true,\n\t\t\tattributeFilter: [stateKeyAttribute],\n\t\t\tattributeOldValue: true,\n\t\t});\n\n\t\t$$(`[${stateKeyAttribute}]`, target).forEach(el => {\n\t\t\tel[updateSymbol] = _updateElement.bind(el);\n\t\t\tobserveStateChanges(el[updateSymbol], el.dataset[stateKey]);\n\t\t\tel[updateSymbol]({ state: history.state });\n\t\t});\n\n\t\tif (signal instanceof AbortSignal) {\n\t\t\tsignal.addEventListener('abort', () => domObserver.disconnect(), { once: true });\n\t\t}\n\t}\n}\n\n/**\n * Binds a DOM element to a specific state key to be updated on state changes\n *\n * @param {Element|string} target Target element or a selector\n * @param {string} key Name/key to observe\n * @param {object} options\n * @param {string} [options.attr] Optional attribute to bind state to\n * @param {string} [options.style] Optional style property to bind state to\n * @param {Element} [options.base=document.body] Base to query from when `target` is a selector\n */\nexport function bindState(target, key, { attr, style, base = document.body } = {}) {\n\tif (typeof target === 'string') {\n\t\tbindState(base.querySelector(target), key, { attr, style });\n\t} else if (! (target instanceof Element)) {\n\t\tthrow new TypeError('Target must be an element or selector.');\n\t} else if (typeof stateKey !== 'string' || stateKey.length === 0) {\n\t\tthrow new TypeError('State key must be a non-empty string.');\n\t} else if (target instanceof HTMLElement) {\n\t\ttarget.dataset[stateKey] = key;\n\n\t\tif (typeof attr === 'string') {\n\t\t\ttarget.dataset[stateAttr] = attr;\n\t\t} else if (typeof style === 'string') {\n\t\t\ttarget.dataset[stateStyle] = style;\n\t\t}\n\n\t\trequestAnimationFrame(() => _updateElement.call(target, { state: history.state ?? {}}));\n\t} else if (target instanceof Element) {\n\t\ttarget.setAttribute(stateKeyAttribute, key);\n\n\t\tif (typeof attr === 'string') {\n\t\t\ttarget.setAttribute(stateAttrAttribute, attr);\n\t\t} else if (typeof style === 'string') {\n\t\t\ttarget.setAttribute(stateStyleAttribute, style);\n\t\t}\n\n\t\trequestAnimationFrame(() => _updateElement.call(target, { state: history.state ?? {}}));\n\t}\n}\n\n/**\n * Creates and registers a callback on for given element (`target`) for state changes specified by `key`\n *\n * @param {Element|string} target Element or selector\n * @param {string} key Name/value for key in state obejct\n * @param {Function} handler The callback to register on for state changes\n * @param {object} options\n * @param {Element} [options.base=document.body] Base to query from when `target` is a selector\n * @param {AbortSignal} [options.signal] Optional signal to unregister callback when aborted\n * @returns {Function} The resulting callback, bound to the target Element\n */\nexport function createStateHandler(target, key, handler, { base = document.documentElement, signal } = {}) {\n\tif (signal instanceof AbortSignal && signal.aborted) {\n\t\tthrow signal.reason;\n\t} else if (typeof target === 'string') {\n\t\treturn createStateHandler(base.querySelector(target), key, handler, {});\n\t} else if (! (target instanceof Element)) {\n\t\tthrow new TypeError('Target must be an element or selector.');\n\t} else if (typeof key !== 'string' || key.length === 0) {\n\t\tthrow new TypeError('State key must be a non-empty string.');\n\t} else if (! (handler instanceof Function)) {\n\t\tthrow new TypeError('Callback must be a function.');\n\t} else {\n\t\tconst callback = (function({ state = {} } = {}) {\n\t\t\treturn handler.call(this, state[key], this);\n\t\t}).bind(target);\n\n\t\tobserveStateChanges(callback, key);\n\n\t\tif (signal instanceof AbortSignal) {\n\t\t\tsignal.addEventListener('abort', () => unobserveStateChanges(callback), { once: true });\n\t\t}\n\n\t\treturn callback;\n\t}\n}\n\n/**\n * A change or input handler for inputs, updating state to new values\n *\n * @param {Event} event A change or input event\n * @throws {TypeError} If the event target is not an HTMLElement\n */\nexport function changeHandler({ target, currentTarget, type }) {\n\tif (! (target instanceof HTMLElement)) {\n\t\tthrow new TypeError(`Event ${type} target must be an HTMLElement.`);\n\t} else if (target.isContentEditable && typeof target.dataset.name === 'string' && target.dataset.name.length !== 0) {\n\t\tsetState(target.dataset.name, target.textContent);\n\t} else if (typeof target.name !== 'string' || target.name.length === 0) {\n\t\t// Remove event listener if event target is the element the listener was set on\n\t\tif (target.isSameNode(currentTarget)) {\n\t\t\ttarget.removeEventListener(type, changeHandler);\n\t\t}\n\t} else if (target instanceof HTMLSelectElement) {\n\t\tsetState(target.name, target.multiple ? Array.from(target.selectedOptions, opt => opt.value) : target.value);\n\t} else if (target instanceof HTMLInputElement) {\n\t\tswitch(target.type) {\n\t\t\tcase 'checkbox': {\n\t\t\t\tconst checkboxes = Array.from(target.form?.elements ?? [target])\n\t\t\t\t\t.filter(input => input.name === target.name && input.type === 'checkbox');\n\n\t\t\t\tif (checkboxes.length === 1) {\n\t\t\t\t\tsetState(target.name, target.value === 'on' ? target.checked : target.value);\n\t\t\t\t} else {\n\t\t\t\t\tsetState(target.name, Array.from(checkboxes).filter(item => item.checked).map(item => item.value));\n\t\t\t\t}\n\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase 'radio':\n\t\t\t\tsetState(\n\t\t\t\t\ttarget.name,\n\t\t\t\t\tArray.from(target.form?.elements ?? [target])\n\t\t\t\t\t\t.filter(input => input.name === target.name && input.checked)\n\t\t\t\t\t\t.find(input => input.value)?.value\n\t\t\t\t);\n\t\t\t\tbreak;\n\n\t\t\tcase 'number':\n\t\t\tcase 'range':\n\t\t\t\tsetState(target.name, target.valueAsNumber);\n\t\t\t\tbreak;\n\n\t\t\tcase 'date':\n\t\t\t\tsetState(target.name, target.valueAsDate?.toISOString()?.split('T')?.at(0));\n\t\t\t\tbreak;\n\n\t\t\tcase 'file':\n\t\t\t\tsetState(target.name, target.multiple ? Array.from(target.files) : target.files.item(0));\n\t\t\t\tbreak;\n\n\t\t\tcase 'datetime-local':\n\t\t\t\tsetState(target.name, target.valueAsDate);\n\t\t\t\tbreak;\n\n\t\t\tdefault:\n\t\t\t\tsetState(target.name, target.value);\n\t\t}\n\t} else if (target instanceof HTMLTextAreaElement) {\n\t\tsetState(target.name, target.value);\n\t} else if (target.constructor.formAssociated) {\n\t\tsetState(target.name, target.value);\n\t} else {\n\t\tthrow new TypeError(`Event ${type} target is not a valid form element.`);\n\t}\n}\n\n/**\n * Adds an event listener for a `change` event on state.\n *\n * @param {Function} callback - The callback function to handle the `change` event.\n * @param {object} [options] - Optional configuration object to customize the listener behavior.\n * @param {AbortSignal} [options.signal] - An optional `AbortSignal` object that allows you to cancel the event listener (useful for cleanup).\n * @param {boolean} [options.once=false] - If `true`, the listener will be invoked at most once and then removed after the first invocation.\n * @param {boolean} [options.passive=false] - If `true`, the listener will never call `preventDefault()`, improving performance for some types of events (e.g., scrolling).\n */\nexport function onStateChange(callback, { signal, once = false, passive = false } = {}) {\n\tEVENT_TARGET.addEventListener(changeEvent, callback, { signal, once, passive });\n}\n\n/**\n * Adds an event listener for a cancelable `beforechange` event on state\n *\n * @param {Function} callback - The callback function to handle the `beforechange` event.\n * @param {object} [options] - Optional configuration object to customize the listener behavior.\n * @param {AbortSignal} [options.signal] - An optional `AbortSignal` object that allows you to cancel the event listener (useful for cleanup).\n * @param {boolean} [options.once=false] - If `true`, the listener will be invoked at most once and then removed after the first invocation.\n * @param {boolean} [options.passive=false] - If `true`, the listener will never call `preventDefault()`, improving performance for some types of events (e.g., scrolling).\n */\nexport function onBeforeStateChange(callback, { signal, once = false, passive = false } = {}) {\n\tEVENT_TARGET.addEventListener(beforeChangeEvent, callback, { signal, once, passive });\n}\n"],"names":["stateRegistry","Map","channel","BroadcastChannel","sender","crypto","randomUUID","proxySymbol","Symbol","updateSymbol","isChannelOpen","EVENT_TARGET","EventTarget","stateKey","stateAttr","stateStyle","stateProperty","stateKeyAttribute","stateAttrAttribute","statePropertyAttr","stateStyleAttribute","changeEvent","beforeChangeEvent","_getState","key","fallback","history","state","$$","selector","base","document","documentElement","results","querySelectorAll","matches","Function","Array","from","async","_updateElement","this","dataset","aegisStateKey","val","scheduler","yield","attr","oldVal","getAttribute","startsWith","URL","revokeObjectURL","toggleAttribute","removeAttribute","Blob","setAttribute","createObjectURL","style","removeProperty","setProperty","HTMLInputElement","HTMLSelectElement","HTMLTextAreaElement","value","textContent","domObserver","MutationObserver","mutations","forEach","record","type","addedNodes","node","nodeType","Node","ELEMENT_NODE","target","el","bind","observeStateChanges","removedNodes","unobserveStateChanges","oldValue","_getStateMessage","recipient","data","getStateObj","msgId","location","href","timestamp","Date","now","_updateState","url","diff","diffState","length","replaceState","notifyStateChange","closeChannel","close","newState","oldState","oldKeys","Object","keys","newKeys","addedKeys","filter","includes","removedKeys","changedKeys","freeze","isArray","currState","fromEntries","map","Promise","allSettled","entries","callback","observedStates","some","has","set","getState","Proxy","toString","valueOf","toJSON","toPrimitive","hint","parseFloat","toStringTag","iterator","defineProperty","prop","attributes","Reflect","setState","deleteProperty","get","result","getOwnPropertyDescriptor","getPrototypeOf","isExtensible","ownKeys","preventExtensions","newValue","delete","structuredClone","hasState","TypeError","updateState","detail","event","CustomEvent","cancelable","dispatchEvent","defaultPrevented","cb","try","then","manageState","initialValue","newVal","deleteState","saveState","localStorage","setItem","JSON","stringify","restoreState","parse","getItem","clearState","postMessage","watchState","signal","addEventListener","isTrusted","currentState","reportError","Error","AbortSignal","aborted","once","observeDOMState","reason","querySelector","Element","ShadowRoot","observe","childList","subtree","attributeFilter","attributeOldValue","disconnect","bindState","body","HTMLElement","requestAnimationFrame","call","createStateHandler","handler","changeHandler","currentTarget","isContentEditable","name","isSameNode","removeEventListener","multiple","selectedOptions","opt","checkboxes","form","elements","input","checked","item","find","valueAsNumber","valueAsDate","toISOString","split","at","files","constructor","formAssociated","onStateChange","passive","onBeforeStateChange"],"mappings":"AAAA,MAAMA,EAAgB,IAAIC,IACpBC,EAAU,IAAIC,iBAAiB,oBAC/BC,EAASC,OAAOC,aAChBC,EAAcC,OAAO,SACrBC,EAAeD,OAAO,sBAC5B,IAAIE,GAAgB,EAER,MAACC,EAAe,IAAIC,YACnBC,EAAW,gBACXC,EAAY,iBACZC,EAAa,kBACbC,EAAgB,qBAChBC,EAAoB,uBACpBC,EAAqB,wBACrBC,EAAoB,4BACpBC,EAAsB,yBACtBC,EAAc,SACdC,EAAoB,eAE3BC,EAAY,CAACC,EAAKC,EAAW,OAASC,QAAQC,QAAQH,IAAQC,EAEpE,SAASG,EAAGC,EAAUC,EAAOC,SAASC,iBACrC,MAAMC,EAAUH,EAAKI,iBAAiBL,GACtC,OAAOC,EAAKK,mBAAmBC,UAAYN,EAAKK,QAAQN,GAAY,CAACC,KAASG,GAAWI,MAAMC,KAAKL,EAC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