@preact/signals-react 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @preact/signals-react
2
2
 
3
+ ## 1.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#243](https://github.com/preactjs/signals/pull/243) [`e41b8b1`](https://github.com/preactjs/signals/commit/e41b8b16bf68da7004a3174912fe95a109a453ed) Thanks [@melnikov-s](https://github.com/melnikov-s)! - Replace `Map` useage with `WeakMap`
8
+
9
+ * [#282](https://github.com/preactjs/signals/pull/282) [`cafbdaa`](https://github.com/preactjs/signals/commit/cafbdaabd525a034e38da10b04eee0688c026152) Thanks [@developit](https://github.com/developit)! - Fix a bug that caused cleanup functions returned from a `useSignalEffect()` callback not to be called.
10
+
11
+ * Updated dependencies [[`7e15d3c`](https://github.com/preactjs/signals/commit/7e15d3cf5f5e66258105e6f27cd7838b52fbbf9f)]:
12
+ - @preact/signals-core@1.2.3
13
+
14
+ ## 1.2.1
15
+
16
+ ### Patch Changes
17
+
18
+ - [#238](https://github.com/preactjs/signals/pull/238) [`bcf4b0b`](https://github.com/preactjs/signals/commit/bcf4b0b25d774483ddafa29c2fa133c467668b8c) Thanks [@eddyw](https://github.com/eddyw)! - Fix ERR_UNSUPPORTED_DIR_IMPORT error when importing `use-sync-external-store/shim` from ESM build
19
+
3
20
  ## 1.2.0
4
21
 
5
22
  ### Minor Changes
package/README.md CHANGED
@@ -40,7 +40,7 @@ import { signal } from "@preact/signals-react";
40
40
  const count = signal(0);
41
41
 
42
42
  function CounterValue() {
43
- // Whenver the `count` signal is updated, we'll
43
+ // Whenever the `count` signal is updated, we'll
44
44
  // re-render this component automatically for you
45
45
  return <p>Value: {count.value}</p>;
46
46
  }
package/dist/signals.js CHANGED
@@ -1 +1 @@
1
- var r=require("react"),n=require("react/jsx-runtime"),t=require("react/jsx-dev-runtime"),e=require("use-sync-external-store/shim"),u=require("@preact/signals-core");function o(r){return r&&"object"==typeof r&&"default"in r?r.default:r}var i=/*#__PURE__*/o(r),f=/*#__PURE__*/o(n),c=/*#__PURE__*/o(t),a=[],s=Symbol.for("react.element"),p=Symbol.for("react.memo"),l=new Map,v="function"==typeof Proxy,y={apply:function(n,t,u){var o=r.useMemo(b,a);e.useSyncExternalStore(o.subscribe,o.getSnapshot,o.getSnapshot);var i=o.updater.S();try{return n.apply(t,u)}catch(r){throw r}finally{i()}}};function x(r){return l.get(r)||function(r){if(v){var n=new Proxy(r,y);l.set(r,n);l.set(n,n);return n}var t=function(){return y.apply(r,void 0,arguments)};l.set(r,t);l.set(t,t);return t}(r)}function b(){var r,n,t=0,e=u.effect(function(){r=this});r.c=function(){t=t+1|0;if(n)n()};return{updater:r,subscribe:function(r){n=r;return function(){t=t+1|0;n=void 0;e()}},getSnapshot:function(){return t}}}function g(n){if("function"!=typeof n)return n;else return function(t,e){var o=[].slice.call(arguments,2);if("function"==typeof t&&!(t instanceof r.Component))return n.call.apply(n,[n,x(t),e].concat(o));if(t&&"object"==typeof t&&t.$$typeof===p){t.type=x(t.type);return n.call.apply(n,[n,t,e].concat(o))}if("string"==typeof t&&e)for(var i in e){var f=e[i];if("children"!==i&&f instanceof u.Signal)e[i]=f.value}return n.call.apply(n,[n,t,e].concat(o))}}var m=f,d=c;i.createElement=g(i.createElement);d.jsx&&(d.jsx=g(d.jsx));m.jsx&&(m.jsx=g(m.jsx));d.jsxs&&(d.jsxs=g(d.jsxs));m.jsxs&&(m.jsxs=g(m.jsxs));d.jsxDEV&&(d.jsxDEV=g(d.jsxDEV));m.jsxDEV&&(m.jsxDEV=g(m.jsxDEV));Object.defineProperties(u.Signal.prototype,{$$typeof:{configurable:!0,value:s},type:{configurable:!0,value:x(function(r){return r.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});exports.Signal=u.Signal;exports.batch=u.batch;exports.computed=u.computed;exports.effect=u.effect;exports.signal=u.signal;exports.useComputed=function(n){var t=r.useRef(n);t.current=n;return r.useMemo(function(){return u.computed(function(){return t.current()})},a)};exports.useSignal=function(n){return r.useMemo(function(){return u.signal(n)},a)};exports.useSignalEffect=function(n){var t=r.useRef(n);t.current=n;r.useEffect(function(){return u.effect(function(){return t.current()})},a)};//# sourceMappingURL=signals.js.map
1
+ var r=require("react"),n=require("react/jsx-runtime"),t=require("react/jsx-dev-runtime"),e=require("use-sync-external-store/shim/index.js"),u=require("@preact/signals-core");function o(r){return r&&"object"==typeof r&&"default"in r?r.default:r}var i=/*#__PURE__*/o(r),f=/*#__PURE__*/o(n),c=/*#__PURE__*/o(t),a=[],s=Symbol.for("react.element"),p=Symbol.for("react.memo"),l=new WeakMap,v="function"==typeof Proxy,y={apply:function(n,t,u){var o=r.useMemo(b,a);e.useSyncExternalStore(o.subscribe,o.getSnapshot,o.getSnapshot);var i=o.updater.S();try{return n.apply(t,u)}catch(r){throw r}finally{i()}}};function x(r){return l.get(r)||function(r){if(v){var n=new Proxy(r,y);l.set(r,n);l.set(n,n);return n}var t=function(){return y.apply(r,void 0,arguments)};l.set(r,t);l.set(t,t);return t}(r)}function b(){var r,n,t=0,e=u.effect(function(){r=this});r.c=function(){t=t+1|0;if(n)n()};return{updater:r,subscribe:function(r){n=r;return function(){t=t+1|0;n=void 0;e()}},getSnapshot:function(){return t}}}function d(n){if("function"!=typeof n)return n;else return function(t,e){var o=[].slice.call(arguments,2);if("function"==typeof t&&!(t instanceof r.Component))return n.call.apply(n,[n,x(t),e].concat(o));if(t&&"object"==typeof t&&t.$$typeof===p){t.type=x(t.type);return n.call.apply(n,[n,t,e].concat(o))}if("string"==typeof t&&e)for(var i in e){var f=e[i];if("children"!==i&&f instanceof u.Signal)e[i]=f.value}return n.call.apply(n,[n,t,e].concat(o))}}var g=f,m=c;i.createElement=d(i.createElement);m.jsx&&(m.jsx=d(m.jsx));g.jsx&&(g.jsx=d(g.jsx));m.jsxs&&(m.jsxs=d(m.jsxs));g.jsxs&&(g.jsxs=d(g.jsxs));m.jsxDEV&&(m.jsxDEV=d(m.jsxDEV));g.jsxDEV&&(g.jsxDEV=d(g.jsxDEV));Object.defineProperties(u.Signal.prototype,{$$typeof:{configurable:!0,value:s},type:{configurable:!0,value:x(function(r){return r.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});exports.Signal=u.Signal;exports.batch=u.batch;exports.computed=u.computed;exports.effect=u.effect;exports.signal=u.signal;exports.useComputed=function(n){var t=r.useRef(n);t.current=n;return r.useMemo(function(){return u.computed(function(){return t.current()})},a)};exports.useSignal=function(n){return r.useMemo(function(){return u.signal(n)},a)};exports.useSignalEffect=function(n){var t=r.useRef(n);t.current=n;r.useEffect(function(){return u.effect(function(){return t.current()})},a)};//# sourceMappingURL=signals.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new Map<FunctionComponent<any>, FunctionComponent<any>>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\treturn callback.current();\n\t\t});\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","Map","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","version","unsubscribe","effect","this","_callback","onStoreChange","WrapJsx","jsx","type","props","slice","call","rest","$$typeof","i","v","Signal","value","concat","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","exports","signalsCore","batch","computed","signal","useComputed","compute","$compute","useRef","current","useSignal","useSignalEffect","cb","callback","useEffect"],"mappings":"2SAuBWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,IACPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAAA,QAAQC,EAAmBZ,GAEzCa,EAAoBA,qBAACC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC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EAACE,SAAf,EAAlB,EAA6C3E,EAC5D,EAWAkE,QAAAU,UAnBK,SAAuB5B,GAC5B,SAAcrC,QAAC,WAAA,gBAAgBqC,EAAhB,EAAwBhD,EACvC,EAiBAkE,QAAAW,gBATeA,SAAgBC,GAC/B,IAAMC,EAAWL,EAAMA,OAACI,GACxBC,EAASJ,QAAUG,EAEnBE,EAAAA,UAAU,WACT,OAAO/C,EAAAA,OAAO,WACb,OAAO8C,EAASJ,SAChB,EACD,EAAE3E,EACH"}
1
+ {"version":3,"file":"signals.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim/index.js\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new WeakMap<\n\tFunctionComponent<any>,\n\tFunctionComponent<any>\n>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => callback.current());\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","WeakMap","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","unsubscribe","effect","this","_callback","version","onStoreChange","WrapJsx","jsx","type","props","rest","slice","call","concat","$$typeof","v","i","Signal","value","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","exports","signalsCore","batch","computed","signal","useComputed","compute","useRef","$compute","current","useSignal","useSignalEffect","cb","callback","useEffect"],"mappings":"oTAuBWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,QAIPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAAA,QAAQC,EAAmBZ,GAEzCa,EAAoBA,qBAACC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC,OADiBV,EAAUD,MAAME,EAASC,EAU1C,CARC,MAAOS,GAGR,MAAMA,CACN,CAPD,QAUCC,GACA,CACD,GAGF,SAASC,EAAyBb,GACjC,SAAqBc,IAAId,IAE1B,SAAuBA,GACtB,GAAIH,EAAe,CAClB,IAAMkB,EAAiB,IAAIjB,MAAME,E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EAEnBE,EAAAA,UAAU,WACT,OAAOhD,EAAAA,OAAO,WAAM+C,OAAAA,EAASJ,SAAf,EACd,EAAE3E,EACH"}
@@ -1 +1 @@
1
- !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react/jsx-runtime"),require("react/jsx-dev-runtime"),require("use-sync-external-store/shim"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","react","react/jsx-runtime","react/jsx-dev-runtime","use-sync-external-store/shim","@preact/signals-core"],e):e((n||self).reactSignals={},n.react,n.jsxRuntime,n.jsxRuntimeDev,n.shim,n.signalsCore)}(this,function(n,e,r,t,u,i){function o(n){return n&&"object"==typeof n&&"default"in n?n.default:n}var f=/*#__PURE__*/o(e),c=/*#__PURE__*/o(r),a=/*#__PURE__*/o(t),s=[],l=Symbol.for("react.element"),p=Symbol.for("react.memo"),y=new Map,v="function"==typeof Proxy,d={apply:function(n,r,t){var i=e.useMemo(m,s);u.useSyncExternalStore(i.subscribe,i.getSnapshot,i.getSnapshot);var o=i.updater.S();try{return n.apply(r,t)}catch(n){throw n}finally{o()}}};function b(n){return y.get(n)||function(n){if(v){var e=new Proxy(n,d);y.set(n,e);y.set(e,e);return e}var r=function(){return d.apply(n,void 0,arguments)};y.set(n,r);y.set(r,r);return r}(n)}function m(){var n,e,r=0,t=i.effect(function(){n=this});n.c=function(){r=r+1|0;if(e)e()};return{updater:n,subscribe:function(n){e=n;return function(){r=r+1|0;e=void 0;t()}},getSnapshot:function(){return r}}}function g(n){if("function"!=typeof n)return n;else return function(r,t){var u=[].slice.call(arguments,2);if("function"==typeof r&&!(r instanceof e.Component))return n.call.apply(n,[n,b(r),t].concat(u));if(r&&"object"==typeof r&&r.$$typeof===p){r.type=b(r.type);return n.call.apply(n,[n,r,t].concat(u))}if("string"==typeof r&&t)for(var o in t){var f=t[o];if("children"!==o&&f instanceof i.Signal)t[o]=f.value}return n.call.apply(n,[n,r,t].concat(u))}}var h=c,x=a;f.createElement=g(f.createElement);x.jsx&&(x.jsx=g(x.jsx));h.jsx&&(h.jsx=g(h.jsx));x.jsxs&&(x.jsxs=g(x.jsxs));h.jsxs&&(h.jsxs=g(h.jsxs));x.jsxDEV&&(x.jsxDEV=g(x.jsxDEV));h.jsxDEV&&(h.jsxDEV=g(h.jsxDEV));Object.defineProperties(i.Signal.prototype,{$$typeof:{configurable:!0,value:l},type:{configurable:!0,value:b(function(n){return n.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});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=e.useRef(n);r.current=n;return e.useMemo(function(){return i.computed(function(){return r.current()})},s)};n.useSignal=function(n){return e.useMemo(function(){return i.signal(n)},s)};n.useSignalEffect=function(n){var r=e.useRef(n);r.current=n;e.useEffect(function(){return i.effect(function(){return r.current()})},s)}});//# sourceMappingURL=signals.min.js.map
1
+ !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react/jsx-runtime"),require("react/jsx-dev-runtime"),require("use-sync-external-store/shim/index.js"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","react","react/jsx-runtime","react/jsx-dev-runtime","use-sync-external-store/shim/index.js","@preact/signals-core"],e):e((n||self).reactSignals={},n.react,n.jsxRuntime,n.jsxRuntimeDev,n.index_js,n.signalsCore)}(this,function(n,e,r,t,u,i){function o(n){return n&&"object"==typeof n&&"default"in n?n.default:n}var f=/*#__PURE__*/o(e),c=/*#__PURE__*/o(r),a=/*#__PURE__*/o(t),s=[],l=Symbol.for("react.element"),p=Symbol.for("react.memo"),y=new WeakMap,v="function"==typeof Proxy,d={apply:function(n,r,t){var i=e.useMemo(x,s);u.useSyncExternalStore(i.subscribe,i.getSnapshot,i.getSnapshot);var o=i.updater.S();try{return n.apply(r,t)}catch(n){throw n}finally{o()}}};function b(n){return y.get(n)||function(n){if(v){var e=new Proxy(n,d);y.set(n,e);y.set(e,e);return e}var r=function(){return d.apply(n,void 0,arguments)};y.set(n,r);y.set(r,r);return r}(n)}function x(){var n,e,r=0,t=i.effect(function(){n=this});n.c=function(){r=r+1|0;if(e)e()};return{updater:n,subscribe:function(n){e=n;return function(){r=r+1|0;e=void 0;t()}},getSnapshot:function(){return r}}}function m(n){if("function"!=typeof n)return n;else return function(r,t){var u=[].slice.call(arguments,2);if("function"==typeof r&&!(r instanceof e.Component))return n.call.apply(n,[n,b(r),t].concat(u));if(r&&"object"==typeof r&&r.$$typeof===p){r.type=b(r.type);return n.call.apply(n,[n,r,t].concat(u))}if("string"==typeof r&&t)for(var o in t){var f=t[o];if("children"!==o&&f instanceof i.Signal)t[o]=f.value}return n.call.apply(n,[n,r,t].concat(u))}}var g=c,h=a;f.createElement=m(f.createElement);h.jsx&&(h.jsx=m(h.jsx));g.jsx&&(g.jsx=m(g.jsx));h.jsxs&&(h.jsxs=m(h.jsxs));g.jsxs&&(g.jsxs=m(g.jsxs));h.jsxDEV&&(h.jsxDEV=m(h.jsxDEV));g.jsxDEV&&(g.jsxDEV=m(g.jsxDEV));Object.defineProperties(i.Signal.prototype,{$$typeof:{configurable:!0,value:l},type:{configurable:!0,value:b(function(n){return n.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});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=e.useRef(n);r.current=n;return e.useMemo(function(){return i.computed(function(){return r.current()})},s)};n.useSignal=function(n){return e.useMemo(function(){return i.signal(n)},s)};n.useSignalEffect=function(n){var r=e.useRef(n);r.current=n;e.useEffect(function(){return i.effect(function(){return r.current()})},s)}});//# sourceMappingURL=signals.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new Map<FunctionComponent<any>, FunctionComponent<any>>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\treturn callback.current();\n\t\t});\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","Map","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","version","unsubscribe","effect","this","_callback","onStoreChange","WrapJsx","jsx","type","props","slice","call","rest","$$typeof","i","v","Signal","value","concat","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","exports","signalsCore","batch","computed","signal","useComputed","compute","$compute","useRef","current","useSignal","useSignalEffect","cb","callback","useEffect"],"mappings":"irBAuBWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,IACPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAAA,QAAQC,EAAmBZ,GAEzCa,EAAoBA,qBAACC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC,OADiBV,EAAUD,MAAME,EAASC,EAU1C,CARC,MAAOS,GAGR,MAAMA,CACN,CAPD,QAUCC,GACA,CACD,GAGF,SAASC,EAAyBb,GACjC,SAAqBc,IAAId,IAE1B,SAAuBA,GACtB,GAAIH,EAAe,CAClB,IAAMkB,EAAiB,IAAIjB,MAAME,EAAWgB,GAE5CrB,EAAcsB,IAAIjB,EAAWe,GAC7BpB,EAAcsB,IAAIF,EAAgBA,GAElC,OACAA,CAAA,CAgBD,IAAsBG,EAAG,WACxB,OAAoBF,EAACjB,MAAMC,OAAWmB,EAAWC,UACjD,EACDzB,EAAcsB,IAAIjB,EAAWkB,GAC7BvB,EAAcsB,IAAIC,EAAkBA,GAEpC,OAAOA,CACP,CAjCuCG,CAAcrB,EACrD,CA+CD,SAASI,IACR,IAAAK,EAEAa,EADIC,EAAU,EAGCC,EAAGC,SAAO,WACxBhB,EAAUiB,IACV,GACDjB,EAAQkB,EAAY,WACnBJ,EAAWA,EAAU,EAAK,EAC1B,GAAID,EAAqBA,GACzB,EAED,MAAO,CACNb,QAAAA,E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AAA,OAAOa,UAAW,CACzChB,SAAU,CAAEiB,cAAc,EAAMb,MAAOc,GACvCvB,KAAM,CAAEsB,cAAc,EAAMb,MAAO3B,EAPpC,SAAA0C,GACC,OADuCA,EAAxBC,KACHhB,KACZ,IAMAR,MAAO,CACNqB,cAAc,EACdvC,eACC,MAAO,CAAE0C,KAAM9B,KACf,GAEF+B,IAAK,CAAEJ,cAAc,EAAMb,MAAO,QAsBlCkB,EAAAnB,OAAAoB,EAAApB,OAAAmB,EAAAE,MAAAD,EAAAC,MAAAF,EAAAG,SAAAF,EAAAE,SAAAH,EAAAjC,OAAAkC,EAAAlC,OAAAiC,EAAAI,OAAAH,EAAAG,OAAAJ,EAAAK,YAfK,SAAyBC,GAC9B,IAAcC,EAAGC,EAAMA,OAACF,GACxBC,EAASE,QAAUH,EACnB,OAAO7D,UAAQ,WAAM0D,OAAAA,EAAAA,SAAY,WAAA,OAAcI,EAACE,SAAf,EAAlB,EAA6C3E,EAC5D,EAWAkE,EAAAU,UAnBK,SAAuB5B,GAC5B,SAAcrC,QAAC,WAAA,gBAAgBqC,EAAhB,EAAwBhD,EACvC,EAiBAkE,EAAAW,gBATeA,SAAgBC,GAC/B,IAAMC,EAAWL,EAAMA,OAACI,GACxBC,EAASJ,QAAUG,EAEnBE,EAAAA,UAAU,WACT,OAAO/C,EAAAA,OAAO,WACb,OAAO8C,EAASJ,SAChB,EACD,EAAE3E,EACH,CAAA"}
1
+ {"version":3,"file":"signals.min.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim/index.js\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new WeakMap<\n\tFunctionComponent<any>,\n\tFunctionComponent<any>\n>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => callback.current());\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","WeakMap","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","unsubscribe","effect","this","_callback","version","onStoreChange","WrapJsx","jsx","type","props","rest","slice","call","concat","$$typeof","v","i","Signal","value","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","exports","signalsCore","batch","computed","signal","useComputed","compute","useRef","$compute","current","useSignal","useSignalEffect","cb","callback","useEffect"],"mappings":"usBAuBWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,QAIPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAAA,QAAQC,EAAmBZ,GAEzCa,EAAoBA,qBAACC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC,OADiBV,EAAUD,MAAME,EAASC,EAU1C,CARC,MAAOS,GAGR,MAAMA,CACN,CAPD,QAUCC,GACA,CACD,GAGF,SAASC,EAAyBb,GACjC,SAAqBc,IAAId,IAE1B,SAAuBA,GACtB,GAAIH,EAAe,CAClB,IAAMkB,EAAiB,IAAIjB,MAAME,EAAWgB,GAE5CrB,EAAcsB,IAAIjB,EAAWe,GAC7BpB,EAAcsB,IAAIF,EAAgBA,GAElC,OACAA,CAAA,CAgBD,IAAsBG,EAAG,WACxB,OAAOF,EAAcjB,MAAMC,OAAWmB,EAAWC,UACjD,EACDzB,EAAcsB,IAAIjB,EAAWkB,GAC7BvB,EAAcsB,IAAIC,EAAkBA,GAEpC,QACA,CAjCuCG,CAAcrB,EACrD,CA+CD,aACC,IAAIS,EAEAa,IADU,EAGVC,EAAcC,SAAO,WACxBf,EAAUgB,IACV,GACDhB,EAAQiB,EAAY,WACnBC,EAAWA,EAAU,EAAK,EAC1B,GAAIL,EAAqBA,GACzB,EAED,MAAO,CACNb,QAAAA,EACAF,UAFM,SAEIqB,GACTN,EAAsBM,EAEtB,kBAWCD,EAAWA,EAAU,EAAK,EAC1BL,OAAsBH,EACtBI,GACA,CACD,EACDf,uBACC,QACA,EAEF,CAED,SAASqB,EAAWC,GACnB,GAAmB,qBAAY,OAAOA,OAEtC,OAAiBC,SAAAA,EAAWC,OAA0BC,EAA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B,KACf,GAEFgC,IAAK,CAAEJ,cAAc,EAAMZ,MAAO,QAoBlCiB,EAAAlB,OAAAmB,EAAAnB,OAAAkB,EAAAE,MAAAD,EAAAC,MAAAF,EAAAG,SAAAF,EAAAE,SAAAH,EAAAlC,OAAAmC,EAAAnC,OAAAkC,EAAAI,OAAAH,EAAAG,OAAAJ,EAAAK,YAbK,SAAyBC,GAC9B,MAAiBC,EAAMA,OAACD,GACxBE,EAASC,QAAUH,EACnB,OAAc7D,UAAC,WAAM0D,OAAAA,EAAAA,SAAY,WAAA,SAAeM,SAAf,EAAlB,EAA6C3E,EAC5D,EASAkE,EAAAU,UAjBeA,SAAa3B,GAC5B,OAAOtC,EAAOA,QAAC,WAAM2D,OAAAA,EAAAA,OAAUrB,EAAhB,EAAwBjD,EACvC,EAeAkE,EAAAW,yBAP+BC,GAC/B,IAAcC,EAAGN,EAAMA,OAACK,GACxBC,EAASJ,QAAUG,EAEnBE,EAAAA,UAAU,WACT,OAAOhD,EAAAA,OAAO,WAAM+C,OAAAA,EAASJ,SAAf,EACd,EAAE3E,EACH,CAAA"}
package/dist/signals.mjs CHANGED
@@ -1 +1 @@
1
- import t,{Component as n,useMemo as r,useRef as e,useEffect as o}from"react";import i from"react/jsx-runtime";import c from"react/jsx-dev-runtime";import{useSyncExternalStore as f}from"use-sync-external-store/shim";import{Signal as u,signal as a,computed as s,effect as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const p=[],m=Symbol.for("react.element"),y=Symbol.for("react.memo"),b=new Map,g="function"==typeof Proxy,d={apply(t,n,e){const o=r(x,p);f(o.subscribe,o.getSnapshot,o.getSnapshot);const i=o.updater.S();try{return t.apply(n,e)}catch(t){throw t}finally{i()}}};function h(t){return b.get(t)||function(t){if(g){const n=new Proxy(t,d);b.set(t,n);b.set(n,n);return n}const n=function(){return d.apply(t,void 0,arguments)};b.set(t,n);b.set(n,n);return n}(t)}function x(){let t,n,r=0,e=l(function(){t=this});t.c=function(){r=r+1|0;if(n)n()};return{updater:t,subscribe(t){n=t;return function(){r=r+1|0;n=void 0;e()}},getSnapshot:()=>r}}function v(t){if("function"!=typeof t)return t;else return function(r,e,...o){if("function"==typeof r&&!(r instanceof n))return t.call(t,h(r),e,...o);if(r&&"object"==typeof r&&r.$$typeof===y){r.type=h(r.type);return t.call(t,r,e,...o)}if("string"==typeof r&&e)for(let t in e){let n=e[t];if("children"!==t&&n instanceof u)e[t]=n.value}return t.call(t,r,e,...o)}}const j=i,S=c;t.createElement=v(t.createElement);S.jsx&&(S.jsx=v(S.jsx));j.jsx&&(j.jsx=v(j.jsx));S.jsxs&&(S.jsxs=v(S.jsxs));j.jsxs&&(j.jsxs=v(j.jsxs));S.jsxDEV&&(S.jsxDEV=v(S.jsxDEV));j.jsxDEV&&(j.jsxDEV=v(j.jsxDEV));Object.defineProperties(u.prototype,{$$typeof:{configurable:!0,value:m},type:{configurable:!0,value:h(function({data:t}){return t.value})},props:{configurable:!0,get(){return{data:this}}},ref:{configurable:!0,value:null}});function useSignal(t){return r(()=>a(t),p)}function useComputed(t){const n=e(t);n.current=t;return r(()=>s(()=>n.current()),p)}function useSignalEffect(t){const n=e(t);n.current=t;o(()=>l(()=>n.current()),p)}export{useComputed,useSignal,useSignalEffect};//# sourceMappingURL=signals.mjs.map
1
+ import t,{Component as n,useMemo as e,useRef as r,useEffect as o}from"react";import i from"react/jsx-runtime";import c from"react/jsx-dev-runtime";import{useSyncExternalStore as f}from"use-sync-external-store/shim/index.js";import{Signal as u,signal as a,computed as s,effect as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";const p=[],m=Symbol.for("react.element"),y=Symbol.for("react.memo"),b=new WeakMap,g="function"==typeof Proxy,d={apply(t,n,r){const o=e(x,p);f(o.subscribe,o.getSnapshot,o.getSnapshot);const i=o.updater.S();try{return t.apply(n,r)}catch(t){throw t}finally{i()}}};function h(t){return b.get(t)||function(t){if(g){const n=new Proxy(t,d);b.set(t,n);b.set(n,n);return n}const n=function(){return d.apply(t,void 0,arguments)};b.set(t,n);b.set(n,n);return n}(t)}function x(){let t,n,e=0,r=l(function(){t=this});t.c=function(){e=e+1|0;if(n)n()};return{updater:t,subscribe(t){n=t;return function(){e=e+1|0;n=void 0;r()}},getSnapshot:()=>e}}function v(t){if("function"!=typeof t)return t;else return function(e,r,...o){if("function"==typeof e&&!(e instanceof n))return t.call(t,h(e),r,...o);if(e&&"object"==typeof e&&e.$$typeof===y){e.type=h(e.type);return t.call(t,e,r,...o)}if("string"==typeof e&&r)for(let t in r){let n=r[t];if("children"!==t&&n instanceof u)r[t]=n.value}return t.call(t,e,r,...o)}}const j=i,S=c;t.createElement=v(t.createElement);S.jsx&&(S.jsx=v(S.jsx));j.jsx&&(j.jsx=v(j.jsx));S.jsxs&&(S.jsxs=v(S.jsxs));j.jsxs&&(j.jsxs=v(j.jsxs));S.jsxDEV&&(S.jsxDEV=v(S.jsxDEV));j.jsxDEV&&(j.jsxDEV=v(j.jsxDEV));Object.defineProperties(u.prototype,{$$typeof:{configurable:!0,value:m},type:{configurable:!0,value:h(function({data:t}){return t.value})},props:{configurable:!0,get(){return{data:this}}},ref:{configurable:!0,value:null}});function useSignal(t){return e(()=>a(t),p)}function useComputed(t){const n=r(t);n.current=t;return e(()=>s(()=>n.current()),p)}function useSignalEffect(t){const n=r(t);n.current=t;o(()=>l(()=>n.current()),p)}export{useComputed,useSignal,useSignalEffect};//# sourceMappingURL=signals.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new Map<FunctionComponent<any>, FunctionComponent<any>>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\treturn callback.current();\n\t\t});\n\t}, Empty);\n}\n"],"names":["React","Component","useMemo","useRef","useEffect","jsxRuntime","jsxRuntimeDev","useSyncExternalStore","Signal","signal","computed","effect","batch","Empty","Symbol","for","ReactMemoType","ProxyInstance","Map","ProxyHandlers","apply","thisArg","argumentsList","store","createEffectStore","subscribe","getSnapshot","stop","updater","_start","e","get","SupportsProxy","ProxyComponent","Proxy","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","unsubscribe","this","_callback","version","onStoreChange","jsx","type","props","rest","call","ProxyFunctionalComponent","$$typeof","v","i","value","JsxDev","createElement","WrapJsx","JsxPro","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","data","ref","compute","$compute","current","useSignalEffect","cb","callback","useComputed","useSignal"],"mappings":"OAuBAA,gBAAAC,aAAAC,YAAAC,eAAAC,MAAA,eAAAC,MAAA,2BAAAC,MAAA,uDAAAC,MAAA,gDAAAC,YAAAC,cAAAC,YAAAC,MAAA,8BAAAH,OAAAI,MAAAF,SAAAC,OAAAF,WAAA,uBAAA,MAAWI,EAAG,KACQC,OAAOC,IAAI,iBAC3BC,EAAgBF,OAAOC,IAAI,cACdE,EAAG,IAAIC,MACa,yBAEjCC,EAAgB,CAYrBC,MAAMnB,EAA8BoB,EAAcC,GACjD,MAAMC,EAAQrB,EAAQsB,EAAmBX,GAEzCN,EAAqBgB,EAAME,UAAWF,EAAMG,YAAaH,EAAMG,aAE/D,MAAMC,EAAOJ,EAAMK,QAAQC,IAE3B,IAEC,OADiB5B,EAAUmB,MAAMC,EAASC,EAU1C,CARC,MAAOQ,GAGR,MAAMA,CACN,CAPD,QAUCH,GACA,CACD,GAGF,WAAkC1B,GACjC,SAAqB8B,IAAI9B,IAE1B,SAAuBA,GACtB,GAAI+B,EAAe,CAClB,MAAMC,EAAiB,IAAIC,MAAMjC,EAAWkB,GAE5CF,EAAckB,IAAIlC,EAAWgC,GAC7BhB,EAAckB,IAAIF,EAAgBA,GAElC,OACAA,CAAA,CAgBD,MAAsBG,EAAG,WACxB,OAAOjB,EAAcC,MAAMnB,OAAWoC,EAAWC,UACjD,EACDrB,EAAckB,IAAIlC,EAAWmC,GAC7BnB,EAAckB,IAAIC,EAAkBA,GAEpC,QACA,CAjCuCG,CAActC,EACrD,CA+CD,SAAAuB,IACC,MAEIgB,IADU,EAGCC,EAAG9B,EAAO,WACxBiB,EAAUc,IACV,GACDd,EAAQe,EAAY,WACnBC,EAAWA,EAAU,EAAK,EAC1B,GAAIJ,EAAqBA,GACzB,EAED,MAAO,CACNZ,UACAH,UAAUoB,GACTL,EAAsBK,EAEtB,OAA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AAEoC,KAAMzB,KACf,GAEF0B,IAAK,CAAEH,cAAc,EAAMV,MAAO,QAG7B,mBAAuBA,GAC5B,SAAe,IAAM9C,EAAU8C,GAAQ1C,EACvC,CAEK,qBAAyBwD,GAC9B,QAAiBlE,EAAOkE,GACxBC,EAASC,QAAUF,EACnB,OAAcnE,EAAC,IAAMQ,EAAY,IAAM4D,EAASC,WAAY1D,EAC5D,CAEe2D,SAAAA,gBAAgBC,GAC/B,MAAMC,EAAWvE,EAAOsE,GACxBC,EAASH,QAAUE,EAEnBrE,EAAU,IACFO,EAAO,IACN+D,EAASH,WAEf1D,EACH,QAAA8D,YAAAC,UAAAJ"}
1
+ {"version":3,"file":"signals.mjs","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim/index.js\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new WeakMap<\n\tFunctionComponent<any>,\n\tFunctionComponent<any>\n>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => callback.current());\n\t}, Empty);\n}\n"],"names":["React","Component","useMemo","useRef","useEffect","jsxRuntime","jsxRuntimeDev","useSyncExternalStore","Signal","signal","computed","effect","batch","Empty","Symbol","for","ReactMemoType","ProxyInstance","WeakMap","SupportsProxy","Proxy","ProxyHandlers","apply","thisArg","argumentsList","createEffectStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","set","ProxyComponent","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","version","unsubscribe","this","_callback","onStoreChange","WrapJsx","jsx","type","props","rest","call","$$typeof","i","v","value","JsxPro","JsxDev","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","data","ref","useSignal","useComputed","compute","$compute","current","useSignalEffect","cb","callback"],"mappings":"OAuBAA,gBAAAC,aAAAC,YAAAC,eAAAC,MAAA,eAAAC,MAAA,2BAAAC,MAAA,uDAAAC,MAAA,yDAAAC,YAAAC,cAAAC,YAAAC,MAAA,8BAAAH,OAAAI,MAAAF,SAAAC,OAAAF,WAAA,uBAAA,MAAWI,EAAG,KACQC,OAAOC,IAAI,iBAC3BC,EAAgBF,OAAOC,IAAI,cACdE,EAAG,IAAtBC,QAImBC,EAAoB,mBAAjBC,MAEHC,EAAG,CAYrBC,MAAMrB,EAA8BsB,EAAcC,GACjD,QAActB,EAAQuB,EAAmBZ,GAEzCN,EAAqBmB,EAAMC,UAAWD,EAAME,YAAaF,E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/jsx-runtime";import i from"react/jsx-dev-runtime";import{useSyncExternalStore as f}from"use-sync-external-store/shim";import{Signal as c,signal as a,computed as p,effect as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var s=[],m=Symbol.for("react.element"),v=Symbol.for("react.memo"),y=new Map,b="function"==typeof Proxy,g={apply:function(n,r,e){var o=t(d,s);f(o.subscribe,o.getSnapshot,o.getSnapshot);var u=o.updater.S();try{return n.apply(r,e)}catch(n){throw n}finally{u()}}};function h(n){return y.get(n)||function(n){if(b){var r=new Proxy(n,g);y.set(n,r);y.set(r,r);return r}var t=function(){return g.apply(n,void 0,arguments)};y.set(n,t);y.set(t,t);return t}(n)}function d(){var n,r,t=0,e=l(function(){n=this});n.c=function(){t=t+1|0;if(r)r()};return{updater:n,subscribe:function(n){r=n;return function(){t=t+1|0;r=void 0;e()}},getSnapshot:function(){return t}}}function x(n){if("function"!=typeof n)return n;else return function(t,e){var o=[].slice.call(arguments,2);if("function"==typeof t&&!(t instanceof r))return n.call.apply(n,[n,h(t),e].concat(o));if(t&&"object"==typeof t&&t.$$typeof===v){t.type=h(t.type);return n.call.apply(n,[n,t,e].concat(o))}if("string"==typeof t&&e)for(var u in e){var i=e[u];if("children"!==u&&i instanceof c)e[u]=i.value}return n.call.apply(n,[n,t,e].concat(o))}}var j=u,S=i;n.createElement=x(n.createElement);S.jsx&&(S.jsx=x(S.jsx));j.jsx&&(j.jsx=x(j.jsx));S.jsxs&&(S.jsxs=x(S.jsxs));j.jsxs&&(j.jsxs=x(j.jsxs));S.jsxDEV&&(S.jsxDEV=x(S.jsxDEV));j.jsxDEV&&(j.jsxDEV=x(j.jsxDEV));Object.defineProperties(c.prototype,{$$typeof:{configurable:!0,value:m},type:{configurable:!0,value:h(function(n){return n.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});function useSignal(n){return t(function(){return a(n)},s)}function useComputed(n){var r=e(n);r.current=n;return t(function(){return p(function(){return r.current()})},s)}function useSignalEffect(n){var r=e(n);r.current=n;o(function(){return l(function(){return r.current()})},s)}export{useComputed,useSignal,useSignalEffect};//# sourceMappingURL=signals.module.js.map
1
+ import n,{Component as r,useMemo as t,useRef as e,useEffect as o}from"react";import u from"react/jsx-runtime";import i from"react/jsx-dev-runtime";import{useSyncExternalStore as f}from"use-sync-external-store/shim/index.js";import{Signal as c,signal as a,computed as p,effect as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var s=[],m=Symbol.for("react.element"),v=Symbol.for("react.memo"),y=new WeakMap,b="function"==typeof Proxy,g={apply:function(n,r,e){var o=t(h,s);f(o.subscribe,o.getSnapshot,o.getSnapshot);var u=o.updater.S();try{return n.apply(r,e)}catch(n){throw n}finally{u()}}};function d(n){return y.get(n)||function(n){if(b){var r=new Proxy(n,g);y.set(n,r);y.set(r,r);return r}var t=function(){return g.apply(n,void 0,arguments)};y.set(n,t);y.set(t,t);return t}(n)}function h(){var n,r,t=0,e=l(function(){n=this});n.c=function(){t=t+1|0;if(r)r()};return{updater:n,subscribe:function(n){r=n;return function(){t=t+1|0;r=void 0;e()}},getSnapshot:function(){return t}}}function x(n){if("function"!=typeof n)return n;else return function(t,e){var o=[].slice.call(arguments,2);if("function"==typeof t&&!(t instanceof r))return n.call.apply(n,[n,d(t),e].concat(o));if(t&&"object"==typeof t&&t.$$typeof===v){t.type=d(t.type);return n.call.apply(n,[n,t,e].concat(o))}if("string"==typeof t&&e)for(var u in e){var i=e[u];if("children"!==u&&i instanceof c)e[u]=i.value}return n.call.apply(n,[n,t,e].concat(o))}}var j=u,S=i;n.createElement=x(n.createElement);S.jsx&&(S.jsx=x(S.jsx));j.jsx&&(j.jsx=x(j.jsx));S.jsxs&&(S.jsxs=x(S.jsxs));j.jsxs&&(j.jsxs=x(j.jsxs));S.jsxDEV&&(S.jsxDEV=x(S.jsxDEV));j.jsxDEV&&(j.jsxDEV=x(j.jsxDEV));Object.defineProperties(c.prototype,{$$typeof:{configurable:!0,value:m},type:{configurable:!0,value:d(function(n){return n.data.value})},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});function useSignal(n){return t(function(){return a(n)},s)}function useComputed(n){var r=e(n);r.current=n;return t(function(){return p(function(){return r.current()})},s)}function useSignalEffect(n){var r=e(n);r.current=n;o(function(){return l(function(){return r.current()})},s)}export{useComputed,useSignal,useSignalEffect};//# sourceMappingURL=signals.module.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new Map<FunctionComponent<any>, FunctionComponent<any>>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => {\n\t\t\treturn callback.current();\n\t\t});\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","Map","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","version","unsubscribe","effect","this","_callback","onStoreChange","WrapJsx","jsx","type","props","slice","call","rest","$$typeof","i","v","Signal","value","concat","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","useComputed","compute","$compute","useRef","current","computed","useSignalEffect","cb","callback","useEffect","useSignal"],"mappings":"iXAuBA,IAAWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,IACPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAQC,EAAmBZ,GAEzCa,EAAqBC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC,OADiBV,EAAUD,MAAME,EAASC,EAU1C,CARC,MAAOS,GAGR,MAAMA,CACN,CAPD,QAUCC,GACA,CACD,GAGF,SAASC,EAAyBb,GACjC,SAAqBc,IAAId,IAE1B,SAAuBA,GACtB,GAAIH,EAAe,CAClB,IAAMkB,EAAiB,IAAIjB,MAAME,EAAWgB,GAE5CrB,EAAcsB,IAAIjB,EAAWe,GAC7BpB,EAAcsB,I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,EASjCC,EAAMC,cAAgBlB,EAAQiB,EAAMC,eACpCH,EAAOd,MAAgBc,EAAOd,IAAMD,EAAQe,EAAOd,MACnDY,EAAOZ,MAAgBY,EAAOZ,IAAMD,EAAQa,EAAOZ,MACnDc,EAAOI,OAAgBJ,EAAOI,KAAOnB,EAAQe,EAAOI,OACpDN,EAAOM,OAAgBN,EAAOM,KAAOnB,EAAQa,EAAOM,OACpDJ,EAAOK,SAAgBL,EAAOK,OAASpB,EAAQe,EAAOK,SACtDP,EAAOO,SAAgBP,EAAOO,OAASpB,EAAQa,EAAOO,SAUtDC,OAAOC,iBAAiBZ,EAAOa,UAAW,CACzChB,SAAU,CAAEiB,cAAc,EAAMb,MAAOc,GACvCvB,KAAM,CAAEsB,cAAc,EAAMb,MAAO3B,EAPpC,SAAA0C,GACC,OADuCA,EAAxBC,KACHhB,KACZ,IAMAR,MAAO,CACNqB,cAAc,EACdvC,eACC,MAAO,CAAE0C,KAAM9B,KACf,GAEF+B,IAAK,CAAEJ,cAAc,EAAMb,MAAO,QAG7B,mBAAuBA,GAC5B,SAAe,WAAA,SAAgBA,EAAhB,EAAwBhD,EACvC,CAEK,SAAAkE,YAAyBC,GAC9B,IAAcC,EAAGC,EAAOF,GACxBC,EAASE,QAAUH,EACnB,OAAOxD,EAAQ,WAAM4D,OAAAA,EAAY,WAAA,OAAcH,EAACE,SAAf,EAAlB,EAA6CtE,EAC5D,CAEewE,SAAAA,gBAAgBC,GAC/B,IAAMC,EAAWL,EAAOI,GACxBC,EAASJ,QAAUG,EAEnBE,EAAU,WACT,OAAO1C,EAAO,WACb,OAAOyC,EAASJ,SAChB,EACD,EAAEtE,EACH,QAAAkE,YAAAU,UAAAJ"}
1
+ {"version":3,"file":"signals.module.js","sources":["../src/index.ts"],"sourcesContent":["import {\n\tuseRef,\n\tuseMemo,\n\tuseEffect,\n\tComponent,\n\ttype FunctionComponent,\n} from \"react\";\nimport React from \"react\";\nimport jsxRuntime from \"react/jsx-runtime\";\nimport jsxRuntimeDev from \"react/jsx-dev-runtime\";\nimport { useSyncExternalStore } from \"use-sync-external-store/shim/index.js\";\nimport {\n\tsignal,\n\tcomputed,\n\tbatch,\n\teffect,\n\tSignal,\n\ttype ReadonlySignal,\n} from \"@preact/signals-core\";\nimport type { Effect, JsxRuntimeModule } from \"./internal\";\n\nexport { signal, computed, batch, effect, Signal, type ReadonlySignal };\n\nconst Empty = [] as const;\nconst ReactElemType = Symbol.for(\"react.element\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15\nconst ReactMemoType = Symbol.for(\"react.memo\"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30\nconst ProxyInstance = new WeakMap<\n\tFunctionComponent<any>,\n\tFunctionComponent<any>\n>();\nconst SupportsProxy = typeof Proxy === \"function\";\n\nconst ProxyHandlers = {\n\t/**\n\t * This is a function call trap for functional components.\n\t * When this is called, we know it means React did run 'Component()',\n\t * that means we can use any hooks here to setup our effect and store.\n\t *\n\t * With the native Proxy, all other calls such as access/setting to/of properties will\n\t * be forwarded to the target Component, so we don't need to copy the Component's\n\t * own or inherited properties.\n\t *\n\t * @see https://github.com/facebook/react/blob/2d80a0cd690bb5650b6c8a6c079a87b5dc42bd15/packages/react-reconciler/src/ReactFiberHooks.old.js#L460\n\t */\n\tapply(Component: FunctionComponent, thisArg: any, argumentsList: any) {\n\t\tconst store = useMemo(createEffectStore, Empty);\n\n\t\tuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);\n\n\t\tconst stop = store.updater._start();\n\n\t\ttry {\n\t\t\tconst children = Component.apply(thisArg, argumentsList);\n\t\t\treturn children;\n\t\t} catch (e) {\n\t\t\t// Re-throwing promises that'll be handled by suspense\n\t\t\t// or an actual error.\n\t\t\tthrow e;\n\t\t} finally {\n\t\t\t// Stop effects in either case before return or throw,\n\t\t\t// Otherwise the effect will leak.\n\t\t\tstop();\n\t\t}\n\t},\n};\n\nfunction ProxyFunctionalComponent(Component: FunctionComponent<any>) {\n\treturn ProxyInstance.get(Component) || WrapWithProxy(Component);\n}\nfunction WrapWithProxy(Component: FunctionComponent<any>) {\n\tif (SupportsProxy) {\n\t\tconst ProxyComponent = new Proxy(Component, ProxyHandlers);\n\n\t\tProxyInstance.set(Component, ProxyComponent);\n\t\tProxyInstance.set(ProxyComponent, ProxyComponent);\n\n\t\treturn ProxyComponent;\n\t}\n\n\t/**\n\t * Emulate a Proxy if environment doesn't support it.\n\t *\n\t * @TODO - unlike Proxy, it's not possible to access the type/Component's\n\t * static properties this way. Not sure if we want to copy all statics here.\n\t * Omitting this for now.\n\t *\n\t * @example - works with Proxy, doesn't with wrapped function.\n\t * ```\n\t * const el = <SomeFunctionalComponent />\n\t * el.type.someOwnOrInheritedProperty;\n\t * el.type.defaultProps;\n\t * ```\n\t */\n\tconst WrappedComponent = function () {\n\t\treturn ProxyHandlers.apply(Component, undefined, arguments);\n\t};\n\tProxyInstance.set(Component, WrappedComponent);\n\tProxyInstance.set(WrappedComponent, WrappedComponent);\n\n\treturn WrappedComponent;\n}\n\n/**\n * A redux-like store whose store value is a positive 32bit integer (a 'version').\n *\n * React subscribes to this store and gets a snapshot of the current 'version',\n * whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange').\n *\n * How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called,\n * we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it).\n *\n * [1]\n * @see https://reactjs.org/docs/hooks-reference.html#usesyncexternalstore\n * @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md\n */\nfunction createEffectStore() {\n\tlet updater!: Effect;\n\tlet version = 0;\n\tlet onChangeNotifyReact: (() => void) | undefined;\n\n\tlet unsubscribe = effect(function (this: Effect) {\n\t\tupdater = this;\n\t});\n\tupdater._callback = function () {\n\t\tversion = (version + 1) | 0;\n\t\tif (onChangeNotifyReact) onChangeNotifyReact();\n\t};\n\n\treturn {\n\t\tupdater,\n\t\tsubscribe(onStoreChange: () => void) {\n\t\t\tonChangeNotifyReact = onStoreChange;\n\n\t\t\treturn function () {\n\t\t\t\t/**\n\t\t\t\t * Rotate to next version when unsubscribing to ensure that components are re-run\n\t\t\t\t * when subscribing again.\n\t\t\t\t *\n\t\t\t\t * In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so\n\t\t\t\t * don't re-run after subscribing again if the version is the same as last time.\n\t\t\t\t *\n\t\t\t\t * Because we unsubscribe from the effect, the version may not change. We simply\n\t\t\t\t * set a new initial version in case of stale snapshots here.\n\t\t\t\t */\n\t\t\t\tversion = (version + 1) | 0;\n\t\t\t\tonChangeNotifyReact = undefined;\n\t\t\t\tunsubscribe();\n\t\t\t};\n\t\t},\n\t\tgetSnapshot() {\n\t\t\treturn version;\n\t\t},\n\t};\n}\n\nfunction WrapJsx<T>(jsx: T): T {\n\tif (typeof jsx !== \"function\") return jsx;\n\n\treturn function (type: any, props: any, ...rest: any[]) {\n\t\tif (typeof type === \"function\" && !(type instanceof Component)) {\n\t\t\treturn jsx.call(jsx, ProxyFunctionalComponent(type), props, ...rest);\n\t\t}\n\n\t\tif (type && typeof type === \"object\" && type.$$typeof === ReactMemoType) {\n\t\t\ttype.type = ProxyFunctionalComponent(type.type);\n\t\t\treturn jsx.call(jsx, type, props, ...rest);\n\t\t}\n\n\t\tif (typeof type === \"string\" && props) {\n\t\t\tfor (let i in props) {\n\t\t\t\tlet v = props[i];\n\t\t\t\tif (i !== \"children\" && v instanceof Signal) {\n\t\t\t\t\tprops[i] = v.value;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn jsx.call(jsx, type, props, ...rest);\n\t} as any as T;\n}\n\nconst JsxPro: JsxRuntimeModule = jsxRuntime;\nconst JsxDev: JsxRuntimeModule = jsxRuntimeDev;\n\n/**\n * createElement _may_ be called by jsx runtime as a fallback in certain cases,\n * so we need to wrap it regardless.\n *\n * The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't\n * include both, so one of them will be set with `undefined` values.\n */\nReact.createElement = WrapJsx(React.createElement);\nJsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx));\nJsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx));\nJsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs));\nJsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs));\nJsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV));\nJsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV));\n\n/**\n * A wrapper component that renders a Signal's value directly as a Text node.\n */\nfunction Text({ data }: { data: Signal }) {\n\treturn data.value;\n}\n\n// Decorate Signals so React renders them as <Text> components.\nObject.defineProperties(Signal.prototype, {\n\t$$typeof: { configurable: true, value: ReactElemType },\n\ttype: { configurable: true, value: ProxyFunctionalComponent(Text) },\n\tprops: {\n\t\tconfigurable: true,\n\t\tget() {\n\t\t\treturn { data: this };\n\t\t},\n\t},\n\tref: { configurable: true, value: null },\n});\n\nexport function useSignal<T>(value: T) {\n\treturn useMemo(() => signal<T>(value), Empty);\n}\n\nexport function useComputed<T>(compute: () => T) {\n\tconst $compute = useRef(compute);\n\t$compute.current = compute;\n\treturn useMemo(() => computed<T>(() => $compute.current()), Empty);\n}\n\nexport function useSignalEffect(cb: () => void | (() => void)) {\n\tconst callback = useRef(cb);\n\tcallback.current = cb;\n\n\tuseEffect(() => {\n\t\treturn effect(() => callback.current());\n\t}, Empty);\n}\n"],"names":["Empty","Symbol","ReactMemoType","ProxyInstance","WeakMap","SupportsProxy","Proxy","apply","Component","thisArg","argumentsList","useMemo","createEffectStore","useSyncExternalStore","store","subscribe","getSnapshot","updater","_start","e","stop","ProxyFunctionalComponent","get","ProxyComponent","ProxyHandlers","set","WrappedComponent","undefined","arguments","WrapWithProxy","onChangeNotifyReact","unsubscribe","effect","this","_callback","version","onStoreChange","WrapJsx","jsx","type","props","rest","slice","call","concat","$$typeof","v","i","Signal","value","JsxPro","jsxRuntime","JsxDev","jsxRuntimeDev","React","createElement","jsxs","jsxDEV","Object","defineProperties","prototype","configurable","ReactElemType","_ref","data","ref","useSignal","signal","compute","useRef","$compute","current","computed","useSignalEffect","cb","callback","useEffect","useComputed"],"mappings":"0XAuBA,IAAWA,EAAG,KACQC,WAAW,iBACdC,EAAGD,OAAM,IAAK,cAC3BE,EAAgB,IAAIC,QAIPC,EAAoB,mBAAjBC,QAEA,CAYrBC,MAZqB,SAYfC,EAA8BC,EAAcC,GACjD,MAAcC,EAAQC,EAAmBZ,GAEzCa,EAAqBC,EAAMC,UAAWD,EAAME,YAAaF,EAAME,aAE/D,MAAaF,EAAMG,QAAQC,IAE3B,IAEC,OADiBV,EAAUD,MAAME,EAASC,EAU1C,CARC,MAAOS,GAGR,MAAMA,CACN,CAPD,QAUCC,GACA,CACD,GAGF,SAASC,EAAyBb,GACjC,SAAqBc,IAAId,IAE1B,SAAuBA,GACtB,GAAIH,EAAe,CAClB,IAAMkB,EAAiB,IAAIjB,MAAME,EAAWgB,GAE5CrB,EAAcsB,IAAIjB,EAAWe,GAC7BpB,EAAcsB,IAAIF,EAAgBA,GAElC,OACAA,CAAA,CAgBD,IAAsBG,EAAG,WACxB,OAAOF,EAAcjB,MAAMC,OAAWmB,EAAWC,UACjD,EACDzB,EAAcsB,IAAIjB,EAAWkB,GAC7BvB,EAAcsB,IAAIC,EAAkBA,GAEpC,QACA,CAjCuCG,CAAcrB,EACrD,CA+CD,aACC,IAAIS,EAEAa,IADU,EAGVC,EAAcC,EAAO,WACxBf,EAAUgB,IACV,GACDhB,EAAQiB,EAAY,WACnBC,EAAWA,EAAU,EAAK,EAC1B,GAAIL,EAAqBA,GACzB,EAED,MAAO,CACNb,QAAAA,EACAF,UAFM,SAEIqB,GACTN,EAAsBM,EAEtB,kBAWCD,EAAWA,EAAU,EAAK,E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MAAO5B,EAPpC,SAAwC0C,GACvC,OADeC,EAAAA,KACHf,KACZ,IAMAT,MAAO,CACNqB,cAAc,EACdvC,IAFM,WAGL,MAAO,CAAE0C,KAAM/B,KACf,GAEFgC,IAAK,CAAEJ,cAAc,EAAMZ,MAAO,QAGnBiB,SAAAA,UAAajB,GAC5B,OAAOtC,EAAQ,WAAMwD,OAAAA,EAAUlB,EAAhB,EAAwBjD,EACvC,CAEK,qBAAyBoE,GAC9B,MAAiBC,EAAOD,GACxBE,EAASC,QAAUH,EACnB,OAAczD,EAAC,WAAM6D,OAAAA,EAAY,WAAA,SAAeD,SAAf,EAAlB,EAA6CvE,EAC5D,UAEKyE,gBAA0BC,GAC/B,IAAcC,EAAGN,EAAOK,GACxBC,EAASJ,QAAUG,EAEnBE,EAAU,WACT,OAAO5C,EAAO,WAAM2C,OAAAA,EAASJ,SAAf,EACd,EAAEvE,EACH,QAAA6E,YAAAX,UAAAO"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@preact/signals-react",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "license": "MIT",
5
- "description": "",
5
+ "description": "Manage state with style in React",
6
6
  "keywords": [],
7
7
  "authors": [
8
8
  "The Preact Authors (https://github.com/preactjs/signals/contributors)"
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "mangle": "../../mangle.json",
37
37
  "dependencies": {
38
- "@preact/signals-core": "^1.2.2",
38
+ "@preact/signals-core": "^1.2.3",
39
39
  "use-sync-external-store": "^1.2.0"
40
40
  },
41
41
  "peerDependencies": {
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  import React from "react";
9
9
  import jsxRuntime from "react/jsx-runtime";
10
10
  import jsxRuntimeDev from "react/jsx-dev-runtime";
11
- import { useSyncExternalStore } from "use-sync-external-store/shim";
11
+ import { useSyncExternalStore } from "use-sync-external-store/shim/index.js";
12
12
  import {
13
13
  signal,
14
14
  computed,
@@ -24,7 +24,10 @@ export { signal, computed, batch, effect, Signal, type ReadonlySignal };
24
24
  const Empty = [] as const;
25
25
  const ReactElemType = Symbol.for("react.element"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15
26
26
  const ReactMemoType = Symbol.for("react.memo"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L30
27
- const ProxyInstance = new Map<FunctionComponent<any>, FunctionComponent<any>>();
27
+ const ProxyInstance = new WeakMap<
28
+ FunctionComponent<any>,
29
+ FunctionComponent<any>
30
+ >();
28
31
  const SupportsProxy = typeof Proxy === "function";
29
32
 
30
33
  const ProxyHandlers = {
@@ -229,8 +232,6 @@ export function useSignalEffect(cb: () => void | (() => void)) {
229
232
  callback.current = cb;
230
233
 
231
234
  useEffect(() => {
232
- return effect(() => {
233
- return callback.current();
234
- });
235
+ return effect(() => callback.current());
235
236
  }, Empty);
236
237
  }
@@ -1,8 +1,8 @@
1
1
  // @ts-ignore-next-line
2
2
  globalThis.IS_REACT_ACT_ENVIRONMENT = true;
3
3
 
4
- import { signal, useComputed } from "@preact/signals-react";
5
- import { createElement, useMemo, memo, StrictMode } from "react";
4
+ import { signal, useComputed, useSignalEffect } from "@preact/signals-react";
5
+ import { createElement, useMemo, memo, StrictMode, createRef } from "react";
6
6
  import { createRoot, Root } from "react-dom/client";
7
7
  import { renderToStaticMarkup } from "react-dom/server";
8
8
  import { act } from "react-dom/test-utils";
@@ -222,4 +222,135 @@ describe("@preact/signals-react", () => {
222
222
  }
223
223
  });
224
224
  });
225
+
226
+ describe("useSignalEffect()", () => {
227
+ it("should be invoked after commit", async () => {
228
+ const ref = createRef<HTMLDivElement>();
229
+ const sig = signal("foo");
230
+ const spy = sinon.spy();
231
+ let count = 0;
232
+
233
+ function App() {
234
+ useSignalEffect(() =>
235
+ spy(
236
+ sig.value,
237
+ ref.current,
238
+ ref.current!.getAttribute("data-render-id")
239
+ )
240
+ );
241
+ return (
242
+ <p ref={ref} data-render-id={count++}>
243
+ {sig.value}
244
+ </p>
245
+ );
246
+ }
247
+
248
+ render(<App />);
249
+ expect(scratch.textContent).to.equal("foo");
250
+
251
+ expect(spy).to.have.been.calledOnceWith(
252
+ "foo",
253
+ scratch.firstElementChild,
254
+ "0"
255
+ );
256
+
257
+ spy.resetHistory();
258
+
259
+ act(() => {
260
+ sig.value = "bar";
261
+ });
262
+
263
+ expect(scratch.textContent).to.equal("bar");
264
+
265
+ // NOTE: Ideally, call should receive "1" as its third argument!
266
+ // The "0" indicates that Preact's DOM mutations hadn't yet been performed when the callback ran.
267
+ // This happens because we do signal-based effect runs after the first, not VDOM.
268
+ // Perhaps we could find a way to defer the callback when it coincides with a render?
269
+ expect(spy).to.have.been.calledOnceWith(
270
+ "bar",
271
+ scratch.firstElementChild,
272
+ "0" // ideally "1" - update if we find a nice way to do so!
273
+ );
274
+ });
275
+
276
+ it("should invoke any returned cleanup function for updates", async () => {
277
+ const ref = createRef<HTMLDivElement>();
278
+ const sig = signal("foo");
279
+ const spy = sinon.spy();
280
+ const cleanup = sinon.spy();
281
+ let count = 0;
282
+
283
+ function App() {
284
+ useSignalEffect(() => {
285
+ const id = ref.current!.getAttribute("data-render-id");
286
+ const value = sig.value;
287
+ spy(value, ref.current, id);
288
+ return () => cleanup(value, ref.current, id);
289
+ });
290
+ return (
291
+ <p ref={ref} data-render-id={count++}>
292
+ {sig.value}
293
+ </p>
294
+ );
295
+ }
296
+
297
+ render(<App />);
298
+
299
+ expect(cleanup).not.to.have.been.called;
300
+ expect(spy).to.have.been.calledOnceWith(
301
+ "foo",
302
+ scratch.firstElementChild,
303
+ "0"
304
+ );
305
+ spy.resetHistory();
306
+
307
+ act(() => {
308
+ sig.value = "bar";
309
+ });
310
+
311
+ expect(scratch.textContent).to.equal("bar");
312
+
313
+ const child = scratch.firstElementChild;
314
+
315
+ expect(cleanup).to.have.been.calledOnceWith("foo", child, "0");
316
+
317
+ expect(spy).to.have.been.calledOnceWith(
318
+ "bar",
319
+ child,
320
+ "0" // ideally "1" - update if we find a nice way to do so!
321
+ );
322
+ });
323
+
324
+ it("should invoke any returned cleanup function for unmounts", async () => {
325
+ const ref = createRef<HTMLDivElement>();
326
+ const sig = signal("foo");
327
+ const spy = sinon.spy();
328
+ const cleanup = sinon.spy();
329
+
330
+ function App() {
331
+ useSignalEffect(() => {
332
+ const value = sig.value;
333
+ spy(value, ref.current);
334
+ return () => cleanup(value, ref.current);
335
+ });
336
+ return <p ref={ref}>{sig.value}</p>;
337
+ }
338
+
339
+ render(<App />);
340
+
341
+ const child = scratch.firstElementChild;
342
+
343
+ expect(cleanup).not.to.have.been.called;
344
+ expect(spy).to.have.been.calledOnceWith("foo", child);
345
+ spy.resetHistory();
346
+
347
+ render(null);
348
+
349
+ expect(spy).not.to.have.been.called;
350
+ expect(cleanup).to.have.been.calledOnce;
351
+ // @note: React cleans up the ref eagerly, so it's already null by the time the callback runs.
352
+ // this is probably worth fixing at some point.
353
+ expect(cleanup).to.have.been.calledWith("foo", null);
354
+ });
355
+ });
225
356
  });