@preact/signals 1.0.4 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # @preact/signals
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#91](https://github.com/preactjs/signals/pull/91) [`fb74bb9`](https://github.com/preactjs/signals/commit/fb74bb9ce4e44192e1ee7d3d041274cc985db767) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - add the `useSignalEffect` hook
8
+
9
+ * [#183](https://github.com/preactjs/signals/pull/183) [`79ff1e7`](https://github.com/preactjs/signals/commit/79ff1e794dde9952db2d6d43b22cebfb2accc770) Thanks [@jviide](https://github.com/jviide)! - Add ability to run custom cleanup logic when an effect is disposed.
10
+
11
+ ```js
12
+ effect(() => {
13
+ console.log("This runs whenever a dependency changes");
14
+ return () => {
15
+ console.log("This runs when the effect is disposed");
16
+ });
17
+ });
18
+ ```
19
+
20
+ ### Patch Changes
21
+
22
+ - [#186](https://github.com/preactjs/signals/pull/186) [`7242bd6`](https://github.com/preactjs/signals/commit/7242bd68cc570c6159600f271ee95977d3970d0f) Thanks [@marvinhagemeister](https://github.com/marvinhagemeister)! - Fix unable to set SVG attribute via Signal
23
+
24
+ * [#161](https://github.com/preactjs/signals/pull/161) [`6ac6923`](https://github.com/preactjs/signals/commit/6ac6923e5294f8a31ee1a009550b9891c3996cb4) Thanks [@jviide](https://github.com/jviide)! - Remove all usages of `Set`, `Map` and other allocation heavy objects in signals-core. This substaintially increases performance across all measurements.
25
+
26
+ - [#171](https://github.com/preactjs/signals/pull/171) [`fcbb3f4`](https://github.com/preactjs/signals/commit/fcbb3f4b9077e201badec77b91f75c23623d1a9c) Thanks [@jviide](https://github.com/jviide)! - Reduce size of Preact adapter by replacing `WeakSet`s with bitmasks.
27
+
28
+ - Updated dependencies [[`b4611cc`](https://github.com/preactjs/signals/commit/b4611cc9dee0ae09f4b378ba293c3203edc32be4), [`9802da5`](https://github.com/preactjs/signals/commit/9802da5274bb45c3cc28dda961b9b2d18535729a), [`6ac6923`](https://github.com/preactjs/signals/commit/6ac6923e5294f8a31ee1a009550b9891c3996cb4), [`79ff1e7`](https://github.com/preactjs/signals/commit/79ff1e794dde9952db2d6d43b22cebfb2accc770), [`3e31aab`](https://github.com/preactjs/signals/commit/3e31aabb812ddb0f7451deba38267f8384eff9d1)]:
29
+ - @preact/signals-core@1.2.0
30
+
3
31
  ## 1.0.4
4
32
 
5
33
  ### Patch Changes
package/dist/signals.d.ts CHANGED
@@ -2,6 +2,7 @@ import { signal, computed, batch, effect, Signal, type ReadonlySignal } from "@p
2
2
  export { signal, computed, batch, effect, Signal, type ReadonlySignal };
3
3
  export declare function useSignal<T>(value: T): Signal<T>;
4
4
  export declare function useComputed<T>(compute: () => T): ReadonlySignal<T>;
5
+ export declare function useSignalEffect(cb: () => void | (() => void)): void;
5
6
  /**
6
7
  * @todo Determine which Reactive implementation we'll be using.
7
8
  * @internal
package/dist/signals.js CHANGED
@@ -1 +1 @@
1
- var r,n,t,i=require("preact"),e=require("preact/hooks"),o=require("@preact/signals-core"),u=new WeakSet,f=new WeakSet,a=new WeakSet;function c(r,n){i.options[r]=n.bind(null,i.options[r]||function(){})}var v=new WeakMap;function s(r){t&&t(!0,!0),n=r,t=r&&r._()}function p(r){var n=o.signal(void 0);return n._u=r,n}function l(r){var t=this,i=r.data,u=h(i);u.value=i;var f=e.useMemo(function(){for(var r=t.__v;r=r.__;)if(r.__c){a.add(r.__c);break}return n._u=function(){t.base.data=f._v},o.computed(function(){var r=u.value.value;return 0===r?0:!0===r?"":r||""})},[]);return f.value}function _(r){var n={__proto__:null},t=p(function(i){var e=t.__;for(var u in e)if("children"!==u){var f=e[u];if(f instanceof o.Signal){var a=f.value,c=n[u];n[u]=a,!0===i||c===a||(u in r?r[u]=a:a?r.setAttribute(u,a):r.removeAttribute(u))}}});return t}function h(r){return e.useMemo(function(){return o.signal(r)},[])}l.displayName="_st",Object.defineProperties(o.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:l},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),c("__b",function(r,n){if("string"==typeof n.type){var t,i=n.props;for(var e in i)if("children"!==e){var u=i[e];u instanceof o.Signal&&(t||(n.__np=t={}),t[e]=u,i[e]=u.peek())}}r(n)}),c("__r",function(n,t){var i,e=t.__c;e&&(u.delete(e),void 0===(i=v.get(e))&&(i=p(function(){u.add(e),e.setState({})}),v.set(e,i))),r=e,s(i),n(t)}),c("__e",function(n,t,i,e){s(),r=void 0,n(t,i,e)}),c("diffed",function(n,t){var i,e;if(s(),r=void 0,"string"==typeof t.type&&(i=t.__e)){var o=t.__np;o&&((e=i._u)||(e=_(i),i._u=e),e.__=o,s(e),e._u(!0))}n(t)}),c("unmount",function(r,n){var t=n.__c,i=t&&v.get(t);if(i&&(v.delete(t),i._()(!0,!0)),"string"==typeof n.type){var e=n.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}r(n)}),c("__h",function(r,n,t,i){i<3&&f.add(n),r(n,t,i)}),i.Component.prototype.shouldComponentUpdate=function(r,n){var t,i=v.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||a.has(this)))return!0;if(u.has(this))return!0;if(f.has(this))return!0;for(var e in n)return!0;for(var o in r)if("__source"!==o&&r[o]!==this.props[o])return!0;for(var c in this.props)if(!(c in r))return!0;return!1},exports.Signal=o.Signal,exports.batch=o.batch,exports.computed=o.computed,exports.effect=o.effect,exports.signal=o.signal,exports.useComputed=function(n){var t=e.useRef(n);return t.current=n,a.add(r),e.useMemo(function(){return o.computed(function(){return t.current()})},[])},exports.useSignal=h;//# sourceMappingURL=signals.js.map
1
+ var r,n,i=require("preact"),t=require("preact/hooks"),f=require("@preact/signals-core");function o(r,n){i.options[r]=n.bind(null,i.options[r]||function(){})}function e(r){if(n)n();n=r&&r.S()}function u(r){var n=this,i=r.data,o=c(i);o.value=i;var e=t.useMemo(function(){var r=n.__v;while(r=r.__)if(r.__c){r.__c.__$f|=4;break}n.__$u.c=function(){n.base.data=e.peek()};return f.computed(function(){var r=o.value.value;return 0===r?0:!0===r?"":r||""})},[]);return e.value}u.displayName="_st";Object.defineProperties(f.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:u},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}});o("__b",function(r,n){if("string"==typeof n.type){var i,t=n.props;for(var o in t)if("children"!==o){var e=t[o];if(e instanceof f.Signal){if(!i)n.__np=i={};i[o]=e;t[o]=e.peek()}}}r(n)});o("__r",function(n,i){e();var t,o=i.__c;if(o){o.__$f&=-2;if(void 0===(t=o.__$u))o.__$u=t=function(r){var n;f.effect(function(){n=this});n.c=function(){o.__$f|=1;o.setState({})};return n}()}r=o;e(t);n(i)});o("__e",function(n,i,t,f){e();r=void 0;n(i,t,f)});o("diffed",function(n,i){e();r=void 0;var t;if("string"==typeof i.type&&(t=i.__e)){var f=i.__np,o=i.props;if(f){var u=t.U;if(u)for(var c in u){var v=u[c];if(void 0!==v&&!(c in f)){v.d();u[c]=void 0}}else t.U=u={};for(var s in f){var p=u[s],l=f[s];if(void 0===p){p=a(t,s,l,o);u[s]=p}else p.o(l,o)}}}n(i)});function a(r,n,i,t){var o=n in r&&void 0===r.ownerSVGElement,e=f.signal(i);return{o:function(r,n){e.value=r;t=n},d:f.effect(function(){var i=e.value.value;if(t[n]!==i){t[n]=i;if(o)r[n]=i;else if(i)r.setAttribute(n,i);else r.removeAttribute(n)}})}}o("unmount",function(r,n){var i=n.__c,t=i&&i.__$u;if(t)t.d();if("string"==typeof n.type){var f=n.__e,o=f.U;if(o){f.U=null;for(var e in o){var u=o[e];if(u)u.d()}}}r(n)});o("__h",function(r,n,i,t){if(t<3)n.__$f|=2;r(n,i,t)});i.Component.prototype.shouldComponentUpdate=function(r,n){var i=this.__$u;if(!(i&&void 0!==i.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(var t in n)return!0;for(var f in r)if("__source"!==f&&r[f]!==this.props[f])return!0;for(var o in this.props)if(!(o in r))return!0;return!1};function c(r){return t.useMemo(function(){return f.signal(r)},[])}exports.Signal=f.Signal;exports.batch=f.batch;exports.computed=f.computed;exports.effect=f.effect;exports.signal=f.signal;exports.useComputed=function(n){var i=t.useRef(n);i.current=n;r.__$f|=4;return t.useMemo(function(){return f.computed(function(){return i.current()})},[])};exports.useSignal=c;exports.useSignalEffect=function(r){var n=t.useRef(r);n.current=r;t.useEffect(function(){return f.effect(function(){n.current()})},[])};//# sourceMappingURL=signals.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["currentComponent","currentUpdater","preact","require","hooks","signalsCore","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","options","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","signal","undefined","s","_updater","_ref","_this","this","data","currentSignal","useSignal","value","useMemo","v","__v","__","__c","add","base","_value","computed","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","Component","shouldComponentUpdate","state","_updater$_deps","_deps","size","has","exports","batch","effect","useComputed","compute","$compute","useRef","current"],"mappings":"AAsBA,IAcAA,EACIC,IAfJC,EAAAC,QAAA,UAAAC,EAAAD,QAAA,gBAAAE,EAAAF,QAAA,wBAAMG,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErChB,EAAiBe,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUI,EAAMA,YAACC,GAEjB,OADAC,EAAEC,GAAWP,EACNM,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAML,EAAIS,EAAOA,QAAC,WAGjB,IADA,IAAKC,EAAGP,EAAKQ,IACLD,EAAIA,EAAEE,IACb,GAAIF,EAAEG,IAAK,CACV1B,EAAa2B,IAAIJ,EAA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DF,EAAI,MAAoB,SAACC,EAAKM,EAAWO,EAAOjB,GAC3CA,EAAO,GAAGpD,EAAa4B,IAAIkC,GAC/BN,EAAIM,EAAWO,EAAOjB,EACtB,GAMDkB,YAAUrB,UAAUsB,sBAAwB,SAAUlC,EAAOmC,GAE5D,IAAAC,EAAajE,EAAGF,EAAoB+C,IAAInC,MA2BxC,KAzBmBV,GAAmC,KAAxB,OAAAiE,EAAAjE,EAAQkE,SAAR,EAAAD,EAAeE,OAyBzB1E,EAAa2E,IAAI1D,OAAO,OAAO,EAGnD,GAAIpB,EAAiB8E,IAAI1D,MAAO,OAAO,EAGvC,GAAIlB,EAAa4E,IAAI1D,MAAO,OAAO,EAEnC,IAAK,IAAIyC,KAATa,EAAqB,OAAO,EAG5B,IAAK,IAAIb,KAATtB,EACC,GAAU,aAANsB,GAAoBtB,EAAMsB,KAAOzC,KAAKmB,MAAMsB,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAUtB,KAAAA,MAAO,KAAMsB,KAAKtB,GAAQ,OAA7C,EAGA,OAAO,CACP,EAWAwC,QAAA7B,OAAAnD,EAAAmD,OAAA6B,QAAAC,MAAAjF,EAAAiF,MAAAD,QAAA9C,SAAAlC,EAAAkC,SAAA8C,QAAAE,OAAAlF,EAAAkF,OAAAF,QAAAjE,OAAAf,EAAAe,OAAAiE,QAAAG,YALK,SAAyBC,GAC9B,IAAMC,EAAWC,EAAAA,OAAOF,GAGxB,OAFAC,EAASE,QAAUH,EACnBhF,EAAa2B,IAAIpC,GACV+B,UAAQ,WAAA,OAAcQ,EAAAA,SAAI,kBAAcmD,EAACE,SAAf,EAAlB,EAA6C,GAC5D,EAAAP,QAAAxD,UAAAA"}
1
+ {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo, useEffect } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tOptionsTypes,\n\tHookFn,\n\tEffect,\n\tPropertyUpdater,\n\tAugmentedComponent,\n\tAugmentedElement as Element,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst HAS_PENDING_UPDATE = 1 << 0;\nconst HAS_HOOK_STATE = 1 << 1;\nconst HAS_COMPUTEDS = 1 << 2;\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: AugmentedComponent | undefined;\nlet finishUpdate: (() => void) | undefined;\n\nfunction setCurrentUpdater(updater?: Effect) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate();\n\t// start tracking the new update:\n\tfinishUpdate = updater && updater._start();\n}\n\nfunction createUpdater(update: () => void) {\n\tlet updater!: Effect;\n\teffect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = update;\n\treturn updater;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: AugmentedComponent, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\tv.__c._updateFlags |= HAS_COMPUTEDS;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tthis._updater!._callback = () => {\n\t\t\t(this.base as Text).data = s.peek();\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tsetCurrentUpdater();\n\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\tcomponent._updateFlags &= ~HAS_PENDING_UPDATE;\n\n\t\tupdater = component._updater;\n\t\tif (updater === undefined) {\n\t\t\tcomponent._updater = updater = createUpdater(() => {\n\t\t\t\tcomponent._updateFlags |= HAS_PENDING_UPDATE;\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tlet renderedProps = vnode.props;\n\t\tif (props) {\n\t\t\tlet updaters = dom._updaters;\n\t\t\tif (updaters) {\n\t\t\t\tfor (let prop in updaters) {\n\t\t\t\t\tlet updater = updaters[prop];\n\t\t\t\t\tif (updater !== undefined && !(prop in props)) {\n\t\t\t\t\t\tupdater._dispose();\n\t\t\t\t\t\t// @todo we could just always invoke _dispose() here\n\t\t\t\t\t\tupdaters[prop] = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tupdaters = {};\n\t\t\t\tdom._updaters = updaters;\n\t\t\t}\n\t\t\tfor (let prop in props) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tlet signal = props[prop];\n\t\t\t\tif (updater === undefined) {\n\t\t\t\t\tupdater = createPropUpdater(dom, prop, signal, renderedProps);\n\t\t\t\t\tupdaters[prop] = updater;\n\t\t\t\t} else {\n\t\t\t\t\tupdater._update(signal, renderedProps);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\nfunction createPropUpdater(\n\tdom: Element,\n\tprop: string,\n\tpropSignal: Signal,\n\tprops: Record<string, any>\n): PropertyUpdater {\n\tconst setAsProperty =\n\t\tprop in dom &&\n\t\t// SVG elements need to go through `setAttribute` because they\n\t\t// expect things like SVGAnimatedTransformList instead of strings.\n\t\t// @ts-ignore\n\t\tdom.ownerSVGElement === undefined;\n\n\tconst changeSignal = signal(propSignal);\n\treturn {\n\t\t_update: (newSignal: Signal, newProps: typeof props) => {\n\t\t\tchangeSignal.value = newSignal;\n\t\t\tprops = newProps;\n\t\t},\n\t\t_dispose: effect(() => {\n\t\t\tconst value = changeSignal.value.value;\n\t\t\t// If Preact just rendered this value, don't render it again:\n\t\t\tif (props[prop] === value) return;\n\t\t\tprops[prop] = value;\n\t\t\tif (setAsProperty) {\n\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\tdom[prop] = value;\n\t\t\t} else if (value) {\n\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t} else {\n\t\t\t\tdom.removeAttribute(prop);\n\t\t\t}\n\t\t}),\n\t};\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && component._updater;\n\tif (updater) {\n\t\tupdater._dispose();\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\tconst updaters = dom._updaters;\n\t\tif (updaters) {\n\t\t\tdom._updaters = null;\n\t\t\tfor (let prop in updaters) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tif (updater) updater._dispose();\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3)\n\t\t(component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (\n\tthis: AugmentedComponent,\n\tprops,\n\tstate\n) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = this._updater;\n\tconst hasSignals = updater && updater._sources !== undefined;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !(this._updateFlags & HAS_COMPUTEDS)) return true;\n\n\t// if there is a pending re-render triggered from Signals,\n\t// or if there is hook or class state, update:\n\tif (this._updateFlags & (HAS_PENDING_UPDATE | HAS_HOOK_STATE)) return true;\n\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\t(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\tcallback.current();\n\t\t});\n\t}, []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["currentComponent","finishUpdate","preact","require","hooks","signalsCore","hook","hookName","hookFn","options","bind","setCurrentUpdater","updater","_start","_ref","_this","this","data","currentSignal","useSignal","value","s","useMemo","v","__v","__","__c","_updateFlags","_updater","_callback","base","peek","computed","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","props","get","__b","old","vnode","signalProps","i","__np","component","undefined","update","effect","setState","createUpdater","error","oldVNode","dom","__e","updaters","_updaters","prop","_dispose","signal","createPropUpdater","renderedProps","_update","propSignal","ownerSVGElement","changeSignal","newSignal","newProps","setAsProperty","setAttribute","removeAttribute","index","Component","shouldComponentUpdate","state","_sources","exports","batch","useComputed","compute","$compute","useRef","current","useSignalEffect","cb","callback","useEffect"],"mappings":"AAsBA,IAUAA,EACAC,EAXAC,EAAAC,QAAA,UAAAC,EAAAD,QAAA,gBAAAE,EAAAF,QAAA,wBAKA,SAAAG,EAAsCC,EAAaC,GAElDC,EAAOA,QAACF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAACF,IAAc,WAAO,EACnE,CAKD,SAASI,EAAkBC,GAE1B,GAAIX,EAAcA,IAElBA,EAAeW,GAAWA,EAAQC,GAClC,CAwBD,WAAkEC,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAKtBC,EAAGC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAMI,EAAIC,EAAOA,QAAC,WAEjB,IAAIC,EAAIR,EAAKS,IACb,MAAQD,EAAIA,EAAEE,GACb,GAAIF,EAAEG,IAAK,CACVH,EAAEG,IAAIC,MArDY,EAsDlB,KACA,CAIFZ,EAAKa,KAAUC,EAAY,WACzBd,EAAKe,KAAcb,KAAOI,EAAEU,MAC7B,EAED,OAAeC,EAAAA,SAAC,WACf,IACIX,EADOH,EAAcE,MACZA,MACb,OAAa,IAALC,EAAS,GAAU,IAANA,EAAa,GAAKA,GAAK,EAC5C,EACD,EA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A,EAAA8D,QAAAO,gBATeA,SAAgBC,GAC/B,IAAMC,EAAWJ,EAAMA,OAACG,GACxBC,EAASH,QAAUE,EAEnBE,EAAAA,UAAU,WACT,OAAatC,EAAAA,OAAC,WACbqC,EAASH,SACT,EACD,EAAE,GACH"}
@@ -1 +1 @@
1
- !function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],r):r((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,r,t,i){var e,o,f,u=new WeakSet,a=new WeakSet,c=new WeakSet;function v(n,t){r.options[n]=t.bind(null,r.options[n]||function(){})}var s=new WeakMap;function l(n){f&&f(!0,!0),o=n,f=n&&n._()}function p(n){var r=i.signal(void 0);return r._u=n,r}function d(n){var r=this,e=n.data,f=h(e);f.value=e;var u=t.useMemo(function(){for(var n=r.__v;n=n.__;)if(n.__c){c.add(n.__c);break}return o._u=function(){r.base.data=u._v},i.computed(function(){var n=f.value.value;return 0===n?0:!0===n?"":n||""})},[]);return u.value}function _(n){var r={__proto__:null},t=p(function(e){var o=t.__;for(var f in o)if("children"!==f){var u=o[f];if(u instanceof i.Signal){var a=u.value,c=r[f];r[f]=a,!0===e||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function h(n){return t.useMemo(function(){return i.signal(n)},[])}d.displayName="_st",Object.defineProperties(i.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),v("__b",function(n,r){if("string"==typeof r.type){var t,e=r.props;for(var o in e)if("children"!==o){var f=e[o];f instanceof i.Signal&&(t||(r.__np=t={}),t[o]=f,e[o]=f.peek())}}n(r)}),v("__r",function(n,r){var t,i=r.__c;i&&(u.delete(i),void 0===(t=s.get(i))&&(t=p(function(){u.add(i),i.setState({})}),s.set(i,t))),e=i,l(t),n(r)}),v("__e",function(n,r,t,i){l(),e=void 0,n(r,t,i)}),v("diffed",function(n,r){var t,i;if(l(),e=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=_(t),t._u=i),i.__=o,l(i),i._u(!0))}n(r)}),v("unmount",function(n,r){var t=r.__c,i=t&&s.get(t);if(i&&(s.delete(t),i._()(!0,!0)),"string"==typeof r.type){var e=r.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}n(r)}),v("__h",function(n,r,t,i){i<3&&a.add(r),n(r,t,i)}),r.Component.prototype.shouldComponentUpdate=function(n,r){var t,i=s.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||c.has(this)))return!0;if(u.has(this))return!0;if(a.has(this))return!0;for(var e in r)return!0;for(var o in n)if("__source"!==o&&n[o]!==this.props[o])return!0;for(var f in this.props)if(!(f in n))return!0;return!1},n.Signal=i.Signal,n.batch=i.batch,n.computed=i.computed,n.effect=i.effect,n.signal=i.signal,n.useComputed=function(n){var r=t.useRef(n);return r.current=n,c.add(e),t.useMemo(function(){return i.computed(function(){return r.current()})},[])},n.useSignal=h});//# sourceMappingURL=signals.min.js.map
1
+ !function(n,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],i):i((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,i,r,t){var f,e;function o(n,r){i.options[n]=r.bind(null,i.options[n]||function(){})}function u(n){if(e)e();e=n&&n.S()}function c(n){var i=this,f=n.data,e=v(f);e.value=f;var o=r.useMemo(function(){var n=i.__v;while(n=n.__)if(n.__c){n.__c.__$f|=4;break}i.__$u.c=function(){i.base.data=o.peek()};return t.computed(function(){var n=e.value.value;return 0===n?0:!0===n?"":n||""})},[]);return o.value}c.displayName="_st";Object.defineProperties(t.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:c},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}});o("__b",function(n,i){if("string"==typeof i.type){var r,f=i.props;for(var e in f)if("children"!==e){var o=f[e];if(o instanceof t.Signal){if(!r)i.__np=r={};r[e]=o;f[e]=o.peek()}}}n(i)});o("__r",function(n,i){u();var r,e=i.__c;if(e){e.__$f&=-2;if(void 0===(r=e.__$u))e.__$u=r=function(n){var i;t.effect(function(){i=this});i.c=function(){e.__$f|=1;e.setState({})};return i}()}f=e;u(r);n(i)});o("__e",function(n,i,r,t){u();f=void 0;n(i,r,t)});o("diffed",function(n,i){u();f=void 0;var r;if("string"==typeof i.type&&(r=i.__e)){var t=i.__np,e=i.props;if(t){var o=r.U;if(o)for(var c in o){var v=o[c];if(void 0!==v&&!(c in t)){v.d();o[c]=void 0}}else r.U=o={};for(var s in t){var l=o[s],d=t[s];if(void 0===l){l=a(r,s,d,e);o[s]=l}else l.o(d,e)}}}n(i)});function a(n,i,r,f){var e=i in n&&void 0===n.ownerSVGElement,o=t.signal(r);return{o:function(n,i){o.value=n;f=i},d:t.effect(function(){var r=o.value.value;if(f[i]!==r){f[i]=r;if(e)n[i]=r;else if(r)n.setAttribute(i,r);else n.removeAttribute(i)}})}}o("unmount",function(n,i){var r=i.__c,t=r&&r.__$u;if(t)t.d();if("string"==typeof i.type){var f=i.__e,e=f.U;if(e){f.U=null;for(var o in e){var u=e[o];if(u)u.d()}}}n(i)});o("__h",function(n,i,r,t){if(t<3)i.__$f|=2;n(i,r,t)});i.Component.prototype.shouldComponentUpdate=function(n,i){var r=this.__$u;if(!(r&&void 0!==r.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(var t in i)return!0;for(var f in n)if("__source"!==f&&n[f]!==this.props[f])return!0;for(var e in this.props)if(!(e in n))return!0;return!1};function v(n){return r.useMemo(function(){return t.signal(n)},[])}n.Signal=t.Signal;n.batch=t.batch;n.computed=t.computed;n.effect=t.effect;n.signal=t.signal;n.useComputed=function(n){var i=r.useRef(n);i.current=n;f.__$f|=4;return r.useMemo(function(){return t.computed(function(){return i.current()})},[])};n.useSignal=v;n.useSignalEffect=function(n){var i=r.useRef(n);i.current=n;r.useEffect(function(){return t.effect(function(){i.current()})},[])}});//# sourceMappingURL=signals.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["g","f","exports","module","require","define","amd","globalThis","self","preactSignals","preact","hooks","signalsCore","this","currentComponent","currentUpdater","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","options","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","signal","undefined","s","_updater","_ref","_this","data","currentSignal","useSignal","value","useMemo","v","__v","__","__c","add","base","_value","computed","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","Component","shouldComponentUpdate","state","_updater$_deps","_deps","size","has","batch","effect","useComputed","compute","$compute","useRef","current"],"mappings":"CAsBA,SAAAA,EAAAC,GAAA,iBAAAC,SAAA,oBAAAC,OAAAF,EAAAC,QAAAE,QAAA,UAAAA,QAAA,gBAAAA,QAAA,yBAAA,mBAAAC,QAAAA,OAAAC,IAAAD,OAAA,CAAA,UAAA,SAAA,eAAA,wBAAAJ,GAAAA,GAAAD,EAAA,oBAAAO,WAAAA,WAAAP,GAAAQ,MAAAC,cAAA,CAAA,EAAAT,EAAAU,OAAAV,EAAAW,MAAAX,EAAAY,YAAA,CAAA,CAAAC,KAAA,SAAAX,EAAAQ,EAAAC,EAAAC,GAAA,IAcAE,EACIC,IAfEC,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErCZ,EAAiBW,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUI,EAAMA,YAACC,GAEjB,OADAC,EAAEC,GAAWP,EACNM,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAtB,KAAAuB,EAAAF,EAAxBE,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAMJ,EAAIQ,EAAOA,QAAC,WAGjB,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port { useRef, useMemo, useEffect } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tOptionsTypes,\n\tHookFn,\n\tEffect,\n\tPropertyUpdater,\n\tAugmentedComponent,\n\tAugmentedElement as Element,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst HAS_PENDING_UPDATE = 1 << 0;\nconst HAS_HOOK_STATE = 1 << 1;\nconst HAS_COMPUTEDS = 1 << 2;\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: AugmentedComponent | undefined;\nlet finishUpdate: (() => void) | undefined;\n\nfunction setCurrentUpdater(updater?: Effect) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate();\n\t// start tracking the new update:\n\tfinishUpdate = updater && updater._start();\n}\n\nfunction createUpdater(update: () => void) {\n\tlet updater!: Effect;\n\teffect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = update;\n\treturn updater;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: AugmentedComponent, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\tv.__c._updateFlags |= HAS_COMPUTEDS;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tthis._updater!._callback = () => {\n\t\t\t(this.base as Text).data = s.peek();\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tsetCurrentUpdater();\n\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\tcomponent._updateFlags &= ~HAS_PENDING_UPDATE;\n\n\t\tupdater = component._updater;\n\t\tif (updater === undefined) {\n\t\t\tcomponent._updater = updater = createUpdater(() => {\n\t\t\t\tcomponent._updateFlags |= HAS_PENDING_UPDATE;\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tlet renderedProps = vnode.props;\n\t\tif (props) {\n\t\t\tlet updaters = dom._updaters;\n\t\t\tif (updaters) {\n\t\t\t\tfor (let prop in updaters) {\n\t\t\t\t\tlet updater = updaters[prop];\n\t\t\t\t\tif (updater !== undefined && !(prop in props)) {\n\t\t\t\t\t\tupdater._dispose();\n\t\t\t\t\t\t// @todo we could just always invoke _dispose() here\n\t\t\t\t\t\tupdaters[prop] = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tupdaters = {};\n\t\t\t\tdom._updaters = updaters;\n\t\t\t}\n\t\t\tfor (let prop in props) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tlet signal = props[prop];\n\t\t\t\tif (updater === undefined) {\n\t\t\t\t\tupdater = createPropUpdater(dom, prop, signal, renderedProps);\n\t\t\t\t\tupdaters[prop] = updater;\n\t\t\t\t} else {\n\t\t\t\t\tupdater._update(signal, renderedProps);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\nfunction createPropUpdater(\n\tdom: Element,\n\tprop: string,\n\tpropSignal: Signal,\n\tprops: Record<string, any>\n): PropertyUpdater {\n\tconst setAsProperty =\n\t\tprop in dom &&\n\t\t// SVG elements need to go through `setAttribute` because they\n\t\t// expect things like SVGAnimatedTransformList instead of strings.\n\t\t// @ts-ignore\n\t\tdom.ownerSVGElement === undefined;\n\n\tconst changeSignal = signal(propSignal);\n\treturn {\n\t\t_update: (newSignal: Signal, newProps: typeof props) => {\n\t\t\tchangeSignal.value = newSignal;\n\t\t\tprops = newProps;\n\t\t},\n\t\t_dispose: effect(() => {\n\t\t\tconst value = changeSignal.value.value;\n\t\t\t// If Preact just rendered this value, don't render it again:\n\t\t\tif (props[prop] === value) return;\n\t\t\tprops[prop] = value;\n\t\t\tif (setAsProperty) {\n\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\tdom[prop] = value;\n\t\t\t} else if (value) {\n\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t} else {\n\t\t\t\tdom.removeAttribute(prop);\n\t\t\t}\n\t\t}),\n\t};\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && component._updater;\n\tif (updater) {\n\t\tupdater._dispose();\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\tconst updaters = dom._updaters;\n\t\tif (updaters) {\n\t\t\tdom._updaters = null;\n\t\t\tfor (let prop in updaters) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tif (updater) updater._dispose();\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3)\n\t\t(component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (\n\tthis: AugmentedComponent,\n\tprops,\n\tstate\n) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = this._updater;\n\tconst hasSignals = updater && updater._sources !== undefined;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !(this._updateFlags & HAS_COMPUTEDS)) return true;\n\n\t// if there is a pending re-render triggered from Signals,\n\t// or if there is hook or class state, update:\n\tif (this._updateFlags & (HAS_PENDING_UPDATE | HAS_HOOK_STATE)) return true;\n\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\t(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\tcallback.current();\n\t\t});\n\t}, []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["g","f","exports","module","require","define","amd","globalThis","self","preactSignals","preact","hooks","signalsCore","this","currentComponent","finishUpdate","hook","hookName","hookFn","options","bind","setCurrentUpdater","updater","_start","_ref","_this","data","currentSignal","useSignal","value","s","useMemo","v","__v","__","__c","_updateFlags","_updater","_callback","base","peek","computed","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","props","get","__b","old","vnode","signalProps","i","__np","component","undefined","update","effect","setState","createUpdater","error","oldVNode","dom","__e","updaters","_updaters","prop","_dispose","signal","createPropUpdater","renderedProps","_update","propSignal","ownerSVGElement","changeSignal","newSignal","newProps","setAsProperty","setAttribute","removeAttribute","index","Component","shouldComponentUpdate","state","_sources","batch","useComputed","compute","$compute","useRef","current","useSignalEffect","cb","callback","useEffect"],"mappings":"CAsBA,SAAAA,EAAAC,GAAA,iBAAAC,SAAA,oBAAAC,OAAAF,EAAAC,QAAAE,QAAA,UAAAA,QAAA,gBAAAA,QAAA,yBAAA,mBAAAC,QAAAA,OAAAC,IAAAD,OAAA,CAAA,UAAA,SAAA,eAAA,wBAAAJ,GAAAA,GAAAD,EAAA,oBAAAO,WAAAA,WAAAP,GAAAQ,MAAAC,cAAA,CAAA,EAAAT,EAAAU,OAAAV,EAAAW,MAAAX,EAAAY,YAAA,CAAA,CAAAC,KAAA,SAAAX,EAAAQ,EAAAC,EAAAC,GAAA,IAUAE,EACAC,EANA,SAAAC,EAAsCC,EAAaC,GAElDC,EAAOA,QAACF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAACF,IAAc,WAAO,EACnE,CAKD,SAASI,EAAkBC,GAE1B,GAAIP,EAAcA,IAElBA,EAAeO,GAAWA,EAAQC,GAClC,CAwBD,WAAkEC,GAAA,IAAA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OAAI5C,EAAhB,EAAwB,GACvC,CAkBA3B,EAAA4C,OAAAlC,EAAAkC,OAAA5C,EAAAwF,MAAA9E,EAAA8E,MAAAxF,EAAAuC,SAAA7B,EAAA6B,SAAAvC,EAAA4D,OAAAlD,EAAAkD,OAAA5D,EAAAuE,OAAA7D,EAAA6D,OAAAvE,EAAAyF,YAhBK,SAAyBC,GAC9B,IAAcC,EAAGC,SAAOF,GACxBC,EAASE,QAAUH,EAClB9E,EAAwCsB,MAvTpB,EAwTrB,OAAOL,EAAAA,QAAQ,WAAA,OAAcU,EAAAA,SAAI,WAAMoD,OAAAA,EAASE,SAAf,EAAlB,EAA6C,GAC5D,EAWA7F,EAAA0B,UAAAA,EAAA1B,EAAA8F,gBATeA,SAAgBC,GAC/B,IAAMC,EAAWJ,EAAMA,OAACG,GACxBC,EAASH,QAAUE,EAEnBE,EAAAA,UAAU,WACT,OAAarC,EAAAA,OAAC,WACboC,EAASH,SACT,EACD,EAAE,GACH,CAAA"}
package/dist/signals.mjs CHANGED
@@ -1 +1 @@
1
- import{Component as t,options as n}from"preact";import{useMemo as e,useRef as r}from"preact/hooks";import{Signal as i,computed as o,signal as f}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const c=new WeakSet,u=new WeakSet,l=new WeakSet;function s(t,e){n[t]=e.bind(null,n[t]||(()=>{}))}let a,p,_;const h=new WeakMap;function d(t){_&&_(!0,!0),p=t,_=t&&t._()}function g(t){const n=f(void 0);return n._u=t,n}function b({data:t}){const n=v(t);n.value=t;const r=e(()=>{let t=this.__v;for(;t=t.__;)if(t.__c){l.add(t.__c);break}return p._u=()=>{this.base.data=r._v},o(()=>{let t=n.value.value;return 0===t?0:!0===t?"":t||""})},[]);return r.value}function m(t){const n={__proto__:null},e=g(r=>{const o=e.__;for(let e in o){if("children"===e)continue;let f=o[e];if(f instanceof i){let i=f.value,o=n[e];n[e]=i,!0===r||o===i||(e in t?t[e]=i:i?t.setAttribute(e,i):t.removeAttribute(e))}}});return e}function v(t){return e(()=>f(t),[])}function k(t){const n=r(t);return n.current=t,l.add(a),e(()=>o(()=>n.current()),[])}b.displayName="_st",Object.defineProperties(i.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:b},props:{configurable:!0,get(){return{data:this}}},__b:{configurable:!0,value:1}}),s("__b",(t,n)=>{if("string"==typeof n.type){let t,e=n.props;for(let r in e){if("children"===r)continue;let o=e[r];o instanceof i&&(t||(n.__np=t={}),t[r]=o,e[r]=o.peek())}}t(n)}),s("__r",(t,n)=>{let e,r=n.__c;r&&(c.delete(r),e=h.get(r),void 0===e&&(e=g(()=>{c.add(r),r.setState({})}),h.set(r,e))),a=r,d(e),t(n)}),s("__e",(t,n,e,r)=>{d(),a=void 0,t(n,e,r)}),s("diffed",(t,n)=>{let e,r;if(d(),a=void 0,"string"==typeof n.type&&(e=n.__e)){let t=n.__np;t&&(r=e._u,r||(r=m(e),e._u=r),r.__=t,d(r),r._u(!0))}t(n)}),s("unmount",(t,n)=>{let e=n.__c;const r=e&&h.get(e);if(r&&(h.delete(e),r._()(!0,!0)),"string"==typeof n.type){const t=n.__e,e=t._u;e&&(e._()(!0,!0),t._u=null)}t(n)}),s("__h",(t,n,e,r)=>{r<3&&u.add(n),t(n,e,r)}),t.prototype.shouldComponentUpdate=function(t,n){var e;const r=h.get(this);if(!(r&&0!==(null==(e=r._d)?void 0:e.size)||l.has(this)))return!0;if(c.has(this))return!0;if(u.has(this))return!0;for(let t in n)return!0;for(let n in t)if("__source"!==n&&t[n]!==this.props[n])return!0;for(let n in this.props)if(!(n in t))return!0;return!1};export{k as useComputed,v as useSignal};//# sourceMappingURL=signals.mjs.map
1
+ import{Component as t,options as i}from"preact";import{useMemo as e,useRef as n,useEffect as r}from"preact/hooks";import{Signal as o,computed as f,signal as l,effect as c}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";function s(t,e){i[t]=e.bind(null,i[t]||(()=>{}))}let u,a;function p(t){if(a)a();a=t&&t.S()}function d({data:t}){const i=_(t);i.value=t;const n=e(()=>{let t=this.__v;while(t=t.__)if(t.__c){t.__c.__$f|=4;break}this.__$u.c=()=>{this.base.data=n.peek()};return f(()=>{let t=i.value.value;return 0===t?0:!0===t?"":t||""})},[]);return n.value}d.displayName="_st";Object.defineProperties(o.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get(){return{data:this}}},__b:{configurable:!0,value:1}});s("__b",(t,i)=>{if("string"==typeof i.type){let t,e=i.props;for(let n in e){if("children"===n)continue;let r=e[n];if(r instanceof o){if(!t)i.__np=t={};t[n]=r;e[n]=r.peek()}}}t(i)});s("__r",(t,i)=>{p();let e,n=i.__c;if(n){n.__$f&=-2;e=n.__$u;if(void 0===e)n.__$u=e=function(t){let i;c(function(){i=this});i.c=()=>{n.__$f|=1;n.setState({})};return i}()}u=n;p(e);t(i)});s("__e",(t,i,e,n)=>{p();u=void 0;t(i,e,n)});s("diffed",(t,i)=>{p();u=void 0;let e;if("string"==typeof i.type&&(e=i.__e)){let t=i.__np,n=i.props;if(t){let i=e.U;if(i)for(let e in i){let n=i[e];if(void 0!==n&&!(e in t)){n.d();i[e]=void 0}}else{i={};e.U=i}for(let r in t){let o=i[r],f=t[r];if(void 0===o){o=h(e,r,f,n);i[r]=o}else o.o(f,n)}}}t(i)});function h(t,i,e,n){const r=i in t&&void 0===t.ownerSVGElement,o=l(e);return{o:(t,i)=>{o.value=t;n=i},d:c(()=>{const e=o.value.value;if(n[i]!==e){n[i]=e;if(r)t[i]=e;else if(e)t.setAttribute(i,e);else t.removeAttribute(i)}})}}s("unmount",(t,i)=>{let e=i.__c;const n=e&&e.__$u;if(n)n.d();if("string"==typeof i.type){const t=i.__e,e=t.U;if(e){t.U=null;for(let t in e){let i=e[t];if(i)i.d()}}}t(i)});s("__h",(t,i,e,n)=>{if(n<3)i.__$f|=2;t(i,e,n)});t.prototype.shouldComponentUpdate=function(t,i){const e=this.__$u;if(!(e&&void 0!==e.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(let t in i)return!0;for(let i in t)if("__source"!==i&&t[i]!==this.props[i])return!0;for(let i in this.props)if(!(i in t))return!0;return!1};function _(t){return e(()=>l(t),[])}function g(t){const i=n(t);i.current=t;u.__$f|=4;return e(()=>f(()=>i.current()),[])}function v(t){const i=n(t);i.current=t;r(()=>c(()=>{i.current()}),[])}export{g as useComputed,_ as useSignal,v as useSignalEffect};//# sourceMappingURL=signals.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","Signal","computed","signal","batch","effect","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","bind","currentComponent","currentUpdater","finishUpdate","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","undefined","_updater","Text","data","currentSignal","useSignal","value","v","this","__v","__","__c","add","base","_value","dom","cache","__proto__","skip","props","_props","prop","cached","setAttribute","removeAttribute","useComputed","compute","$compute","current","displayName","Object","defineProperties","prototype","constructor","configurable","type","get","__b","old","vnode","signalProps","i","__np","peek","component","delete","setState","set","error","oldVNode","__e","createElementUpdater","index","shouldComponentUpdate","state","_updater$_deps","size","_deps","has"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,cAAAC,YAAAC,MAAA,8BAAAF,OAAAG,MAAAF,SAAAG,OAAAF,WAAA,uBAAA,MAAsBG,EAAG,IAAIC,QAGXC,EAAG,IAArBD,QAGME,EAAe,IAArBF,QAGA,SAAAG,EAAsCC,EAAaC,GAElDd,EAAQa,GAAYC,EAAOC,KAAK,KAAMf,EAAQa,IAAc,MAAtB,GACtC,CAED,IAAAG,EACIC,EACAC,EACJ,MAAyBC,EAAG,IAAIC,QAEhC,SAASC,EAAkBC,GAEtBJ,GAAcA,GAAa,GAAM,GAErCD,EAAiBK,EACjBJ,EAAeI,GAAWA,EAAQC,GAClC,CAED,SAAAC,EAAuBF,GACtB,MAAMG,EAAIpB,OAAOqB,GAEjB,OADAD,EAAEE,GAAWL,EACNG,CACP,CAeD,SAASG,GAA0BC,KAAEA,IAKpC,MAAmBC,EAAGC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,MAAMJ,EAAIxB,EAAQ,KAEjB,IAAKgC,EAAGC,KAAKC,IACb,KAAQF,EAAIA,EAAEG,IACb,GAAIH,EAAEI,IAAK,CACV1B,EAAa2B,IAAIL,EAAEI,KACnB,KACA,CAQF,OAJApB,EAAgBU,GAAW,KACzBO,KAAKK,KAAcV,KAAOJ,EAAEe,EAC7B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5BmB,EAAId,GAAW,KAEhB,CACDqC,EAAIC,KAILrD,EAAI,MAAoB,CAACoD,EAAKM,EAAWQ,EAAOjB,KAC3CA,EAAO,GAAGnD,EAAa4B,IAAIgC,GAC/BN,EAAIM,EAAWQ,EAAOjB,EAAnB,GAOJ9D,EAAU2D,UAAUqB,sBAAwB,SAAUlC,EAAOmC,GAE5D,IAAAC,EAAA,QAAgB9D,EAAoB2C,IAAI5B,MA2BxC,KAzBmBZ,GAAmC,KAAT4D,OAAfD,EAAA3D,EAAQ6D,SAAOD,EAAAA,EAAAA,OAyBzBvE,EAAayE,IAAIlD,OAAO,OAAA,EAG5C,GAAI1B,EAAiB4E,IAAIlD,MAAO,OAAO,EAGvC,GAAIxB,EAAa0E,IAAIlD,MAAO,OAAO,EAEnC,IAAK,IAALiC,KAAAa,EAAqB,OAAO,EAG5B,IAAK,IAALb,KAAAtB,EACC,GAAU,aAANsB,GAAoBtB,EAAMsB,KAAOjC,KAAKW,MAAMsB,GAAI,OAAO,EAE5D,IAAK,IAALA,KAAmBtB,KAAAA,MAAO,KAAMsB,KAAFtB,GAAe,OAAA,EAG7C,OAAO,CACP,SAWAM,iBAAApB"}
1
+ {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo, useEffect } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tOptionsTypes,\n\tHookFn,\n\tEffect,\n\tPropertyUpdater,\n\tAugmentedComponent,\n\tAugmentedElement as Element,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst HAS_PENDING_UPDATE = 1 << 0;\nconst HAS_HOOK_STATE = 1 << 1;\nconst HAS_COMPUTEDS = 1 << 2;\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: AugmentedComponent | undefined;\nlet finishUpdate: (() => void) | undefined;\n\nfunction setCurrentUpdater(updater?: Effect) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate();\n\t// start tracking the new update:\n\tfinishUpdate = updater && updater._start();\n}\n\nfunction createUpdater(update: () => void) {\n\tlet updater!: Effect;\n\teffect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = update;\n\treturn updater;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: AugmentedComponent, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\tv.__c._updateFlags |= HAS_COMPUTEDS;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tthis._updater!._callback = () => {\n\t\t\t(this.base as Text).data = s.peek();\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tsetCurrentUpdater();\n\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\tcomponent._updateFlags &= ~HAS_PENDING_UPDATE;\n\n\t\tupdater = component._updater;\n\t\tif (updater === undefined) {\n\t\t\tcomponent._updater = updater = createUpdater(() => {\n\t\t\t\tcomponent._updateFlags |= HAS_PENDING_UPDATE;\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tlet renderedProps = vnode.props;\n\t\tif (props) {\n\t\t\tlet updaters = dom._updaters;\n\t\t\tif (updaters) {\n\t\t\t\tfor (let prop in updaters) {\n\t\t\t\t\tlet updater = updaters[prop];\n\t\t\t\t\tif (updater !== undefined && !(prop in props)) {\n\t\t\t\t\t\tupdater._dispose();\n\t\t\t\t\t\t// @todo we could just always invoke _dispose() here\n\t\t\t\t\t\tupdaters[prop] = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tupdaters = {};\n\t\t\t\tdom._updaters = updaters;\n\t\t\t}\n\t\t\tfor (let prop in props) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tlet signal = props[prop];\n\t\t\t\tif (updater === undefined) {\n\t\t\t\t\tupdater = createPropUpdater(dom, prop, signal, renderedProps);\n\t\t\t\t\tupdaters[prop] = updater;\n\t\t\t\t} else {\n\t\t\t\t\tupdater._update(signal, renderedProps);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\nfunction createPropUpdater(\n\tdom: Element,\n\tprop: string,\n\tpropSignal: Signal,\n\tprops: Record<string, any>\n): PropertyUpdater {\n\tconst setAsProperty =\n\t\tprop in dom &&\n\t\t// SVG elements need to go through `setAttribute` because they\n\t\t// expect things like SVGAnimatedTransformList instead of strings.\n\t\t// @ts-ignore\n\t\tdom.ownerSVGElement === undefined;\n\n\tconst changeSignal = signal(propSignal);\n\treturn {\n\t\t_update: (newSignal: Signal, newProps: typeof props) => {\n\t\t\tchangeSignal.value = newSignal;\n\t\t\tprops = newProps;\n\t\t},\n\t\t_dispose: effect(() => {\n\t\t\tconst value = changeSignal.value.value;\n\t\t\t// If Preact just rendered this value, don't render it again:\n\t\t\tif (props[prop] === value) return;\n\t\t\tprops[prop] = value;\n\t\t\tif (setAsProperty) {\n\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\tdom[prop] = value;\n\t\t\t} else if (value) {\n\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t} else {\n\t\t\t\tdom.removeAttribute(prop);\n\t\t\t}\n\t\t}),\n\t};\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && component._updater;\n\tif (updater) {\n\t\tupdater._dispose();\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\tconst updaters = dom._updaters;\n\t\tif (updaters) {\n\t\t\tdom._updaters = null;\n\t\t\tfor (let prop in updaters) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tif (updater) updater._dispose();\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3)\n\t\t(component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (\n\tthis: AugmentedComponent,\n\tprops,\n\tstate\n) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = this._updater;\n\tconst hasSignals = updater && updater._sources !== undefined;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !(this._updateFlags & HAS_COMPUTEDS)) return true;\n\n\t// if there is a pending re-render triggered from Signals,\n\t// or if there is hook or class state, update:\n\tif (this._updateFlags & (HAS_PENDING_UPDATE | HAS_HOOK_STATE)) return true;\n\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\t(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\tcallback.current();\n\t\t});\n\t}, []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","useEffect","Signal","computed","signal","effect","batch","hook","hookName","hookFn","bind","finishUpdate","setCurrentUpdater","updater","_start","Text","data","currentSignal","useSignal","value","s","v","this","__v","__","__c","_updateFlags","_updater","_callback","base","peek","displayName","Object","defineProperties","prototype","constructor","configurable","type","props","get","__b","old","vnode","signalProps","i","__np","component","undefined","update","setState","createUpdater","currentComponent","error","oldVNode","dom","__e","updaters","_updaters","prop","_dispose","createPropUpdater","renderedProps","_update","propSignal","setAsProperty","ownerSVGElement","newSignal","newProps","changeSignal","setAttribute","removeAttribute","index","shouldComponentUpdate","state","_sources","HAS_PENDING_UPDATE","useComputed","compute","$compute","current","useSignalEffect","cb","callback"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,eAAAC,MAAA,gCAAAC,cAAAC,YAAAC,YAAAC,MAAA,8BAAAH,OAAAI,MAAAH,SAAAE,OAAAD,WAAA,uBAKA,SAAAG,EAAsCC,EAAaC,GAElDX,EAAQU,GAAYC,EAAOC,KAAK,KAAMZ,EAAQU,IAAc,MAAtB,GACtC,CAED,MACAG,EAEA,SAASC,EAAkBC,GAE1B,GAAIF,EAAcA,IAElBA,EAAeE,GAAWA,EAAQC,GAClC,CAwBD,SAASC,GAA+BC,KAAEA,IAKzC,MAAmBC,EAAGC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,MAAOI,EAAGrB,EAAQ,KAEjB,IAAIsB,EAAIC,KAAKC,IACb,MAAQF,EAAIA,EAAEG,GACb,GAAIH,EAAEI,IAAK,CACVJ,EAAEI,IAAIC,MArDY,EAsDlB,KACA,CAIFJ,KAAKK,KAAUC,EAAY,KACzBN,KAAKO,KAAcb,KAAOI,EAAEU,MAAF,EAG5B,SAAgB,KACf,IACKV,EADMH,EAAcE,MACZA,MACb,OAAa,MAAI,GAAU,IAANC,EAAa,GAAKA,GAAK,IAH9B,EAKb,IAEH,OAAOA,EAAED,KACT,CACDJ,EAAKgB,YAAc,MAEnBC,OAAOC,iBAAiB/B,EAAOgC,UAAW,CACzCC,YAAa,CAAEC,cAAc,GAC7BC,KAAM,CAAED,cAAc,EAAMjB,MAAOJ,GACnCuB,MAAO,CACNF,cAAc,EACdG,MACC,MAAO,CAAEvB,KAAMM,KACf,GAKFkB,IAAK,CAAEJ,cAAc,EAAMjB,MAAO,KAInCZ,QAAwB,CAACkC,EAAKC,KAC7B,GAA0B,iBAAVA,EAACL,KA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ACtB,GAMDxC,EAAUqC,UAAUsC,sBAAwB,SAE3ClC,EACAmC,GAGA,MAAa5D,EAAGS,KAAKK,KA0BrB,KAzBmBd,QAAgCkC,IAArBlC,EAAQ6D,GApQjB,EA6RApD,KAAKI,MAA+B,SAIzD,GAAyBiD,EAArBrD,KAAKI,KAAsD,OAAA,EAG/D,IAAK,IAAIkB,KAAK6B,EAAO,OAAO,EAG5B,IAAK,IAAI7B,KAAKN,EACb,GAAU,aAANM,GAAoBN,EAAMM,KAAOtB,KAAKgB,MAAMM,GAAI,OACpD,EACD,IAAK,SAAStB,KAAKgB,MAAO,KAAMM,KAAKN,GAAQ,OAAO,EAGpD,OAAO,CACP,EAEepB,SAAAA,EAAaC,GAC5B,OAAcpB,EAAC,IAAMK,EAAUe,GAAQ,GACvC,CAEeyD,SAAAA,EAAeC,GAC9B,MAAcC,EAAG9E,EAAO6E,GACxBC,EAASC,QAAUF,EAClB1B,EAAwCzB,MAvTpB,EAwTrB,OAAO3B,EAAQ,IAAMI,EAAY,IAAM2E,EAASC,WAAY,GAC5D,CAEeC,SAAAA,EAAgBC,GAC/B,MAAMC,EAAWlF,EAAOiF,GACxBC,EAASH,QAAUE,EAEnBhF,EAAU,IACII,EAAC,KACb6E,EAASH,YAER,GACH,QAAAH,iBAAA1D,eAAA8D"}
@@ -1 +1 @@
1
- import{Component as n,options as r}from"preact";import{useMemo as t,useRef as i}from"preact/hooks";import{Signal as o,computed as e,signal as f}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var u,a,c,v=new WeakSet,s=new WeakSet,l=new WeakSet;function p(n,t){r[n]=t.bind(null,r[n]||function(){})}var _=new WeakMap;function h(n){c&&c(!0,!0),a=n,c=n&&n._()}function g(n){var r=f(void 0);return r._u=n,r}function d(n){var r=this,i=n.data,o=m(i);o.value=i;var f=t(function(){for(var n=r.__v;n=n.__;)if(n.__c){l.add(n.__c);break}return a._u=function(){r.base.data=f._v},e(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function b(n){var r={__proto__:null},t=g(function(i){var e=t.__;for(var f in e)if("children"!==f){var u=e[f];if(u instanceof o){var a=u.value,c=r[f];r[f]=a,!0===i||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function m(n){return t(function(){return f(n)},[])}function k(n){var r=i(n);return r.current=n,l.add(u),t(function(){return e(function(){return r.current()})},[])}d.displayName="_st",Object.defineProperties(o.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),p("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var e in i)if("children"!==e){var f=i[e];f instanceof o&&(t||(r.__np=t={}),t[e]=f,i[e]=f.peek())}}n(r)}),p("__r",function(n,r){var t,i=r.__c;i&&(v.delete(i),void 0===(t=_.get(i))&&(t=g(function(){v.add(i),i.setState({})}),_.set(i,t))),u=i,h(t),n(r)}),p("__e",function(n,r,t,i){h(),u=void 0,n(r,t,i)}),p("diffed",function(n,r){var t,i;if(h(),u=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=b(t),t._u=i),i.__=o,h(i),i._u(!0))}n(r)}),p("unmount",function(n,r){var t=r.__c,i=t&&_.get(t);if(i&&(_.delete(t),i._()(!0,!0)),"string"==typeof r.type){var o=r.__e,e=o._u;e&&(e._()(!0,!0),o._u=null)}n(r)}),p("__h",function(n,r,t,i){i<3&&s.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=_.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||l.has(this)))return!0;if(v.has(this))return!0;if(s.has(this))return!0;for(var o in r)return!0;for(var e in n)if("__source"!==e&&n[e]!==this.props[e])return!0;for(var f in this.props)if(!(f in n))return!0;return!1};export{k as useComputed,m as useSignal};//# sourceMappingURL=signals.module.js.map
1
+ import{Component as n,options as r}from"preact";import{useMemo as i,useRef as t,useEffect as f}from"preact/hooks";import{Signal as o,computed as u,signal as e,effect as a}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var c,v;function s(n,i){r[n]=i.bind(null,r[n]||function(){})}function l(n){if(v)v();v=n&&n.S()}function p(n){var r=this,t=n.data,f=_(t);f.value=t;var o=i(function(){var n=r.__v;while(n=n.__)if(n.__c){n.__c.__$f|=4;break}r.__$u.c=function(){r.base.data=o.peek()};return u(function(){var n=f.value.value;return 0===n?0:!0===n?"":n||""})},[]);return o.value}p.displayName="_st";Object.defineProperties(o.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:p},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}});s("__b",function(n,r){if("string"==typeof r.type){var i,t=r.props;for(var f in t)if("children"!==f){var u=t[f];if(u instanceof o){if(!i)r.__np=i={};i[f]=u;t[f]=u.peek()}}}n(r)});s("__r",function(n,r){l();var i,t=r.__c;if(t){t.__$f&=-2;if(void 0===(i=t.__$u))t.__$u=i=function(n){var r;a(function(){r=this});r.c=function(){t.__$f|=1;t.setState({})};return r}()}c=t;l(i);n(r)});s("__e",function(n,r,i,t){l();c=void 0;n(r,i,t)});s("diffed",function(n,r){l();c=void 0;var i;if("string"==typeof r.type&&(i=r.__e)){var t=r.__np,f=r.props;if(t){var o=i.U;if(o)for(var u in o){var e=o[u];if(void 0!==e&&!(u in t)){e.d();o[u]=void 0}}else i.U=o={};for(var a in t){var v=o[a],s=t[a];if(void 0===v){v=d(i,a,s,f);o[a]=v}else v.o(s,f)}}}n(r)});function d(n,r,i,t){var f=r in n&&void 0===n.ownerSVGElement,o=e(i);return{o:function(n,r){o.value=n;t=r},d:a(function(){var i=o.value.value;if(t[r]!==i){t[r]=i;if(f)n[r]=i;else if(i)n.setAttribute(r,i);else n.removeAttribute(r)}})}}s("unmount",function(n,r){var i=r.__c,t=i&&i.__$u;if(t)t.d();if("string"==typeof r.type){var f=r.__e,o=f.U;if(o){f.U=null;for(var u in o){var e=o[u];if(e)e.d()}}}n(r)});s("__h",function(n,r,i,t){if(t<3)r.__$f|=2;n(r,i,t)});n.prototype.shouldComponentUpdate=function(n,r){var i=this.__$u;if(!(i&&void 0!==i.s||4&this.__$f))return!0;if(3&this.__$f)return!0;for(var t in r)return!0;for(var f in n)if("__source"!==f&&n[f]!==this.props[f])return!0;for(var o in this.props)if(!(o in n))return!0;return!1};function _(n){return i(function(){return e(n)},[])}function h(n){var r=t(n);r.current=n;c.__$f|=4;return i(function(){return u(function(){return r.current()})},[])}function g(n){var r=t(n);r.current=n;f(function(){return a(function(){r.current()})},[])}export{h as useComputed,_ as useSignal,g as useSignalEffect};//# sourceMappingURL=signals.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","Signal","computed","signal","batch","effect","currentComponent","currentUpdater","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","undefined","s","_updater","_ref","_this","this","data","currentSignal","useSignal","value","v","__v","__","__c","add","base","_value","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","useComputed","compute","$compute","current","Text","displayName","Object","defineProperties","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","shouldComponentUpdate","state","_updater$_deps","_deps","size","has"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,cAAAC,YAAAC,MAAA,8BAAAF,OAAAG,MAAAF,SAAAG,OAAAF,WAAA,uBAAA,IAcAG,EACIC,IAfEC,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDf,EAAQc,GAAYC,EAAOC,KAAK,KAAMhB,EAAQc,IAAc,WAAO,EACnE,CAKD,IAAyBG,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErCX,EAAiBU,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUd,OAAOkB,GAEjB,OADAC,EAAEC,GAAWN,EACNK,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAML,EAAIvB,EAAQ,WAGjB,IADA,IAAKgC,EAAGN,EAAKO,IACLD,EAAIA,EAAEE,IAC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,GAARF,EAAsB,GAAM,IAGH,iBAAViD,EAACL,KAAmB,CACnC,IAAStB,EAAG2B,EAAMW,IAGLtD,EAAGgB,EAAIhB,GAChBN,IACHA,EAAQE,GAARF,EAAsB,GAAM,GAE5BsB,EAAIhB,GAAW,KAEhB,CACD0C,EAAIC,EACJ,GAGDF,EAAI,MAAoB,SAACC,EAAKM,EAAWO,EAAOjB,GAC3CA,EAAO,GAAGnD,EAAayB,IAAIoC,GAC/BN,EAAIM,EAAWO,EAAOjB,EACtB,GAMDhE,EAAU6D,UAAUqB,sBAAwB,SAAUpC,EAAOqC,GAE5D,IAAAC,EAAahE,EAAGF,EAAoB+C,IAAIpC,MA2BxC,KAzBmBT,GAAmC,KAAxB,OAAAgE,EAAAhE,EAAQiE,SAAR,EAAAD,EAAeE,OAyBzBxE,EAAayE,IAAI1D,OAAO,OAAO,EAGnD,GAAIlB,EAAiB4E,IAAI1D,MAAO,OAAO,EAGvC,GAAIhB,EAAa0E,IAAI1D,MAAO,OAAO,EAEnC,IAAK,IAAI0C,KAATY,EAAqB,OAAO,EAG5B,IAAK,IAAIZ,KAATzB,EACC,GAAU,aAANyB,GAAoBzB,EAAMyB,KAAO1C,KAAKiB,MAAMyB,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAUzB,KAAAA,MAAO,KAAMyB,KAAKzB,GAAQ,OAA7C,EAGA,OAAO,CACP,SAWAO,iBAAArB"}
1
+ {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo, useEffect } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tOptionsTypes,\n\tHookFn,\n\tEffect,\n\tPropertyUpdater,\n\tAugmentedComponent,\n\tAugmentedElement as Element,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst HAS_PENDING_UPDATE = 1 << 0;\nconst HAS_HOOK_STATE = 1 << 1;\nconst HAS_COMPUTEDS = 1 << 2;\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: AugmentedComponent | undefined;\nlet finishUpdate: (() => void) | undefined;\n\nfunction setCurrentUpdater(updater?: Effect) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate();\n\t// start tracking the new update:\n\tfinishUpdate = updater && updater._start();\n}\n\nfunction createUpdater(update: () => void) {\n\tlet updater!: Effect;\n\teffect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = update;\n\treturn updater;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: AugmentedComponent, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\tv.__c._updateFlags |= HAS_COMPUTEDS;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tthis._updater!._callback = () => {\n\t\t\t(this.base as Text).data = s.peek();\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tsetCurrentUpdater();\n\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\tcomponent._updateFlags &= ~HAS_PENDING_UPDATE;\n\n\t\tupdater = component._updater;\n\t\tif (updater === undefined) {\n\t\t\tcomponent._updater = updater = createUpdater(() => {\n\t\t\t\tcomponent._updateFlags |= HAS_PENDING_UPDATE;\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tlet renderedProps = vnode.props;\n\t\tif (props) {\n\t\t\tlet updaters = dom._updaters;\n\t\t\tif (updaters) {\n\t\t\t\tfor (let prop in updaters) {\n\t\t\t\t\tlet updater = updaters[prop];\n\t\t\t\t\tif (updater !== undefined && !(prop in props)) {\n\t\t\t\t\t\tupdater._dispose();\n\t\t\t\t\t\t// @todo we could just always invoke _dispose() here\n\t\t\t\t\t\tupdaters[prop] = undefined;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tupdaters = {};\n\t\t\t\tdom._updaters = updaters;\n\t\t\t}\n\t\t\tfor (let prop in props) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tlet signal = props[prop];\n\t\t\t\tif (updater === undefined) {\n\t\t\t\t\tupdater = createPropUpdater(dom, prop, signal, renderedProps);\n\t\t\t\t\tupdaters[prop] = updater;\n\t\t\t\t} else {\n\t\t\t\t\tupdater._update(signal, renderedProps);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\nfunction createPropUpdater(\n\tdom: Element,\n\tprop: string,\n\tpropSignal: Signal,\n\tprops: Record<string, any>\n): PropertyUpdater {\n\tconst setAsProperty =\n\t\tprop in dom &&\n\t\t// SVG elements need to go through `setAttribute` because they\n\t\t// expect things like SVGAnimatedTransformList instead of strings.\n\t\t// @ts-ignore\n\t\tdom.ownerSVGElement === undefined;\n\n\tconst changeSignal = signal(propSignal);\n\treturn {\n\t\t_update: (newSignal: Signal, newProps: typeof props) => {\n\t\t\tchangeSignal.value = newSignal;\n\t\t\tprops = newProps;\n\t\t},\n\t\t_dispose: effect(() => {\n\t\t\tconst value = changeSignal.value.value;\n\t\t\t// If Preact just rendered this value, don't render it again:\n\t\t\tif (props[prop] === value) return;\n\t\t\tprops[prop] = value;\n\t\t\tif (setAsProperty) {\n\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\tdom[prop] = value;\n\t\t\t} else if (value) {\n\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t} else {\n\t\t\t\tdom.removeAttribute(prop);\n\t\t\t}\n\t\t}),\n\t};\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && component._updater;\n\tif (updater) {\n\t\tupdater._dispose();\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\tconst updaters = dom._updaters;\n\t\tif (updaters) {\n\t\t\tdom._updaters = null;\n\t\t\tfor (let prop in updaters) {\n\t\t\t\tlet updater = updaters[prop];\n\t\t\t\tif (updater) updater._dispose();\n\t\t\t}\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3)\n\t\t(component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (\n\tthis: AugmentedComponent,\n\tprops,\n\tstate\n) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = this._updater;\n\tconst hasSignals = updater && updater._sources !== undefined;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !(this._updateFlags & HAS_COMPUTEDS)) return true;\n\n\t// if there is a pending re-render triggered from Signals,\n\t// or if there is hook or class state, update:\n\tif (this._updateFlags & (HAS_PENDING_UPDATE | HAS_HOOK_STATE)) return true;\n\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\t(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\tcallback.current();\n\t\t});\n\t}, []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","useEffect","Signal","computed","signal","effect","batch","currentComponent","finishUpdate","hook","hookName","hookFn","bind","setCurrentUpdater","updater","_start","_ref","_this","this","data","currentSignal","useSignal","value","s","v","__v","__","__c","_updateFlags","_updater","_callback","base","peek","Text","displayName","Object","defineProperties","prototype","constructor","configurable","type","props","get","__b","old","vnode","signalProps","i","__np","component","undefined","update","setState","createUpdater","error","oldVNode","dom","__e","updaters","_updaters","prop","_dispose","createPropUpdater","renderedProps","_update","propSignal","ownerSVGElement","changeSignal","newSignal","newProps","setAsProperty","setAttribute","removeAttribute","index","shouldComponentUpdate","state","_sources","compute","$compute","current","useSignalEffect","cb","callback","useComputed"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,eAAAC,MAAA,gCAAAC,cAAAC,YAAAC,YAAAC,MAAA,8BAAAH,OAAAI,MAAAH,SAAAE,OAAAD,WAAA,uBAAA,IAUAG,EACAC,EANA,SAAAC,EAAsCC,EAAaC,GAElDb,EAAQY,GAAYC,EAAOC,KAAK,KAAMd,EAAQY,IAAc,WAAO,EACnE,CAKD,SAASG,EAAkBC,GAE1B,GAAIN,EAAcA,IAElBA,EAAeM,GAAWA,EAAQC,GAClC,CAwBD,WAAkEC,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAKtBC,EAAGC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAMI,EAAIxB,EAAQ,WAEjB,IAAIyB,EAAIP,EAAKQ,IACb,MAAQD,EAAIA,EAAEE,GACb,GAAIF,EAAEG,IAAK,CACVH,EAAEG,IAAIC,MArDY,EAsDlB,KACA,CAIFX,EAAKY,KAAUC,EAAY,WACzBb,EAAKc,KAAcZ,KAAOI,EAAES,MAC7B,EAED,OAAe7B,EAAC,WACf,IACIoB,EADOH,EAAcE,MACZA,MACb,OAAa,IAALC,EAAS,GAAU,IAANA,EAAa,GAAKA,GAAK,EAC5C,EACD,EAAE,IAEH,OAAQA,EAACD,KACT,CACDW,EAAKC,YAAc,MAEnBC,OAAOC,iBAAiBlC,EAAOmC,UAAW,CACzCC,YAAa,CAAEC,cAAc,GAC7BC,KAAM,CAAED,cAAc,EAAMjB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,CACD,CACDjB,EAAIC,EACJ,GAGDpC,EAAI,MAAoB,SAACmC,EAAKK,EAAWwB,EAAOjC,GAC/C,GAAIA,EAAO,EACTS,EAAiCrB,MAtPb,EAuPtBgB,EAAIK,EAAWwB,EAAOjC,EACtB,GAMD3C,EAAUwC,UAAUqC,sBAAwB,SAE3CjC,EACAkC,GAGA,IAAM7D,EAAUI,KAAKW,KA0BrB,KAzBmBf,QAAgCoC,IAArBpC,EAAQ8D,GApQjB,EA6RA1D,KAAKU,MAA+B,OAAA,EAIzD,KAAIV,KAAKU,KAAsD,OAAA,EAG/D,IAAK,SAAS+C,EAAO,SAGrB,IAAK,IAAI5B,KAAKN,EACb,GAAU,aAANM,GAAoBN,EAAMM,KAAO7B,KAAKuB,MAAMM,GAAI,OACpD,EACD,IAAK,IAAIA,KAAK7B,KAAKuB,MAAO,KAAMM,KAAFN,GAAe,OAA7C,EAGA,OAAO,CACP,EAEepB,SAAAA,EAAaC,GAC5B,SAAe,WAAA,OAAYlB,EAAIkB,EAAhB,EAAwB,GACvC,CAEK,WAAyBuD,GAC9B,IAAcC,EAAG9E,EAAO6E,GACxBC,EAASC,QAAUF,EAClBtE,EAAwCqB,MAvTpB,EAwTrB,OAAO7B,EAAQ,WAAA,OAAcI,EAAI,WAAM2E,OAAAA,EAASC,SAAf,EAAlB,EAA6C,GAC5D,CAEeC,SAAAA,EAAgBC,GAC/B,IAAMC,EAAWlF,EAAOiF,GACxBC,EAASH,QAAUE,EAEnBhF,EAAU,WACT,OAAaI,EAAC,WACb6E,EAASH,SACT,EACD,EAAE,GACH,QAAAI,iBAAA9D,eAAA2D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preact/signals",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "description": "",
6
6
  "keywords": [],
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "mangle": "../../mangle.json",
37
37
  "dependencies": {
38
- "@preact/signals-core": "^1.1.1"
38
+ "@preact/signals-core": "^1.2.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "preact": "10.x"
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { options, Component } from "preact";
2
- import { useRef, useMemo } from "preact/hooks";
2
+ import { useRef, useMemo, useEffect } from "preact/hooks";
3
3
  import {
4
4
  signal,
5
5
  computed,
@@ -10,23 +10,19 @@ import {
10
10
  } from "@preact/signals-core";
11
11
  import {
12
12
  VNode,
13
- ComponentType,
14
13
  OptionsTypes,
15
14
  HookFn,
16
- Updater,
17
- ElementUpdater,
15
+ Effect,
16
+ PropertyUpdater,
17
+ AugmentedComponent,
18
+ AugmentedElement as Element,
18
19
  } from "./internal";
19
20
 
20
21
  export { signal, computed, batch, effect, Signal, type ReadonlySignal };
21
22
 
22
- // Components that have a pending Signal update: (used to bypass default sCU:false)
23
- const hasPendingUpdate = new WeakSet<Component>();
24
-
25
- // Components that have useState()/useReducer() hooks:
26
- const hasHookState = new WeakSet<Component>();
27
-
28
- // Components that have useComputed():
29
- const hasComputeds = new WeakSet<Component>();
23
+ const HAS_PENDING_UPDATE = 1 << 0;
24
+ const HAS_HOOK_STATE = 1 << 1;
25
+ const HAS_COMPUTEDS = 1 << 2;
30
26
 
31
27
  // Install a Preact options hook
32
28
  function hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {
@@ -34,23 +30,23 @@ function hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {
34
30
  options[hookName] = hookFn.bind(null, options[hookName] || (() => {}));
35
31
  }
36
32
 
37
- let currentComponent: Component | undefined;
38
- let currentUpdater: Updater | undefined;
39
- let finishUpdate: ReturnType<Updater["_setCurrent"]> | undefined;
40
- const updaterForComponent = new WeakMap<Component | VNode, Updater>();
33
+ let currentComponent: AugmentedComponent | undefined;
34
+ let finishUpdate: (() => void) | undefined;
41
35
 
42
- function setCurrentUpdater(updater?: Updater) {
36
+ function setCurrentUpdater(updater?: Effect) {
43
37
  // end tracking for the current update:
44
- if (finishUpdate) finishUpdate(true, true);
38
+ if (finishUpdate) finishUpdate();
45
39
  // start tracking the new update:
46
- currentUpdater = updater;
47
- finishUpdate = updater && updater._setCurrent();
40
+ finishUpdate = updater && updater._start();
48
41
  }
49
42
 
50
- function createUpdater(updater: () => void) {
51
- const s = signal(undefined) as Updater;
52
- s._updater = updater;
53
- return s;
43
+ function createUpdater(update: () => void) {
44
+ let updater!: Effect;
45
+ effect(function (this: Effect) {
46
+ updater = this;
47
+ });
48
+ updater._callback = update;
49
+ return updater;
54
50
  }
55
51
 
56
52
  /** @todo This may be needed for complex prop value detection. */
@@ -66,7 +62,7 @@ function createUpdater(updater: () => void) {
66
62
  * A wrapper component that renders a Signal directly as a Text node.
67
63
  * @todo: in Preact 11, just decorate Signal with `type:null`
68
64
  */
69
- function Text(this: ComponentType, { data }: { data: Signal }) {
65
+ function Text(this: AugmentedComponent, { data }: { data: Signal }) {
70
66
  // hasComputeds.add(this);
71
67
 
72
68
  // Store the props.data signal in another signal so that
@@ -79,14 +75,14 @@ function Text(this: ComponentType, { data }: { data: Signal }) {
79
75
  let v = this.__v;
80
76
  while ((v = v.__!)) {
81
77
  if (v.__c) {
82
- hasComputeds.add(v.__c);
78
+ v.__c._updateFlags |= HAS_COMPUTEDS;
83
79
  break;
84
80
  }
85
81
  }
86
82
 
87
83
  // Replace this component's vdom updater with a direct text one:
88
- currentUpdater!._updater = () => {
89
- (this.base as Text).data = s._value;
84
+ this._updater!._callback = () => {
85
+ (this.base as Text).data = s.peek();
90
86
  };
91
87
 
92
88
  return computed(() => {
@@ -138,19 +134,20 @@ hook(OptionsTypes.DIFF, (old, vnode) => {
138
134
 
139
135
  /** Set up Updater before rendering a component */
140
136
  hook(OptionsTypes.RENDER, (old, vnode) => {
137
+ setCurrentUpdater();
138
+
141
139
  let updater;
142
140
 
143
141
  let component = vnode.__c;
144
142
  if (component) {
145
- hasPendingUpdate.delete(component);
143
+ component._updateFlags &= ~HAS_PENDING_UPDATE;
146
144
 
147
- updater = updaterForComponent.get(component);
145
+ updater = component._updater;
148
146
  if (updater === undefined) {
149
- updater = createUpdater(() => {
150
- hasPendingUpdate.add(component);
147
+ component._updater = updater = createUpdater(() => {
148
+ component._updateFlags |= HAS_PENDING_UPDATE;
151
149
  component.setState({});
152
150
  });
153
- updaterForComponent.set(component, updater);
154
151
  }
155
152
  }
156
153
 
@@ -172,75 +169,96 @@ hook(OptionsTypes.DIFFED, (old, vnode) => {
172
169
  currentComponent = undefined;
173
170
 
174
171
  let dom: Element;
175
- let updater: ElementUpdater;
176
172
 
177
173
  // vnode._dom is undefined during string rendering,
178
174
  // so we use this to skip prop subscriptions during SSR.
179
175
  if (typeof vnode.type === "string" && (dom = vnode.__e as Element)) {
180
176
  let props = vnode.__np;
177
+ let renderedProps = vnode.props;
181
178
  if (props) {
182
- // @ts-ignore-next
183
- updater = dom._updater;
184
- if (!updater) {
185
- updater = createElementUpdater(dom);
186
- // @ts-ignore-next
187
- dom._updater = updater;
179
+ let updaters = dom._updaters;
180
+ if (updaters) {
181
+ for (let prop in updaters) {
182
+ let updater = updaters[prop];
183
+ if (updater !== undefined && !(prop in props)) {
184
+ updater._dispose();
185
+ // @todo we could just always invoke _dispose() here
186
+ updaters[prop] = undefined;
187
+ }
188
+ }
189
+ } else {
190
+ updaters = {};
191
+ dom._updaters = updaters;
192
+ }
193
+ for (let prop in props) {
194
+ let updater = updaters[prop];
195
+ let signal = props[prop];
196
+ if (updater === undefined) {
197
+ updater = createPropUpdater(dom, prop, signal, renderedProps);
198
+ updaters[prop] = updater;
199
+ } else {
200
+ updater._update(signal, renderedProps);
201
+ }
188
202
  }
189
- updater!._props = props;
190
- setCurrentUpdater(updater);
191
- // @ts-ignore-next we're adding an argument here
192
- updater._updater(true);
193
203
  }
194
204
  }
195
205
  old(vnode);
196
206
  });
197
207
 
198
- // per-element updater for 1+ signal bindings
199
- function createElementUpdater(dom: Element) {
200
- const cache: Record<string, any> = { __proto__: null };
201
- const updater = createUpdater((skip?: boolean) => {
202
- const props = updater._props;
203
- for (let prop in props) {
204
- if (prop === "children") continue;
205
- let signal = props[prop];
206
- if (signal instanceof Signal) {
207
- let value = signal.value;
208
- let cached = cache[prop];
209
- cache[prop] = value;
210
- if (skip === true || cached === value) {
211
- // this is just a subscribe run, not an update
212
- } else if (prop in dom) {
213
- // @ts-ignore-next-line silly
214
- dom[prop] = value;
215
- } else if (value) {
216
- dom.setAttribute(prop, value);
217
- } else {
218
- dom.removeAttribute(prop);
219
- }
208
+ function createPropUpdater(
209
+ dom: Element,
210
+ prop: string,
211
+ propSignal: Signal,
212
+ props: Record<string, any>
213
+ ): PropertyUpdater {
214
+ const setAsProperty =
215
+ prop in dom &&
216
+ // SVG elements need to go through `setAttribute` because they
217
+ // expect things like SVGAnimatedTransformList instead of strings.
218
+ // @ts-ignore
219
+ dom.ownerSVGElement === undefined;
220
+
221
+ const changeSignal = signal(propSignal);
222
+ return {
223
+ _update: (newSignal: Signal, newProps: typeof props) => {
224
+ changeSignal.value = newSignal;
225
+ props = newProps;
226
+ },
227
+ _dispose: effect(() => {
228
+ const value = changeSignal.value.value;
229
+ // If Preact just rendered this value, don't render it again:
230
+ if (props[prop] === value) return;
231
+ props[prop] = value;
232
+ if (setAsProperty) {
233
+ // @ts-ignore-next-line silly
234
+ dom[prop] = value;
235
+ } else if (value) {
236
+ dom.setAttribute(prop, value);
237
+ } else {
238
+ dom.removeAttribute(prop);
220
239
  }
221
- }
222
- }) as ElementUpdater;
223
- return updater;
240
+ }),
241
+ };
224
242
  }
225
243
 
226
244
  /** Unsubscribe from Signals when unmounting components/vnodes */
227
245
  hook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {
228
246
  let component = vnode.__c;
229
- const updater = component && updaterForComponent.get(component);
247
+ const updater = component && component._updater;
230
248
  if (updater) {
231
- updaterForComponent.delete(component);
232
- updater._setCurrent()(true, true);
249
+ updater._dispose();
233
250
  }
234
251
 
235
252
  if (typeof vnode.type === "string") {
236
253
  const dom = vnode.__e as Element;
237
254
 
238
- // @ts-ignore-next
239
- const updater = dom._updater;
240
- if (updater) {
241
- updater._setCurrent()(true, true);
242
- // @ts-ignore-next
243
- dom._updater = null;
255
+ const updaters = dom._updaters;
256
+ if (updaters) {
257
+ dom._updaters = null;
258
+ for (let prop in updaters) {
259
+ let updater = updaters[prop];
260
+ if (updater) updater._dispose();
261
+ }
244
262
  }
245
263
  }
246
264
  old(vnode);
@@ -248,7 +266,8 @@ hook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {
248
266
 
249
267
  /** Mark components that use hook state so we can skip sCU optimization. */
250
268
  hook(OptionsTypes.HOOK, (old, component, index, type) => {
251
- if (type < 3) hasHookState.add(component);
269
+ if (type < 3)
270
+ (component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;
252
271
  old(component, index, type);
253
272
  });
254
273
 
@@ -256,11 +275,14 @@ hook(OptionsTypes.HOOK, (old, component, index, type) => {
256
275
  * Auto-memoize components that use Signals/Computeds.
257
276
  * Note: Does _not_ optimize components that use hook/class state.
258
277
  */
259
- Component.prototype.shouldComponentUpdate = function (props, state) {
278
+ Component.prototype.shouldComponentUpdate = function (
279
+ this: AugmentedComponent,
280
+ props,
281
+ state
282
+ ) {
260
283
  // @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:
261
- const updater = updaterForComponent.get(this);
262
-
263
- const hasSignals = updater && updater._deps?.size !== 0;
284
+ const updater = this._updater;
285
+ const hasSignals = updater && updater._sources !== undefined;
264
286
 
265
287
  // let reason;
266
288
  // if (!hasSignals && !hasComputeds.has(this)) {
@@ -285,13 +307,12 @@ Component.prototype.shouldComponentUpdate = function (props, state) {
285
307
  // }
286
308
 
287
309
  // if this component used no signals or computeds, update:
288
- if (!hasSignals && !hasComputeds.has(this)) return true;
310
+ if (!hasSignals && !(this._updateFlags & HAS_COMPUTEDS)) return true;
289
311
 
290
- // if there is a pending re-render triggered from Signals, update:
291
- if (hasPendingUpdate.has(this)) return true;
312
+ // if there is a pending re-render triggered from Signals,
313
+ // or if there is hook or class state, update:
314
+ if (this._updateFlags & (HAS_PENDING_UPDATE | HAS_HOOK_STATE)) return true;
292
315
 
293
- // if there is hook or class state, update:
294
- if (hasHookState.has(this)) return true;
295
316
  // @ts-ignore
296
317
  for (let i in state) return true;
297
318
 
@@ -312,10 +333,21 @@ export function useSignal<T>(value: T) {
312
333
  export function useComputed<T>(compute: () => T) {
313
334
  const $compute = useRef(compute);
314
335
  $compute.current = compute;
315
- hasComputeds.add(currentComponent!);
336
+ (currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;
316
337
  return useMemo(() => computed<T>(() => $compute.current()), []);
317
338
  }
318
339
 
340
+ export function useSignalEffect(cb: () => void | (() => void)) {
341
+ const callback = useRef(cb);
342
+ callback.current = cb;
343
+
344
+ useEffect(() => {
345
+ return effect(() => {
346
+ callback.current();
347
+ });
348
+ }, []);
349
+ }
350
+
319
351
  /**
320
352
  * @todo Determine which Reactive implementation we'll be using.
321
353
  * @internal
package/src/internal.d.ts CHANGED
@@ -1,9 +1,31 @@
1
1
  import { Component } from "preact";
2
2
  import { Signal } from "@preact/signals-core";
3
3
 
4
+ export interface Effect {
5
+ _sources: object | undefined;
6
+ _start(): () => void;
7
+ _callback(): void;
8
+ _dispose(): void;
9
+ }
10
+
11
+ export interface PropertyUpdater {
12
+ _update: (newSignal: Signal, newProps: Record<string, any>) => void;
13
+ _dispose: () => void;
14
+ }
15
+
16
+ export interface AugmentedElement extends HTMLElement {
17
+ _updaters?: Record<string, PropertyUpdater | undefined> | null;
18
+ }
19
+
20
+ export interface AugmentedComponent extends Component<any, any> {
21
+ __v: VNode;
22
+ _updater?: Effect;
23
+ _updateFlags: number;
24
+ }
25
+
4
26
  export interface VNode<P = any> extends preact.VNode<P> {
5
27
  /** The component instance for this VNode */
6
- __c: Component;
28
+ __c: AugmentedComponent;
7
29
  /** The parent VNode */
8
30
  __?: VNode;
9
31
  /** The DOM node for this VNode */
@@ -12,17 +34,6 @@ export interface VNode<P = any> extends preact.VNode<P> {
12
34
  __np?: Record<string, any> | null;
13
35
  }
14
36
 
15
- export interface ComponentType extends Component {
16
- /** This component's owner VNode */
17
- __v: VNode;
18
- }
19
-
20
- export type Updater = Signal<unknown>;
21
-
22
- export interface ElementUpdater extends Updater {
23
- _props: Record<string, any>;
24
- }
25
-
26
37
  export const enum OptionsTypes {
27
38
  HOOK = "__h",
28
39
  DIFF = "__b",
@@ -1,6 +1,6 @@
1
1
  import { signal, useComputed } from "@preact/signals";
2
2
  import { createElement, render } from "preact";
3
- import { setupRerender } from "preact/test-utils";
3
+ import { setupRerender, act } from "preact/test-utils";
4
4
 
5
5
  const sleep = (ms?: number) => new Promise(r => setTimeout(r, ms));
6
6
 
@@ -254,5 +254,86 @@ describe("@preact/signals", () => {
254
254
  await sleep();
255
255
  expect(spy).not.to.have.been.called;
256
256
  });
257
+
258
+ it("should set updated signal prop values at most once", async () => {
259
+ const s = signal("initial");
260
+ const spy = sinon.spy();
261
+ function Wrap() {
262
+ spy();
263
+ // @ts-ignore
264
+ return <span ariaLabel={s} ariaDescription={s.value} />;
265
+ }
266
+ render(<Wrap />, scratch);
267
+ spy.resetHistory();
268
+
269
+ const span = scratch.firstElementChild as HTMLSpanElement;
270
+ const ariaLabel = sinon.spy();
271
+ Object.defineProperty(span, "ariaLabel", {
272
+ set: ariaLabel,
273
+ });
274
+ const ariaDescription = sinon.spy();
275
+ Object.defineProperty(span, "ariaDescription", {
276
+ set: ariaDescription,
277
+ });
278
+
279
+ act(() => {
280
+ s.value = "updated";
281
+ });
282
+
283
+ expect(spy).to.have.been.calledOnce;
284
+
285
+ expect(ariaLabel).to.have.been.calledOnce;
286
+ expect(ariaLabel).to.have.been.calledWith("updated");
287
+ ariaLabel.resetHistory();
288
+
289
+ expect(ariaDescription).to.have.been.calledOnce;
290
+ expect(ariaDescription).to.have.been.calledWith("updated");
291
+ ariaDescription.resetHistory();
292
+
293
+ // ensure the component was never re-rendered: (even after a tick)
294
+ await sleep();
295
+
296
+ expect(ariaLabel).not.to.have.been.called;
297
+ expect(ariaDescription).not.to.have.been.called;
298
+
299
+ act(() => {
300
+ s.value = "second update";
301
+ });
302
+
303
+ expect(ariaLabel).to.have.been.calledOnce;
304
+ expect(ariaLabel).to.have.been.calledWith("second update");
305
+ ariaLabel.resetHistory();
306
+
307
+ expect(ariaDescription).to.have.been.calledOnce;
308
+ expect(ariaDescription).to.have.been.calledWith("second update");
309
+ ariaDescription.resetHistory();
310
+
311
+ // ensure the component was never re-rendered: (even after a tick)
312
+ await sleep();
313
+
314
+ expect(ariaLabel).not.to.have.been.called;
315
+ expect(ariaDescription).not.to.have.been.called;
316
+ });
317
+
318
+ it("should set SVG values", async () => {
319
+ const s = signal("scale(1 1)");
320
+
321
+ function App() {
322
+ return (
323
+ <svg>
324
+ <line
325
+ // @ts-ignore
326
+ transform={s}
327
+ />
328
+ </svg>
329
+ );
330
+ }
331
+ render(<App />, scratch);
332
+
333
+ act(() => {
334
+ // This should not crash
335
+ s.value = "scale(1, 2)";
336
+ });
337
+ });
257
338
  });
258
339
  });