@preact/signals 1.0.0 → 1.0.3

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,25 @@
1
1
  # @preact/signals
2
2
 
3
+ ## 1.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - ab5bd99: Fix swapping and HMR for signals when used as text
8
+
9
+ ## 1.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 2383684: Correctly replace props-value with peeked value
14
+ - Updated dependencies [5644c1f]
15
+ - @preact/signals-core@1.0.1
16
+
17
+ ## 1.0.1
18
+
19
+ ### Patch Changes
20
+
21
+ - c7c0d91: Add marker for devtools to `Text` that is created when a signal is passed into JSX
22
+
3
23
  ## 1.0.0
4
24
 
5
25
  ### Major Changes
package/README.md ADDED
@@ -0,0 +1,112 @@
1
+
2
+ # Signals
3
+
4
+ Signals is a performant state management library with two primary goals:
5
+
6
+ 1. Make it as easy as possible to write business logic for small up to complex apps. No matter how complex your logic is, your app updates should stay fast without you needing to think about it. Signals automatically optimize state updates behind the scenes to trigger the fewest updates necessary. They are lazy by default and automatically skip signals that no one listens to.
7
+ 2. Integrate into frameworks as if they were native built-in primitives. You don't need any selectors, wrapper functions, or anything else. Signals can be accessed directly and your component will automatically re-render when the signal's value changes.
8
+
9
+ Read the [announcement post](https://preactjs.com/blog/introducing-signals/) to learn more about which problems signals solves and how it came to be.
10
+
11
+ ## Installation:
12
+
13
+ ```sh
14
+ npm install @preact/signals
15
+ ```
16
+
17
+ - [Guide / API](../../README.md#guide--api)
18
+ - [`signal(initialValue)`](../../README.md#signalinitialvalue)
19
+ - [`signal.peek()`](../../README.md#signalpeek)
20
+ - [`computed(fn)`](../../README.md#computedfn)
21
+ - [`effect(fn)`](../../README.md#effectfn)
22
+ - [`batch(fn)`](../../README.md#batchfn)
23
+ - [Preact Integration](#preact-integration)
24
+ - [Hooks](#hooks)
25
+ - [Rendering optimizations](#rendering-optimizations)
26
+ - [Attribute optimization (experimental)](#attribute-optimization-experimental)
27
+ - [License](#license)
28
+
29
+ ## Preact Integration
30
+
31
+ The Preact integration can be installed via:
32
+
33
+ ```sh
34
+ npm install @preact/signals
35
+ ```
36
+
37
+ It allows you to access signals as if they were native to Preact. Whenever you read a signal inside a component we'll automatically subscribe the component to that. When you update the signal we'll know that this component needs to be updated and will do that for you.
38
+
39
+ ```js
40
+ // The Preact adapter re-exports the core library
41
+ import { signal } from "@preact/signals";
42
+
43
+ const count = signal(0);
44
+
45
+ function CounterValue() {
46
+ // Whenever the `count` signal is updated, we'll
47
+ // re-render this component automatically for you
48
+ return <p>Value: {count.value}</p>;
49
+ }
50
+ ```
51
+
52
+ ### Hooks
53
+
54
+ If you need to instantiate new signals inside your components, you can use the `useSignal` or `useComputed` hook.
55
+
56
+ ```js
57
+ import { useSignal, useComputed } from "@preact/signals";
58
+
59
+ function Counter() {
60
+ const count = useSignal(0);
61
+ const double = useComputed(() => count.value * 2);
62
+
63
+ return (
64
+ <button onClick={() => count.value++}>
65
+ Value: {count.value}, value x 2 = {double.value}
66
+ </button>
67
+ );
68
+ }
69
+ ```
70
+
71
+ ### Rendering optimizations
72
+
73
+ The Preact adapter ships with several optimizations it can apply out of the box to skip virtual-dom rendering entirely. If you pass a signal directly into JSX, it will bind directly to the DOM `Text` node that is created and update that whenever the signal changes.
74
+
75
+ ```js
76
+ import { signal } from "@preact/signals";
77
+
78
+ const count = signal(0);
79
+
80
+ // Unoptimized: Will trigger the surrounding
81
+ // component to re-render
82
+ function Counter() {
83
+ return <p>Value: {count.value}</p>;
84
+ }
85
+
86
+ // Optimized: Will update the text node directly
87
+ function Counter() {
88
+ return <p>Value: {count}</p>;
89
+ }
90
+ ```
91
+
92
+ To opt into this optimization, simply pass the signal directly instead of accessing the `.value` property.
93
+
94
+ #### Attribute optimization (experimental)
95
+
96
+ We can also pass signals directly as an attribute to an HTML element node.
97
+
98
+ ```js
99
+ import { signal } from "@preact/signals";
100
+
101
+ const inputValue = signal("foobar");
102
+
103
+ function Person() {
104
+ return <input value={inputValue} />;
105
+ }
106
+ ```
107
+
108
+ This way we'll bypass checking the virtual-dom and update the DOM property directly.
109
+
110
+ ## License
111
+
112
+ `MIT`, see the [LICENSE](../../LICENSE) file.
package/dist/signals.js CHANGED
@@ -1 +1,2 @@
1
- var n,r,t,i=require("preact"),e=require("preact/hooks"),o=require("@preact/signals-core"),u=new WeakSet,f=new WeakSet,a=new WeakSet;function c(n,r){i.options[n]=r.bind(null,i.options[n]||function(){})}var v=new WeakMap;function s(n){t&&t(!0,!0),r=n,t=n&&n._()}function p(n){var r=o.signal(void 0);return r._c=!0,r._u=n,r}function h(n){var r=v.get(n);if(r)r.__.length=0;else{var t=[];(r=p(function(){for(var r=n.__e,i=n.props,e=0;e<t.length;e++){var o=t[e],u=i[o]._v;o in r?r[o]=u:u?r.setAttribute(o,u):r.removeAttribute(o)}})).__=t,v.set(n,r)}return r}function d(n,r,t){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(d):n instanceof o.Signal&&(t[r]=i.createElement(l,{data:n})))}function l(n){var t=this,i=n.data,u=e.useMemo(function(){for(var n=t.__v;n=n.__;)if(n.__c){a.add(n.__c);break}return r._u=function(){t.base.data=u._v},o.computed(function(){var n=i.value;return 0===n?0:!0===n?"":n||""})},[]);return u.value}c("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var e in i){var u=i[e];"children"===e?d(u,"children",i):u instanceof o.Signal&&(t||(t=h(r)),t.__.push(e))}s(t)}n(r)}),c("__r",function(r,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))),n=e,s(i),r(t)}),c("__e",function(r,t,i,e){s(),n=void 0,r(t,i,e)}),c("diffed",function(r,t){s(),n=void 0,r(t)}),c("unmount",function(n,r){var t=r.__c||r,i=v.get(t);if(i){v.delete(t);var e=i._d;e&&(e.forEach(function(n){return n._s.delete(i)}),e.clear())}n(r)}),c("__h",function(n,r,t,i){i<3&&f.add(r),n(r,t,i)}),i.Component.prototype.shouldComponentUpdate=function(n,r){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 r)return!0;for(var o in n)if("__source"!==o&&n[o]!==this.props[o])return!0;for(var c in this.props)if(!(c in n))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(r){var t=e.useRef(r);return t.current=r,a.add(n),e.useMemo(function(){return o.computed(function(){return t.current()})},[])},exports.useSignal=function(n){return e.useMemo(function(){return o.signal(n)},[])};//# sourceMappingURL=signals.js.map
1
+ var n,r,t,e=require("preact"),i=require("preact/hooks"),u=require("@preact/signals-core"),o=new WeakSet,f=new WeakSet,c=new WeakSet;function a(n,r){e.options[n]=r.bind(null,e.options[n]||function(){})}var v=new WeakMap;function s(n){t&&t(!0,!0),r=n,t=n&&n._()}function l(n){var r=u.signal(void 0);return r._c=!0,r._u=n,r}function b(n){var r=v.get(n);if(r)r.__.length=0;else{var t=[];(r=l(function(){for(var r=n.__e,e=0;e<t.length;e++){var i=t[e],u=i.t,o=i.i._v;if(!r)return;u in r?r[u]=o:o?r.setAttribute(u,o):r.removeAttribute(u)}})).__=t,v.set(n,r)}return r}function p(n,r,t){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(p):n instanceof u.Signal&&(t[r]=e.createElement(_,{data:n})))}function _(n){var t=this,e=n.data,o=h(e);o.value=e;var f=i.useMemo(function(){for(var n=t.__v;n=n.__;)if(n.__c){c.add(n.__c);break}return r._u=function(){t.base.data=f._v},u.computed(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function h(n){return i.useMemo(function(){return u.signal(n)},[])}_.displayName="_st",a("__b",function(n,r){if("string"==typeof r.type){var t,e=r.props;for(var i in e){var o=e[i];"children"===i?p(o,"children",e):o instanceof u.Signal&&function(){t||(t=b(r)),t.__.push({t:i,i:o});var n=t._u;if(o._u){var u=o._u;o._u=function(){n(),u()}}else o._u=n;e[i]=o.peek()}()}s(t)}n(r)}),a("__r",function(r,t){var e,i=t.__c;i&&(o.delete(i),void 0===(e=v.get(i))&&(e=l(function(){o.add(i),i.setState({})}),v.set(i,e))),n=i,s(e),r(t)}),a("__e",function(r,t,e,i){s(),n=void 0,r(t,e,i)}),a("diffed",function(r,t){s(),n=void 0,r(t)}),a("unmount",function(n,r){var t=r.__c||r,e=v.get(t);if(e){v.delete(t);var i=e._d;i&&(i.forEach(function(n){return n._s.delete(e)}),i.clear())}n(r)}),a("__h",function(n,r,t,e){e<3&&f.add(r),n(r,t,e)}),e.Component.prototype.shouldComponentUpdate=function(n,r){var t,e=v.get(this);if(!(e&&0!==(null==(t=e._d)?void 0:t.size)||c.has(this)))return!0;if(o.has(this))return!0;if(f.has(this))return!0;for(var i in r)return!0;for(var u in n)if("__source"!==u&&n[u]!==this.props[u])return!0;for(var a in this.props)if(!(a in n))return!0;return!1},Object.defineProperty(exports,"Signal",{enumerable:!0,get:function(){return u.Signal}}),Object.defineProperty(exports,"batch",{enumerable:!0,get:function(){return u.batch}}),Object.defineProperty(exports,"computed",{enumerable:!0,get:function(){return u.computed}}),Object.defineProperty(exports,"effect",{enumerable:!0,get:function(){return u.effect}}),Object.defineProperty(exports,"signal",{enumerable:!0,get:function(){return u.signal}}),exports.useComputed=function(r){var t=i.useRef(r);return t.current=r,c.add(n),i.useMemo(function(){return u.computed(function(){return t.current()})},[])},exports.useSignal=h;
2
+ //# sourceMappingURL=signals.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: string[] = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\t\t\tlet props = vnode.props;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet prop = signalProps[i];\n\t\t\t\tlet value = props[prop]._value;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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 s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push(i);\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","finishUpdate","preact","require","hooks","signalsCore","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","props","i","prop","value","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","this","useMemo","v","__v","__","__c","add","base","computed","old","type","push","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","exports","batch","effect","useComputed","compute","$compute","useRef","current","useSignal"],"mappings":"AAsBA,IAcIA,EACJC,EACIC,EAhBJC,EAAAC,QAAA,UAAAC,EAAAD,QAAA,gBAAAE,EAAAF,QAAA,wBAAsBG,EAAG,IAAIC,QAGXC,EAAG,IAAID,QAGPE,EAAG,IAAIF,QAGzB,SAAAG,EAAsCC,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAACF,IAAc,WAAxC,EACpB,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,SAAAC,EAA2BC,GAEtBjB,GAAcA,GAAa,GAAM,GAErCD,EAAiBkB,EACjBjB,EAAeiB,GAAWA,EAAQC,GAClC,CAED,SAASC,EAAcF,GACtB,IAAMG,EAAIC,EAAMA,YAACC,GAGjB,OAFAF,EAAEG,IAAe,EACjBH,EAAEI,GAAWP,EACNG,CACP,CAGD,SAAAK,EAA2BC,GAC1B,IAAWT,EAAGH,EAAoBa,IAAID,GACtC,GAAKT,EAsBJA,EAAQW,GAAOC,OAAS,MAtBX,CACb,IAAIC,EAAwB,IAC5Bb,EAAUE,EAAc,WAIvB,IAHA,IAAOY,EA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5B,MAAO,OAAO,EAGvC,GAAI9C,EAAa0E,IAAI5B,MAAO,OAAA,EAC5B,IAAK,IAALnB,KAAA4C,EAAqB,OAAO,EAG5B,IAAK,IAAI5C,KAAKD,EACb,GAAU,aAANC,GAAoBD,EAAMC,KAAOmB,KAAKpB,MAAMC,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAKmB,KAAKpB,MAAO,KAAMC,KAAFD,GAAe,OAA7C,EAGA,OAAO,CACP,EAWAiD,QAAAnC,OAAA3C,EAAA2C,OAAAmC,QAAAC,MAAA/E,EAAA+E,MAAAD,QAAArB,SAAAzD,EAAAyD,SAAAqB,QAAAE,OAAAhF,EAAAgF,OAAAF,QAAA7D,OAAAjB,EAAAiB,OAAA6D,QAAAG,YALK,SAAyBC,GAC9B,IAAcC,EAAGC,EAAAA,OAAOF,GAGxB,OAFAC,EAASE,QAAUH,EACnB9E,EAAamD,IAAI7D,GACHwD,EAAAA,QAAC,WAAMO,OAAAA,EAAAA,SAAY,WAAA,OAAc0B,EAACE,SAAf,EAAlB,EAA6C,GAC5D,EAAAP,QAAAQ,UATK,SAAuBtD,GAC5B,OAAckB,EAAAA,QAAC,WAAMjC,OAAAA,EAAAA,OAAUe,EAAhB,EAAwB,GACvC"}
1
+ {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: Array<{ _key: string; _signal: Signal }> = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet { _key: prop, _signal: signal } = signalProps[i];\n\t\t\t\tlet value = signal._value;\n\t\t\t\tif (!dom) return;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push({ _key: i, _signal: value });\n\t\t\t\tlet newUpdater = updater._updater;\n\t\t\t\tif (value._updater) {\n\t\t\t\t\tlet oldUpdater = value._updater;\n\t\t\t\t\tvalue._updater = () => {\n\t\t\t\t\t\tnewUpdater();\n\t\t\t\t\t\toldUpdater();\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tvalue._updater = newUpdater;\n\t\t\t\t}\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","finishUpdate","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","setCurrentUpdater","updater","currentUpdater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","i","prop","_key","_signal","_value","value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","this","currentSignal","useSignal","useMemo","v","__v","__","__c","add","base","computed","displayName","old","type","props","push","newUpdater","oldUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2","useComputed","compute","$compute","useRef","current"],"mappings":"IAoCIA,IAEJC,kFAhBsBC,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,YAGrB,SAAAC,EAAsCC,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAMI,EAAsB,YAE5B,SAAAC,EAA2BC,GAEtBX,GAAcA,GAAa,GAAM,GAErCY,EAAiBD,EACjBX,EAAeW,GAAWA,EAAQE,GAClC,CAED,SAASC,EAAcH,GACtB,IAAOI,EAAGC,EAAMA,YAACC,GAGjB,OAFAF,EAAEG,IAAe,EACjBH,EAAEI,GAAWR,EACNI,CACP,CAGD,SAAAK,EAA2BC,GAC1B,IAAWV,EAAGF,EAAoBa,IAAID,GACtC,GAAKV,EA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,GAC3B,IAAMC,EAAU9D,EAAQ+D,GACpBD,IACHA,EAAQ/B,QAAQ,SAAA1B,GAAUA,OAAAA,EAAO2D,UAAahE,EAAxB,GACtB8D,EAAQG,QAET,CACDf,EAAIxC,EACJ,GAGDjB,EAAI,MAAoB,SAACyD,EAAKO,EAAWS,EAAOf,GAC3CA,EAAO,GAAGgB,EAAarB,IAAIW,GAC/BP,EAAIO,EAAWS,EAAOf,EACtB,GAMDiB,YAAUC,UAAUC,sBAAwB,SAAUlB,EAAOmB,GAAK,IAAAC,EAE3DxE,EAAUF,EAAoBa,IAAI2B,MA2BxC,KAzBmBtC,GAAmC,KAATyE,OAAfzE,EAAAA,EAAQ+D,SAAOU,EAAAA,EAAAA,OAyBzBjF,EAAakF,IAAIpC,OAAO,OAAA,EAG5C,GAAIhD,EAAiBoF,IAAIpC,MAAO,OAAA,EAGhC,GAAI6B,EAAaO,IAAIpC,MAAO,OAAA,EAC5B,IAAK,IAALrB,OAAqB,OAArB,EAGA,IAAK,IAAL0D,OACC,GAAU,aAAN1D,GAAoBmC,EAAMnC,KAAOqB,KAAKc,MAAMnC,GAAI,OACpD,EACD,IAAK,IAAL2D,UAAmBxB,MAAO,KAAMnC,KAAFmC,GAAe,OAAO,EAGpD,OACA,CAAA,gdAMeyB,SAAeC,GAC9B,IAAcC,EAAGC,EAAAA,OAAOF,GAGxB,OAFAC,EAASE,QAAUH,EACnBtF,EAAasD,IAAI1D,GACVqD,UAAQ,WAAA,OAAcO,EAAAA,SAAI,kBAAc+B,EAACE,SAAf,EAAlB,EAA6C,GAC5D"}
@@ -1 +1,2 @@
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,e){var i,o,f,u=new WeakSet,c=new WeakSet,a=new WeakSet;function v(n,t){r.options[n]=t.bind(null,r.options[n]||function(){})}var s=new WeakMap;function d(n){f&&f(!0,!0),o=n,f=n&&n._()}function l(n){var r=e.signal(void 0);return r._c=!0,r._u=n,r}function p(n){var r=s.get(n);if(r)r.__.length=0;else{var t=[];(r=l(function(){for(var r=n.__e,e=n.props,i=0;i<t.length;i++){var o=t[i],f=e[o]._v;o in r?r[o]=f:f?r.setAttribute(o,f):r.removeAttribute(o)}})).__=t,s.set(n,r)}return r}function h(n,t,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(h):n instanceof e.Signal&&(i[t]=r.createElement(_,{data:n})))}function _(n){var r=this,i=n.data,f=t.useMemo(function(){for(var n=r.__v;n=n.__;)if(n.__c){a.add(n.__c);break}return o._u=function(){r.base.data=f._v},e.computed(function(){var n=i.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}v("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var o in i){var f=i[o];"children"===o?h(f,"children",i):f instanceof e.Signal&&(t||(t=p(r)),t.__.push(o))}d(t)}n(r)}),v("__r",function(n,r){var t,e=r.__c;e&&(u.delete(e),void 0===(t=s.get(e))&&(t=l(function(){u.add(e),e.setState({})}),s.set(e,t))),i=e,d(t),n(r)}),v("__e",function(n,r,t,e){d(),i=void 0,n(r,t,e)}),v("diffed",function(n,r){d(),i=void 0,n(r)}),v("unmount",function(n,r){var t=r.__c||r,e=s.get(t);if(e){s.delete(t);var i=e._d;i&&(i.forEach(function(n){return n._s.delete(e)}),i.clear())}n(r)}),v("__h",function(n,r,t,e){e<3&&c.add(r),n(r,t,e)}),r.Component.prototype.shouldComponentUpdate=function(n,r){var t,e=s.get(this);if(!(e&&0!==(null==(t=e._d)?void 0:t.size)||a.has(this)))return!0;if(u.has(this))return!0;if(c.has(this))return!0;for(var i 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=e.Signal,n.batch=e.batch,n.computed=e.computed,n.effect=e.effect,n.signal=e.signal,n.useComputed=function(n){var r=t.useRef(n);return r.current=n,a.add(i),t.useMemo(function(){return e.computed(function(){return r.current()})},[])},n.useSignal=function(n){return t.useMemo(function(){return e.signal(n)},[])}});//# sourceMappingURL=signals.min.js.map
1
+ !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],e):e((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,e,r,t){var i,u,o,f=new WeakSet,c=new WeakSet,a=new WeakSet;function s(n,r){e.options[n]=r.bind(null,e.options[n]||function(){})}var v=new WeakMap;function l(n){o&&o(!0,!0),u=n,o=n&&n._()}function d(n){var e=t.signal(void 0);return e._c=!0,e._u=n,e}function b(n){var e=v.get(n);if(e)e.__.length=0;else{var r=[];(e=d(function(){for(var e=n.__e,t=0;t<r.length;t++){var i=r[t],u=i.t,o=i.i._v;if(!e)return;u in e?e[u]=o:o?e.setAttribute(u,o):e.removeAttribute(u)}})).__=r,v.set(n,e)}return e}function p(n,r,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(p):n instanceof t.Signal&&(i[r]=e.createElement(h,{data:n})))}function h(n){var e=this,i=n.data,o=g(i);o.value=i;var f=r.useMemo(function(){for(var n=e.__v;n=n.__;)if(n.__c){a.add(n.__c);break}return u._u=function(){e.base.data=f._v},t.computed(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function g(n){return r.useMemo(function(){return t.signal(n)},[])}h.displayName="_st",s("__b",function(n,e){if("string"==typeof e.type){var r,i=e.props;for(var u in i){var o=i[u];"children"===u?p(o,"children",i):o instanceof t.Signal&&function(){r||(r=b(e)),r.__.push({t:u,i:o});var n=r._u;if(o._u){var t=o._u;o._u=function(){n(),t()}}else o._u=n;i[u]=o.peek()}()}l(r)}n(e)}),s("__r",function(n,e){var r,t=e.__c;t&&(f.delete(t),void 0===(r=v.get(t))&&(r=d(function(){f.add(t),t.setState({})}),v.set(t,r))),i=t,l(r),n(e)}),s("__e",function(n,e,r,t){l(),i=void 0,n(e,r,t)}),s("diffed",function(n,e){l(),i=void 0,n(e)}),s("unmount",function(n,e){var r=e.__c||e,t=v.get(r);if(t){v.delete(r);var i=t._d;i&&(i.forEach(function(n){return n._s.delete(t)}),i.clear())}n(e)}),s("__h",function(n,e,r,t){t<3&&c.add(e),n(e,r,t)}),e.Component.prototype.shouldComponentUpdate=function(n,e){var r,t=v.get(this);if(!(t&&0!==(null==(r=t._d)?void 0:r.size)||a.has(this)))return!0;if(f.has(this))return!0;if(c.has(this))return!0;for(var i in e)return!0;for(var u in n)if("__source"!==u&&n[u]!==this.props[u])return!0;for(var o in this.props)if(!(o in n))return!0;return!1},Object.defineProperty(n,"Signal",{enumerable:!0,get:function(){return t.Signal}}),Object.defineProperty(n,"batch",{enumerable:!0,get:function(){return t.batch}}),Object.defineProperty(n,"computed",{enumerable:!0,get:function(){return t.computed}}),Object.defineProperty(n,"effect",{enumerable:!0,get:function(){return t.effect}}),Object.defineProperty(n,"signal",{enumerable:!0,get:function(){return t.signal}}),n.useComputed=function(n){var e=r.useRef(n);return e.current=n,a.add(i),r.useMemo(function(){return t.computed(function(){return e.current()})},[])},n.useSignal=g});
2
+ //# sourceMappingURL=signals.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: string[] = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\t\t\tlet props = vnode.props;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet prop = signalProps[i];\n\t\t\t\tlet value = props[prop]._value;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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 s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push(i);\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","finishUpdate","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","props","i","prop","value","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","useMemo","v","__v","__","__c","add","base","computed","old","type","push","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","batch","effect","useComputed","compute","$compute","useRef","current","useSignal"],"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,IAcIE,EACJC,EACIC,EAhBkBC,EAAG,IAAIC,QAGXC,EAAG,IAAID,QAGPE,EAAG,IAAIF,QAGzB,SAAAG,EAAsCC,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAACF,IAAc,WAAxC,EACpB,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,SAAAC,EAA2BC,GAEtBb,GAAcA,GAAa,GAAM,GAErCD,EAAiBc,EACjBb,EAAea,GAAWA,EAAQC,GAClC,CAED,SAASC,EAAcF,GACtB,IAAMG,EAAIC,EAAMA,YAACC,GAGjB,OAFAF,EAAEG,IAAe,EACjBH,EAAEI,GAAWP,EACNG,CACP,CAGD,SAAAK,EAA2BC,GAC1B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BmBgB,GAAmC,KAAT8D,OAAf9D,EAAAA,EAAQqD,SAAOS,EAAAA,EAAAA,OAyBzBvE,EAAawE,IAAI/E,OAAO,OAAA,EAG5C,GAAII,EAAiB2E,IAAI/E,MAAO,OAAO,EAGvC,GAAIM,EAAayE,IAAI/E,MAAO,OAAA,EAC5B,IAAK,IAALiC,KAAA2C,EAAqB,OAAO,EAG5B,IAAK,IAAI3C,KAAKD,EACb,GAAU,aAANC,GAAoBD,EAAMC,KAAOjC,KAAKgC,MAAMC,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAKjC,KAAKgC,MAAO,KAAMC,KAAFD,GAAe,OAA7C,EAGA,OAAO,CACP,EAWA3C,EAAAyD,OAAA/C,EAAA+C,OAAAzD,EAAA2F,MAAAjF,EAAAiF,MAAA3F,EAAAsE,SAAA5D,EAAA4D,SAAAtE,EAAA4F,OAAAlF,EAAAkF,OAAA5F,EAAA+B,OAAArB,EAAAqB,OAAA/B,EAAA6F,YALK,SAAyBC,GAC9B,IAAcC,EAAGC,EAAAA,OAAOF,GAGxB,OAFAC,EAASE,QAAUH,EACnB5E,EAAakD,IAAIxD,GACHmD,EAAAA,QAAC,WAAMO,OAAAA,EAAAA,SAAY,WAAA,OAAcyB,EAACE,SAAf,EAAlB,EAA6C,GAC5D,EAAAjG,EAAAkG,UATK,SAAuBpD,GAC5B,OAAciB,EAAAA,QAAC,WAAMhC,OAAAA,EAAAA,OAAUe,EAAhB,EAAwB,GACvC,CAOA"}
1
+ {"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: Array<{ _key: string; _signal: Signal }> = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet { _key: prop, _signal: signal } = signalProps[i];\n\t\t\t\tlet value = signal._value;\n\t\t\t\tif (!dom) return;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push({ _key: i, _signal: value });\n\t\t\t\tlet newUpdater = updater._updater;\n\t\t\t\tif (value._updater) {\n\t\t\t\t\tlet oldUpdater = value._updater;\n\t\t\t\t\tvalue._updater = () => {\n\t\t\t\t\t\tnewUpdater();\n\t\t\t\t\t\toldUpdater();\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tvalue._updater = newUpdater;\n\t\t\t\t}\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","finishUpdate","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","setCurrentUpdater","updater","currentUpdater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","i","prop","_key","_signal","_value","value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","this","currentSignal","useSignal","useMemo","v","__v","__","__c","add","base","computed","displayName","old","type","props","push","newUpdater","oldUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2","useComputed","compute","$compute","useRef","current"],"mappings":"qYAsBA,IAcIA,IAEJC,EAhBsBC,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,YAGrB,SAAAC,EAAsCC,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAMI,EAAsB,YAE5B,SAAAC,EAA2BC,GAEtBX,GAAcA,GAAa,GAAM,GAErCY,EAAiBD,EACjBX,EAAeW,GAAWA,EAAQE,GAClC,CAED,SAASC,EAAcH,GACtB,IAAOI,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AIxC,EACJ,GAGDjB,YAA2B,SAACyD,EAAKxC,GAChC,IAAImD,EAAQnD,EAAMmC,KAAOnC,EACnBV,EAAUF,EAAoBa,IAAIkD,GACxC,GAAI7D,EAAS,CACZF,EAAA,OAA2B+D,GAC3B,IAAMC,EAAU9D,EAAQ+D,GACpBD,IACHA,EAAQ/B,QAAQ,SAAA1B,GAAUA,OAAAA,EAAO2D,UAAahE,EAAxB,GACtB8D,EAAQG,QAET,CACDf,EAAIxC,EACJ,GAGDjB,EAAI,MAAoB,SAACyD,EAAKO,EAAWS,EAAOf,GAC3CA,EAAO,GAAGgB,EAAarB,IAAIW,GAC/BP,EAAIO,EAAWS,EAAOf,EACtB,GAMDiB,YAAUC,UAAUC,sBAAwB,SAAUlB,EAAOmB,GAAK,IAAAC,EAE3DxE,EAAUF,EAAoBa,IAAI2B,MA2BxC,KAzBmBtC,GAAmC,KAATyE,OAAfzE,EAAAA,EAAQ+D,SAAOU,EAAAA,EAAAA,OAyBzBjF,EAAakF,IAAIpC,OAAO,OAAA,EAG5C,GAAIhD,EAAiBoF,IAAIpC,MAAO,OAAA,EAGhC,GAAI6B,EAAaO,IAAIpC,MAAO,OAAA,EAC5B,IAAK,IAALrB,OAAqB,OAArB,EAGA,IAAK,IAAL0D,OACC,GAAU,aAAN1D,GAAoBmC,EAAMnC,KAAOqB,KAAKc,MAAMnC,GAAI,OACpD,EACD,IAAK,IAAL2D,UAAmBxB,MAAO,KAAMnC,KAAFmC,GAAe,OAAO,EAGpD,OACA,CAAA,4aAMeyB,SAAeC,GAC9B,IAAcC,EAAGC,EAAAA,OAAOF,GAGxB,OAFAC,EAASE,QAAUH,EACnBtF,EAAasD,IAAI1D,GACVqD,UAAQ,WAAA,OAAcO,EAAAA,SAAI,kBAAc+B,EAACE,SAAf,EAAlB,EAA6C,GAC5D"}
package/dist/signals.mjs CHANGED
@@ -1 +1,2 @@
1
- import{Component as t,options as n,createElement as e}from"preact";import{useMemo as r,useRef as i}from"preact/hooks";import{Signal as o,signal as f,computed as c}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const u=new WeakSet,l=new WeakSet,s=new WeakSet;function a(t,e){n[t]=e.bind(null,n[t]||(()=>{}))}let h,p,d;const _=new WeakMap;function m(t){d&&d(!0,!0),p=t,d=t&&t._()}function k(t){const n=f(void 0);return n._c=!0,n._u=t,n}function v(t){let n=_.get(t);if(n)n.__.length=0;else{let e=[];n=k(()=>{let n=t.__e,r=t.props;for(let t=0;t<e.length;t++){let i=e[t],o=r[i]._v;i in n?n[i]=o:o?n.setAttribute(i,o):n.removeAttribute(i)}}),n.__=e,_.set(t,n)}return n}function g(t,n,r){"object"!=typeof t||null==t||(Array.isArray(t)?t.forEach(g):t instanceof o&&(r[n]=e(b,{data:t})))}function b({data:t}){const n=r(()=>{let e=this.__v;for(;e=e.__;)if(e.__c){s.add(e.__c);break}return p._u=()=>{this.base.data=n._v},c(()=>{let n=t.value;return 0===n?0:!0===n?"":n||""})},[]);return n.value}function w(t){return r(()=>f(t),[])}function S(t){const n=i(t);return n.current=t,s.add(h),r(()=>c(()=>n.current()),[])}a("__b",(t,n)=>{if("string"==typeof n.type){let t,e=n.props;for(let r in e){let i=e[r];"children"===r?g(i,"children",e):i instanceof o&&(t||(t=v(n)),t.__.push(r))}m(t)}t(n)}),a("__r",(t,n)=>{let e,r=n.__c;r&&(u.delete(r),e=_.get(r),void 0===e&&(e=k(()=>{u.add(r),r.setState({})}),_.set(r,e))),h=r,m(e),t(n)}),a("__e",(t,n,e,r)=>{m(),h=void 0,t(n,e,r)}),a("diffed",(t,n)=>{m(),h=void 0,t(n)}),a("unmount",(t,n)=>{let e=n.__c||n;const r=_.get(e);if(r){_.delete(e);const t=r._d;t&&(t.forEach(t=>t._s.delete(r)),t.clear())}t(n)}),a("__h",(t,n,e,r)=>{r<3&&l.add(n),t(n,e,r)}),t.prototype.shouldComponentUpdate=function(t,n){var e;const r=_.get(this);if(!(r&&0!==(null==(e=r._d)?void 0:e.size)||s.has(this)))return!0;if(u.has(this))return!0;if(l.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{S as useComputed,w as useSignal};//# sourceMappingURL=signals.mjs.map
1
+ import{Component as t,options as e,createElement as n}from"preact";import{useMemo as r,useRef as i}from"preact/hooks";import{Signal as o,signal as f,computed as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const c=new WeakSet,s=new WeakSet,u=new WeakSet;function a(t,n){e[t]=n.bind(null,e[t]||(()=>{}))}let _,h,p;const d=new WeakMap;function m(t){p&&p(!0,!0),h=t,p=t&&t._()}function k(t){const e=f(void 0);return e._c=!0,e._u=t,e}function g(t){let e=d.get(t);if(e)e.__.length=0;else{let n=[];e=k(()=>{let e=t.__e;for(let t=0;t<n.length;t++){let{t:r,i}=n[t],o=i._v;if(!e)return;r in e?e[r]=o:o?e.setAttribute(r,o):e.removeAttribute(r)}}),e.__=n,d.set(t,e)}return e}function v(t,e,r){"object"!=typeof t||null==t||(Array.isArray(t)?t.forEach(v):t instanceof o&&(r[e]=n(y,{data:t})))}function y({data:t}){const e=b(t);e.value=t;const n=r(()=>{let t=this.__v;for(;t=t.__;)if(t.__c){u.add(t.__c);break}return h._u=()=>{this.base.data=n._v},l(()=>{let t=e.value.value;return 0===t?0:!0===t?"":t||""})},[]);return n.value}function b(t){return r(()=>f(t),[])}function w(t){const e=i(t);return e.current=t,u.add(_),r(()=>l(()=>e.current()),[])}y.displayName="_st",a("__b",(t,e)=>{if("string"==typeof e.type){let t,n=e.props;for(let r in n){let i=n[r];if("children"===r)v(i,"children",n);else if(i instanceof o){t||(t=g(e)),t.__.push({t:r,i});let o=t._u;if(i._u){let t=i._u;i._u=()=>{o(),t()}}else i._u=o;n[r]=i.peek()}}m(t)}t(e)}),a("__r",(t,e)=>{let n,r=e.__c;r&&(c.delete(r),n=d.get(r),void 0===n&&(n=k(()=>{c.add(r),r.setState({})}),d.set(r,n))),_=r,m(n),t(e)}),a("__e",(t,e,n,r)=>{m(),_=void 0,t(e,n,r)}),a("diffed",(t,e)=>{m(),_=void 0,t(e)}),a("unmount",(t,e)=>{let n=e.__c||e;const r=d.get(n);if(r){d.delete(n);const t=r._d;t&&(t.forEach(t=>t._s.delete(r)),t.clear())}t(e)}),a("__h",(t,e,n,r)=>{r<3&&s.add(e),t(e,n,r)}),t.prototype.shouldComponentUpdate=function(t,e){var n;const r=d.get(this);if(!(r&&0!==(null==(n=r._d)?void 0:n.size)||u.has(this)))return!0;if(c.has(this))return!0;if(s.has(this))return!0;for(let t in e)return!0;for(let e in t)if("__source"!==e&&t[e]!==this.props[e])return!0;for(let e in this.props)if(!(e in t))return!0;return!1};export{w as useComputed,b as useSignal};
2
+ //# sourceMappingURL=signals.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: string[] = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\t\t\tlet props = vnode.props;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet prop = signalProps[i];\n\t\t\t\tlet value = props[prop]._value;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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 s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push(i);\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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":["hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","options","bind","currentComponent","currentUpdater","finishUpdate","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","props","i","prop","value","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","useMemo","v","this","__v","__","__c","add","base","computed","useSignal","useComputed","compute","$compute","useRef","current","old","type","push","component","delete","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","Component","prototype","shouldComponentUpd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EAAO+C,KAClDzD,IACAL,OAAmBW,EACnB4C,EAAIM,EAAO9C,EAAO+C,EAAf,GAIJnE,WAA0B,CAAC4D,EAAKxC,KAC/BV,IACAL,OAAmBW,EACnB4C,EAAIxC,EAAD,GAIJpB,YAA2B,CAAC4D,EAAKxC,KAChC,IAAIgD,EAAQhD,EAAM8B,KAAO9B,EACzB,MAAaT,EAAGH,EAAoBa,IAAI+C,GACxC,GAAIzD,EAAS,CACZH,EAAoBwD,OAAOI,GAC3B,MAAaC,EAAG1D,EAAQ2D,GACpBD,IACHA,EAAQ7B,QAAQzB,GAAUA,EAAOwD,GAAMP,OAAOrD,IAC9C0D,EAAQG,QAET,CACDZ,EAAIxC,EAAD,GAIJpB,EAAI,MAAoB,CAAC4D,EAAKG,EAAWU,EAAOZ,KAC3CA,EAAO,GAAG/D,EAAaqD,IAAIY,GAC/BH,EAAIG,EAAWU,EAAOZ,EACtB,GAMDa,EAAUC,UAAUC,sBAAwB,SAAUjD,EAAOkD,GAAK,IAAAC,EAEjE,MAAMnE,EAAUH,EAAoBa,IAAI0B,MA2BxC,KAzBmBpC,GAAmC,KAAxB,OAAAmE,EAAAnE,EAAQ2D,SAAR,EAAAQ,EAAeC,OAyBzBhF,EAAaiF,IAAIjC,OAAO,OAAO,EAGnD,GAAInD,EAAiBoF,IAAIjC,MAAO,OAAA,EAGhC,GAAIjD,EAAakF,IAAIjC,MAAO,OAAO,EACnC,IAAK,IAAInB,KAAKiD,EAAO,OAAA,EAGrB,IAAK,IAALjD,KAAAD,EACC,GAAU,aAANC,GAAoBD,EAAMC,KAAOmB,KAAKpB,MAAMC,GAAI,OACpD,EACD,IAAK,IAALA,KAAmBD,KAAAA,MAAO,KAAMC,KAAKD,GAAQ,OAAO,EAGpD,OACA,CAAA,SAWA4B,iBAAAD"}
1
+ {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: Array<{ _key: string; _signal: Signal }> = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet { _key: prop, _signal: signal } = signalProps[i];\n\t\t\t\tlet value = signal._value;\n\t\t\t\tif (!dom) return;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push({ _key: i, _signal: value });\n\t\t\t\tlet newUpdater = updater._updater;\n\t\t\t\tif (value._updater) {\n\t\t\t\t\tlet oldUpdater = value._updater;\n\t\t\t\t\tvalue._updater = () => {\n\t\t\t\t\t\tnewUpdater();\n\t\t\t\t\t\toldUpdater();\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tvalue._updater = newUpdater;\n\t\t\t\t}\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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":["hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","currentComponent","currentUpdater","finishUpdate","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","__e","i","_key","prop","_signal","value","_value","dom","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","currentSignal","useSignal","useMemo","v","this","__v","__","__c","add","base","computed","useComputed","compute","$compute","useRef","current","displayName","old","type","props","push","newUpdater","oldUpdater","peek","component","delete","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has"],"mappings":"oQAsBA,MAAsBA,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,YAGrB,SAAAC,EAAsCC,EAAaC,GAElDC,EAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAQF,IAAc,MAA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{Component as n,options as r,createElement as t}from"preact";import{useMemo as i,useRef as o}from"preact/hooks";import{Signal as e,signal as f,computed as u}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var a,c,v,s=new WeakSet,l=new WeakSet,p=new WeakSet;function h(n,t){r[n]=t.bind(null,r[n]||function(){})}var d=new WeakMap;function _(n){v&&v(!0,!0),c=n,v=n&&n._()}function m(n){var r=f(void 0);return r._c=!0,r._u=n,r}function k(n){var r=d.get(n);if(r)r.__.length=0;else{var t=[];(r=m(function(){for(var r=n.__e,i=n.props,o=0;o<t.length;o++){var e=t[o],f=i[e]._v;e in r?r[e]=f:f?r.setAttribute(e,f):r.removeAttribute(e)}})).__=t,d.set(n,r)}return r}function g(n,r,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(g):n instanceof e&&(i[r]=t(b,{data:n})))}function b(n){var r=this,t=n.data,o=i(function(){for(var n=r.__v;n=n.__;)if(n.__c){p.add(n.__c);break}return c._u=function(){r.base.data=o._v},u(function(){var n=t.value;return 0===n?0:!0===n?"":n||""})},[]);return o.value}function w(n){return i(function(){return f(n)},[])}function S(n){var r=o(n);return r.current=n,p.add(a),i(function(){return u(function(){return r.current()})},[])}h("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var o in i){var f=i[o];"children"===o?g(f,"children",i):f instanceof e&&(t||(t=k(r)),t.__.push(o))}_(t)}n(r)}),h("__r",function(n,r){var t,i=r.__c;i&&(s.delete(i),void 0===(t=d.get(i))&&(t=m(function(){s.add(i),i.setState({})}),d.set(i,t))),a=i,_(t),n(r)}),h("__e",function(n,r,t,i){_(),a=void 0,n(r,t,i)}),h("diffed",function(n,r){_(),a=void 0,n(r)}),h("unmount",function(n,r){var t=r.__c||r,i=d.get(t);if(i){d.delete(t);var o=i._d;o&&(o.forEach(function(n){return n._s.delete(i)}),o.clear())}n(r)}),h("__h",function(n,r,t,i){i<3&&l.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=d.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||p.has(this)))return!0;if(s.has(this))return!0;if(l.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{S as useComputed,w as useSignal};//# sourceMappingURL=signals.module.js.map
1
+ import{Component as n,options as r,createElement as t}from"preact";import{useMemo as i,useRef as o}from"preact/hooks";import{Signal as e,signal as f,computed as u}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var a,c,v,s=new WeakSet,l=new WeakSet,p=new WeakSet;function _(n,t){r[n]=t.bind(null,r[n]||function(){})}var h=new WeakMap;function d(n){v&&v(!0,!0),c=n,v=n&&n._()}function m(n){var r=f(void 0);return r._c=!0,r._u=n,r}function k(n){var r=h.get(n);if(r)r.__.length=0;else{var t=[];(r=m(function(){for(var r=n.__e,i=0;i<t.length;i++){var o=t[i],e=o.t,f=o.i._v;if(!r)return;e in r?r[e]=f:f?r.setAttribute(e,f):r.removeAttribute(e)}})).__=t,h.set(n,r)}return r}function g(n,r,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(g):n instanceof e&&(i[r]=t(b,{data:n})))}function b(n){var r=this,t=n.data,o=w(t);o.value=t;var e=i(function(){for(var n=r.__v;n=n.__;)if(n.__c){p.add(n.__c);break}return c._u=function(){r.base.data=e._v},u(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return e.value}function w(n){return i(function(){return f(n)},[])}function y(n){var r=o(n);return r.current=n,p.add(a),i(function(){return u(function(){return r.current()})},[])}b.displayName="_st",_("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var o in i){var f=i[o];"children"===o?g(f,"children",i):f instanceof e&&function(){t||(t=k(r)),t.__.push({t:o,i:f});var n=t._u;if(f._u){var e=f._u;f._u=function(){n(),e()}}else f._u=n;i[o]=f.peek()}()}d(t)}n(r)}),_("__r",function(n,r){var t,i=r.__c;i&&(s.delete(i),void 0===(t=h.get(i))&&(t=m(function(){s.add(i),i.setState({})}),h.set(i,t))),a=i,d(t),n(r)}),_("__e",function(n,r,t,i){d(),a=void 0,n(r,t,i)}),_("diffed",function(n,r){d(),a=void 0,n(r)}),_("unmount",function(n,r){var t=r.__c||r,i=h.get(t);if(i){h.delete(t);var o=i._d;o&&(o.forEach(function(n){return n._s.delete(i)}),o.clear())}n(r)}),_("__h",function(n,r,t,i){i<3&&l.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=h.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||p.has(this)))return!0;if(s.has(this))return!0;if(l.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{y as useComputed,w as useSignal};
2
+ //# sourceMappingURL=signals.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: string[] = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\t\t\tlet props = vnode.props;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet prop = signalProps[i];\n\t\t\t\tlet value = props[prop]._value;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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 s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push(i);\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","createElement","useMemo","useRef","Signal","signal","computed","batch","effect","currentComponent","currentUpdater","finishUpdate","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","props","i","prop","value","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Text","data","_ref","_this","this","v","__v","__","__c","add","base","useSignal","useComputed","compute","$compute","current","old","type","push","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","prototype","shouldComponentUpdate","state","_updater$_deps","size","has"],"mappings":"oBAsBAA,aAAAC,mBAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,YAAAC,cAAAC,MAAA,8BAAAF,OAAAG,MAAAD,SAAAE,OAAAH,WAAA,uBAAA,IAcII,EACJC,EACIC,EAhBkBC,EAAG,IAAIC,QAGXC,EAAG,IAAID,QAGPE,EAAG,IAAIF,QAGzB,SAAAG,EAAsCC,EAAaC,GAElDlB,EAAQiB,GAAYC,EAAOC,KAAK,KAAMnB,EAAQiB,IAAc,WAAxC,EACpB,CAKD,IAAyBG,EAAG,IAAIC,QAEhC,SAAAC,EAA2BC,GAEtBZ,GAAcA,GAAa,GAAM,GAErCD,EAAiBa,EACjBZ,EAAeY,GAAWA,EAAQC,GAClC,CAED,SAASC,EAAcF,GACtB,IAAMG,EAAIrB,OAAOsB,GAGjB,OAFAD,EAAEE,IAAe,EACjBF,EAAEG,GAAWN,EACNG,CACP,CAGD,SAAAI,EAA2BC,GAC1B,IAAWR,EAAGH,EAAoBY,IAAID,GACtC,GAAKR,EAsBJA,EAAQU,GAAOC,OAAS,MAtBX,CACb,IAAIC,EAAwB,IAC5BZ,EAAUE,EAAc,WAIvB,IAHA,IAAOW,EAAGL,EAAMM,IACZC,EAAQP,EAAMO,MAETC,EAAI,EAAGA,EAAIJ,EAAYD,OAAQK,IAAK,CAC5C,IAAQC,EAAGL,EAAYI,GACdE,EAAGH,EAAME,GAAME,GACpBF,KAAJJ,EAECA,EAAII,GAAQC,EACFA,EACVL,EAAIO,aAAaH,EAAMC,GAEvBL,EAAIQ,gBAAgBJ,EAErB,CACD,IACOP,GAASE,EACjBf,EAAoByB,IAAId,EAAOR,EAC/B,CAGD,OAAOA,CACP,CAYD,SAAAuB,EAA0BC,EAAYR,EAAYS,GAC5B,iBAAVD,GAA+B,MAATA,IAEtBE,MAAMC,QAAQH,GACxBA,EAAMI,QAAQL,GACJC,aAA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AAO,EAE5D,IAAK,IAAIA,KAAKiB,KAAKlB,MAAO,KAAMC,KAAFD,GAAe,OAA7C,EAGA,OAAO,CACP,SAWA0B,iBAAAD"}
1
+ {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component, createElement } 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._canActivate = true;\n\ts._updater = updater;\n\treturn s;\n}\n\n// Get a (cached) Signal property updater for an element VNode\nfunction getElementUpdater(vnode: VNode) {\n\tlet updater = updaterForComponent.get(vnode) as ElementUpdater;\n\tif (!updater) {\n\t\tlet signalProps: Array<{ _key: string; _signal: Signal }> = [];\n\t\tupdater = createUpdater(() => {\n\t\t\tlet dom = vnode.__e as Element;\n\n\t\t\tfor (let i = 0; i < signalProps.length; i++) {\n\t\t\t\tlet { _key: prop, _signal: signal } = signalProps[i];\n\t\t\t\tlet value = signal._value;\n\t\t\t\tif (!dom) return;\n\t\t\t\tif (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}) as ElementUpdater;\n\t\tupdater._props = signalProps;\n\t\tupdaterForComponent.set(vnode, updater);\n\t} else {\n\t\tupdater._props.length = 0;\n\t}\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/** Convert Signals within (nested) props.children into Text components */\nfunction childToSignal<T>(child: any, i: keyof T, arr: T) {\n\tif (typeof child !== \"object\" || child == null) {\n\t\t// can't be a signal\n\t} else if (Array.isArray(child)) {\n\t\tchild.forEach(childToSignal);\n\t} else if (child instanceof Signal) {\n\t\t// @ts-ignore-next-line yes, arr can accept VNodes:\n\t\tarr[i] = createElement(Text, { data: child });\n\t}\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\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\t// let orig = vnode.__o || vnode;\n\t\tlet props = vnode.props;\n\t\tlet updater;\n\n\t\tfor (let i in props) {\n\t\t\tlet value = props[i];\n\t\t\tif (i === \"children\") {\n\t\t\t\tchildToSignal(value, \"children\", props);\n\t\t\t} else if (value instanceof Signal) {\n\t\t\t\t// first Signal prop triggers creation/cleanup of the updater:\n\t\t\t\tif (!updater) updater = getElementUpdater(vnode);\n\t\t\t\t// track which props are Signals for precise updates:\n\t\t\t\tupdater._props.push({ _key: i, _signal: value });\n\t\t\t\tlet newUpdater = updater._updater;\n\t\t\t\tif (value._updater) {\n\t\t\t\t\tlet oldUpdater = value._updater;\n\t\t\t\t\tvalue._updater = () => {\n\t\t\t\t\t\tnewUpdater();\n\t\t\t\t\t\toldUpdater();\n\t\t\t\t\t};\n\t\t\t\t} else {\n\t\t\t\t\tvalue._updater = newUpdater;\n\t\t\t\t}\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\n\t\tsetCurrentUpdater(updater);\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\told(vnode);\n});\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet thing = vnode.__c || vnode;\n\tconst updater = updaterForComponent.get(thing);\n\tif (updater) {\n\t\tupdaterForComponent.delete(thing);\n\t\tconst signals = updater._deps;\n\t\tif (signals) {\n\t\t\tsignals.forEach(signal => signal._subs.delete(updater));\n\t\t\tsignals.clear();\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\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","finishUpdate","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","setCurrentUpdater","updater","currentUpdater","_setCurrent","createUpdater","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","dom","__e","i","prop","_key","_signal","_value","value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","this","currentSignal","useSignal","useMemo","v","__v","__","__c","add","base","computed","useComputed","compute","$compute","useRef","current","displayName","old","type","props","push","newUpdater","oldUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2"],"mappings":"oQAsBA,IAcIA,IAEJC,EAhBsBC,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,YAGrB,SAAAC,EAAsCC,EAAaC,GAElDC,EAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAQF,IAAc,WAAO,EACnE,CAKD,IAAMI,EAAsB,YAE5B,SAAAC,EAA2BC,GAEtBX,GAAcA,GAAa,GAAM,GAErCY,EAAiBD,EACjBX,EAAeW,GAAWA,EAAQE,GAClC,CAED,SAASC,EAAcH,GACtB,IAAOI,EAAGC,OAAOC,GAGjB,OAFAF,EAAEG,IAAe,EACjBH,EAAEI,GAAWR,EACNI,CACP,CAGD,SAAAK,EAA2BC,GAC1B,IAAWV,EAAGF,EAAoBa,IAAID,GACtC,GAAKV,EAsBJA,EAAQY,GAAOC,OAAS,MAtBX,CACb,IAAeC,EAA6C,IAC5Dd,EAAUG,EAAc,WAGvB,IAFA,IAAOY,EAAGL,EAAMM,IAEPC,EAAI,EAAGA,EAAIH,EAAYD,OAAQI,IAAK,CAC5C,IAAsCH,EAAAA,EAAYG,GAAtCC,EAANC,EAAAA,IAAYC,EAAAA,EACCC,GACnB,IAAKN,EAAK,OACNG,KAAJH,EAECA,EAAIG,GAAQI,EACFA,EACVP,EAAIQ,aAAaL,EAAMI,GAEvBP,EAAIS,gBAAgBN,EAErB,CACD,IACON,GAASE,EACjBhB,EAAoB2B,IAAIf,EAAOV,EAC/B,CAGD,OAAOA,CACP,CAYD,SAAS0B,EAAiBC,EAAYV,EAAYW,GAC5B,iBAAjBD,GAAsC,MAATA,IAEtBE,MAAMC,QAAQH,GACxBA,EAAMI,QAAQL,GACJC,aAAJK,IAENJ,EAAIX,GAAKgB,EAAcC,EAAM,CAAEC,KAAMR,KAEtC,CAMD,SAASO,EAAoDE,GAAA,IAAAC,EAAAC,KAAAH,EAAAC,EAAxBD,KAK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A,EAGhC,GAAIkC,EAAaO,IAAIzC,MAAO,OAAA,EAC5B,IAAK,IAALrB,OAAqB,OAArB,EAGA,IAAK,IAAL+D,OACC,GAAU,aAAN/D,GAAoBwC,EAAMxC,KAAOqB,KAAKmB,MAAMxC,GAAI,OACpD,EACD,IAAK,IAALgE,UAAmBxB,MAAO,KAAMxC,KAAFwC,GAAe,OAAO,EAGpD,OACA,CAAA"}
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@preact/signals",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
4
4
  "license": "MIT",
5
5
  "description": "",
6
6
  "keywords": [],
7
7
  "authors": [
8
8
  "The Preact Authors (https://github.com/preactjs/signals/contributors)"
9
9
  ],
10
- "repository": "preactjs/signals",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/preactjs/signals"
13
+ },
11
14
  "bugs": "https://github.com/preactjs/signals/issues",
12
15
  "homepage": "https://preactjs.com",
13
16
  "funding": {
@@ -31,7 +34,7 @@
31
34
  },
32
35
  "mangle": "../../mangle.json",
33
36
  "dependencies": {
34
- "@preact/signals-core": "^1.0.0"
37
+ "@preact/signals-core": "^1.0.1"
35
38
  },
36
39
  "peerDependencies": {
37
40
  "preact": "10.x"
package/src/index.ts CHANGED
@@ -58,14 +58,14 @@ function createUpdater(updater: () => void) {
58
58
  function getElementUpdater(vnode: VNode) {
59
59
  let updater = updaterForComponent.get(vnode) as ElementUpdater;
60
60
  if (!updater) {
61
- let signalProps: string[] = [];
61
+ let signalProps: Array<{ _key: string; _signal: Signal }> = [];
62
62
  updater = createUpdater(() => {
63
63
  let dom = vnode.__e as Element;
64
- let props = vnode.props;
65
64
 
66
65
  for (let i = 0; i < signalProps.length; i++) {
67
- let prop = signalProps[i];
68
- let value = props[prop]._value;
66
+ let { _key: prop, _signal: signal } = signalProps[i];
67
+ let value = signal._value;
68
+ if (!dom) return;
69
69
  if (prop in dom) {
70
70
  // @ts-ignore-next-line silly
71
71
  dom[prop] = value;
@@ -112,6 +112,11 @@ function childToSignal<T>(child: any, i: keyof T, arr: T) {
112
112
  function Text(this: ComponentType, { data }: { data: Signal }) {
113
113
  // hasComputeds.add(this);
114
114
 
115
+ // Store the props.data signal in another signal so that
116
+ // passing a new signal reference re-runs the text computed:
117
+ const currentSignal = useSignal(data);
118
+ currentSignal.value = data;
119
+
115
120
  const s = useMemo(() => {
116
121
  // mark the parent component as having computeds so it gets optimized
117
122
  let v = this.__v;
@@ -128,6 +133,7 @@ function Text(this: ComponentType, { data }: { data: Signal }) {
128
133
  };
129
134
 
130
135
  return computed(() => {
136
+ let data = currentSignal.value;
131
137
  let s = data.value;
132
138
  return s === 0 ? 0 : s === true ? "" : s || "";
133
139
  });
@@ -135,6 +141,7 @@ function Text(this: ComponentType, { data }: { data: Signal }) {
135
141
 
136
142
  return s.value;
137
143
  }
144
+ Text.displayName = "_st";
138
145
 
139
146
  /** Inject low-level property/attribute bindings for Signals into Preact's diff */
140
147
  hook(OptionsTypes.DIFF, (old, vnode) => {
@@ -151,7 +158,18 @@ hook(OptionsTypes.DIFF, (old, vnode) => {
151
158
  // first Signal prop triggers creation/cleanup of the updater:
152
159
  if (!updater) updater = getElementUpdater(vnode);
153
160
  // track which props are Signals for precise updates:
154
- updater._props.push(i);
161
+ updater._props.push({ _key: i, _signal: value });
162
+ let newUpdater = updater._updater;
163
+ if (value._updater) {
164
+ let oldUpdater = value._updater;
165
+ value._updater = () => {
166
+ newUpdater();
167
+ oldUpdater();
168
+ };
169
+ } else {
170
+ value._updater = newUpdater;
171
+ }
172
+ props[i] = value.peek();
155
173
  }
156
174
  }
157
175
 
package/src/internal.d.ts CHANGED
@@ -18,7 +18,7 @@ export interface ComponentType extends Component {
18
18
  export type Updater = Signal<unknown>;
19
19
 
20
20
  export interface ElementUpdater extends Updater {
21
- _props: string[];
21
+ _props: Array<{ _key: string, _signal: Signal }>;
22
22
  }
23
23
 
24
24
  export const enum OptionsTypes {
@@ -3,6 +3,8 @@ import { h, render } from "preact";
3
3
  import { useMemo } from "preact/hooks";
4
4
  import { setupRerender } from "preact/test-utils";
5
5
 
6
+ const sleep = (ms?: number) => new Promise(r => setTimeout(r, ms));
7
+
6
8
  describe("@preact/signals", () => {
7
9
  let scratch: HTMLDivElement;
8
10
  let rerender: () => void;
@@ -48,12 +50,15 @@ describe("@preact/signals", () => {
48
50
  expect(text).to.have.property("data", "changed");
49
51
  });
50
52
 
51
- it("should update Signal-based Text (in a parent component)", () => {
53
+ it("should update Signal-based Text (in a parent component)", async () => {
52
54
  const sig = signal("test");
55
+ const spy = sinon.spy();
53
56
  function App({ x }: { x: typeof sig }) {
57
+ spy();
54
58
  return h("span", null, x);
55
59
  }
56
60
  render(h(App, { x: sig }), scratch);
61
+ spy.resetHistory();
57
62
 
58
63
  const text = scratch.firstChild!.firstChild!;
59
64
  expect(text).to.have.property("data", "test");
@@ -64,6 +69,51 @@ describe("@preact/signals", () => {
64
69
  expect(scratch.firstChild!.firstChild!).to.equal(text);
65
70
  // should update the text in-place
66
71
  expect(text).to.have.property("data", "changed");
72
+
73
+ await sleep();
74
+ expect(spy).not.to.have.been.called;
75
+ });
76
+
77
+ it("should support swapping Signals in Text positions", async () => {
78
+ const sig = signal("test");
79
+ const spy = sinon.spy();
80
+ function App({ x }: { x: typeof sig }) {
81
+ spy();
82
+ return h("span", null, x);
83
+ }
84
+ render(h(App, { x: sig }), scratch);
85
+ spy.resetHistory();
86
+
87
+ const text = scratch.firstChild!.firstChild!;
88
+ expect(text).to.have.property("data", "test");
89
+
90
+ const sig2 = signal("different");
91
+ render(h(App, { x: sig2 }), scratch);
92
+ expect(spy).to.have.been.called;
93
+ spy.resetHistory();
94
+
95
+ // should not remount/replace Text
96
+ expect(scratch.firstChild!.firstChild!).to.equal(text);
97
+ // should update the text in-place
98
+ expect(text).to.have.property("data", "different");
99
+
100
+ await sleep();
101
+ expect(spy).not.to.have.been.called;
102
+
103
+ sig.value = "changed old signal";
104
+
105
+ await sleep();
106
+ expect(spy).not.to.have.been.called;
107
+ // the text should _not_ have changed:
108
+ expect(text).to.have.property("data", "different");
109
+
110
+ sig2.value = "changed";
111
+
112
+ expect(scratch.firstChild!.firstChild!).to.equal(text);
113
+ expect(text).to.have.property("data", "changed");
114
+
115
+ await sleep();
116
+ expect(spy).not.to.have.been.called;
67
117
  });
68
118
  });
69
119
 
@@ -124,26 +174,86 @@ describe("@preact/signals", () => {
124
174
  rerender();
125
175
  expect(spy).to.be.calledOnce;
126
176
  });
177
+ });
127
178
 
128
- it("should update memo'ed component via signals", async () => {
129
- const sig = signal("foo");
179
+ describe("prop bindings", () => {
180
+ it("should set the initial value of the checked property", () => {
181
+ const s = signal(true);
182
+ // @ts-ignore
183
+ render(h("input", { checked: s }), scratch);
130
184
 
131
- function Inner() {
132
- const value = sig.value;
133
- return h("p", null, value);
185
+ expect(scratch.firstChild).to.have.property("checked", true);
186
+ expect(s.value).to.equal(true);
187
+ });
188
+
189
+ it("should update the checked property on change", () => {
190
+ const s = signal(true);
191
+ // @ts-ignore
192
+ render(h("input", { checked: s }), scratch);
193
+
194
+ expect(scratch.firstChild).to.have.property("checked", true);
195
+
196
+ s.value = false;
197
+
198
+ expect(scratch.firstChild).to.have.property("checked", false);
199
+ });
200
+
201
+ it("should update props without re-rendering", async () => {
202
+ const s = signal("initial");
203
+ const spy = sinon.spy();
204
+ function Wrap() {
205
+ spy();
206
+ // @ts-ignore
207
+ return h("input", { value: s });
134
208
  }
209
+ render(h(Wrap, {}), scratch);
210
+ spy.resetHistory();
135
211
 
136
- function App() {
137
- sig.value;
138
- return useMemo(() => h(Inner, { foo: 1 }), []);
212
+ expect(scratch.firstChild).to.have.property("value", "initial");
213
+
214
+ s.value = "updated";
215
+
216
+ expect(scratch.firstChild).to.have.property("value", "updated");
217
+
218
+ // ensure the component was never re-rendered: (even after a tick)
219
+ await sleep();
220
+ expect(spy).not.to.have.been.called;
221
+
222
+ s.value = "second update";
223
+
224
+ expect(scratch.firstChild).to.have.property("value", "second update");
225
+
226
+ // ensure the component was never re-rendered: (even after a tick)
227
+ await sleep();
228
+ expect(spy).not.to.have.been.called;
229
+ });
230
+
231
+ it("should set and update string style property", async () => {
232
+ const style = signal("left: 10px");
233
+ const spy = sinon.spy();
234
+ function Wrap() {
235
+ spy();
236
+ // @ts-ignore
237
+ return h("div", { style });
139
238
  }
239
+ render(h(Wrap, {}), scratch);
240
+ spy.resetHistory();
140
241
 
141
- render(h(App, {}), scratch);
142
- expect(scratch.textContent).to.equal("foo");
242
+ const div = scratch.firstChild as HTMLDivElement;
143
243
 
144
- sig.value = "bar";
145
- rerender();
146
- expect(scratch.textContent).to.equal("bar");
244
+ expect(div.style).to.have.property("left", "10px");
245
+
246
+ // ensure the component was never re-rendered: (even after a tick)
247
+ await sleep();
248
+ expect(spy).not.to.have.been.called;
249
+
250
+ style.value = "left: 20px;";
251
+
252
+ expect(div.style).to.have.property("left", "20px");
253
+
254
+ // ensure the component was never re-rendered: (even after a tick)
255
+ await sleep();
256
+ expect(spy).not.to.have.been.called;
147
257
  });
148
258
  });
149
259
  });