frosty 0.0.85 → 0.0.87
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dom.js +1 -1
- package/dist/dom.mjs +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internals/{common--Kl7OPwM.js → common-CnL8ftw8.js} +105 -15
- package/dist/internals/common-CnL8ftw8.js.map +1 -0
- package/dist/internals/{common-IghNU-hG.mjs → common-DmBH-tsg.mjs} +105 -15
- package/dist/internals/common-DmBH-tsg.mjs.map +1 -0
- package/dist/internals/common-jmXMOod4.d.ts.map +1 -1
- package/dist/internals/{sync-BlGknxx_.mjs → sync-CagQh1jI.mjs} +3 -3
- package/dist/internals/sync-CagQh1jI.mjs.map +1 -0
- package/dist/internals/{sync-CAt-fS8g.js → sync-aZg8gOj1.js} +3 -3
- package/dist/internals/sync-aZg8gOj1.js.map +1 -0
- package/dist/server-dom.js +1 -1
- package/dist/server-dom.mjs +1 -1
- package/dist/web.d.ts +3 -1
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +112 -2
- package/dist/web.js.map +1 -1
- package/dist/web.mjs +113 -4
- package/dist/web.mjs.map +1 -1
- package/package.json +1 -1
- package/packages/frosty-cli/src/server/index.js +1 -1
- package/dist/internals/common--Kl7OPwM.js.map +0 -1
- package/dist/internals/common-IghNU-hG.mjs.map +0 -1
- package/dist/internals/sync-BlGknxx_.mjs.map +0 -1
- package/dist/internals/sync-CAt-fS8g.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common-jmXMOod4.d.ts","sources":["../../src/renderer/common.ts"],"sourcesContent":["//\n// common.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { VNode } from '../core/reconciler/vnode';\nimport { myersSync } from 'myers.js';\nimport { globalEvents } from '../core/web/event';\nimport { ComponentNode, NativeElementType } from '../core/types/component';\nimport { svgProps, htmlProps, tags } from '../../generated/elements';\nimport { _propValue } from '../core/web/props';\nimport { ClassName, StyleProp } from '../core/types/style';\nimport { ExtendedCSSProperties } from '../core/web/styles/css';\nimport { _Renderer } from '../core/renderer';\nimport { processCss } from '../core/web/styles/process';\nimport { StyleBuilder } from './style';\nimport { mergeRefs } from '../core/utils';\nimport type { DOMWindow } from 'jsdom';\n\nconst SVG_NS = 'http://www.w3.org/2000/svg';\nconst HTML_NS = 'http://www.w3.org/1999/xhtml';\nconst MATHML_NS = 'http://www.w3.org/1998/Math/MathML';\n\nconst findPrototypeProperty = (object: any, propertyName: string) => {\n while (object && object.constructor && object.constructor.name !== 'Object') {\n let desc = Object.getOwnPropertyDescriptor(object, propertyName);\n if (desc) return desc;\n object = Object.getPrototypeOf(object);\n }\n return null;\n};\n\nconst isWriteable = (object: any, propertyName: string) => {\n let desc = findPrototypeProperty(object, propertyName);\n if (!desc) {\n return false;\n }\n if (desc.writable && typeof desc.value !== 'function') {\n return true;\n }\n return !!desc.set;\n};\n\nexport abstract class DOMNativeNode extends NativeElementType {\n\n static createElement: (doc: Document, renderer: _DOMRenderer) => DOMNativeNode;\n\n abstract get target(): Element | Element[];\n\n abstract update(props: Record<string, any> & {\n className?: string;\n style?: string;\n }): void;\n\n abstract replaceChildren(children: (string | Element | DOMNativeNode)[]): void;\n\n abstract destroy(): void;\n}\n\nexport abstract class _DOMRenderer extends _Renderer<Element | DOMNativeNode> {\n\n private _window: Window | DOMWindow;\n private _namespace_map = new WeakMap<VNode, string | undefined>();\n\n private _tracked_head_children: (string | Element | DOMNativeNode)[] = [];\n private _tracked_style = new StyleBuilder();\n private _tracked_elements = new Map<Element | DOMNativeNode, { props: string[]; className: string[]; listener: Record<string, EventListener | undefined>; }>();\n\n constructor(window: Window | DOMWindow) {\n super();\n this._window = window;\n }\n\n get document() {\n return this.window.document;\n }\n\n get window() {\n return this._window;\n }\n\n /** @internal */\n _beforeUpdate() {\n if (this._server) {\n this._tracked_head_children = [];\n }\n }\n\n /** @internal */\n _afterUpdate() {\n this._tracked_style.select([...this._tracked_elements.values().flatMap(({ className }) => className)]);\n const head = this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n if (this._tracked_style.isEmpty) {\n if (this._server) {\n this.__replaceChildren(head, this._tracked_head_children);\n }\n } else {\n const styleElem = this.document.querySelector('style[data-frosty-style]') ?? this.document.createElementNS(HTML_NS, 'style');\n styleElem.setAttribute('data-frosty-style', '');\n if (styleElem.textContent !== this._tracked_style.css)\n styleElem.textContent = this._tracked_style.css;\n if (this._server) {\n this.__replaceChildren(head, [...this._tracked_head_children, styleElem]);\n } else if (styleElem.parentNode !== head) {\n head.appendChild(styleElem);\n }\n }\n if (!this.document.head) {\n this.document.documentElement.insertBefore(head, this.document.body);\n }\n }\n\n /** @internal */\n _createElement(node: VNode, stack: VNode[]) {\n const { type } = node;\n if (!_.isString(type) && type.prototype instanceof DOMNativeNode) {\n const ElementType = type as typeof DOMNativeNode;\n const elem = ElementType.createElement(this.document, this);\n this._tracked_elements.set(elem, {\n props: [],\n className: [],\n listener: {},\n });\n this._updateElement(node, elem, stack);\n return elem;\n }\n if (!_.isString(type)) throw Error('Invalid type');\n switch (type) {\n case 'html': return this.document.documentElement;\n case 'head': return this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n case 'body': return this.document.body ?? this.document.createElementNS(HTML_NS, 'body');\n default: break;\n }\n const _ns_list = _.compact([\n _.includes(tags.svg, type) && SVG_NS,\n _.includes(tags.html, type) && HTML_NS,\n _.includes(tags.mathml, type) && MATHML_NS,\n ]);\n const parent = _.last(stack);\n const ns = _ns_list.length > 1 ? parent && _.first(_.intersection([this._namespace_map.get(parent)], _ns_list)) : _.first(_ns_list);\n const elem = ns ? this.document.createElementNS(ns, type) : this.document.createElement(type);\n this._namespace_map.set(node, ns);\n this._tracked_elements.set(elem, {\n props: [],\n className: [],\n listener: {},\n });\n this._updateElement(node, elem, stack);\n return elem;\n }\n\n private __createBuiltClassName(\n element: Element | DOMNativeNode,\n className: ClassName,\n style: StyleProp<ExtendedCSSProperties>,\n ) {\n const _className = _.compact(_.flattenDeep([className]));\n const built = this._tracked_style.buildStyle(_.compact(_.flattenDeep([style])));\n const tracked = this._tracked_elements.get(element);\n if (tracked) tracked.className = built;\n return [..._className, ...built].join(' ');\n }\n\n private __updateEventListener(\n element: Element,\n key: string,\n listener: EventListener | undefined,\n options?: AddEventListenerOptions,\n ) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n const tracked_listener = this._tracked_elements.get(element)?.listener;\n if (!tracked_listener) return;\n if (tracked_listener[key] !== listener) {\n if (_.isFunction(tracked_listener[key])) element.removeEventListener(event, tracked_listener[key], options);\n if (_.isFunction(listener)) element.addEventListener(event, listener, options);\n }\n tracked_listener[key] = listener;\n }\n\n /** @internal */\n _updateElement(node: VNode, element: Element | DOMNativeNode, stack: VNode[]) {\n\n if (element instanceof DOMNativeNode) {\n const {\n props: { ref, className, style, inlineStyle, ..._props }\n } = node;\n if (ref) mergeRefs(ref)(element.target);\n const builtClassName = this.__createBuiltClassName(element, className, style);\n const { css } = processCss(inlineStyle);\n element.update({\n className: builtClassName ? builtClassName : undefined,\n style: css ? css : undefined,\n ..._props\n });\n return;\n }\n\n const {\n type,\n props: { ref, className, style, inlineStyle, innerHTML, ..._props }\n } = node;\n\n if (!_.isString(type)) throw Error('Invalid type');\n switch (type) {\n case 'html': return;\n case 'head': return;\n case 'body': return;\n default: break;\n }\n\n if (ref) mergeRefs(ref)(element);\n\n const builtClassName = this.__createBuiltClassName(element, className, style);\n if (_.isEmpty(builtClassName)) {\n if (!_.isNil(element.getAttribute('class')))\n element.removeAttribute('class');\n } else if (element.className !== builtClassName) {\n element.className = builtClassName;\n }\n if (!_.isEmpty(innerHTML) && element.innerHTML !== innerHTML) element.innerHTML = innerHTML;\n\n if (inlineStyle) {\n const { css } = processCss(inlineStyle);\n const oldValue = element.getAttribute('style');\n const newValue = css.split('\\n').join('');\n if (oldValue !== newValue)\n element.setAttribute('style', newValue);\n } else if (!_.isNil(element.getAttribute('style'))) {\n element.removeAttribute('style');\n }\n\n const tracked = this._tracked_elements.get(element);\n if (!tracked) return;\n const removed = _.difference(tracked.props, _.keys(_props));\n const props = {\n ..._props,\n ..._.fromPairs(_.map(removed, x => [x, undefined])),\n };\n tracked.props = _.keys(_props);\n\n for (const [key, value] of _.entries(props)) {\n if (_.includes(globalEvents, key)) {\n this.__updateEventListener(element, key, value, { capture: false });\n } else if (key.endsWith('Capture') && _.includes(globalEvents, key.slice(0, -7))) {\n this.__updateEventListener(element, key, value, { capture: true });\n } else if (isWriteable(element, key)) {\n if ((element as any)[key] !== value)\n (element as any)[key] = value;\n } else if (key.startsWith('data-')) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n } else {\n const { type: _type, attr } = (htmlProps as any)['*'][key]\n ?? (htmlProps as any)[type]?.[key]\n ?? (svgProps as any)['*'][key]\n ?? (svgProps as any)[type]?.[key]\n ?? {};\n if (_type && attr && (_propValue as any)[_type]) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n }\n }\n }\n }\n\n /** @internal */\n _replaceChildren(node: VNode, element: Element | DOMNativeNode, children: (string | Element | DOMNativeNode)[], stack: VNode[], force?: boolean) {\n if (element instanceof DOMNativeNode) {\n element.replaceChildren(children);\n } else {\n const {\n type,\n props: { innerHTML }\n } = node;\n if (type === 'head') {\n this._tracked_head_children.push(...children);\n } else if (_.isEmpty(innerHTML)) {\n this.__replaceChildren(element, children, force);\n }\n }\n }\n\n /** @internal */\n _destroyElement(node: VNode, element: Element | DOMNativeNode) {\n if (element instanceof DOMNativeNode) {\n element.destroy();\n } else {\n const tracked_listener = this._tracked_elements.get(element)?.listener;\n for (const [key, listener] of _.entries(tracked_listener)) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n if (_.isFunction(listener)) {\n element.removeEventListener(event, listener, { capture: key.endsWith('Capture') });\n }\n }\n }\n this._tracked_elements.delete(element);\n }\n\n __replaceChildren(element: Element, children: (string | Element | DOMNativeNode)[], force?: boolean) {\n const diff = myersSync(\n _.map(element.childNodes, x => x.nodeType === this.document.TEXT_NODE ? x.textContent ?? '' : x),\n _.flatMap(children, x => x instanceof DOMNativeNode ? x.target : x),\n { compare: (a, b) => a === b },\n );\n let i = 0;\n for (const { remove, insert, equivalent } of diff) {\n if (equivalent) {\n i += equivalent.length;\n } else if (remove) {\n for (const child of remove) {\n if (force || _.isString(child) || this._tracked_elements.has(child as any)) {\n element.removeChild(element.childNodes[i]);\n } else {\n i++;\n }\n }\n }\n if (insert) {\n for (const child of insert) {\n const node = _.isString(child) ? this.document.createTextNode(child) : child;\n element.insertBefore(node, element.childNodes[i++]);\n }\n }\n }\n }\n\n async renderToString(component: ComponentNode) {\n const root = this.createRoot();\n try {\n await root.mount(component, { skipMount: true });\n const elements = _.flatMap(_.castArray(root.root ?? []), x => x instanceof DOMNativeNode ? x.target : x);\n const str = _.map(elements, x => x.outerHTML).join('');\n return str.startsWith('<html>') ? `<!DOCTYPE html>${str}` : str;\n } finally {\n root.unmount();\n }\n }\n}\n"],"names":[],"mappings":";;;;AAGO,uBAAA,aAAA,SAAA,iBAAA;AACP,gCAAA,QAAA,YAAA,YAAA,KAAA,aAAA;AACA,2BAAA,OAAA,GAAA,OAAA;AACA,2BAAA,MAAA;AACA;AACA;AACA;AACA,iDAAA,OAAA,GAAA,aAAA;AACA;AACA;AACO,uBAAA,YAAA,SAAA,SAAA,CAAA,OAAA,GAAA,aAAA;AACP;AACA;AACA;AACA;AACA;AACA,wBAAA,MAAA,GAAA,SAAA;AACA,oBAAA,QAAA;AACA,kBAAA,MAAA,GAAA,SAAA;AACA;AACA;AACA,+BAAA,OAAA,sBAAA,OAAA,GAAA,aAAA;AACA,8BAAA,aAAA,GAAA,OAAA;AACA;;;;"}
|
|
1
|
+
{"version":3,"file":"common-jmXMOod4.d.ts","sources":["../../src/renderer/common.ts"],"sourcesContent":["//\n// common.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { VNode } from '../core/reconciler/vnode';\nimport { myersSync } from 'myers.js';\nimport { globalEvents } from '../core/web/event';\nimport { ComponentNode, NativeElementType } from '../core/types/component';\nimport { svgProps, htmlProps, tags } from '../../generated/elements';\nimport { _propValue } from '../core/web/props';\nimport { ClassName, StyleProp } from '../core/types/style';\nimport { ExtendedCSSProperties } from '../core/web/styles/css';\nimport { _Renderer } from '../core/renderer';\nimport { processCss } from '../core/web/styles/process';\nimport { StyleBuilder } from './style';\nimport { mergeRefs } from '../core/utils';\nimport type { DOMWindow } from 'jsdom';\nimport { compress } from './minify/compress';\n\nconst SVG_NS = 'http://www.w3.org/2000/svg';\nconst HTML_NS = 'http://www.w3.org/1999/xhtml';\nconst MATHML_NS = 'http://www.w3.org/1998/Math/MathML';\n\nconst findPrototypeProperty = (object: any, propertyName: string) => {\n while (object && object.constructor && object.constructor.name !== 'Object') {\n let desc = Object.getOwnPropertyDescriptor(object, propertyName);\n if (desc) return desc;\n object = Object.getPrototypeOf(object);\n }\n return null;\n};\n\nconst isWriteable = (object: any, propertyName: string) => {\n let desc = findPrototypeProperty(object, propertyName);\n if (!desc) {\n return false;\n }\n if (desc.writable && typeof desc.value !== 'function') {\n return true;\n }\n return !!desc.set;\n};\n\nexport abstract class DOMNativeNode extends NativeElementType {\n\n static createElement: (doc: Document, renderer: _DOMRenderer) => DOMNativeNode;\n\n abstract get target(): Element | Element[];\n\n abstract update(props: Record<string, any> & {\n className?: string;\n style?: string;\n }): void;\n\n abstract replaceChildren(children: (string | Element | DOMNativeNode)[]): void;\n\n abstract destroy(): void;\n}\n\nexport abstract class _DOMRenderer extends _Renderer<Element | DOMNativeNode> {\n\n private _window: Window | DOMWindow;\n private _namespace_map = new WeakMap<VNode, string | undefined>();\n\n private _tracked_head_children: (string | Element | DOMNativeNode)[] = [];\n private _tracked_style = new StyleBuilder();\n /** @internal */\n _tracked_server_resource = new Map<string, string>();\n private _tracked_elements = new Map<Element | DOMNativeNode, { props: string[]; className: string[]; listener: Record<string, EventListener | undefined>; }>();\n\n constructor(window: Window | DOMWindow) {\n super();\n this._window = window;\n }\n\n get document() {\n return this.window.document;\n }\n\n get window() {\n return this._window;\n }\n\n /** @internal */\n _beforeUpdate() {\n if (this._server) {\n this._tracked_head_children = [];\n this._tracked_server_resource = new Map<string, string>();\n }\n }\n\n /** @internal */\n _afterUpdate() {\n this._tracked_style.select([...this._tracked_elements.values().flatMap(({ className }) => className)]);\n const head = this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n const styleElem = this.document.querySelector('style[data-frosty-style]') ?? this.document.createElementNS(HTML_NS, 'style');\n styleElem.setAttribute('data-frosty-style', '');\n if (styleElem.textContent !== this._tracked_style.css)\n styleElem.textContent = this._tracked_style.css;\n if (this._server) {\n const ssrData = this._tracked_server_resource.size ? this.document.createElementNS(HTML_NS, 'script') : undefined;\n if (ssrData) {\n ssrData.setAttribute('data-frosty-ssr-data', '');\n ssrData.setAttribute('type', 'text/plain');\n ssrData.innerHTML = compress(JSON.stringify(Object.fromEntries(this._tracked_server_resource)));\n }\n this.__replaceChildren(head, _.compact([...this._tracked_head_children, styleElem, ssrData]));\n } else if (styleElem.parentNode !== head) {\n head.appendChild(styleElem);\n }\n if (!this.document.head) {\n this.document.documentElement.insertBefore(head, this.document.body);\n }\n }\n\n /** @internal */\n _createElement(node: VNode, stack: VNode[]) {\n const { type } = node;\n if (!_.isString(type) && type.prototype instanceof DOMNativeNode) {\n const ElementType = type as typeof DOMNativeNode;\n const elem = ElementType.createElement(this.document, this);\n this._tracked_elements.set(elem, {\n props: [],\n className: [],\n listener: {},\n });\n this._updateElement(node, elem, stack);\n return elem;\n }\n if (!_.isString(type)) throw Error('Invalid type');\n switch (type) {\n case 'html': return this.document.documentElement;\n case 'head': return this.document.head ?? this.document.createElementNS(HTML_NS, 'head');\n case 'body': return this.document.body ?? this.document.createElementNS(HTML_NS, 'body');\n default: break;\n }\n const _ns_list = _.compact([\n _.includes(tags.svg, type) && SVG_NS,\n _.includes(tags.html, type) && HTML_NS,\n _.includes(tags.mathml, type) && MATHML_NS,\n ]);\n const parent = _.last(stack);\n const ns = _ns_list.length > 1 ? parent && _.first(_.intersection([this._namespace_map.get(parent)], _ns_list)) : _.first(_ns_list);\n const elem = ns ? this.document.createElementNS(ns, type) : this.document.createElement(type);\n this._namespace_map.set(node, ns);\n this._tracked_elements.set(elem, {\n props: [],\n className: [],\n listener: {},\n });\n this._updateElement(node, elem, stack);\n return elem;\n }\n\n private __createBuiltClassName(\n element: Element | DOMNativeNode,\n className: ClassName,\n style: StyleProp<ExtendedCSSProperties>,\n ) {\n const _className = _.compact(_.flattenDeep([className]));\n const built = this._tracked_style.buildStyle(_.compact(_.flattenDeep([style])));\n const tracked = this._tracked_elements.get(element);\n if (tracked) tracked.className = built;\n return [..._className, ...built].join(' ');\n }\n\n private __updateEventListener(\n element: Element,\n key: string,\n listener: EventListener | undefined,\n options?: AddEventListenerOptions,\n ) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n const tracked_listener = this._tracked_elements.get(element)?.listener;\n if (!tracked_listener) return;\n if (tracked_listener[key] !== listener) {\n if (_.isFunction(tracked_listener[key])) element.removeEventListener(event, tracked_listener[key], options);\n if (_.isFunction(listener)) element.addEventListener(event, listener, options);\n }\n tracked_listener[key] = listener;\n }\n\n /** @internal */\n _updateElement(node: VNode, element: Element | DOMNativeNode, stack: VNode[]) {\n\n if (element instanceof DOMNativeNode) {\n const {\n props: { ref, className, style, inlineStyle, ..._props }\n } = node;\n if (ref) mergeRefs(ref)(element.target);\n const builtClassName = this.__createBuiltClassName(element, className, style);\n const { css } = processCss(inlineStyle);\n element.update({\n className: builtClassName ? builtClassName : undefined,\n style: css ? css : undefined,\n ..._props\n });\n return;\n }\n\n const {\n type,\n props: { ref, className, style, inlineStyle, innerHTML, ..._props }\n } = node;\n\n if (!_.isString(type)) throw Error('Invalid type');\n switch (type) {\n case 'html': return;\n case 'head': return;\n case 'body': return;\n default: break;\n }\n\n if (ref) mergeRefs(ref)(element);\n\n const builtClassName = this.__createBuiltClassName(element, className, style);\n if (_.isEmpty(builtClassName)) {\n if (!_.isNil(element.getAttribute('class')))\n element.removeAttribute('class');\n } else if (element.className !== builtClassName) {\n element.className = builtClassName;\n }\n if (!_.isEmpty(innerHTML) && element.innerHTML !== innerHTML) element.innerHTML = innerHTML;\n\n if (inlineStyle) {\n const { css } = processCss(inlineStyle);\n const oldValue = element.getAttribute('style');\n const newValue = css.split('\\n').join('');\n if (oldValue !== newValue)\n element.setAttribute('style', newValue);\n } else if (!_.isNil(element.getAttribute('style'))) {\n element.removeAttribute('style');\n }\n\n const tracked = this._tracked_elements.get(element);\n if (!tracked) return;\n const removed = _.difference(tracked.props, _.keys(_props));\n const props = {\n ..._props,\n ..._.fromPairs(_.map(removed, x => [x, undefined])),\n };\n tracked.props = _.keys(_props);\n\n for (const [key, value] of _.entries(props)) {\n if (_.includes(globalEvents, key)) {\n this.__updateEventListener(element, key, value, { capture: false });\n } else if (key.endsWith('Capture') && _.includes(globalEvents, key.slice(0, -7))) {\n this.__updateEventListener(element, key, value, { capture: true });\n } else if (isWriteable(element, key)) {\n if ((element as any)[key] !== value)\n (element as any)[key] = value;\n } else if (key.startsWith('data-')) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n } else {\n const { type: _type, attr } = (htmlProps as any)['*'][key]\n ?? (htmlProps as any)[type]?.[key]\n ?? (svgProps as any)['*'][key]\n ?? (svgProps as any)[type]?.[key]\n ?? {};\n if (_type && attr && (_propValue as any)[_type]) {\n const oldValue = element.getAttribute(key);\n if (value === false || _.isNil(value)) {\n if (!_.isNil(oldValue))\n element.removeAttribute(key);\n } else {\n const newValue = value === true ? '' : `${value}`;\n if (oldValue !== newValue)\n element.setAttribute(key, newValue);\n }\n }\n }\n }\n }\n\n /** @internal */\n _replaceChildren(node: VNode, element: Element | DOMNativeNode, children: (string | Element | DOMNativeNode)[], stack: VNode[], force?: boolean) {\n if (element instanceof DOMNativeNode) {\n element.replaceChildren(children);\n } else {\n const {\n type,\n props: { innerHTML }\n } = node;\n if (type === 'head') {\n this._tracked_head_children.push(...children);\n } else if (_.isEmpty(innerHTML)) {\n this.__replaceChildren(element, children, force);\n }\n }\n }\n\n /** @internal */\n _destroyElement(node: VNode, element: Element | DOMNativeNode) {\n if (element instanceof DOMNativeNode) {\n element.destroy();\n } else {\n const tracked_listener = this._tracked_elements.get(element)?.listener;\n for (const [key, listener] of _.entries(tracked_listener)) {\n const event = key.endsWith('Capture') ? key.slice(2, -7).toLowerCase() : key.slice(2).toLowerCase();\n if (_.isFunction(listener)) {\n element.removeEventListener(event, listener, { capture: key.endsWith('Capture') });\n }\n }\n }\n this._tracked_elements.delete(element);\n }\n\n __replaceChildren(element: Element, children: (string | Element | DOMNativeNode)[], force?: boolean) {\n const diff = myersSync(\n _.map(element.childNodes, x => x.nodeType === this.document.TEXT_NODE ? x.textContent ?? '' : x),\n _.flatMap(children, x => x instanceof DOMNativeNode ? x.target : x),\n { compare: (a, b) => a === b },\n );\n let i = 0;\n for (const { remove, insert, equivalent } of diff) {\n if (equivalent) {\n i += equivalent.length;\n } else if (remove) {\n for (const child of remove) {\n if (force || _.isString(child) || this._tracked_elements.has(child as any)) {\n element.removeChild(element.childNodes[i]);\n } else {\n i++;\n }\n }\n }\n if (insert) {\n for (const child of insert) {\n const node = _.isString(child) ? this.document.createTextNode(child) : child;\n element.insertBefore(node, element.childNodes[i++]);\n }\n }\n }\n }\n\n async renderToString(component: ComponentNode) {\n const root = this.createRoot();\n try {\n await root.mount(component, { skipMount: true });\n const elements = _.flatMap(_.castArray(root.root ?? []), x => x instanceof DOMNativeNode ? x.target : x);\n const str = _.map(elements, x => x.outerHTML).join('');\n return str.startsWith('<html>') ? `<!DOCTYPE html>${str}` : str;\n } finally {\n root.unmount();\n }\n }\n}\n"],"names":[],"mappings":";;;;AAGO,uBAAA,aAAA,SAAA,iBAAA;AACP,gCAAA,QAAA,YAAA,YAAA,KAAA,aAAA;AACA,2BAAA,OAAA,GAAA,OAAA;AACA,2BAAA,MAAA;AACA;AACA;AACA;AACA,iDAAA,OAAA,GAAA,aAAA;AACA;AACA;AACO,uBAAA,YAAA,SAAA,SAAA,CAAA,OAAA,GAAA,aAAA;AACP;AACA;AACA;AACA;AACA;AACA,wBAAA,MAAA,GAAA,SAAA;AACA,oBAAA,QAAA;AACA,kBAAA,MAAA,GAAA,SAAA;AACA;AACA;AACA,+BAAA,OAAA,sBAAA,OAAA,GAAA,aAAA;AACA,8BAAA,aAAA,GAAA,OAAA;AACA;;;;"}
|
|
@@ -93,13 +93,13 @@ function useCallback(callback, deps) {
|
|
|
93
93
|
const store = {
|
|
94
94
|
current: callback,
|
|
95
95
|
stable: function (...args) {
|
|
96
|
-
if (store.current)
|
|
96
|
+
if (_.isFunction(store.current))
|
|
97
97
|
return store.current.call(this, ...args);
|
|
98
98
|
},
|
|
99
99
|
};
|
|
100
100
|
return store;
|
|
101
101
|
}, null);
|
|
102
|
-
if (callback)
|
|
102
|
+
if (_.isFunction(callback))
|
|
103
103
|
store.current = callback;
|
|
104
104
|
return callback && store.stable;
|
|
105
105
|
}
|
|
@@ -289,4 +289,4 @@ const useSyncExternalStore = (subscribe, getSnapshot, getServerSnapshot) => {
|
|
|
289
289
|
};
|
|
290
290
|
|
|
291
291
|
export { _useEffect as _, _useMemo as a, useMemo as b, useEffect as c, useSyncExternalStore as d, useCallback as u };
|
|
292
|
-
//# sourceMappingURL=sync-
|
|
292
|
+
//# sourceMappingURL=sync-CagQh1jI.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-CagQh1jI.mjs","sources":["../../../src/core/reconciler/hooks.ts","../../../src/core/hooks/callback.ts","../../../src/core/hooks/effect.ts","../../../src/core/hooks/memo.ts","../../../src/core/hooks/sync.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { reconciler } from './state';\nimport { equalDeps } from './utils';\n\nconst _useHookState = (hook: string) => {\n const state = reconciler.currentHookState;\n if (!state) throw Error(`${hook} must be used within a render function.`);\n const { prevState, state: newState } = state;\n if (prevState && prevState[newState.length]?.hook !== hook) {\n console.warn([\n `Hook \"${hook}\" is called conditionally.`,\n 'Hooks must be called in the exact same order in every component render.',\n 'Did you accidentally call a hook after an early return?'\n ].join(' '));\n }\n return state;\n};\n\nexport const _useEffect = (\n hook: string,\n effect: (state: ReturnType<typeof _useHookState>) => () => void,\n deps?: any\n) => {\n const state = _useHookState(hook);\n const { state: newState } = state;\n newState.push({\n mount: () => effect(state),\n deps,\n hook,\n });\n};\n\nexport const _useMemo = <T>(\n hook: string,\n factory: (state: ReturnType<typeof _useHookState>) => T,\n deps?: any\n): T => {\n const state = _useHookState(hook);\n const { prevState, state: newState } = state;\n const idx = newState.length;\n const changed = prevState?.[idx]?.hook !== hook || !equalDeps(prevState[idx].deps, deps);\n const data = changed ? factory(state) : prevState[idx].data;\n newState.push({\n deps,\n hook,\n data\n });\n return data;\n};\n","//\n// memo.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { _useMemo } from '../reconciler/hooks';\n\n/**\n * A hook that memoizes a callback function, ensuring that it only changes\n * if its dependencies change. This is useful for optimizing performance by preventing\n * unnecessary re-creations of functions.\n *\n * @template T - The type of the callback function.\n * @param callback - The callback function to be memoized.\n * @param deps - An optional dependencies. If provided, the callback\n * will only be updated when one of these dependencies changes.\n * If not provided, the callback will remain stable.\n * @returns - A stable version of the callback function that will not change unless\n * its dependencies change.\n *\n * @example\n * const memoizedCallback = useCallback(() => {\n * console.log('This function is memoized!');\n * }, [dependency]);\n */\n\nexport function useCallback<T extends (...args: any) => any>(\n callback: T,\n deps?: any\n): T;\n\nexport function useCallback<T extends ((...args: any) => any) | _.Falsey>(\n callback: T,\n deps?: any\n): T;\n\nexport function useCallback<T extends ((...args: any) => any) | _.Falsey>(\n callback: T,\n deps?: any\n): T {\n if (!_.isUndefined(deps)) return _useMemo('useCallback', () => callback, deps);\n const store = _useMemo('useCallback', () => {\n const store = {\n current: callback,\n stable: function (this: any, ...args: any) {\n if (_.isFunction(store.current))\n return store.current.call(this, ...args);\n },\n };\n return store;\n }, null);\n if (_.isFunction(callback)) store.current = callback;\n return callback && (store.stable as T);\n}\n","//\n// effect.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { Awaitable } from '@o2ter/utils-js';\nimport { _useEffect } from '../reconciler/hooks';\n\n/**\n * A hook that manages side effects with support for an `AbortSignal`.\n * It ensures proper cleanup logic and handles asynchronous operations effectively.\n *\n * @param effect - \n * A function that receives an `AbortSignal` and performs side effects. \n * It can optionally return a cleanup function or a promise that resolves to a cleanup function.\n *\n * @param deps - \n * An optional dependencies that determines when the effect should be re-executed.\n *\n * @example\n * useEffect((signal) => {\n * fetch('/api/data', { signal })\n * .then(response => response.json())\n * .then(data => console.log(data))\n * .catch(err => {\n * if (err.name === 'AbortError') {\n * console.log('Fetch aborted');\n * } else {\n * console.error(err);\n * }\n * });\n * \n * return () => {\n * console.log('Cleanup logic here');\n * };\n * }, []);\n */\nexport const useEffect = (\n effect: (\n signal: AbortSignal,\n ) => Awaitable<void | (() => Awaitable<void>)>,\n deps?: any,\n) => _useEffect('useEffect', () => {\n const abort = new AbortController();\n try {\n const destructor = effect(abort.signal);\n return () => {\n abort.abort();\n (async () => {\n try {\n const _destructor = await destructor;\n if (_.isFunction(_destructor)) _destructor();\n } catch (e) {\n console.error(e);\n }\n })();\n };\n } catch (e) {\n console.error(e);\n return () => abort.abort();\n }\n}, deps);","//\n// memo.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { _useMemo } from '../reconciler/hooks';\n\n/**\n * A utility function that memoizes the result of a factory function.\n * \n * @template T The type of the value returned by the factory function.\n * @param factory A function that produces a value to be memoized.\n * @param deps An optional dependency array. The memoized value is recalculated \n * only when the dependencies change.\n * @returns The memoized value produced by the factory function.\n */\nexport const useMemo = <T>(\n factory: () => T,\n deps?: any,\n) => _useMemo('useMemo', () => factory(), deps);\n","//\n// sync.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { Awaitable } from '@o2ter/utils-js';\nimport { _useEffect } from '../reconciler/hooks';\nimport { reconciler } from '../reconciler/state';\n\n/**\n * A hook utility for synchronizing with an external store.\n *\n * @template Snapshot - The type of the snapshot returned by the store.\n * @param subscribe - A function that sets up a subscription to the external store.\n * - `onStoreChange`: A callback to invoke when the store changes.\n * - `signal`: An `AbortSignal` to handle cleanup when the subscription is no longer needed.\n * - Returns an optional cleanup function or a promise resolving to one.\n * @param getSnapshot - A function that retrieves the current snapshot of the store.\n * @param getServerSnapshot - (Optional) A function that retrieves the snapshot of the store\n * in a server environment.\n * @returns The current snapshot of the store.\n *\n * @throws Will throw an error if used outside of a valid render context.\n */\nexport const useSyncExternalStore = <Snapshot>(\n subscribe: (\n onStoreChange: () => void,\n signal: AbortSignal,\n ) => Awaitable<void | (() => Awaitable<void>)>,\n getSnapshot: () => Snapshot,\n getServerSnapshot?: () => Snapshot,\n) => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useSyncExternalStore must be used within a render function.');\n _useEffect('useSyncExternalStore', ({ node }) => {\n const abort = new AbortController();\n try {\n const destructor = subscribe(() => { node?._setDirty(); }, abort.signal);\n return () => {\n abort.abort();\n (async () => {\n try {\n const _destructor = await destructor;\n if (_.isFunction(_destructor)) _destructor();\n } catch (e) {\n console.error(e);\n }\n })();\n };\n } catch (e) {\n console.error(e);\n return () => abort.abort();\n }\n }, null);\n if (getServerSnapshot && state.renderer._server) {\n return getServerSnapshot();\n }\n return getSnapshot();\n};"],"names":[],"mappings":";;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,GAAG,CAClC,SAG8C,EAC9C,WAA2B,EAC3B,iBAAkC,KAChC;AACF,IAAA,MAAM,KAAK,GAAG,UAAU,CAAC,gBAAgB;AACzC,IAAA,IAAI,CAAC,KAAK;AAAE,QAAA,MAAM,KAAK,CAAC,6DAA6D,CAAC;IACtF,UAAU,CAAC,sBAAsB,EAAE,CAAC,EAAE,IAAI,EAAE,KAAI;AAC9C,QAAA,MAAM,KAAK,GAAG,IAAI,eAAe,EAAE;AACnC,QAAA,IAAI;AACF,YAAA,MAAM,UAAU,GAAG,SAAS,CAAC,QAAQ,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC;AACxE,YAAA,OAAO,MAAK;gBACV,KAAK,CAAC,KAAK,EAAE;gBACb,CAAC,YAAW;AACV,oBAAA,IAAI;AACF,wBAAA,MAAM,WAAW,GAAG,MAAM,UAAU;AACpC,wBAAA,IAAI,CAAC,CAAC,UAAU,CAAC,WAAW,CAAC;AAAE,4BAAA,WAAW,EAAE;oBAC9C;oBAAE,OAAO,CAAC,EAAE;AACV,wBAAA,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;oBAClB;gBACF,CAAC,GAAG;AACN,YAAA,CAAC;QACH;QAAE,OAAO,CAAC,EAAE;AACV,YAAA,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;AAChB,YAAA,OAAO,MAAM,KAAK,CAAC,KAAK,EAAE;QAC5B;IACF,CAAC,EAAE,IAAI,CAAC;IACR,IAAI,iBAAiB,IAAI,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE;QAC/C,OAAO,iBAAiB,EAAE;IAC5B;IACA,OAAO,WAAW,EAAE;AACtB;;;;"}
|
|
@@ -95,13 +95,13 @@ function useCallback(callback, deps) {
|
|
|
95
95
|
const store = {
|
|
96
96
|
current: callback,
|
|
97
97
|
stable: function (...args) {
|
|
98
|
-
if (store.current)
|
|
98
|
+
if (_.isFunction(store.current))
|
|
99
99
|
return store.current.call(this, ...args);
|
|
100
100
|
},
|
|
101
101
|
};
|
|
102
102
|
return store;
|
|
103
103
|
}, null);
|
|
104
|
-
if (callback)
|
|
104
|
+
if (_.isFunction(callback))
|
|
105
105
|
store.current = callback;
|
|
106
106
|
return callback && store.stable;
|
|
107
107
|
}
|
|
@@ -296,4 +296,4 @@ exports.useCallback = useCallback;
|
|
|
296
296
|
exports.useEffect = useEffect;
|
|
297
297
|
exports.useMemo = useMemo;
|
|
298
298
|
exports.useSyncExternalStore = useSyncExternalStore;
|
|
299
|
-
//# sourceMappingURL=sync-
|
|
299
|
+
//# sourceMappingURL=sync-aZg8gOj1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-aZg8gOj1.js","sources":["../../../src/core/reconciler/hooks.ts","../../../src/core/hooks/callback.ts","../../../src/core/hooks/effect.ts","../../../src/core/hooks/memo.ts","../../../src/core/hooks/sync.ts"],"sourcesContent":["//\n// index.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { reconciler } from './state';\nimport { equalDeps } from './utils';\n\nconst _useHookState = (hook: string) => {\n const state = reconciler.currentHookState;\n if (!state) throw Error(`${hook} must be used within a render function.`);\n const { prevState, state: newState } = state;\n if (prevState && prevState[newState.length]?.hook !== hook) {\n console.warn([\n `Hook \"${hook}\" is called conditionally.`,\n 'Hooks must be called in the exact same order in every component render.',\n 'Did you accidentally call a hook after an early return?'\n ].join(' '));\n }\n return state;\n};\n\nexport const _useEffect = (\n hook: string,\n effect: (state: ReturnType<typeof _useHookState>) => () => void,\n deps?: any\n) => {\n const state = _useHookState(hook);\n const { state: newState } = state;\n newState.push({\n mount: () => effect(state),\n deps,\n hook,\n });\n};\n\nexport const _useMemo = <T>(\n hook: string,\n factory: (state: ReturnType<typeof _useHookState>) => T,\n deps?: any\n): T => {\n const state = _useHookState(hook);\n const { prevState, state: newState } = state;\n const idx = newState.length;\n const changed = prevState?.[idx]?.hook !== hook || !equalDeps(prevState[idx].deps, deps);\n const data = changed ? factory(state) : prevState[idx].data;\n newState.push({\n deps,\n hook,\n data\n });\n return data;\n};\n","//\n// memo.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { _useMemo } from '../reconciler/hooks';\n\n/**\n * A hook that memoizes a callback function, ensuring that it only changes\n * if its dependencies change. This is useful for optimizing performance by preventing\n * unnecessary re-creations of functions.\n *\n * @template T - The type of the callback function.\n * @param callback - The callback function to be memoized.\n * @param deps - An optional dependencies. If provided, the callback\n * will only be updated when one of these dependencies changes.\n * If not provided, the callback will remain stable.\n * @returns - A stable version of the callback function that will not change unless\n * its dependencies change.\n *\n * @example\n * const memoizedCallback = useCallback(() => {\n * console.log('This function is memoized!');\n * }, [dependency]);\n */\n\nexport function useCallback<T extends (...args: any) => any>(\n callback: T,\n deps?: any\n): T;\n\nexport function useCallback<T extends ((...args: any) => any) | _.Falsey>(\n callback: T,\n deps?: any\n): T;\n\nexport function useCallback<T extends ((...args: any) => any) | _.Falsey>(\n callback: T,\n deps?: any\n): T {\n if (!_.isUndefined(deps)) return _useMemo('useCallback', () => callback, deps);\n const store = _useMemo('useCallback', () => {\n const store = {\n current: callback,\n stable: function (this: any, ...args: any) {\n if (_.isFunction(store.current))\n return store.current.call(this, ...args);\n },\n };\n return store;\n }, null);\n if (_.isFunction(callback)) store.current = callback;\n return callback && (store.stable as T);\n}\n","//\n// effect.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { Awaitable } from '@o2ter/utils-js';\nimport { _useEffect } from '../reconciler/hooks';\n\n/**\n * A hook that manages side effects with support for an `AbortSignal`.\n * It ensures proper cleanup logic and handles asynchronous operations effectively.\n *\n * @param effect - \n * A function that receives an `AbortSignal` and performs side effects. \n * It can optionally return a cleanup function or a promise that resolves to a cleanup function.\n *\n * @param deps - \n * An optional dependencies that determines when the effect should be re-executed.\n *\n * @example\n * useEffect((signal) => {\n * fetch('/api/data', { signal })\n * .then(response => response.json())\n * .then(data => console.log(data))\n * .catch(err => {\n * if (err.name === 'AbortError') {\n * console.log('Fetch aborted');\n * } else {\n * console.error(err);\n * }\n * });\n * \n * return () => {\n * console.log('Cleanup logic here');\n * };\n * }, []);\n */\nexport const useEffect = (\n effect: (\n signal: AbortSignal,\n ) => Awaitable<void | (() => Awaitable<void>)>,\n deps?: any,\n) => _useEffect('useEffect', () => {\n const abort = new AbortController();\n try {\n const destructor = effect(abort.signal);\n return () => {\n abort.abort();\n (async () => {\n try {\n const _destructor = await destructor;\n if (_.isFunction(_destructor)) _destructor();\n } catch (e) {\n console.error(e);\n }\n })();\n };\n } catch (e) {\n console.error(e);\n return () => abort.abort();\n }\n}, deps);","//\n// memo.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { _useMemo } from '../reconciler/hooks';\n\n/**\n * A utility function that memoizes the result of a factory function.\n * \n * @template T The type of the value returned by the factory function.\n * @param factory A function that produces a value to be memoized.\n * @param deps An optional dependency array. The memoized value is recalculated \n * only when the dependencies change.\n * @returns The memoized value produced by the factory function.\n */\nexport const useMemo = <T>(\n factory: () => T,\n deps?: any,\n) => _useMemo('useMemo', () => factory(), deps);\n","//\n// sync.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { Awaitable } from '@o2ter/utils-js';\nimport { _useEffect } from '../reconciler/hooks';\nimport { reconciler } from '../reconciler/state';\n\n/**\n * A hook utility for synchronizing with an external store.\n *\n * @template Snapshot - The type of the snapshot returned by the store.\n * @param subscribe - A function that sets up a subscription to the external store.\n * - `onStoreChange`: A callback to invoke when the store changes.\n * - `signal`: An `AbortSignal` to handle cleanup when the subscription is no longer needed.\n * - Returns an optional cleanup function or a promise resolving to one.\n * @param getSnapshot - A function that retrieves the current snapshot of the store.\n * @param getServerSnapshot - (Optional) A function that retrieves the snapshot of the store\n * in a server environment.\n * @returns The current snapshot of the store.\n *\n * @throws Will throw an error if used outside of a valid render context.\n */\nexport const useSyncExternalStore = <Snapshot>(\n subscribe: (\n onStoreChange: () => void,\n signal: AbortSignal,\n ) => Awaitable<void | (() => Awaitable<void>)>,\n getSnapshot: () => Snapshot,\n getServerSnapshot?: () => Snapshot,\n) => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useSyncExternalStore must be used within a render function.');\n _useEffect('useSyncExternalStore', ({ node }) => {\n const abort = new AbortController();\n try {\n const destructor = subscribe(() => { node?._setDirty(); }, abort.signal);\n return () => {\n abort.abort();\n (async () => {\n try {\n const _destructor = await destructor;\n if (_.isFunction(_destructor)) _destructor();\n } catch (e) {\n console.error(e);\n }\n })();\n };\n } catch (e) {\n console.error(e);\n return () => abort.abort();\n }\n }, null);\n if (getServerSnapshot && state.renderer._server) {\n return getServerSnapshot();\n }\n return getSnapshot();\n};"],"names":["state","reconciler","equalDeps"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAMA,MAAM,aAAa,GAAG,CAAC,IAAY,KAAI;AACrC,IAAA,MAAMA,OAAK,GAAGC,gBAAU,CAAC,gBAAgB;AACzC,IAAA,IAAI,CAACD,OAAK;AAAE,QAAA,MAAM,KAAK,CAAC,CAAA,EAAG,IAAI,CAAA,uCAAA,CAAyC,CAAC;IACzE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAGA,OAAK;AAC5C,IAAA,IAAI,SAAS,IAAI,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,IAAI,KAAK,IAAI,EAAE;QAC1D,OAAO,CAAC,IAAI,CAAC;AACX,YAAA,CAAA,MAAA,EAAS,IAAI,CAAA,0BAAA,CAA4B;YACzC,yEAAyE;YACzE;AACD,SAAA,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACd;AACA,IAAA,OAAOA,OAAK;AACd,CAAC;AAEM,MAAM,UAAU,GAAG,CACxB,IAAY,EACZ,MAA+D,EAC/D,IAAU,KACR;AACF,IAAA,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC;AACjC,IAAA,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,KAAK;IACjC,QAAQ,CAAC,IAAI,CAAC;AACZ,QAAA,KAAK,EAAE,MAAM,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI;QACJ,IAAI;AACL,KAAA,CAAC;AACJ;AAEO,MAAM,QAAQ,GAAG,CACtB,IAAY,EACZ,OAAuD,EACvD,IAAU,KACL;AACL,IAAA,MAAMA,O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
package/dist/server-dom.js
CHANGED
package/dist/server-dom.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { JSDOM } from 'jsdom';
|
|
2
2
|
export * from 'jsdom';
|
|
3
|
-
import { _ as _DOMRenderer } from './internals/common-
|
|
3
|
+
import { _ as _DOMRenderer } from './internals/common-DmBH-tsg.mjs';
|
|
4
4
|
import 'lodash';
|
|
5
5
|
import 'myers.js';
|
|
6
6
|
import './internals/component-BzurKp_J.mjs';
|
package/dist/web.d.ts
CHANGED
|
@@ -77,6 +77,8 @@ declare const usePerformanceObserver: (callback: PerformanceObserverCallback, op
|
|
|
77
77
|
|
|
78
78
|
declare const useOnline: () => boolean;
|
|
79
79
|
|
|
80
|
+
declare const useServerResource: (key: string, resource: () => string) => string | undefined;
|
|
81
|
+
|
|
80
82
|
declare const useLocalStorage: (key: string, initialValue?: string | null) => readonly [string | null, (v: SetStateAction<string | null | undefined>) => void];
|
|
81
83
|
declare const useSessionStorage: (key: string, initialValue?: string | null) => readonly [string | null, (v: SetStateAction<string | null | undefined>) => void];
|
|
82
84
|
|
|
@@ -107,5 +109,5 @@ declare const useWindowScroll: () => {
|
|
|
107
109
|
};
|
|
108
110
|
declare const useColorScheme: () => "dark" | "light";
|
|
109
111
|
|
|
110
|
-
export { useColorScheme, useDocument, useIntersectionObserver, useLocalStorage, useLocation, useMutationObserver, useOnline, usePerformanceObserver, useResizeObserver, useSearchParams, useSessionStorage, useVisibility, useVisualViewportMetrics, useWindow, useWindowMetrics, useWindowScroll };
|
|
112
|
+
export { useColorScheme, useDocument, useIntersectionObserver, useLocalStorage, useLocation, useMutationObserver, useOnline, usePerformanceObserver, useResizeObserver, useSearchParams, useServerResource, useSessionStorage, useVisibility, useVisualViewportMetrics, useWindow, useWindowMetrics, useWindowScroll };
|
|
111
113
|
//# sourceMappingURL=web.d.ts.map
|
package/dist/web.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web.d.ts","sources":["../src/web/document.ts","../src/web/location.ts","../src/web/observer.ts","../src/web/online.ts","../src/web/storage.ts","../src/web/visibility.ts","../src/web/window.ts"],"sourcesContent":["//\n// document.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { reconciler } from '../core/reconciler/state';\nimport { _DOMRenderer } from '../renderer/common';\n\nexport const useDocument = () => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useDocument must be used within a render function.');\n if (state.renderer instanceof _DOMRenderer) {\n return state.renderer.document;\n } else {\n throw Error('Unsupported renderer.');\n }\n}","//\n// location.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useMemo } from '../core/hooks/memo';\nimport { useCallback } from '../core/hooks/callback';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { EventEmitter } from '../core/reconciler/events';\nimport { SetStateAction } from '../core/types/common';\nimport { useWindow } from './window';\n\nconst emitters = new WeakMap<Document, EventEmitter>();\nconst emitterFor = (document: Document) => {\n if (!emitters.has(document)) emitters.set(document, new EventEmitter());\n return emitters.get(document)!;\n}\n\n/**\n * A hook that provides the current browser location and methods to manipulate the browser history.\n *\n * @returns An object with the following properties and methods:\n * - `hash`: The fragment identifier of the URL.\n * - `host`: The hostname and port number.\n * - `hostname`: The domain name.\n * - `href`: The full URL.\n * - `origin`: The protocol, hostname, and port.\n * - `pathname`: The path of the URL.\n * - `port`: The port number.\n * - `protocol`: The protocol scheme.\n * - `search`: The query string.\n * - `state`: The current state object associated with the history entry.\n * - `back()`: Navigates to the previous entry in the history stack.\n * - `forward()`: Navigates to the next entry in the history stack.\n * - `pushState(data, url)`: Pushes a new entry onto the history stack.\n * - `replaceState(data, url)`: Replaces the current history entry.\n *\n * The hook subscribes to changes in the browser's history and location, causing components to re-render when navigation occurs.\n *\n * @example\n * const location = useLocation();\n * console.log(location.pathname); // e.g., \"/about\"\n * location.pushState({ some: 'state' }, '/new-path');\n */\nexport const useLocation = () => {\n const window = useWindow();\n const emitter = emitterFor(window.document);\n const result = (history?: History) => ({\n ..._.pick(window.document.location, 'hash', 'host', 'hostname', 'href', 'origin', 'pathname', 'port', 'protocol', 'search'),\n state: history?.state ?? null,\n back: () => {\n history?.back();\n },\n forward: () => {\n history?.forward();\n emitter.emit('change');\n },\n pushState: (data: any, url?: string | URL | null) => {\n history?.pushState(data, '', url);\n emitter.emit('change');\n },\n replaceState: (data: any, url?: string | URL | null) => {\n history?.replaceState(data, '', url);\n emitter.emit('change');\n },\n });\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('popstate', onStoreChange);\n const event = emitter.register('change', onStoreChange);\n return () => {\n window.removeEventListener('popstate', onStoreChange);\n event.remove();\n }\n }, () => result(window.history));\n}\n\ntype URLSearchParamsInit = ConstructorParameters<typeof URLSearchParams>[0];\n\n/**\n * A hook for reading and updating the URL's query string (search parameters).\n *\n * @returns A tuple:\n * - The first element is a `URLSearchParams` instance representing the current query string.\n * - The second element is a function to update the search parameters, which accepts any valid\n * `URLSearchParams` initializer (string, array, or object).\n *\n * Updating the search parameters will push a new history entry and update the URL in the address bar.\n *\n * @example\n * const [searchParams, setSearchParams] = useSearchParams();\n * const page = searchParams.get('page');\n * setSearchParams({ page: '2', filter: 'active' });\n */\nexport const useSearchParams = () => {\n const location = useLocation();\n const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search]);\n const setSearchParams = useCallback((\n dispatch: SetStateAction<URLSearchParamsInit, URLSearchParams>,\n config?: {\n replace?: boolean;\n },\n ) => {\n const params = _.isFunction(dispatch) ? dispatch(new URLSearchParams(location.search)) : dispatch;\n const newParams = new URLSearchParams(params);\n if (config?.replace === false) {\n location.pushState(location.state, `?${newParams.toString()}`);\n } else {\n location.replaceState(location.state, `?${newParams.toString()}`);\n }\n });\n return [searchParams, setSearchParams] as const;\n}\n","//\n// observer.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useEffect } from '../core/hooks/effect';\nimport { useCallback } from '../core/hooks/callback';\nimport { RefObject } from '../core/types/common';\n\ninterface _Observer<T> {\n observe(target: Element, options?: T): void;\n unobserve(target: Element): void;\n}\n\nconst createObserver = <E extends { target: Element; }, T>(\n constructor: new (callback: (entries: E[]) => void) => _Observer<T>\n) => {\n const listeners = new WeakMap<Element, ((entry: E) => void)[]>();\n const observer = new constructor((entries) => {\n for (const entry of entries) {\n for (const listener of listeners.get(entry.target) ?? []) {\n (async () => {\n try {\n await listener(entry);\n } catch (e) {\n console.error(e);\n }\n })();\n }\n }\n });\n return {\n observe: (target: Element, callback: (entry: E) => void, options?: T) => {\n observer.observe(target, options);\n listeners.set(target, [...listeners.get(target) ?? [], callback]);\n },\n unobserve: (target: Element, callback: (entry: E) => void) => {\n const list = _.filter(listeners.get(target), x => x !== callback);\n listeners.set(target, list);\n if (_.isEmpty(list)) observer.unobserve?.(target);\n },\n };\n};\n\nconst observer = typeof window === 'undefined' ? undefined : {\n resize: createObserver(ResizeObserver),\n intersection: createObserver(IntersectionObserver),\n};\n\nexport const useResizeObserver = (\n target: RefObject<Element | null | undefined> | Element | null | undefined,\n callback: (entry: ResizeObserverEntry) => void,\n options?: ResizeObserverOptions,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (!observer || !_target) return;\n observer.resize.observe(_target, _callback, options);\n return () => observer.resize.unobserve(_target, _callback);\n }, [target]);\n}\n\nexport const useIntersectionObserver = (\n target: RefObject<Element | null | undefined> | Element | null | undefined,\n callback: (entry: IntersectionObserverEntry) => void,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (!observer || !_target) return;\n observer.intersection.observe(_target, _callback);\n return () => observer.intersection.unobserve(_target, _callback);\n }, [target]);\n}\n\nexport const useMutationObserver = (\n target: RefObject<Node | null | undefined> | Node | null | undefined,\n callback: MutationCallback,\n options?: MutationObserverInit,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (typeof window === 'undefined' || !_target) return;\n const observer = new MutationObserver(_callback);\n observer.observe(_target, options);\n return () => observer.disconnect();\n }, [target]);\n}\n\nexport const usePerformanceObserver = (\n callback: PerformanceObserverCallback,\n options?: PerformanceObserverInit,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n if (typeof window === 'undefined') return;\n const observer = new PerformanceObserver(_callback);\n observer.observe(options);\n return () => observer.disconnect();\n }, []);\n}\n","//\n// online.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\n\nexport const useOnline = () => useSyncExternalStore((onStoreChange) => {\n window.addEventListener('offline', onStoreChange);\n window.addEventListener('online', onStoreChange);\n return () => {\n window.removeEventListener('offline', onStoreChange);\n window.removeEventListener('online', onStoreChange);\n };\n}, () => navigator.onLine, () => false);","//\n// storage.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { useCallback } from '../core/hooks/callback';\nimport { SetStateAction } from '../core/types/common';\nimport { EventEmitter } from '../core/reconciler/events';\n\nconst emitters = new WeakMap<Storage, EventEmitter>();\nconst emitterFor = (storage: Storage) => {\n if (!emitters.has(storage)) emitters.set(storage, new EventEmitter());\n return emitters.get(storage)!;\n}\n\nconst _useStorage = (\n storage: () => Storage,\n key: string,\n initialValue?: string | null\n) => {\n const state = useSyncExternalStore((onStoreChange) => {\n const _storage = storage();\n const emitter = emitterFor(_storage);\n const callback = (ev: StorageEvent) => { \n if (!ev.storageArea || ev.storageArea === _storage) onStoreChange();\n };\n window.addEventListener('storage', callback);\n const event = emitter.register('change', onStoreChange);\n return () => {\n window.removeEventListener('storage', callback);\n event.remove();\n }\n }, () => storage().getItem(key), () => undefined);\n const setState = useCallback((v: SetStateAction<string | null | undefined>) => {\n try {\n const _storage = storage();\n const newValue = _.isFunction(v) ? v(state) : v;\n if (_.isNil(newValue)) {\n _storage.removeItem(key);\n } else {\n _storage.setItem(key, newValue);\n }\n const emitter = emitterFor(_storage);\n emitter.emit('change');\n } catch (e) {\n console.error(e);\n }\n }, [key]);\n return [state ?? initialValue ?? null, setState] as const;\n}\n\nexport const useLocalStorage = (\n key: string,\n initialValue?: string | null\n) => _useStorage(() => window.localStorage, key, initialValue);\n\nexport const useSessionStorage = (\n key: string,\n initialValue?: string | null\n) => _useStorage(() => window.sessionStorage, key, initialValue);\n","//\n// visibility.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { useDocument } from './document';\n\nexport const useVisibility = () => {\n const document = useDocument();\n return useSyncExternalStore((onStoreChange) => {\n document.addEventListener('visibilitychange', onStoreChange);\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n }\n }, () => {\n if (document.hasFocus()) {\n return 'active' as const;\n } else if (document.visibilityState === 'visible') {\n return 'inactive' as const;\n } else {\n return 'background' as const;\n }\n }, () => 'unknown' as const);\n}\n","//\n// window.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { uniqueId } from '../core/utils';\nimport { reconciler } from '../core/reconciler/state';\nimport { _DOMRenderer } from '../renderer/common';\n\nexport const useWindow = () => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useWindow must be used within a render function.');\n if (state.renderer instanceof _DOMRenderer) {\n return state.renderer.window;\n } else {\n throw Error('Unsupported renderer.');\n }\n}\n\nconst emptyInsets = { top: 0, left: 0, right: 0, bottom: 0 };\nconst safeAreaInsets = (window: ReturnType<typeof useWindow>) => {\n let support;\n if (!('CSS' in window) || !_.isFunction(window.CSS.supports)) {\n return emptyInsets;\n }\n if (window.CSS.supports('top: env(safe-area-inset-top)')) {\n support = 'env'\n } else if (window.CSS.supports('top: constant(safe-area-inset-top)')) {\n support = 'constant'\n } else {\n return emptyInsets;\n }\n const id = uniqueId();\n const style = document.createElement('style');\n style.textContent = `:root {\n --${id}-top: ${support}(safe-area-inset-top);\n --${id}-left: ${support}(safe-area-inset-left);\n --${id}-right: ${support}(safe-area-inset-right);\n --${id}-bottom: ${support}(safe-area-inset-bottom);\n }`;\n document.head.appendChild(style);\n const computedStyle = getComputedStyle(document.documentElement);\n const insets = {\n top: computedStyle.getPropertyValue(`--${id}-top`),\n left: computedStyle.getPropertyValue(`--${id}-left`),\n right: computedStyle.getPropertyValue(`--${id}-right`),\n bottom: computedStyle.getPropertyValue(`--${id}-bottom`),\n };\n style.remove();\n return _.mapValues(insets, v => parseFloat(v));\n}\n\nexport const useWindowMetrics = () => {\n const window = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('resize', onStoreChange);\n return () => {\n window.removeEventListener('resize', onStoreChange);\n };\n }, () => ({\n safeAreaInsets: safeAreaInsets(window),\n devicePixelRatio: window.devicePixelRatio,\n outerWidth: window.outerWidth,\n outerHeight: window.outerHeight,\n innerWidth: window.innerWidth,\n innerHeight: window.innerHeight,\n }));\n}\n\nexport const useVisualViewportMetrics = () => {\n const { visualViewport } = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n visualViewport?.addEventListener('resize', onStoreChange);\n return () => {\n visualViewport?.removeEventListener('resize', onStoreChange);\n };\n }, () => visualViewport && ({\n width: visualViewport.width,\n height: visualViewport.height,\n scale: visualViewport.scale,\n }));\n}\n\nexport const useWindowScroll = () => {\n const window = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('scroll', onStoreChange);\n return () => {\n window.removeEventListener('scroll', onStoreChange);\n };\n }, () => ({\n scrollX: window.scrollX,\n scrollY: window.scrollY,\n }));\n}\n\nconst colorSchemeDarkCache = new WeakMap<ReturnType<typeof useWindow>, MediaQueryList | undefined>();\n\nexport const useColorScheme = () => {\n const window = useWindow();\n if (!colorSchemeDarkCache.has(window)) colorSchemeDarkCache.set(window, window.matchMedia?.('(prefers-color-scheme: dark)'));\n const colorSchemeDark = colorSchemeDarkCache.get(window);\n return useSyncExternalStore((onStoreChange) => {\n colorSchemeDark?.addEventListener('change', onStoreChange);\n return () => {\n colorSchemeDark?.removeEventListener('change', onStoreChange);\n };\n }, () => colorSchemeDark?.matches ? 'dark' : 'light');\n}\n"],"names":[],"mappings":";;;;;;;;AAAO;;ACCP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;;AC5DO;AACA;AACA;AACA;;ACJA;;ACCA;AACA;;ACFA;;ACAA;AACA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACO;;"}
|
|
1
|
+
{"version":3,"file":"web.d.ts","sources":["../src/web/document.ts","../src/web/location.ts","../src/web/observer.ts","../src/web/online.ts","../src/web/server.ts","../src/web/storage.ts","../src/web/visibility.ts","../src/web/window.ts"],"sourcesContent":["//\n// document.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { reconciler } from '../core/reconciler/state';\nimport { _DOMRenderer } from '../renderer/common';\n\nexport const useDocument = () => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useDocument must be used within a render function.');\n if (state.renderer instanceof _DOMRenderer) {\n return state.renderer.document;\n } else {\n throw Error('Unsupported renderer.');\n }\n}","//\n// location.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useMemo } from '../core/hooks/memo';\nimport { useCallback } from '../core/hooks/callback';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { EventEmitter } from '../core/reconciler/events';\nimport { SetStateAction } from '../core/types/common';\nimport { useWindow } from './window';\n\nconst emitters = new WeakMap<Document, EventEmitter>();\nconst emitterFor = (document: Document) => {\n if (!emitters.has(document)) emitters.set(document, new EventEmitter());\n return emitters.get(document)!;\n}\n\n/**\n * A hook that provides the current browser location and methods to manipulate the browser history.\n *\n * @returns An object with the following properties and methods:\n * - `hash`: The fragment identifier of the URL.\n * - `host`: The hostname and port number.\n * - `hostname`: The domain name.\n * - `href`: The full URL.\n * - `origin`: The protocol, hostname, and port.\n * - `pathname`: The path of the URL.\n * - `port`: The port number.\n * - `protocol`: The protocol scheme.\n * - `search`: The query string.\n * - `state`: The current state object associated with the history entry.\n * - `back()`: Navigates to the previous entry in the history stack.\n * - `forward()`: Navigates to the next entry in the history stack.\n * - `pushState(data, url)`: Pushes a new entry onto the history stack.\n * - `replaceState(data, url)`: Replaces the current history entry.\n *\n * The hook subscribes to changes in the browser's history and location, causing components to re-render when navigation occurs.\n *\n * @example\n * const location = useLocation();\n * console.log(location.pathname); // e.g., \"/about\"\n * location.pushState({ some: 'state' }, '/new-path');\n */\nexport const useLocation = () => {\n const window = useWindow();\n const emitter = emitterFor(window.document);\n const result = (history?: History) => ({\n ..._.pick(window.document.location, 'hash', 'host', 'hostname', 'href', 'origin', 'pathname', 'port', 'protocol', 'search'),\n state: history?.state ?? null,\n back: () => {\n history?.back();\n },\n forward: () => {\n history?.forward();\n emitter.emit('change');\n },\n pushState: (data: any, url?: string | URL | null) => {\n history?.pushState(data, '', url);\n emitter.emit('change');\n },\n replaceState: (data: any, url?: string | URL | null) => {\n history?.replaceState(data, '', url);\n emitter.emit('change');\n },\n });\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('popstate', onStoreChange);\n const event = emitter.register('change', onStoreChange);\n return () => {\n window.removeEventListener('popstate', onStoreChange);\n event.remove();\n }\n }, () => result(window.history));\n}\n\ntype URLSearchParamsInit = ConstructorParameters<typeof URLSearchParams>[0];\n\n/**\n * A hook for reading and updating the URL's query string (search parameters).\n *\n * @returns A tuple:\n * - The first element is a `URLSearchParams` instance representing the current query string.\n * - The second element is a function to update the search parameters, which accepts any valid\n * `URLSearchParams` initializer (string, array, or object).\n *\n * Updating the search parameters will push a new history entry and update the URL in the address bar.\n *\n * @example\n * const [searchParams, setSearchParams] = useSearchParams();\n * const page = searchParams.get('page');\n * setSearchParams({ page: '2', filter: 'active' });\n */\nexport const useSearchParams = () => {\n const location = useLocation();\n const searchParams = useMemo(() => new URLSearchParams(location.search), [location.search]);\n const setSearchParams = useCallback((\n dispatch: SetStateAction<URLSearchParamsInit, URLSearchParams>,\n config?: {\n replace?: boolean;\n },\n ) => {\n const params = _.isFunction(dispatch) ? dispatch(new URLSearchParams(location.search)) : dispatch;\n const newParams = new URLSearchParams(params);\n if (config?.replace === false) {\n location.pushState(location.state, `?${newParams.toString()}`);\n } else {\n location.replaceState(location.state, `?${newParams.toString()}`);\n }\n });\n return [searchParams, setSearchParams] as const;\n}\n","//\n// observer.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useEffect } from '../core/hooks/effect';\nimport { useCallback } from '../core/hooks/callback';\nimport { RefObject } from '../core/types/common';\n\ninterface _Observer<T> {\n observe(target: Element, options?: T): void;\n unobserve(target: Element): void;\n}\n\nconst createObserver = <E extends { target: Element; }, T>(\n constructor: new (callback: (entries: E[]) => void) => _Observer<T>\n) => {\n const listeners = new WeakMap<Element, ((entry: E) => void)[]>();\n const observer = new constructor((entries) => {\n for (const entry of entries) {\n for (const listener of listeners.get(entry.target) ?? []) {\n (async () => {\n try {\n await listener(entry);\n } catch (e) {\n console.error(e);\n }\n })();\n }\n }\n });\n return {\n observe: (target: Element, callback: (entry: E) => void, options?: T) => {\n observer.observe(target, options);\n listeners.set(target, [...listeners.get(target) ?? [], callback]);\n },\n unobserve: (target: Element, callback: (entry: E) => void) => {\n const list = _.filter(listeners.get(target), x => x !== callback);\n listeners.set(target, list);\n if (_.isEmpty(list)) observer.unobserve?.(target);\n },\n };\n};\n\nconst observer = typeof window === 'undefined' ? undefined : {\n resize: createObserver(ResizeObserver),\n intersection: createObserver(IntersectionObserver),\n};\n\nexport const useResizeObserver = (\n target: RefObject<Element | null | undefined> | Element | null | undefined,\n callback: (entry: ResizeObserverEntry) => void,\n options?: ResizeObserverOptions,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (!observer || !_target) return;\n observer.resize.observe(_target, _callback, options);\n return () => observer.resize.unobserve(_target, _callback);\n }, [target]);\n}\n\nexport const useIntersectionObserver = (\n target: RefObject<Element | null | undefined> | Element | null | undefined,\n callback: (entry: IntersectionObserverEntry) => void,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (!observer || !_target) return;\n observer.intersection.observe(_target, _callback);\n return () => observer.intersection.unobserve(_target, _callback);\n }, [target]);\n}\n\nexport const useMutationObserver = (\n target: RefObject<Node | null | undefined> | Node | null | undefined,\n callback: MutationCallback,\n options?: MutationObserverInit,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n const _target = target && 'current' in target ? target.current : target;\n if (typeof window === 'undefined' || !_target) return;\n const observer = new MutationObserver(_callback);\n observer.observe(_target, options);\n return () => observer.disconnect();\n }, [target]);\n}\n\nexport const usePerformanceObserver = (\n callback: PerformanceObserverCallback,\n options?: PerformanceObserverInit,\n) => {\n const _callback = useCallback(callback);\n useEffect(() => {\n if (typeof window === 'undefined') return;\n const observer = new PerformanceObserver(_callback);\n observer.observe(options);\n return () => observer.disconnect();\n }, []);\n}\n","//\n// online.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\n\nexport const useOnline = () => useSyncExternalStore((onStoreChange) => {\n window.addEventListener('offline', onStoreChange);\n window.addEventListener('online', onStoreChange);\n return () => {\n window.removeEventListener('offline', onStoreChange);\n window.removeEventListener('online', onStoreChange);\n };\n}, () => navigator.onLine, () => false);","//\n// server.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { reconciler } from '../core/reconciler/state';\nimport { _DOMRenderer } from '../renderer/common';\nimport { decompress } from '../renderer/minify/decompress';\n\nconst decodedSsrData = new WeakMap<Document, any>();\n\nexport const useServerResource = (key: string, resource: () => string): string | undefined => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useServerResource must be used within a render function.');\n if (state.renderer instanceof _DOMRenderer) {\n if (state.renderer._server) {\n const data = resource();\n if (!_.isString(data)) throw Error('Invalid return type of resource');\n state.renderer._tracked_server_resource.set(key, data);\n return data;\n } else {\n const cached = decodedSsrData.get(state.renderer.document);\n if (!_.isNil(cached)) return cached[key];\n const ssrData = state.renderer.document.querySelector('script[data-frosty-ssr-data]');\n if (ssrData instanceof HTMLElement) {\n try {\n const decoded = JSON.parse(decompress(ssrData.innerText.trim()));\n decodedSsrData.set(state.renderer.document, decoded);\n return decoded[key];\n } catch (e) {\n console.error(e);\n decodedSsrData.set(state.renderer.document, {});\n }\n ssrData.remove();\n } else {\n decodedSsrData.set(state.renderer.document, {});\n }\n }\n } else {\n throw Error('Unsupported renderer.');\n }\n}\n","//\n// storage.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { useCallback } from '../core/hooks/callback';\nimport { SetStateAction } from '../core/types/common';\nimport { EventEmitter } from '../core/reconciler/events';\n\nconst emitters = new WeakMap<Storage, EventEmitter>();\nconst emitterFor = (storage: Storage) => {\n if (!emitters.has(storage)) emitters.set(storage, new EventEmitter());\n return emitters.get(storage)!;\n}\n\nconst _useStorage = (\n storage: () => Storage,\n key: string,\n initialValue?: string | null\n) => {\n const state = useSyncExternalStore((onStoreChange) => {\n const _storage = storage();\n const emitter = emitterFor(_storage);\n const callback = (ev: StorageEvent) => { \n if (!ev.storageArea || ev.storageArea === _storage) onStoreChange();\n };\n window.addEventListener('storage', callback);\n const event = emitter.register('change', onStoreChange);\n return () => {\n window.removeEventListener('storage', callback);\n event.remove();\n }\n }, () => storage().getItem(key), () => undefined);\n const setState = useCallback((v: SetStateAction<string | null | undefined>) => {\n try {\n const _storage = storage();\n const newValue = _.isFunction(v) ? v(state) : v;\n if (_.isNil(newValue)) {\n _storage.removeItem(key);\n } else {\n _storage.setItem(key, newValue);\n }\n const emitter = emitterFor(_storage);\n emitter.emit('change');\n } catch (e) {\n console.error(e);\n }\n }, [key]);\n return [state ?? initialValue ?? null, setState] as const;\n}\n\nexport const useLocalStorage = (\n key: string,\n initialValue?: string | null\n) => _useStorage(() => window.localStorage, key, initialValue);\n\nexport const useSessionStorage = (\n key: string,\n initialValue?: string | null\n) => _useStorage(() => window.sessionStorage, key, initialValue);\n","//\n// visibility.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { useDocument } from './document';\n\nexport const useVisibility = () => {\n const document = useDocument();\n return useSyncExternalStore((onStoreChange) => {\n document.addEventListener('visibilitychange', onStoreChange);\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n }\n }, () => {\n if (document.hasFocus()) {\n return 'active' as const;\n } else if (document.visibilityState === 'visible') {\n return 'inactive' as const;\n } else {\n return 'background' as const;\n }\n }, () => 'unknown' as const);\n}\n","//\n// window.ts\n//\n// The MIT License\n// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in\n// all copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n\nimport _ from 'lodash';\nimport { useSyncExternalStore } from '../core/hooks/sync';\nimport { uniqueId } from '../core/utils';\nimport { reconciler } from '../core/reconciler/state';\nimport { _DOMRenderer } from '../renderer/common';\n\nexport const useWindow = () => {\n const state = reconciler.currentHookState;\n if (!state) throw Error('useWindow must be used within a render function.');\n if (state.renderer instanceof _DOMRenderer) {\n return state.renderer.window;\n } else {\n throw Error('Unsupported renderer.');\n }\n}\n\nconst emptyInsets = { top: 0, left: 0, right: 0, bottom: 0 };\nconst safeAreaInsets = (window: ReturnType<typeof useWindow>) => {\n let support;\n if (!('CSS' in window) || !_.isFunction(window.CSS.supports)) {\n return emptyInsets;\n }\n if (window.CSS.supports('top: env(safe-area-inset-top)')) {\n support = 'env'\n } else if (window.CSS.supports('top: constant(safe-area-inset-top)')) {\n support = 'constant'\n } else {\n return emptyInsets;\n }\n const id = uniqueId();\n const style = document.createElement('style');\n style.textContent = `:root {\n --${id}-top: ${support}(safe-area-inset-top);\n --${id}-left: ${support}(safe-area-inset-left);\n --${id}-right: ${support}(safe-area-inset-right);\n --${id}-bottom: ${support}(safe-area-inset-bottom);\n }`;\n document.head.appendChild(style);\n const computedStyle = getComputedStyle(document.documentElement);\n const insets = {\n top: computedStyle.getPropertyValue(`--${id}-top`),\n left: computedStyle.getPropertyValue(`--${id}-left`),\n right: computedStyle.getPropertyValue(`--${id}-right`),\n bottom: computedStyle.getPropertyValue(`--${id}-bottom`),\n };\n style.remove();\n return _.mapValues(insets, v => parseFloat(v));\n}\n\nexport const useWindowMetrics = () => {\n const window = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('resize', onStoreChange);\n return () => {\n window.removeEventListener('resize', onStoreChange);\n };\n }, () => ({\n safeAreaInsets: safeAreaInsets(window),\n devicePixelRatio: window.devicePixelRatio,\n outerWidth: window.outerWidth,\n outerHeight: window.outerHeight,\n innerWidth: window.innerWidth,\n innerHeight: window.innerHeight,\n }));\n}\n\nexport const useVisualViewportMetrics = () => {\n const { visualViewport } = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n visualViewport?.addEventListener('resize', onStoreChange);\n return () => {\n visualViewport?.removeEventListener('resize', onStoreChange);\n };\n }, () => visualViewport && ({\n width: visualViewport.width,\n height: visualViewport.height,\n scale: visualViewport.scale,\n }));\n}\n\nexport const useWindowScroll = () => {\n const window = useWindow();\n return useSyncExternalStore((onStoreChange) => {\n window.addEventListener('scroll', onStoreChange);\n return () => {\n window.removeEventListener('scroll', onStoreChange);\n };\n }, () => ({\n scrollX: window.scrollX,\n scrollY: window.scrollY,\n }));\n}\n\nconst colorSchemeDarkCache = new WeakMap<ReturnType<typeof useWindow>, MediaQueryList | undefined>();\n\nexport const useColorScheme = () => {\n const window = useWindow();\n if (!colorSchemeDarkCache.has(window)) colorSchemeDarkCache.set(window, window.matchMedia?.('(prefers-color-scheme: dark)'));\n const colorSchemeDark = colorSchemeDarkCache.get(window);\n return useSyncExternalStore((onStoreChange) => {\n colorSchemeDark?.addEventListener('change', onStoreChange);\n return () => {\n colorSchemeDark?.removeEventListener('change', onStoreChange);\n };\n }, () => colorSchemeDark?.matches ? 'dark' : 'light');\n}\n"],"names":[],"mappings":";;;;;;;;AAAO;;ACCP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;;AC5DO;AACA;AACA;AACA;;ACJA;;ACAA;;ACCA;AACA;;ACFA;;ACAA;AACA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACO;;"}
|
package/dist/web.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('./internals/state-CdizLCyu.js');
|
|
4
|
-
var common = require('./internals/common
|
|
4
|
+
var common = require('./internals/common-CnL8ftw8.js');
|
|
5
5
|
var _ = require('lodash');
|
|
6
|
-
var sync = require('./internals/sync-
|
|
6
|
+
var sync = require('./internals/sync-aZg8gOj1.js');
|
|
7
7
|
require('./internals/component-BiP3XIPe.js');
|
|
8
8
|
require('myers.js');
|
|
9
9
|
require('./internals/renderer-DCxs31Tn.js');
|
|
@@ -423,6 +423,115 @@ const useOnline = () => sync.useSyncExternalStore((onStoreChange) => {
|
|
|
423
423
|
};
|
|
424
424
|
}, () => navigator.onLine, () => false);
|
|
425
425
|
|
|
426
|
+
//
|
|
427
|
+
// decompress.js
|
|
428
|
+
//
|
|
429
|
+
// The MIT License
|
|
430
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
431
|
+
//
|
|
432
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
433
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
434
|
+
// in the Software without restriction, including without limitation the rights
|
|
435
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
436
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
437
|
+
// furnished to do so, subject to the following conditions:
|
|
438
|
+
//
|
|
439
|
+
// The above copyright notice and this permission notice shall be included in
|
|
440
|
+
// all copies or substantial portions of the Software.
|
|
441
|
+
//
|
|
442
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
443
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
444
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
445
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
446
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
447
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
448
|
+
// THE SOFTWARE.
|
|
449
|
+
//
|
|
450
|
+
// @ts-nocheck
|
|
451
|
+
const decompress = (o) => {
|
|
452
|
+
function r(r) {
|
|
453
|
+
for (i = f = 0; i < r;)
|
|
454
|
+
A < 2 && ((A = 64), (a = 92 < (a = o.charCodeAt(C++)) ? a - 59 : a - 58)),
|
|
455
|
+
(f |= (0 < (a & (A /= 2))) << i),
|
|
456
|
+
++i;
|
|
457
|
+
}
|
|
458
|
+
for (var n, f, i, t, a, e = [], u = 1, _ = 3, c = 1, h = [], A = 0, C = 0;;) {
|
|
459
|
+
if ((r(c + 1), 2 == f))
|
|
460
|
+
return h.join("");
|
|
461
|
+
-2 & (t = f) ||
|
|
462
|
+
(r(8 * f + 8),
|
|
463
|
+
(e[(t = _++)] = String.fromCharCode(f)),
|
|
464
|
+
--u || (u = 2 << c++)),
|
|
465
|
+
h.push((t = e[t] || n + n[0])),
|
|
466
|
+
n && ((e[_++] = n + t[0]), --u || (u = 2 << c++)),
|
|
467
|
+
(n = t);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
//
|
|
472
|
+
// server.ts
|
|
473
|
+
//
|
|
474
|
+
// The MIT License
|
|
475
|
+
// Copyright (c) 2021 - 2025 O2ter Limited. All rights reserved.
|
|
476
|
+
//
|
|
477
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
478
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
479
|
+
// in the Software without restriction, including without limitation the rights
|
|
480
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
481
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
482
|
+
// furnished to do so, subject to the following conditions:
|
|
483
|
+
//
|
|
484
|
+
// The above copyright notice and this permission notice shall be included in
|
|
485
|
+
// all copies or substantial portions of the Software.
|
|
486
|
+
//
|
|
487
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
488
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
489
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
490
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
491
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
492
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
493
|
+
// THE SOFTWARE.
|
|
494
|
+
//
|
|
495
|
+
const decodedSsrData = new WeakMap();
|
|
496
|
+
const useServerResource = (key, resource) => {
|
|
497
|
+
const state$1 = state.reconciler.currentHookState;
|
|
498
|
+
if (!state$1)
|
|
499
|
+
throw Error('useServerResource must be used within a render function.');
|
|
500
|
+
if (state$1.renderer instanceof common._DOMRenderer) {
|
|
501
|
+
if (state$1.renderer._server) {
|
|
502
|
+
const data = resource();
|
|
503
|
+
if (!_.isString(data))
|
|
504
|
+
throw Error('Invalid return type of resource');
|
|
505
|
+
state$1.renderer._tracked_server_resource.set(key, data);
|
|
506
|
+
return data;
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
const cached = decodedSsrData.get(state$1.renderer.document);
|
|
510
|
+
if (!_.isNil(cached))
|
|
511
|
+
return cached[key];
|
|
512
|
+
const ssrData = state$1.renderer.document.querySelector('script[data-frosty-ssr-data]');
|
|
513
|
+
if (ssrData instanceof HTMLElement) {
|
|
514
|
+
try {
|
|
515
|
+
const decoded = JSON.parse(decompress(ssrData.innerText.trim()));
|
|
516
|
+
decodedSsrData.set(state$1.renderer.document, decoded);
|
|
517
|
+
return decoded[key];
|
|
518
|
+
}
|
|
519
|
+
catch (e) {
|
|
520
|
+
console.error(e);
|
|
521
|
+
decodedSsrData.set(state$1.renderer.document, {});
|
|
522
|
+
}
|
|
523
|
+
ssrData.remove();
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
decodedSsrData.set(state$1.renderer.document, {});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
throw Error('Unsupported renderer.');
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
426
535
|
//
|
|
427
536
|
// storage.ts
|
|
428
537
|
//
|
|
@@ -545,6 +654,7 @@ exports.useOnline = useOnline;
|
|
|
545
654
|
exports.usePerformanceObserver = usePerformanceObserver;
|
|
546
655
|
exports.useResizeObserver = useResizeObserver;
|
|
547
656
|
exports.useSearchParams = useSearchParams;
|
|
657
|
+
exports.useServerResource = useServerResource;
|
|
548
658
|
exports.useSessionStorage = useSessionStorage;
|
|
549
659
|
exports.useVisibility = useVisibility;
|
|
550
660
|
exports.useVisualViewportMetrics = useVisualViewportMetrics;
|