@preact/signals 1.0.2 → 1.0.4
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 +19 -0
- package/README.md +112 -0
- package/dist/signals.js +1 -1
- package/dist/signals.js.map +1 -1
- package/dist/signals.min.js +1 -1
- package/dist/signals.min.js.map +1 -1
- package/dist/signals.mjs +1 -1
- package/dist/signals.mjs.map +1 -1
- package/dist/signals.module.js +1 -1
- package/dist/signals.module.js.map +1 -1
- package/package.json +9 -5
- package/src/index.ts +96 -74
- package/src/internal.d.ts +3 -1
- package/test/{exports.test.ts → exports.test.tsx} +0 -0
- package/test/{index.test.ts → index.test.tsx} +68 -42
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# @preact/signals
|
|
2
2
|
|
|
3
|
+
## 1.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#147](https://github.com/preactjs/signals/pull/147) [`3556499`](https://github.com/preactjs/signals/commit/355649903b766630b62cdd0f90a35d3eafa99fa9) Thanks [@developit](https://github.com/developit)! - Improve performance when rendering Signals as Text in Preact.
|
|
8
|
+
|
|
9
|
+
* [#148](https://github.com/preactjs/signals/pull/148) [`b948745`](https://github.com/preactjs/signals/commit/b948745de7b5b60a20ce3bdc5ee72d47d47f38ec) Thanks [@marvinhagemeister](https://github.com/marvinhagemeister)! - Move `types` field in `package.json` to the top of the entry list to ensure that TypeScript always finds it.
|
|
10
|
+
|
|
11
|
+
- [#153](https://github.com/preactjs/signals/pull/153) [`0da9ce3`](https://github.com/preactjs/signals/commit/0da9ce3c6f57cef67c3e84f0d829421aee8defff) Thanks [@developit](https://github.com/developit)! - Optimize the performance of prop bindings in Preact
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [[`f2ba3d6`](https://github.com/preactjs/signals/commit/f2ba3d657bf8169c6ba1d47c0827aa18cfe1c947), [`160ea77`](https://github.com/preactjs/signals/commit/160ea7791f3adb55c562f5990e0b4848d8491a38), [`4385ea8`](https://github.com/preactjs/signals/commit/4385ea8c8358a154d8b789685bb061658ce1153f), [`b948745`](https://github.com/preactjs/signals/commit/b948745de7b5b60a20ce3bdc5ee72d47d47f38ec), [`00a59c6`](https://github.com/preactjs/signals/commit/00a59c6475bd4542fb934474d82d1e242b2ac870)]:
|
|
14
|
+
- @preact/signals-core@1.1.1
|
|
15
|
+
|
|
16
|
+
## 1.0.3
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- ab5bd99: Fix swapping and HMR for signals when used as text
|
|
21
|
+
|
|
3
22
|
## 1.0.2
|
|
4
23
|
|
|
5
24
|
### Patch 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 @@
|
|
|
1
|
-
var n,
|
|
1
|
+
var r,n,t,i=require("preact"),e=require("preact/hooks"),o=require("@preact/signals-core"),u=new WeakSet,f=new WeakSet,a=new WeakSet;function c(r,n){i.options[r]=n.bind(null,i.options[r]||function(){})}var v=new WeakMap;function s(r){t&&t(!0,!0),n=r,t=r&&r._()}function p(r){var n=o.signal(void 0);return n._u=r,n}function l(r){var t=this,i=r.data,u=h(i);u.value=i;var f=e.useMemo(function(){for(var r=t.__v;r=r.__;)if(r.__c){a.add(r.__c);break}return n._u=function(){t.base.data=f._v},o.computed(function(){var r=u.value.value;return 0===r?0:!0===r?"":r||""})},[]);return f.value}function _(r){var n={__proto__:null},t=p(function(i){var e=t.__;for(var u in e)if("children"!==u){var f=e[u];if(f instanceof o.Signal){var a=f.value,c=n[u];n[u]=a,!0===i||c===a||(u in r?r[u]=a:a?r.setAttribute(u,a):r.removeAttribute(u))}}});return t}function h(r){return e.useMemo(function(){return o.signal(r)},[])}l.displayName="_st",Object.defineProperties(o.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:l},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),c("__b",function(r,n){if("string"==typeof n.type){var t,i=n.props;for(var e in i)if("children"!==e){var u=i[e];u instanceof o.Signal&&(t||(n.__np=t={}),t[e]=u,i[e]=u.peek())}}r(n)}),c("__r",function(n,t){var i,e=t.__c;e&&(u.delete(e),void 0===(i=v.get(e))&&(i=p(function(){u.add(e),e.setState({})}),v.set(e,i))),r=e,s(i),n(t)}),c("__e",function(n,t,i,e){s(),r=void 0,n(t,i,e)}),c("diffed",function(n,t){var i,e;if(s(),r=void 0,"string"==typeof t.type&&(i=t.__e)){var o=t.__np;o&&((e=i._u)||(e=_(i),i._u=e),e.__=o,s(e),e._u(!0))}n(t)}),c("unmount",function(r,n){var t=n.__c,i=t&&v.get(t);if(i&&(v.delete(t),i._()(!0,!0)),"string"==typeof n.type){var e=n.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}r(n)}),c("__h",function(r,n,t,i){i<3&&f.add(n),r(n,t,i)}),i.Component.prototype.shouldComponentUpdate=function(r,n){var t,i=v.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||a.has(this)))return!0;if(u.has(this))return!0;if(f.has(this))return!0;for(var e in n)return!0;for(var o in r)if("__source"!==o&&r[o]!==this.props[o])return!0;for(var c in this.props)if(!(c in r))return!0;return!1},exports.Signal=o.Signal,exports.batch=o.batch,exports.computed=o.computed,exports.effect=o.effect,exports.signal=o.signal,exports.useComputed=function(n){var t=e.useRef(n);return t.current=n,a.add(r),e.useMemo(function(){return o.computed(function(){return t.current()})},[])},exports.useSignal=h;//# sourceMappingURL=signals.js.map
|
package/dist/signals.js.map
CHANGED
|
@@ -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: 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\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}\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","currentUpdater","finishUpdate","preact","require","hooks","signalsCore","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","createUpdater","dom","__e","i","prop","_key","value","_signal","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","this","useMemo","__v","v","__","__c","add","base","computed","displayName","old","type","props","push","oldUpdater","newUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2","exports","batch","effect","useComputed","compute","$compute","useRef","current","useSignal"],"mappings":"AAsBA,IAcAA,EACIC,EACJC,EAhBAC,EAAAC,QAAA,UAAAC,EAAAD,QAAA,gBAAAE,EAAAF,QAAA,wBAAsBG,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,IAArBD,QAGA,SAASE,EAA6BC,EAAaC,GAElDC,EAAOA,QAACF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAACF,IAAc,WAAxC,EACpB,CAKD,IAAMI,EAAsB,IAAIC,QAEhC,SAAAC,EAA2BC,GAEtBhB,GAAcA,GAAa,GAAM,GAErCD,EAAiBiB,EACjBhB,EAAegB,GAAWA,EAAQC,GAClC,CAED,WAAuBD,GACtB,IAAME,EAAIC,cAAOC,GAGjB,OAFAF,EAAEG,IAAe,EACjBH,EAAEI,GAAWN,EAEbE,CAAA,CAGD,SAASK,EAAkBC,GAC1B,IAAWR,EAAGH,EAAoBY,IAAID,GACtC,GAAKR,EAsBJA,EAAQU,GAAOC,OAAS,MAtBX,CACb,IAAIC,EAAwD,IAC5DZ,EAAUa,EAAc,WAGvB,IAFA,IAAIC,EAAMN,EAAMO,IAENC,EAAG,EAAGA,EAAIJ,EAAYD,OAAQK,IAAK,CAC5C,IAAsCJ,EAAAA,EAAYI,GAAtCC,EAANC,EAAAA,EACGC,EADSC,EAAAA,EACCC,GACnB,IAAKP,EAAK,OACNG,KAAQH,EAEXA,EAAIG,GAAQE,EACFA,EACVL,EAAIQ,aAAaL,EAAME,GAEvBL,EAAIS,gBAAgBN,EAErB,CACD,IACOP,GAASE,EACjBf,EAAoB2B,IAAIhB,EAAOR,EAC/B,CAGD,OAAOA,CACP,CAYD,SAAAyB,EAA0BC,EAAYV,EAAYW,GAC5B,iBAAjBD,GAAsC,MAATA,IAEtBE,MAAMC,QAAQH,GACxBA,EAAMI,QAAQL,GACJC,aAAJK,EAAAA,SAENJ,EAAIX,GAAKg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,KAAAA,MAAO,KAAMjC,KAAKiC,GAAQ,OAA7C,EAGA,OAAO,CACP,EAWAyB,QAAA3C,OAAA3C,EAAA2C,OAAA2C,QAAAC,MAAAvF,EAAAuF,MAAAD,QAAA7B,SAAAzD,EAAAyD,SAAA6B,QAAAE,OAAAxF,EAAAwF,OAAAF,QAAAvE,OAAAf,EAAAe,OAAAuE,QAAAG,YALeA,SAAeC,GAC9B,IAAcC,EAAGC,EAAMA,OAACF,GAGxB,OAFAC,EAASE,QAAUH,EACnBvF,EAAaoD,IAAI7D,GACVwD,UAAQ,WAAA,OAAcO,EAAAA,SAAI,kBAAckC,EAACE,SAAf,EAAlB,EAA6C,GAC5D,EAAAP,QAAAQ,UATK,SAAuB/D,GAC5B,OAAcmB,EAAAA,QAAC,kBAAYnC,EAAAA,OAAIgB,EAAhB,EAAwB,GACvC"}
|
|
1
|
+
{"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["currentComponent","currentUpdater","preact","require","hooks","signalsCore","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","options","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","signal","undefined","s","_updater","_ref","_this","this","data","currentSignal","useSignal","value","useMemo","v","__v","__","__c","add","base","_value","computed","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","Component","shouldComponentUpdate","state","_updater$_deps","_deps","size","has","exports","batch","effect","useComputed","compute","$compute","useRef","current"],"mappings":"AAsBA,IAcAA,EACIC,IAfJC,EAAAC,QAAA,UAAAC,EAAAD,QAAA,gBAAAE,EAAAF,QAAA,wBAAMG,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErChB,EAAiBe,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUI,EAAMA,YAACC,GAEjB,OADAC,EAAEC,GAAWP,EACNM,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,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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,
|
|
1
|
+
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],r):r((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,r,t,i){var e,o,f,u=new WeakSet,a=new WeakSet,c=new WeakSet;function v(n,t){r.options[n]=t.bind(null,r.options[n]||function(){})}var s=new WeakMap;function l(n){f&&f(!0,!0),o=n,f=n&&n._()}function p(n){var r=i.signal(void 0);return r._u=n,r}function d(n){var r=this,e=n.data,f=h(e);f.value=e;var u=t.useMemo(function(){for(var n=r.__v;n=n.__;)if(n.__c){c.add(n.__c);break}return o._u=function(){r.base.data=u._v},i.computed(function(){var n=f.value.value;return 0===n?0:!0===n?"":n||""})},[]);return u.value}function _(n){var r={__proto__:null},t=p(function(e){var o=t.__;for(var f in o)if("children"!==f){var u=o[f];if(u instanceof i.Signal){var a=u.value,c=r[f];r[f]=a,!0===e||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function h(n){return t.useMemo(function(){return i.signal(n)},[])}d.displayName="_st",Object.defineProperties(i.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),v("__b",function(n,r){if("string"==typeof r.type){var t,e=r.props;for(var o in e)if("children"!==o){var f=e[o];f instanceof i.Signal&&(t||(r.__np=t={}),t[o]=f,e[o]=f.peek())}}n(r)}),v("__r",function(n,r){var t,i=r.__c;i&&(u.delete(i),void 0===(t=s.get(i))&&(t=p(function(){u.add(i),i.setState({})}),s.set(i,t))),e=i,l(t),n(r)}),v("__e",function(n,r,t,i){l(),e=void 0,n(r,t,i)}),v("diffed",function(n,r){var t,i;if(l(),e=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=_(t),t._u=i),i.__=o,l(i),i._u(!0))}n(r)}),v("unmount",function(n,r){var t=r.__c,i=t&&s.get(t);if(i&&(s.delete(t),i._()(!0,!0)),"string"==typeof r.type){var e=r.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}n(r)}),v("__h",function(n,r,t,i){i<3&&a.add(r),n(r,t,i)}),r.Component.prototype.shouldComponentUpdate=function(n,r){var t,i=s.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||c.has(this)))return!0;if(u.has(this))return!0;if(a.has(this))return!0;for(var e in r)return!0;for(var o in n)if("__source"!==o&&n[o]!==this.props[o])return!0;for(var f in this.props)if(!(f in n))return!0;return!1},n.Signal=i.Signal,n.batch=i.batch,n.computed=i.computed,n.effect=i.effect,n.signal=i.signal,n.useComputed=function(n){var r=t.useRef(n);return r.current=n,c.add(e),t.useMemo(function(){return i.computed(function(){return r.current()})},[])},n.useSignal=h});//# sourceMappingURL=signals.min.js.map
|
package/dist/signals.min.js.map
CHANGED
|
@@ -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: 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\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}\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":["g","f","exports","module","require","define","amd","globalThis","self","preactSignals","preact","hooks","signalsCore","this","currentComponent","currentUpdater","finishUpdate","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","options","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","s","signal","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","createUpdater","dom","__e","i","prop","_key","value","_signal","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Signal","createElement","Text","data","_ref","_this","useMemo","__v","v","__","__c","add","base","computed","displayName","old","type","props","push","oldUpdater","newUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","Component","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2","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,IAcAE,EACIC,EACJC,EAhBsBC,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,IAArBD,QAGA,SAASE,EAA6BC,EAAaC,GAElDC,EAAOA,QAACF,GAAYC,EAAOE,KAAK,KAAMD,EAAOA,QAAC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,EAAamD,IAAIxD,GACVmD,UAAQ,WAAA,OAAcO,EAAAA,SAAI,kBAAciC,EAACE,SAAf,EAAlB,EAA6C,GAC5D,EAAAzG,EAAA0G,UATK,SAAuB7D,GAC5B,OAAckB,EAAAA,QAAC,kBAAYlC,EAAAA,OAAIgB,EAAhB,EAAwB,GACvC,CAOA"}
|
|
1
|
+
{"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["g","f","exports","module","require","define","amd","globalThis","self","preactSignals","preact","hooks","signalsCore","this","currentComponent","currentUpdater","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","options","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","signal","undefined","s","_updater","_ref","_this","data","currentSignal","useSignal","value","useMemo","v","__v","__","__c","add","base","_value","computed","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","Text","displayName","Object","defineProperties","Signal","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","Component","shouldComponentUpdate","state","_updater$_deps","_deps","size","has","batch","effect","useComputed","compute","$compute","useRef","current"],"mappings":"CAsBA,SAAAA,EAAAC,GAAA,iBAAAC,SAAA,oBAAAC,OAAAF,EAAAC,QAAAE,QAAA,UAAAA,QAAA,gBAAAA,QAAA,yBAAA,mBAAAC,QAAAA,OAAAC,IAAAD,OAAA,CAAA,UAAA,SAAA,eAAA,wBAAAJ,GAAAA,GAAAD,EAAA,oBAAAO,WAAAA,WAAAP,GAAAQ,MAAAC,cAAA,CAAA,EAAAT,EAAAU,OAAAV,EAAAW,MAAAX,EAAAY,YAAA,CAAA,CAAAC,KAAA,SAAAX,EAAAQ,EAAAC,EAAAC,GAAA,IAcAE,EACIC,IAfEC,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDC,EAAAA,QAAQF,GAAYC,EAAOE,KAAK,KAAMD,EAAAA,QAAQF,IAAc,WAAO,EACnE,CAKD,IAAyBI,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErCZ,EAAiBW,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUI,EAAMA,YAACC,GAEjB,OADAC,EAAEC,GAAWP,EACNM,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAtB,KAAAuB,EAAAF,EAAxBE,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EA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-1 +1 @@
|
|
|
1
|
-
import{Component as t,options as
|
|
1
|
+
import{Component as t,options as n}from"preact";import{useMemo as e,useRef as r}from"preact/hooks";import{Signal as i,computed as o,signal as f}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const c=new WeakSet,u=new WeakSet,l=new WeakSet;function s(t,e){n[t]=e.bind(null,n[t]||(()=>{}))}let a,p,_;const h=new WeakMap;function d(t){_&&_(!0,!0),p=t,_=t&&t._()}function g(t){const n=f(void 0);return n._u=t,n}function b({data:t}){const n=v(t);n.value=t;const r=e(()=>{let t=this.__v;for(;t=t.__;)if(t.__c){l.add(t.__c);break}return p._u=()=>{this.base.data=r._v},o(()=>{let t=n.value.value;return 0===t?0:!0===t?"":t||""})},[]);return r.value}function m(t){const n={__proto__:null},e=g(r=>{const o=e.__;for(let e in o){if("children"===e)continue;let f=o[e];if(f instanceof i){let i=f.value,o=n[e];n[e]=i,!0===r||o===i||(e in t?t[e]=i:i?t.setAttribute(e,i):t.removeAttribute(e))}}});return e}function v(t){return e(()=>f(t),[])}function k(t){const n=r(t);return n.current=t,l.add(a),e(()=>o(()=>n.current()),[])}b.displayName="_st",Object.defineProperties(i.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:b},props:{configurable:!0,get(){return{data:this}}},__b:{configurable:!0,value:1}}),s("__b",(t,n)=>{if("string"==typeof n.type){let t,e=n.props;for(let r in e){if("children"===r)continue;let o=e[r];o instanceof i&&(t||(n.__np=t={}),t[r]=o,e[r]=o.peek())}}t(n)}),s("__r",(t,n)=>{let e,r=n.__c;r&&(c.delete(r),e=h.get(r),void 0===e&&(e=g(()=>{c.add(r),r.setState({})}),h.set(r,e))),a=r,d(e),t(n)}),s("__e",(t,n,e,r)=>{d(),a=void 0,t(n,e,r)}),s("diffed",(t,n)=>{let e,r;if(d(),a=void 0,"string"==typeof n.type&&(e=n.__e)){let t=n.__np;t&&(r=e._u,r||(r=m(e),e._u=r),r.__=t,d(r),r._u(!0))}t(n)}),s("unmount",(t,n)=>{let e=n.__c;const r=e&&h.get(e);if(r&&(h.delete(e),r._()(!0,!0)),"string"==typeof n.type){const t=n.__e,e=t._u;e&&(e._()(!0,!0),t._u=null)}t(n)}),s("__h",(t,n,e,r)=>{r<3&&u.add(n),t(n,e,r)}),t.prototype.shouldComponentUpdate=function(t,n){var e;const r=h.get(this);if(!(r&&0!==(null==(e=r._d)?void 0:e.size)||l.has(this)))return!0;if(c.has(this))return!0;if(u.has(this))return!0;for(let t in n)return!0;for(let n in t)if("__source"!==n&&t[n]!==this.props[n])return!0;for(let n in this.props)if(!(n in t))return!0;return!1};export{k as useComputed,v as useSignal};//# sourceMappingURL=signals.mjs.map
|
package/dist/signals.mjs.map
CHANGED
|
@@ -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: 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\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}\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":["Component","options","createElement","useMemo","useRef","Signal","signal","computed","batch","effect","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","bind","currentComponent","currentUpdater","finishUpdate","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","undefined","_canActivate","_updater","getElementUpdater","vnode","updaterForComponent","get","_props","length","dom","__e","i","signalProps","_key","prop","_signal","value","_value","setAttribute","removeAttribute","set","childToSignal","child","arr","Array","isArray","forEach","Text","data","v","this","__v","__","__c","add","base","useSignal","useComputed","compute","$compute","current","displayName","old","type","props","push","newUpdater","oldUpdater","peek","component","delete","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","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,MAAsBI,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,IAArBD,QAGA,SAASE,EAA6BC,EAAaC,GAElDd,EAAQa,GAAYC,EAAOC,KAAK,KAAMf,EAAQa,IAAR,MAAA,GACtC,CAED,IAAIG,EACJC,EACIC,EACJ,QAA4B,IAA5BC,QAEA,SAASC,EAAkBC,GAEtBH,GAAcA,GAAa,GAAM,GAErCD,EAAiBI,EACjBH,EAAeG,GAAWA,EAAQC,GAClC,CAED,SAAAC,EAAuBF,GACtB,MAAOG,EAAGnB,OAAOoB,GAGjB,OAFAD,EAAEE,IAAe,EACjBF,EAAEG,GAAWN,EACNG,CACP,CAGD,SAAAI,EAA2BC,GAC1B,IAAIR,EAAUS,EAAoBC,IAAIF,GACtC,GAAKR,EAsBJA,EAAQW,GAAOC,OAAS,MAtBX,CACb,MAA4D,GAC5DZ,EAAUE,EAAc,KACvB,IAAOW,EAAGL,EAAMM,IAEhB,IAAK,IAAIC,EAAI,EAAGA,EAAIC,EAAYJ,OAAQG,IAAK,CAC5C,IAAME,EAAMC,EAAMC,GAAoBH,EAAYD,GACzCK,EAAGpC,EAAOqC,GACnB,IAAKR,EAAK,OACNK,KAAQL,EAEXA,EAAIK,GAAQE,EACFA,EACVP,E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,EACtB,GAMDrE,EAAUuF,UAAUC,sBAAwB,SAAUlB,EAAOmB,GAE5D,IAAAC,EAAA,MAAapE,EAAGS,EAAoBC,IAAIwB,MA2BxC,KAzBmBlC,GAAmC,KAAxB,OAAAoE,EAAApE,EAAQ4D,SAAR,EAAAQ,EAAeC,OAyBzB/E,EAAagF,IAAIpC,OAAO,OAAA,EAG5C,GAAI9C,EAAiBkF,IAAIpC,MAAO,OAAA,EAGhC,GAAI8B,EAAaM,IAAIpC,MAAO,OAAA,EAC5B,IAAK,IAALnB,KAAAoD,EAAqB,OAAO,EAG5B,IAAK,IAAIpD,KAATiC,EACC,GAAU,aAANjC,GAAoBiC,EAAMjC,KAAOmB,KAAKc,MAAMjC,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAKmB,KAAKc,MAAO,KAAMjC,QAAa,OAAO,EAGpD,OACA,CAAA,SAWA0B,iBAAAD"}
|
|
1
|
+
{"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","Signal","computed","signal","batch","effect","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hook","hookName","hookFn","bind","currentComponent","currentUpdater","finishUpdate","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","createUpdater","s","undefined","_updater","Text","data","currentSignal","useSignal","value","v","this","__v","__","__c","add","base","_value","dom","cache","__proto__","skip","props","_props","prop","cached","setAttribute","removeAttribute","useComputed","compute","$compute","current","displayName","Object","defineProperties","prototype","constructor","configurable","type","get","__b","old","vnode","signalProps","i","__np","peek","component","delete","setState","set","error","oldVNode","__e","createElementUpdater","index","shouldComponentUpdate","state","_updater$_deps","size","_deps","has"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,cAAAC,YAAAC,MAAA,8BAAAF,OAAAG,MAAAF,SAAAG,OAAAF,WAAA,uBAAA,MAAsBG,EAAG,IAAIC,QAGXC,EAAG,IAArBD,QAGME,EAAe,IAArBF,QAGA,SAAAG,EAAsCC,EAAaC,GAElDd,EAAQa,GAAYC,EAAOC,KAAK,KAAMf,EAAQa,IAAc,MAAtB,GACtC,CAED,IAAAG,EACIC,EACAC,EACJ,MAAyBC,EAAG,IAAIC,QAEhC,SAASC,EAAkBC,GAEtBJ,GAAcA,GAAa,GAAM,GAErCD,EAAiBK,EACjBJ,EAAeI,GAAWA,EAAQC,GAClC,CAED,SAAAC,EAAuBF,GACtB,MAAMG,EAAIpB,OAAOqB,GAEjB,OADAD,EAAEE,GAAWL,EACNG,CACP,CAeD,SAASG,GAA0BC,KAAEA,IAKpC,MA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C,KAChC,IAAaK,EAAGL,EAAM5B,IACtB,MAAMf,EAAUgD,GAAanD,EAAoB2C,IAAIQ,GAMrD,GALIhD,IACHH,EAAoBoD,OAAOD,GAC3BhD,EAAQC,GAARD,EAAsB,GAAM,IAGH,iBAAV2C,EAACJ,KAAmB,CACnC,QAAYI,EAAMW,IAGLjD,EAAGc,EAAId,GAChBL,IACHA,EAAQC,GAARD,EAAsB,GAAM,GAE5BmB,EAAId,GAAW,KAEhB,CACDqC,EAAIC,KAILrD,EAAI,MAAoB,CAACoD,EAAKM,EAAWQ,EAAOjB,KAC3CA,EAAO,GAAGnD,EAAa4B,IAAIgC,GAC/BN,EAAIM,EAAWQ,EAAOjB,EAAnB,GAOJ9D,EAAU2D,UAAUqB,sBAAwB,SAAUlC,EAAOmC,GAE5D,IAAAC,EAAA,QAAgB9D,EAAoB2C,IAAI5B,MA2BxC,KAzBmBZ,GAAmC,KAAT4D,OAAfD,EAAA3D,EAAQ6D,SAAOD,EAAAA,EAAAA,OAyBzBvE,EAAayE,IAAIlD,OAAO,OAAA,EAG5C,GAAI1B,EAAiB4E,IAAIlD,MAAO,OAAO,EAGvC,GAAIxB,EAAa0E,IAAIlD,MAAO,OAAO,EAEnC,IAAK,IAALiC,KAAAa,EAAqB,OAAO,EAG5B,IAAK,IAALb,KAAAtB,EACC,GAAU,aAANsB,GAAoBtB,EAAMsB,KAAOjC,KAAKW,MAAMsB,GAAI,OAAO,EAE5D,IAAK,IAALA,KAAmBtB,KAAAA,MAAO,KAAMsB,KAAFtB,GAAe,OAAA,EAG7C,OAAO,CACP,SAWAM,iBAAApB"}
|
package/dist/signals.module.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Component as n,options as r
|
|
1
|
+
import{Component as n,options as r}from"preact";import{useMemo as t,useRef as i}from"preact/hooks";import{Signal as o,computed as e,signal as f}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var u,a,c,v=new WeakSet,s=new WeakSet,l=new WeakSet;function p(n,t){r[n]=t.bind(null,r[n]||function(){})}var _=new WeakMap;function h(n){c&&c(!0,!0),a=n,c=n&&n._()}function g(n){var r=f(void 0);return r._u=n,r}function d(n){var r=this,i=n.data,o=m(i);o.value=i;var f=t(function(){for(var n=r.__v;n=n.__;)if(n.__c){l.add(n.__c);break}return a._u=function(){r.base.data=f._v},e(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function b(n){var r={__proto__:null},t=g(function(i){var e=t.__;for(var f in e)if("children"!==f){var u=e[f];if(u instanceof o){var a=u.value,c=r[f];r[f]=a,!0===i||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function m(n){return t(function(){return f(n)},[])}function k(n){var r=i(n);return r.current=n,l.add(u),t(function(){return e(function(){return r.current()})},[])}d.displayName="_st",Object.defineProperties(o.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),p("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var e in i)if("children"!==e){var f=i[e];f instanceof o&&(t||(r.__np=t={}),t[e]=f,i[e]=f.peek())}}n(r)}),p("__r",function(n,r){var t,i=r.__c;i&&(v.delete(i),void 0===(t=_.get(i))&&(t=g(function(){v.add(i),i.setState({})}),_.set(i,t))),u=i,h(t),n(r)}),p("__e",function(n,r,t,i){h(),u=void 0,n(r,t,i)}),p("diffed",function(n,r){var t,i;if(h(),u=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=b(t),t._u=i),i.__=o,h(i),i._u(!0))}n(r)}),p("unmount",function(n,r){var t=r.__c,i=t&&_.get(t);if(i&&(_.delete(t),i._()(!0,!0)),"string"==typeof r.type){var o=r.__e,e=o._u;e&&(e._()(!0,!0),o._u=null)}n(r)}),p("__h",function(n,r,t,i){i<3&&s.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=_.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||l.has(this)))return!0;if(v.has(this))return!0;if(s.has(this))return!0;for(var o in r)return!0;for(var e in n)if("__source"!==e&&n[e]!==this.props[e])return!0;for(var f in this.props)if(!(f in n))return!0;return!1};export{k as useComputed,m as useSignal};//# sourceMappingURL=signals.module.js.map
|
|
@@ -1 +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: 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\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}\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":["Component","options","createElement","useMemo","useRef","Signal","signal","computed","batch","effect","currentComponent","currentUpdater","finishUpdate","hasPendingUpdate","WeakSet","hasComputeds","hook","hookName","hookFn","bind","updaterForComponent","WeakMap","setCurrentUpdater","updater","_setCurrent","s","undefined","_canActivate","_updater","getElementUpdater","vnode","get","_props","length","signalProps","createUpdater","dom","__e","i","prop","_key","value","_signal","_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","displayName","old","type","props","push","oldUpdater","newUpdater","peek","component","setState","error","oldVNode","thing","signals","_deps","_subs","clear","index","hasHookState","prototype","shouldComponentUpdate","state","_updater$_deps","size","has","_i","_i2"],"mappings":"oBAsBAA,aAAAC,mBAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,YAAAC,cAAAC,MAAA,8BAAAF,OAAAG,MAAAD,SAAAE,OAAAH,WAAA,uBAAA,IAcAI,EACIC,EACJC,EAhBsBC,EAAG,IAAIC,UAGR,IAArBA,QAGMC,EAAe,IAArBD,QAGA,SAASE,EAA6BC,EAAaC,GAElDjB,EAAQgB,GAAYC,EAAOC,KAAK,KAAMlB,EAAQgB,IAAc,WAAxC,EACpB,CAKD,IAAMG,EAAsB,IAAIC,QAEhC,SAAAC,EAA2BC,GAEtBX,GAAcA,GAAa,GAAM,GAErCD,EAAiBY,EACjBX,EAAeW,GAAWA,EAAQC,GAClC,CAED,WAAuBD,GACtB,IAAME,EAAInB,OAAOoB,GAGjB,OAFAD,EAAEE,IAAe,EACjBF,EAAEG,GAAWL,EAEbE,CAAA,CAGD,SAASI,EAAkBC,GAC1B,IAAWP,EAAGH,EAAoBW,IAAID,GACtC,GAAKP,EAsBJA,EAAQS,GAAOC,OAAS,MAtBX,CACb,IAAIC,EAAwD,IAC5DX,EAAUY,EAAc,WAGvB,IAFA,IAAIC,EAAMN,EAAMO,IAENC,EAAG,EAAGA,EAAIJ,EAAYD,OAAQK,IAAK,CAC5C,IAAsCJ,EAAAA,EAAYI,GAAtCC,EAANC,EAAAA,EACGC,EADSC,EAAAA,EACCC,GACnB,IAAKP,EAAK,OACNG,KAAQH,EAEXA,EAAIG,GAAQE,EACFA,EACVL,EAAIQ,aAAaL,EAAME,GAEvBL,EAAIS,gBAAgBN,EAErB,CACD,IACOP,GAASE,EACjBd,EAAoB0B,IAAIhB,EAAOP,EAC/B,CAGD,OAAOA,CACP,CAYD,SAAAwB,EAA0BC,EAAYV,EAAYW,GAC5B,iBAAjBD,GAAsC,MAATA,IAEtBE,MAAMC,QAAQH,GACxB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AAA,EAAAA,OAyBzB7E,EAAa8E,IAAIpC,OAAO,OAAO,EAGnD,GAAI5C,EAAiBgF,IAAIpC,MAAO,OAAO,EAGvC,GAAI8B,EAAaM,IAAIpC,MAAO,SAC5B,IAAK,IAAInB,KAAKoD,EAAO,OAArB,EAGA,IAAK,IAALI,KAAAtB,EACC,GAAU,aAANlC,GAAoBkC,EAAMlC,KAAOmB,KAAKe,MAAMlC,GAAI,OACpD,EACD,IAAK,IAALyD,KAAmBvB,KAAAA,MAAO,KAAMlC,KAAKkC,GAAQ,OAA7C,EAGA,OAAO,CACP,SAWAP,iBAAAD"}
|
|
1
|
+
{"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import { options, Component } from \"preact\";\nimport { useRef, useMemo } from \"preact/hooks\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport {\n\tVNode,\n\tComponentType,\n\tOptionsTypes,\n\tHookFn,\n\tUpdater,\n\tElementUpdater,\n} from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\n// Components that have a pending Signal update: (used to bypass default sCU:false)\nconst hasPendingUpdate = new WeakSet<Component>();\n\n// Components that have useState()/useReducer() hooks:\nconst hasHookState = new WeakSet<Component>();\n\n// Components that have useComputed():\nconst hasComputeds = new WeakSet<Component>();\n\n// Install a Preact options hook\nfunction hook<T extends OptionsTypes>(hookName: T, hookFn: HookFn<T>) {\n\t// @ts-ignore-next-line private options hooks usage\n\toptions[hookName] = hookFn.bind(null, options[hookName] || (() => {}));\n}\n\nlet currentComponent: Component | undefined;\nlet currentUpdater: Updater | undefined;\nlet finishUpdate: ReturnType<Updater[\"_setCurrent\"]> | undefined;\nconst updaterForComponent = new WeakMap<Component | VNode, Updater>();\n\nfunction setCurrentUpdater(updater?: Updater) {\n\t// end tracking for the current update:\n\tif (finishUpdate) finishUpdate(true, true);\n\t// start tracking the new update:\n\tcurrentUpdater = updater;\n\tfinishUpdate = updater && updater._setCurrent();\n}\n\nfunction createUpdater(updater: () => void) {\n\tconst s = signal(undefined) as Updater;\n\ts._updater = updater;\n\treturn s;\n}\n\n/** @todo This may be needed for complex prop value detection. */\n// function isSignalValue(value: any): value is Signal {\n// \tif (typeof value !== \"object\" || value == null) return false;\n// \tif (value instanceof Signal) return true;\n// \t// @TODO: uncomment this when we land Reactive (ideally behind a brand check)\n// \t// for (let i in value) if (value[i] instanceof Signal) return true;\n// \treturn false;\n// }\n\n/**\n * A wrapper component that renders a Signal directly as a Text node.\n * @todo: in Preact 11, just decorate Signal with `type:null`\n */\nfunction Text(this: ComponentType, { data }: { data: Signal }) {\n\t// hasComputeds.add(this);\n\n\t// Store the props.data signal in another signal so that\n\t// passing a new signal reference re-runs the text computed:\n\tconst currentSignal = useSignal(data);\n\tcurrentSignal.value = data;\n\n\tconst s = useMemo(() => {\n\t\t// mark the parent component as having computeds so it gets optimized\n\t\tlet v = this.__v;\n\t\twhile ((v = v.__!)) {\n\t\t\tif (v.__c) {\n\t\t\t\thasComputeds.add(v.__c);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// Replace this component's vdom updater with a direct text one:\n\t\tcurrentUpdater!._updater = () => {\n\t\t\t(this.base as Text).data = s._value;\n\t\t};\n\n\t\treturn computed(() => {\n\t\t\tlet data = currentSignal.value;\n\t\t\tlet s = data.value;\n\t\t\treturn s === 0 ? 0 : s === true ? \"\" : s || \"\";\n\t\t});\n\t}, []);\n\n\treturn s.value;\n}\nText.displayName = \"_st\";\n\nObject.defineProperties(Signal.prototype, {\n\tconstructor: { configurable: true },\n\ttype: { configurable: true, value: Text },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\t// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:\n\t// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77\n\t// @todo remove this for Preact 11\n\t__b: { configurable: true, value: 1 },\n});\n\n/** Inject low-level property/attribute bindings for Signals into Preact's diff */\nhook(OptionsTypes.DIFF, (old, vnode) => {\n\tif (typeof vnode.type === \"string\") {\n\t\tlet signalProps: Record<string, any> | undefined;\n\n\t\tlet props = vnode.props;\n\t\tfor (let i in props) {\n\t\t\tif (i === \"children\") continue;\n\n\t\t\tlet value = props[i];\n\t\t\tif (value instanceof Signal) {\n\t\t\t\tif (!signalProps) vnode.__np = signalProps = {};\n\t\t\t\tsignalProps[i] = value;\n\t\t\t\tprops[i] = value.peek();\n\t\t\t}\n\t\t}\n\t}\n\n\told(vnode);\n});\n\n/** Set up Updater before rendering a component */\nhook(OptionsTypes.RENDER, (old, vnode) => {\n\tlet updater;\n\n\tlet component = vnode.__c;\n\tif (component) {\n\t\thasPendingUpdate.delete(component);\n\n\t\tupdater = updaterForComponent.get(component);\n\t\tif (updater === undefined) {\n\t\t\tupdater = createUpdater(() => {\n\t\t\t\thasPendingUpdate.add(component);\n\t\t\t\tcomponent.setState({});\n\t\t\t});\n\t\t\tupdaterForComponent.set(component, updater);\n\t\t}\n\t}\n\n\tcurrentComponent = component;\n\tsetCurrentUpdater(updater);\n\told(vnode);\n});\n\n/** Finish current updater if a component errors */\nhook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\told(error, vnode, oldVNode);\n});\n\n/** Finish current updater after rendering any VNode */\nhook(OptionsTypes.DIFFED, (old, vnode) => {\n\tsetCurrentUpdater();\n\tcurrentComponent = undefined;\n\n\tlet dom: Element;\n\tlet updater: ElementUpdater;\n\n\t// vnode._dom is undefined during string rendering,\n\t// so we use this to skip prop subscriptions during SSR.\n\tif (typeof vnode.type === \"string\" && (dom = vnode.__e as Element)) {\n\t\tlet props = vnode.__np;\n\t\tif (props) {\n\t\t\t// @ts-ignore-next\n\t\t\tupdater = dom._updater;\n\t\t\tif (!updater) {\n\t\t\t\tupdater = createElementUpdater(dom);\n\t\t\t\t// @ts-ignore-next\n\t\t\t\tdom._updater = updater;\n\t\t\t}\n\t\t\tupdater!._props = props;\n\t\t\tsetCurrentUpdater(updater);\n\t\t\t// @ts-ignore-next we're adding an argument here\n\t\t\tupdater._updater(true);\n\t\t}\n\t}\n\told(vnode);\n});\n\n// per-element updater for 1+ signal bindings\nfunction createElementUpdater(dom: Element) {\n\tconst cache: Record<string, any> = { __proto__: null };\n\tconst updater = createUpdater((skip?: boolean) => {\n\t\tconst props = updater._props;\n\t\tfor (let prop in props) {\n\t\t\tif (prop === \"children\") continue;\n\t\t\tlet signal = props[prop];\n\t\t\tif (signal instanceof Signal) {\n\t\t\t\tlet value = signal.value;\n\t\t\t\tlet cached = cache[prop];\n\t\t\t\tcache[prop] = value;\n\t\t\t\tif (skip === true || cached === value) {\n\t\t\t\t\t// this is just a subscribe run, not an update\n\t\t\t\t} else if (prop in dom) {\n\t\t\t\t\t// @ts-ignore-next-line silly\n\t\t\t\t\tdom[prop] = value;\n\t\t\t\t} else if (value) {\n\t\t\t\t\tdom.setAttribute(prop, value);\n\t\t\t\t} else {\n\t\t\t\t\tdom.removeAttribute(prop);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}) as ElementUpdater;\n\treturn updater;\n}\n\n/** Unsubscribe from Signals when unmounting components/vnodes */\nhook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {\n\tlet component = vnode.__c;\n\tconst updater = component && updaterForComponent.get(component);\n\tif (updater) {\n\t\tupdaterForComponent.delete(component);\n\t\tupdater._setCurrent()(true, true);\n\t}\n\n\tif (typeof vnode.type === \"string\") {\n\t\tconst dom = vnode.__e as Element;\n\n\t\t// @ts-ignore-next\n\t\tconst updater = dom._updater;\n\t\tif (updater) {\n\t\t\tupdater._setCurrent()(true, true);\n\t\t\t// @ts-ignore-next\n\t\t\tdom._updater = null;\n\t\t}\n\t}\n\told(vnode);\n});\n\n/** Mark components that use hook state so we can skip sCU optimization. */\nhook(OptionsTypes.HOOK, (old, component, index, type) => {\n\tif (type < 3) hasHookState.add(component);\n\told(component, index, type);\n});\n\n/**\n * Auto-memoize components that use Signals/Computeds.\n * Note: Does _not_ optimize components that use hook/class state.\n */\nComponent.prototype.shouldComponentUpdate = function (props, state) {\n\t// @todo: Once preactjs/preact#3671 lands, this could just use `currentUpdater`:\n\tconst updater = updaterForComponent.get(this);\n\n\tconst hasSignals = updater && updater._deps?.size !== 0;\n\n\t// let reason;\n\t// if (!hasSignals && !hasComputeds.has(this)) {\n\t// \treason = \"no signals or computeds\";\n\t// } else if (hasPendingUpdate.has(this)) {\n\t// \treason = \"has pending update\";\n\t// } else if (hasHookState.has(this)) {\n\t// \treason = \"has hook state\";\n\t// }\n\t// if (reason) {\n\t// \tif (!this) reason += \" (`this` bug)\";\n\t// \tconsole.log(\"not optimizing\", this?.constructor?.name, \": \", reason, {\n\t// \t\tdetails: {\n\t// \t\t\thasSignals,\n\t// \t\t\thasComputeds: hasComputeds.has(this),\n\t// \t\t\thasPendingUpdate: hasPendingUpdate.has(this),\n\t// \t\t\thasHookState: hasHookState.has(this),\n\t// \t\t\tdeps: Array.from(updater._deps),\n\t// \t\t\tupdater,\n\t// \t\t},\n\t// \t});\n\t// }\n\n\t// if this component used no signals or computeds, update:\n\tif (!hasSignals && !hasComputeds.has(this)) return true;\n\n\t// if there is a pending re-render triggered from Signals, update:\n\tif (hasPendingUpdate.has(this)) return true;\n\n\t// if there is hook or class state, update:\n\tif (hasHookState.has(this)) return true;\n\t// @ts-ignore\n\tfor (let i in state) return true;\n\n\t// if any non-Signal props changed, update:\n\tfor (let i in props) {\n\t\tif (i !== \"__source\" && props[i] !== this.props[i]) return true;\n\t}\n\tfor (let i in this.props) if (!(i in props)) return true;\n\n\t// this is a purely Signal-driven component, don't update:\n\treturn false;\n};\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), []);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\thasComputeds.add(currentComponent!);\n\treturn useMemo(() => computed<T>(() => $compute.current()), []);\n}\n\n/**\n * @todo Determine which Reactive implementation we'll be using.\n * @internal\n */\n// export function useReactive<T extends object>(value: T): Reactive<T> {\n// \treturn useMemo(() => reactive<T>(value), []);\n// }\n\n/**\n * @internal\n * Update a Reactive's using the properties of an object or other Reactive.\n * Also works for Signals.\n * @example\n * // Update a Reactive with Object.assign()-like syntax:\n * const r = reactive({ name: \"Alice\" });\n * update(r, { name: \"Bob\" });\n * update(r, { age: 42 }); // property 'age' does not exist in type '{ name?: string }'\n * update(r, 2); // '2' has no properties in common with '{ name?: string }'\n * console.log(r.name.value); // \"Bob\"\n *\n * @example\n * // Update a Reactive with the properties of another Reactive:\n * const A = reactive({ name: \"Alice\" });\n * const B = reactive({ name: \"Bob\", age: 42 });\n * update(A, B);\n * console.log(`${A.name} is ${A.age}`); // \"Bob is 42\"\n *\n * @example\n * // Update a signal with assign()-like syntax:\n * const s = signal(42);\n * update(s, \"hi\"); // Argument type 'string' not assignable to type 'number'\n * update(s, {}); // Argument type '{}' not assignable to type 'number'\n * update(s, 43);\n * console.log(s.value); // 43\n *\n * @param obj The Reactive or Signal to be updated\n * @param update The value, Signal, object or Reactive to update `obj` to match\n * @param overwrite If `true`, any properties `obj` missing from `update` are set to `undefined`\n */\n/*\nexport function update<T extends SignalOrReactive>(\n\tobj: T,\n\tupdate: Partial<Unwrap<T>>,\n\toverwrite = false\n) {\n\tif (obj instanceof Signal) {\n\t\tobj.value = peekValue(update);\n\t} else {\n\t\tfor (let i in update) {\n\t\t\tif (i in obj) {\n\t\t\t\tobj[i].value = peekValue(update[i]);\n\t\t\t} else {\n\t\t\t\tlet sig = signal(peekValue(update[i]));\n\t\t\t\tsig[KEY] = i;\n\t\t\t\tobj[i] = sig;\n\t\t\t}\n\t\t}\n\t\tif (overwrite) {\n\t\t\tfor (let i in obj) {\n\t\t\t\tif (!(i in update)) {\n\t\t\t\t\tobj[i].value = undefined;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n*/\n"],"names":["Component","options","useMemo","useRef","Signal","computed","signal","batch","effect","currentComponent","currentUpdater","hasPendingUpdate","WeakSet","hasHookState","hasComputeds","hookName","hookFn","bind","updaterForComponent","WeakMap","updater","finishUpdate","_setCurrent","createUpdater","undefined","s","_updater","_ref","_this","this","data","currentSignal","useSignal","value","v","__v","__","__c","add","base","_value","createElementUpdater","dom","cache","__proto__","skip","props","_props","prop","_signal","cached","setAttribute","removeAttribute","useComputed","compute","$compute","current","Text","displayName","Object","defineProperties","prototype","constructor","configurable","type","get","__b","hook","old","vnode","signalProps","i","__np","peek","component","setState","set","setCurrentUpdater","error","oldVNode","__e","index","shouldComponentUpdate","state","_updater$_deps","_deps","size","has"],"mappings":"oBAsBAA,aAAAC,MAAA,2BAAAC,YAAAC,MAAA,gCAAAC,cAAAC,YAAAC,MAAA,8BAAAF,OAAAG,MAAAF,SAAAG,OAAAF,WAAA,uBAAA,IAcAG,EACIC,IAfEC,EAAmB,IAAzBC,QAGMC,EAAe,YAGHC,EAAG,IAAIF,QAGzB,WAAsCG,EAAaC,GAElDf,EAAQc,GAAYC,EAAOC,KAAK,KAAMhB,EAAQc,IAAc,WAAO,EACnE,CAKD,IAAyBG,EAAG,IAAIC,QAEhC,WAA2BC,GAEtBC,GAAcA,GAAa,GAAM,GAErCX,EAAiBU,EACjBC,EAAeD,GAAWA,EAAQE,GAClC,CAED,SAAAC,EAAuBH,GACtB,MAAUd,OAAOkB,GAEjB,OADAC,EAAEC,GAAWN,EACNK,CACP,CAeD,WAA6DE,GAAA,IAAAC,EAAAC,KAAAC,EAAAH,EAAxBG,KAK9BC,EAAgBC,EAAUF,GAChCC,EAAcE,MAAQH,EAEtB,IAAML,EAAIvB,EAAQ,WAGjB,IADA,IAAKgC,EAAGN,EAAKO,IACLD,EAAIA,EAAEE,IACb,GAAIF,EAAEG,IAAK,CACVvB,EAAawB,IAAIJ,EAAEG,K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IAAStB,EAAG2B,EAAMW,IAGLtD,EAAGgB,EAAIhB,GAChBN,IACHA,EAAQE,GAARF,EAAsB,GAAM,GAE5BsB,EAAIhB,GAAW,KAEhB,CACD0C,EAAIC,EACJ,GAGDF,EAAI,MAAoB,SAACC,EAAKM,EAAWO,EAAOjB,GAC3CA,EAAO,GAAGnD,EAAayB,IAAIoC,GAC/BN,EAAIM,EAAWO,EAAOjB,EACtB,GAMDhE,EAAU6D,UAAUqB,sBAAwB,SAAUpC,EAAOqC,GAE5D,IAAAC,EAAahE,EAAGF,EAAoB+C,IAAIpC,MA2BxC,KAzBmBT,GAAmC,KAAxB,OAAAgE,EAAAhE,EAAQiE,SAAR,EAAAD,EAAeE,OAyBzBxE,EAAayE,IAAI1D,OAAO,OAAO,EAGnD,GAAIlB,EAAiB4E,IAAI1D,MAAO,OAAO,EAGvC,GAAIhB,EAAa0E,IAAI1D,MAAO,OAAO,EAEnC,IAAK,IAAI0C,KAATY,EAAqB,OAAO,EAG5B,IAAK,IAAIZ,KAATzB,EACC,GAAU,aAANyB,GAAoBzB,EAAMyB,KAAO1C,KAAKiB,MAAMyB,GAAI,OAAO,EAE5D,IAAK,IAAIA,KAAUzB,KAAAA,MAAO,KAAMyB,KAAKzB,GAAQ,OAA7C,EAGA,OAAO,CACP,SAWAO,iBAAArB"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preact/signals",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
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":
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/preactjs/signals",
|
|
13
|
+
"directory": "packages/preact"
|
|
14
|
+
},
|
|
11
15
|
"bugs": "https://github.com/preactjs/signals/issues",
|
|
12
16
|
"homepage": "https://preactjs.com",
|
|
13
17
|
"funding": {
|
|
@@ -22,16 +26,16 @@
|
|
|
22
26
|
"source": "src/index.ts",
|
|
23
27
|
"exports": {
|
|
24
28
|
".": {
|
|
29
|
+
"types": "./dist/signals.d.ts",
|
|
25
30
|
"browser": "./dist/signals.module.js",
|
|
26
31
|
"umd": "./dist/signals.umd.js",
|
|
27
32
|
"import": "./dist/signals.mjs",
|
|
28
|
-
"require": "./dist/signals.js"
|
|
29
|
-
"types": "./dist/signals.d.ts"
|
|
33
|
+
"require": "./dist/signals.js"
|
|
30
34
|
}
|
|
31
35
|
},
|
|
32
36
|
"mangle": "../../mangle.json",
|
|
33
37
|
"dependencies": {
|
|
34
|
-
"@preact/signals-core": "^1.
|
|
38
|
+
"@preact/signals-core": "^1.1.1"
|
|
35
39
|
},
|
|
36
40
|
"peerDependencies": {
|
|
37
41
|
"preact": "10.x"
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { options, Component
|
|
1
|
+
import { options, Component } from "preact";
|
|
2
2
|
import { useRef, useMemo } from "preact/hooks";
|
|
3
3
|
import {
|
|
4
4
|
signal,
|
|
@@ -49,41 +49,10 @@ function setCurrentUpdater(updater?: Updater) {
|
|
|
49
49
|
|
|
50
50
|
function createUpdater(updater: () => void) {
|
|
51
51
|
const s = signal(undefined) as Updater;
|
|
52
|
-
s._canActivate = true;
|
|
53
52
|
s._updater = updater;
|
|
54
53
|
return s;
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
// Get a (cached) Signal property updater for an element VNode
|
|
58
|
-
function getElementUpdater(vnode: VNode) {
|
|
59
|
-
let updater = updaterForComponent.get(vnode) as ElementUpdater;
|
|
60
|
-
if (!updater) {
|
|
61
|
-
let signalProps: Array<{ _key: string, _signal: Signal }> = [];
|
|
62
|
-
updater = createUpdater(() => {
|
|
63
|
-
let dom = vnode.__e as Element;
|
|
64
|
-
|
|
65
|
-
for (let i = 0; i < signalProps.length; i++) {
|
|
66
|
-
let { _key: prop, _signal: signal } = signalProps[i];
|
|
67
|
-
let value = signal._value;
|
|
68
|
-
if (!dom) return;
|
|
69
|
-
if (prop in dom) {
|
|
70
|
-
// @ts-ignore-next-line silly
|
|
71
|
-
dom[prop] = value;
|
|
72
|
-
} else if (value) {
|
|
73
|
-
dom.setAttribute(prop, value);
|
|
74
|
-
} else {
|
|
75
|
-
dom.removeAttribute(prop);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}) as ElementUpdater;
|
|
79
|
-
updater._props = signalProps;
|
|
80
|
-
updaterForComponent.set(vnode, updater);
|
|
81
|
-
} else {
|
|
82
|
-
updater._props.length = 0;
|
|
83
|
-
}
|
|
84
|
-
return updater;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
56
|
/** @todo This may be needed for complex prop value detection. */
|
|
88
57
|
// function isSignalValue(value: any): value is Signal {
|
|
89
58
|
// if (typeof value !== "object" || value == null) return false;
|
|
@@ -93,18 +62,6 @@ function getElementUpdater(vnode: VNode) {
|
|
|
93
62
|
// return false;
|
|
94
63
|
// }
|
|
95
64
|
|
|
96
|
-
/** Convert Signals within (nested) props.children into Text components */
|
|
97
|
-
function childToSignal<T>(child: any, i: keyof T, arr: T) {
|
|
98
|
-
if (typeof child !== "object" || child == null) {
|
|
99
|
-
// can't be a signal
|
|
100
|
-
} else if (Array.isArray(child)) {
|
|
101
|
-
child.forEach(childToSignal);
|
|
102
|
-
} else if (child instanceof Signal) {
|
|
103
|
-
// @ts-ignore-next-line yes, arr can accept VNodes:
|
|
104
|
-
arr[i] = createElement(Text, { data: child });
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
65
|
/**
|
|
109
66
|
* A wrapper component that renders a Signal directly as a Text node.
|
|
110
67
|
* @todo: in Preact 11, just decorate Signal with `type:null`
|
|
@@ -112,6 +69,11 @@ function childToSignal<T>(child: any, i: keyof T, arr: T) {
|
|
|
112
69
|
function Text(this: ComponentType, { data }: { data: Signal }) {
|
|
113
70
|
// hasComputeds.add(this);
|
|
114
71
|
|
|
72
|
+
// Store the props.data signal in another signal so that
|
|
73
|
+
// passing a new signal reference re-runs the text computed:
|
|
74
|
+
const currentSignal = useSignal(data);
|
|
75
|
+
currentSignal.value = data;
|
|
76
|
+
|
|
115
77
|
const s = useMemo(() => {
|
|
116
78
|
// mark the parent component as having computeds so it gets optimized
|
|
117
79
|
let v = this.__v;
|
|
@@ -128,6 +90,7 @@ function Text(this: ComponentType, { data }: { data: Signal }) {
|
|
|
128
90
|
};
|
|
129
91
|
|
|
130
92
|
return computed(() => {
|
|
93
|
+
let data = currentSignal.value;
|
|
131
94
|
let s = data.value;
|
|
132
95
|
return s === 0 ? 0 : s === true ? "" : s || "";
|
|
133
96
|
});
|
|
@@ -137,37 +100,37 @@ function Text(this: ComponentType, { data }: { data: Signal }) {
|
|
|
137
100
|
}
|
|
138
101
|
Text.displayName = "_st";
|
|
139
102
|
|
|
103
|
+
Object.defineProperties(Signal.prototype, {
|
|
104
|
+
constructor: { configurable: true },
|
|
105
|
+
type: { configurable: true, value: Text },
|
|
106
|
+
props: {
|
|
107
|
+
configurable: true,
|
|
108
|
+
get() {
|
|
109
|
+
return { data: this };
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:
|
|
113
|
+
// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77
|
|
114
|
+
// @todo remove this for Preact 11
|
|
115
|
+
__b: { configurable: true, value: 1 },
|
|
116
|
+
});
|
|
117
|
+
|
|
140
118
|
/** Inject low-level property/attribute bindings for Signals into Preact's diff */
|
|
141
119
|
hook(OptionsTypes.DIFF, (old, vnode) => {
|
|
142
120
|
if (typeof vnode.type === "string") {
|
|
143
|
-
|
|
144
|
-
let props = vnode.props;
|
|
145
|
-
let updater;
|
|
121
|
+
let signalProps: Record<string, any> | undefined;
|
|
146
122
|
|
|
123
|
+
let props = vnode.props;
|
|
147
124
|
for (let i in props) {
|
|
125
|
+
if (i === "children") continue;
|
|
126
|
+
|
|
148
127
|
let value = props[i];
|
|
149
|
-
if (
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!updater) updater = getElementUpdater(vnode);
|
|
154
|
-
// track which props are Signals for precise updates:
|
|
155
|
-
updater._props.push({ _key: i, _signal: value });
|
|
156
|
-
let newUpdater = updater._updater
|
|
157
|
-
if (value._updater) {
|
|
158
|
-
let oldUpdater = value._updater
|
|
159
|
-
value._updater = () => {
|
|
160
|
-
newUpdater();
|
|
161
|
-
oldUpdater();
|
|
162
|
-
}
|
|
163
|
-
} else {
|
|
164
|
-
value._updater = newUpdater
|
|
165
|
-
}
|
|
166
|
-
props[i] = value.peek()
|
|
128
|
+
if (value instanceof Signal) {
|
|
129
|
+
if (!signalProps) vnode.__np = signalProps = {};
|
|
130
|
+
signalProps[i] = value;
|
|
131
|
+
props[i] = value.peek();
|
|
167
132
|
}
|
|
168
133
|
}
|
|
169
|
-
|
|
170
|
-
setCurrentUpdater(updater);
|
|
171
134
|
}
|
|
172
135
|
|
|
173
136
|
old(vnode);
|
|
@@ -207,19 +170,77 @@ hook(OptionsTypes.CATCH_ERROR, (old, error, vnode, oldVNode) => {
|
|
|
207
170
|
hook(OptionsTypes.DIFFED, (old, vnode) => {
|
|
208
171
|
setCurrentUpdater();
|
|
209
172
|
currentComponent = undefined;
|
|
173
|
+
|
|
174
|
+
let dom: Element;
|
|
175
|
+
let updater: ElementUpdater;
|
|
176
|
+
|
|
177
|
+
// vnode._dom is undefined during string rendering,
|
|
178
|
+
// so we use this to skip prop subscriptions during SSR.
|
|
179
|
+
if (typeof vnode.type === "string" && (dom = vnode.__e as Element)) {
|
|
180
|
+
let props = vnode.__np;
|
|
181
|
+
if (props) {
|
|
182
|
+
// @ts-ignore-next
|
|
183
|
+
updater = dom._updater;
|
|
184
|
+
if (!updater) {
|
|
185
|
+
updater = createElementUpdater(dom);
|
|
186
|
+
// @ts-ignore-next
|
|
187
|
+
dom._updater = updater;
|
|
188
|
+
}
|
|
189
|
+
updater!._props = props;
|
|
190
|
+
setCurrentUpdater(updater);
|
|
191
|
+
// @ts-ignore-next we're adding an argument here
|
|
192
|
+
updater._updater(true);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
210
195
|
old(vnode);
|
|
211
196
|
});
|
|
212
197
|
|
|
198
|
+
// per-element updater for 1+ signal bindings
|
|
199
|
+
function createElementUpdater(dom: Element) {
|
|
200
|
+
const cache: Record<string, any> = { __proto__: null };
|
|
201
|
+
const updater = createUpdater((skip?: boolean) => {
|
|
202
|
+
const props = updater._props;
|
|
203
|
+
for (let prop in props) {
|
|
204
|
+
if (prop === "children") continue;
|
|
205
|
+
let signal = props[prop];
|
|
206
|
+
if (signal instanceof Signal) {
|
|
207
|
+
let value = signal.value;
|
|
208
|
+
let cached = cache[prop];
|
|
209
|
+
cache[prop] = value;
|
|
210
|
+
if (skip === true || cached === value) {
|
|
211
|
+
// this is just a subscribe run, not an update
|
|
212
|
+
} else if (prop in dom) {
|
|
213
|
+
// @ts-ignore-next-line silly
|
|
214
|
+
dom[prop] = value;
|
|
215
|
+
} else if (value) {
|
|
216
|
+
dom.setAttribute(prop, value);
|
|
217
|
+
} else {
|
|
218
|
+
dom.removeAttribute(prop);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}) as ElementUpdater;
|
|
223
|
+
return updater;
|
|
224
|
+
}
|
|
225
|
+
|
|
213
226
|
/** Unsubscribe from Signals when unmounting components/vnodes */
|
|
214
227
|
hook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {
|
|
215
|
-
let
|
|
216
|
-
const updater = updaterForComponent.get(
|
|
228
|
+
let component = vnode.__c;
|
|
229
|
+
const updater = component && updaterForComponent.get(component);
|
|
217
230
|
if (updater) {
|
|
218
|
-
updaterForComponent.delete(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
231
|
+
updaterForComponent.delete(component);
|
|
232
|
+
updater._setCurrent()(true, true);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (typeof vnode.type === "string") {
|
|
236
|
+
const dom = vnode.__e as Element;
|
|
237
|
+
|
|
238
|
+
// @ts-ignore-next
|
|
239
|
+
const updater = dom._updater;
|
|
240
|
+
if (updater) {
|
|
241
|
+
updater._setCurrent()(true, true);
|
|
242
|
+
// @ts-ignore-next
|
|
243
|
+
dom._updater = null;
|
|
223
244
|
}
|
|
224
245
|
}
|
|
225
246
|
old(vnode);
|
|
@@ -271,6 +292,7 @@ Component.prototype.shouldComponentUpdate = function (props, state) {
|
|
|
271
292
|
|
|
272
293
|
// if there is hook or class state, update:
|
|
273
294
|
if (hasHookState.has(this)) return true;
|
|
295
|
+
// @ts-ignore
|
|
274
296
|
for (let i in state) return true;
|
|
275
297
|
|
|
276
298
|
// if any non-Signal props changed, update:
|
package/src/internal.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export interface VNode<P = any> extends preact.VNode<P> {
|
|
|
8
8
|
__?: VNode;
|
|
9
9
|
/** The DOM node for this VNode */
|
|
10
10
|
__e?: Element | Text;
|
|
11
|
+
/** Props that had Signal values before diffing (used after diffing to subscribe) */
|
|
12
|
+
__np?: Record<string, any> | null;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export interface ComponentType extends Component {
|
|
@@ -18,7 +20,7 @@ export interface ComponentType extends Component {
|
|
|
18
20
|
export type Updater = Signal<unknown>;
|
|
19
21
|
|
|
20
22
|
export interface ElementUpdater extends Updater {
|
|
21
|
-
_props:
|
|
23
|
+
_props: Record<string, any>;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
export const enum OptionsTypes {
|
|
File without changes
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { signal, useComputed } from "@preact/signals";
|
|
2
|
-
import {
|
|
3
|
-
import { useMemo } from "preact/hooks";
|
|
2
|
+
import { createElement, render } from "preact";
|
|
4
3
|
import { setupRerender } from "preact/test-utils";
|
|
5
4
|
|
|
6
5
|
const sleep = (ms?: number) => new Promise(r => setTimeout(r, ms));
|
|
@@ -20,7 +19,7 @@ describe("@preact/signals", () => {
|
|
|
20
19
|
|
|
21
20
|
describe("Text bindings", () => {
|
|
22
21
|
it("should render text without signals", () => {
|
|
23
|
-
render(
|
|
22
|
+
render(<span>test</span>, scratch);
|
|
24
23
|
const span = scratch.firstChild;
|
|
25
24
|
const text = span?.firstChild;
|
|
26
25
|
expect(text).to.have.property("data", "test");
|
|
@@ -28,7 +27,7 @@ describe("@preact/signals", () => {
|
|
|
28
27
|
|
|
29
28
|
it("should render Signals as Text", () => {
|
|
30
29
|
const sig = signal("test");
|
|
31
|
-
render(
|
|
30
|
+
render(<span>{sig}</span>, scratch);
|
|
32
31
|
const span = scratch.firstChild;
|
|
33
32
|
expect(span).to.have.property("firstChild").that.is.an.instanceOf(Text);
|
|
34
33
|
const text = span?.firstChild;
|
|
@@ -37,7 +36,7 @@ describe("@preact/signals", () => {
|
|
|
37
36
|
|
|
38
37
|
it("should update Signal-based Text (no parent component)", () => {
|
|
39
38
|
const sig = signal("test");
|
|
40
|
-
render(
|
|
39
|
+
render(<span>{sig}</span>, scratch);
|
|
41
40
|
|
|
42
41
|
const text = scratch.firstChild!.firstChild!;
|
|
43
42
|
expect(text).to.have.property("data", "test");
|
|
@@ -50,12 +49,15 @@ describe("@preact/signals", () => {
|
|
|
50
49
|
expect(text).to.have.property("data", "changed");
|
|
51
50
|
});
|
|
52
51
|
|
|
53
|
-
it("should update Signal-based Text (in a parent component)", () => {
|
|
52
|
+
it("should update Signal-based Text (in a parent component)", async () => {
|
|
54
53
|
const sig = signal("test");
|
|
54
|
+
const spy = sinon.spy();
|
|
55
55
|
function App({ x }: { x: typeof sig }) {
|
|
56
|
-
|
|
56
|
+
spy();
|
|
57
|
+
return <span>{x}</span>;
|
|
57
58
|
}
|
|
58
|
-
render(
|
|
59
|
+
render(<App x={sig} />, scratch);
|
|
60
|
+
spy.resetHistory();
|
|
59
61
|
|
|
60
62
|
const text = scratch.firstChild!.firstChild!;
|
|
61
63
|
expect(text).to.have.property("data", "test");
|
|
@@ -66,6 +68,51 @@ describe("@preact/signals", () => {
|
|
|
66
68
|
expect(scratch.firstChild!.firstChild!).to.equal(text);
|
|
67
69
|
// should update the text in-place
|
|
68
70
|
expect(text).to.have.property("data", "changed");
|
|
71
|
+
|
|
72
|
+
await sleep();
|
|
73
|
+
expect(spy).not.to.have.been.called;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should support swapping Signals in Text positions", async () => {
|
|
77
|
+
const sig = signal("test");
|
|
78
|
+
const spy = sinon.spy();
|
|
79
|
+
function App({ x }: { x: typeof sig }) {
|
|
80
|
+
spy();
|
|
81
|
+
return <span>{x}</span>;
|
|
82
|
+
}
|
|
83
|
+
render(<App x={sig} />, scratch);
|
|
84
|
+
spy.resetHistory();
|
|
85
|
+
|
|
86
|
+
const text = scratch.firstChild!.firstChild!;
|
|
87
|
+
expect(text).to.have.property("data", "test");
|
|
88
|
+
|
|
89
|
+
const sig2 = signal("different");
|
|
90
|
+
render(<App x={sig2} />, scratch);
|
|
91
|
+
expect(spy).to.have.been.called;
|
|
92
|
+
spy.resetHistory();
|
|
93
|
+
|
|
94
|
+
// should not remount/replace Text
|
|
95
|
+
expect(scratch.firstChild!.firstChild!).to.equal(text);
|
|
96
|
+
// should update the text in-place
|
|
97
|
+
expect(text).to.have.property("data", "different");
|
|
98
|
+
|
|
99
|
+
await sleep();
|
|
100
|
+
expect(spy).not.to.have.been.called;
|
|
101
|
+
|
|
102
|
+
sig.value = "changed old signal";
|
|
103
|
+
|
|
104
|
+
await sleep();
|
|
105
|
+
expect(spy).not.to.have.been.called;
|
|
106
|
+
// the text should _not_ have changed:
|
|
107
|
+
expect(text).to.have.property("data", "different");
|
|
108
|
+
|
|
109
|
+
sig2.value = "changed";
|
|
110
|
+
|
|
111
|
+
expect(scratch.firstChild!.firstChild!).to.equal(text);
|
|
112
|
+
expect(text).to.have.property("data", "changed");
|
|
113
|
+
|
|
114
|
+
await sleep();
|
|
115
|
+
expect(spy).not.to.have.been.called;
|
|
69
116
|
});
|
|
70
117
|
});
|
|
71
118
|
|
|
@@ -75,10 +122,10 @@ describe("@preact/signals", () => {
|
|
|
75
122
|
|
|
76
123
|
function App() {
|
|
77
124
|
const value = sig.value;
|
|
78
|
-
return
|
|
125
|
+
return <p>{value}</p>;
|
|
79
126
|
}
|
|
80
127
|
|
|
81
|
-
render(
|
|
128
|
+
render(<App />, scratch);
|
|
82
129
|
expect(scratch.textContent).to.equal("foo");
|
|
83
130
|
|
|
84
131
|
sig.value = "bar";
|
|
@@ -98,10 +145,10 @@ describe("@preact/signals", () => {
|
|
|
98
145
|
});
|
|
99
146
|
|
|
100
147
|
const str = arr.value.join(", ");
|
|
101
|
-
return
|
|
148
|
+
return <p>{str}</p>;
|
|
102
149
|
}
|
|
103
150
|
|
|
104
|
-
const fn = () => render(
|
|
151
|
+
const fn = () => render(<App />, scratch);
|
|
105
152
|
expect(fn).not.to.throw;
|
|
106
153
|
});
|
|
107
154
|
|
|
@@ -110,50 +157,29 @@ describe("@preact/signals", () => {
|
|
|
110
157
|
|
|
111
158
|
function Child() {
|
|
112
159
|
const value = sig.value;
|
|
113
|
-
return
|
|
160
|
+
return <p>{value}</p>;
|
|
114
161
|
}
|
|
115
162
|
|
|
116
163
|
const spy = sinon.spy();
|
|
117
164
|
function App() {
|
|
118
165
|
spy();
|
|
119
|
-
return
|
|
166
|
+
return <Child />;
|
|
120
167
|
}
|
|
121
168
|
|
|
122
|
-
render(
|
|
169
|
+
render(<App />, scratch);
|
|
123
170
|
expect(scratch.textContent).to.equal("foo");
|
|
124
171
|
|
|
125
172
|
sig.value = "bar";
|
|
126
173
|
rerender();
|
|
127
174
|
expect(spy).to.be.calledOnce;
|
|
128
175
|
});
|
|
129
|
-
|
|
130
|
-
it("should update memo'ed component via signals", async () => {
|
|
131
|
-
const sig = signal("foo");
|
|
132
|
-
|
|
133
|
-
function Inner() {
|
|
134
|
-
const value = sig.value;
|
|
135
|
-
return h("p", null, value);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function App() {
|
|
139
|
-
sig.value;
|
|
140
|
-
return useMemo(() => h(Inner, { foo: 1 }), []);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
render(h(App, {}), scratch);
|
|
144
|
-
expect(scratch.textContent).to.equal("foo");
|
|
145
|
-
|
|
146
|
-
sig.value = "bar";
|
|
147
|
-
rerender();
|
|
148
|
-
expect(scratch.textContent).to.equal("bar");
|
|
149
|
-
});
|
|
150
176
|
});
|
|
151
177
|
|
|
152
178
|
describe("prop bindings", () => {
|
|
153
179
|
it("should set the initial value of the checked property", () => {
|
|
154
180
|
const s = signal(true);
|
|
155
181
|
// @ts-ignore
|
|
156
|
-
render(
|
|
182
|
+
render(<input checked={s} />, scratch);
|
|
157
183
|
|
|
158
184
|
expect(scratch.firstChild).to.have.property("checked", true);
|
|
159
185
|
expect(s.value).to.equal(true);
|
|
@@ -162,7 +188,7 @@ describe("@preact/signals", () => {
|
|
|
162
188
|
it("should update the checked property on change", () => {
|
|
163
189
|
const s = signal(true);
|
|
164
190
|
// @ts-ignore
|
|
165
|
-
render(
|
|
191
|
+
render(<input checked={s} />, scratch);
|
|
166
192
|
|
|
167
193
|
expect(scratch.firstChild).to.have.property("checked", true);
|
|
168
194
|
|
|
@@ -177,9 +203,9 @@ describe("@preact/signals", () => {
|
|
|
177
203
|
function Wrap() {
|
|
178
204
|
spy();
|
|
179
205
|
// @ts-ignore
|
|
180
|
-
return
|
|
206
|
+
return <input value={s} />;
|
|
181
207
|
}
|
|
182
|
-
render(
|
|
208
|
+
render(<Wrap />, scratch);
|
|
183
209
|
spy.resetHistory();
|
|
184
210
|
|
|
185
211
|
expect(scratch.firstChild).to.have.property("value", "initial");
|
|
@@ -207,9 +233,9 @@ describe("@preact/signals", () => {
|
|
|
207
233
|
function Wrap() {
|
|
208
234
|
spy();
|
|
209
235
|
// @ts-ignore
|
|
210
|
-
return
|
|
236
|
+
return <div style={style} />;
|
|
211
237
|
}
|
|
212
|
-
render(
|
|
238
|
+
render(<Wrap />, scratch);
|
|
213
239
|
spy.resetHistory();
|
|
214
240
|
|
|
215
241
|
const div = scratch.firstChild as HTMLDivElement;
|