@preact/signals-react
Advanced tools
Comparing version 1.3.2 to 1.3.3
# @preact/signals-react | ||
## 1.3.3 | ||
### Patch Changes | ||
- [#373](https://github.com/preactjs/signals/pull/373) [`8c12a0d`](https://github.com/preactjs/signals/commit/8c12a0df74f00e9cab04e999fc443889b3528c04) Thanks [@rschristian](https://github.com/rschristian)! - Removes package.json#exports.umd, which had invalid paths if they were ever to be consumed | ||
* [#372](https://github.com/preactjs/signals/pull/372) [`6717601`](https://github.com/preactjs/signals/commit/6717601a34449080617033d93a87cc9c441a7567) Thanks [@andrewiggins](https://github.com/andrewiggins)! - Reorganize signals-react package | ||
* Updated dependencies [[`8c12a0d`](https://github.com/preactjs/signals/commit/8c12a0df74f00e9cab04e999fc443889b3528c04), [`26f6526`](https://github.com/preactjs/signals/commit/26f6526875ef0968621c4113594ac95b93de5163)]: | ||
- @preact/signals-core@1.3.1 | ||
## 1.3.2 | ||
@@ -4,0 +15,0 @@ |
@@ -1,4 +0,5 @@ | ||
import { type ReactElement } from "react"; | ||
import { signal, computed, batch, effect, Signal, type ReadonlySignal } from "@preact/signals-core"; | ||
export { signal, computed, batch, effect, Signal, type ReadonlySignal }; | ||
import type { ReactElement } from "react"; | ||
import { useSignal, useComputed, useSignalEffect } from "../runtime"; | ||
export { signal, computed, batch, effect, Signal, type ReadonlySignal, useSignal, useComputed, useSignalEffect, }; | ||
declare module "@preact/signals-core" { | ||
@@ -8,4 +9,1 @@ interface Signal extends ReactElement { | ||
} | ||
export declare function useSignal<T>(value: T): Signal<T>; | ||
export declare function useComputed<T>(compute: () => T): ReadonlySignal<T>; | ||
export declare function useSignalEffect(cb: () => void | (() => void)): void; |
@@ -1,1 +0,1 @@ | ||
var e=require("react"),r=require("react/jsx-runtime"),n=require("react/jsx-dev-runtime"),t=require("@preact/signals-core"),u=require("use-sync-external-store/shim/index.js");function i(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var f,o=/*#__PURE__*/i(e),c=/*#__PURE__*/i(r),a=/*#__PURE__*/i(n),s=[],l=Symbol.for("react.element");function v(e){if(f)f();f=e&&e.S()}var p=!1,d=null;Object.defineProperty(e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher,"current",{get:function(){return d},set:function(e){if(!p){var r=b(d),n=b(e);d=e;if(function(e,r){if(1&e&&28&r)return!0;else if(2&e||2&r)return!1;else if(16&r)return!0;else return!1}(r,n)){p=!0;var i=function(e){var r=e.useRef();if(null==r.current)r.current=function(){var e,r,n=0,u=t.effect(function(){e=this});e.c=function(){n=n+1|0;if(r)r()};return{updater:e,subscribe:function(e){r=e;return function(){n=n+1|0;r=void 0;u()}},getSnapshot:function(){return n}}}();var n=r.current;u.useSyncExternalStore(n.subscribe,n.getSnapshot,n.getSnapshot);return n}(e);p=!1;v(i.updater)}else if(function(e,r){return Boolean(28&e&&1&r)}(r,n))v()}else d=e}});var x=new Map;function b(e){if(!e)return 1;var r,n=x.get(e);if(void 0!==n)return n;var t=e.useCallback.toString();if(e.useReducer===e.useEffect)r=1;else if(e.useEffect===e.useImperativeHandle)r=32;else if(/Invalid/.test(t))r=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var u=e.useReducer.toString();if(/rerenderReducer/.test(u)||/return\s*\[\w+,/.test(u))r=16;else r=8}else r=4;x.set(e,r);return r}function g(e){if("function"!=typeof e)return e;else return function(r,n){if("string"==typeof r&&n)for(var u in n){var i=n[u];if("children"!==u&&i instanceof t.Signal)n[u]=i.value}return e.call.apply(e,[e,r,n].concat([].slice.call(arguments,2)))}}var y=c,j=a;o.createElement=g(o.createElement);j.jsx&&(j.jsx=g(j.jsx));y.jsx&&(y.jsx=g(y.jsx));j.jsxs&&(j.jsxs=g(j.jsxs));y.jsxs&&(y.jsxs=g(y.jsxs));j.jsxDEV&&(j.jsxDEV=g(j.jsxDEV));y.jsxDEV&&(y.jsxDEV=g(y.jsxDEV));Object.defineProperties(t.Signal.prototype,{$$typeof:{configurable:!0,value:l},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});exports.Signal=t.Signal;exports.batch=t.batch;exports.computed=t.computed;exports.effect=t.effect;exports.signal=t.signal;exports.useComputed=function(r){var n=e.useRef(r);n.current=r;return e.useMemo(function(){return t.computed(function(){return n.current()})},s)};exports.useSignal=function(r){return e.useMemo(function(){return t.signal(r)},s)};exports.useSignalEffect=function(r){var n=e.useRef(r);n.current=r;e.useEffect(function(){return t.effect(function(){return n.current()})},s)};//# sourceMappingURL=signals.js.map | ||
var e=require("@preact/signals-core"),r=require("react"),n=require("use-sync-external-store/shim/index.js"),t=require("react/jsx-runtime"),u=require("react/jsx-dev-runtime");function i(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var f=/*#__PURE__*/i(r),o=/*#__PURE__*/i(t),c=/*#__PURE__*/i(u),a=[],l=Symbol.for("react.element");Object.defineProperties(e.Signal.prototype,{$$typeof:{configurable:!0,value:l},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var s,v=Symbol.for("react.element");function p(r){if("function"!=typeof r)return r;else return function(n,t){if("string"==typeof n&&t)for(var u in t){var i=t[u];if("children"!==u&&i instanceof e.Signal)t[u]=i.value}return r.call.apply(r,[r,n,t].concat([].slice.call(arguments,2)))}}function b(e){if(s)s();s=e&&e.S()}var g=function(){return b()};Object.defineProperties(e.Signal.prototype,{$$typeof:{configurable:!0,value:v},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var d=null,x=!1,y=null,j=new Map;function m(e){if(!e)return 1;var r,n=j.get(e);if(void 0!==n)return n;var t=e.useCallback.toString();if(e.useReducer===e.useEffect)r=1;else if(e.useEffect===e.useImperativeHandle)r=32;else if(/Invalid/.test(t))r=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var u=e.useReducer.toString();if(/rerenderReducer/.test(u)||/return\s*\[\w+,/.test(u))r=16;else r=8}else r=4;j.set(e,r);return r}!function(){Object.defineProperty(r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher,"current",{get:function(){return y},set:function(t){if(!x){var u=m(y),i=m(t);y=t;if(function(e,r){if(1&e&&28&r)return!0;else if(2&e||2&r)return!1;else if(16&r)return!0;else return!1}(u,i)){x=!0;d=function(){var t=r.useRef();if(null==t.current)t.current=function(){var r,n,t=0,u=e.effect(function(){r=this});r.c=function(){t=t+1|0;if(n)n()};return{updater:r,subscribe:function(e){n=e;return function(){t=t+1|0;n=void 0;u()}},getSnapshot:function(){return t}}}();var u=t.current;n.useSyncExternalStore(u.subscribe,u.getSnapshot,u.getSnapshot);b(u.updater);return g}();x=!1}else if(function(e,r){return Boolean(28&e&&1&r)}(u,i))null==d||d()}else y=t}});!function(){var e=o,r=c;f.createElement=p(f.createElement);r.jsx&&(r.jsx=p(r.jsx));e.jsx&&(e.jsx=p(e.jsx));r.jsxs&&(r.jsxs=p(r.jsxs));e.jsxs&&(e.jsxs=p(e.jsxs));r.jsxDEV&&(r.jsxDEV=p(r.jsxDEV));e.jsxDEV&&(e.jsxDEV=p(e.jsxDEV))}()}();exports.Signal=e.Signal;exports.batch=e.batch;exports.computed=e.computed;exports.effect=e.effect;exports.signal=e.signal;exports.useComputed=function(n){var t=r.useRef(n);return t.current=n,r.useMemo(function(){return e.computed(function(){return t.current()})},a)};exports.useSignal=function(n){return r.useMemo(function(){return e.signal(n)},a)};exports.useSignalEffect=function(n){var t=r.useRef(n);t.current=n,r.useEffect(function(){return e.effect(function(){return t.current()})},a)};//# sourceMappingURL=signals.js.map |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react"),require("react/jsx-runtime"),require("react/jsx-dev-runtime"),require("@preact/signals-core"),require("use-sync-external-store/shim/index.js")):"function"==typeof define&&define.amd?define(["exports","react","react/jsx-runtime","react/jsx-dev-runtime","@preact/signals-core","use-sync-external-store/shim/index.js"],n):n((e||self).reactSignals={},e.react,e.jsxRuntime,e.jsxRuntimeDev,e.signalsCore,e.index_js)}(this,function(e,n,r,t,i,u){function f(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var o,c=/*#__PURE__*/f(n),a=/*#__PURE__*/f(r),s=/*#__PURE__*/f(t),l=[],v=Symbol.for("react.element");function d(e){if(o)o();o=e&&e.S()}var p=!1,b=null;Object.defineProperty(n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher,"current",{get:function(){return b},set:function(e){if(!p){var n=y(b),r=y(e);b=e;if(function(e,n){if(1&e&&28&n)return!0;else if(2&e||2&n)return!1;else if(16&n)return!0;else return!1}(n,r)){p=!0;var t=function(e){var n=e.useRef();if(null==n.current)n.current=function(){var e,n,r=0,t=i.effect(function(){e=this});e.c=function(){r=r+1|0;if(n)n()};return{updater:e,subscribe:function(e){n=e;return function(){r=r+1|0;n=void 0;t()}},getSnapshot:function(){return r}}}();var r=n.current;u.useSyncExternalStore(r.subscribe,r.getSnapshot,r.getSnapshot);return r}(e);p=!1;d(t.updater)}else if(function(e,n){return Boolean(28&e&&1&n)}(n,r))d()}else b=e}});var g=new Map;function y(e){if(!e)return 1;var n,r=g.get(e);if(void 0!==r)return r;var t=e.useCallback.toString();if(e.useReducer===e.useEffect)n=1;else if(e.useEffect===e.useImperativeHandle)n=32;else if(/Invalid/.test(t))n=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var i=e.useReducer.toString();if(/rerenderReducer/.test(i)||/return\s*\[\w+,/.test(i))n=16;else n=8}else n=4;g.set(e,n);return n}function x(e){if("function"!=typeof e)return e;else return function(n,r){if("string"==typeof n&&r)for(var t in r){var u=r[t];if("children"!==t&&u instanceof i.Signal)r[t]=u.value}return e.call.apply(e,[e,n,r].concat([].slice.call(arguments,2)))}}var j=a,h=s;c.createElement=x(c.createElement);h.jsx&&(h.jsx=x(h.jsx));j.jsx&&(j.jsx=x(j.jsx));h.jsxs&&(h.jsxs=x(h.jsxs));j.jsxs&&(j.jsxs=x(j.jsxs));h.jsxDEV&&(h.jsxDEV=x(h.jsxDEV));j.jsxDEV&&(j.jsxDEV=x(j.jsxDEV));Object.defineProperties(i.Signal.prototype,{$$typeof:{configurable:!0,value:v},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});e.Signal=i.Signal;e.batch=i.batch;e.computed=i.computed;e.effect=i.effect;e.signal=i.signal;e.useComputed=function(e){var r=n.useRef(e);r.current=e;return n.useMemo(function(){return i.computed(function(){return r.current()})},l)};e.useSignal=function(e){return n.useMemo(function(){return i.signal(e)},l)};e.useSignalEffect=function(e){var r=n.useRef(e);r.current=e;n.useEffect(function(){return i.effect(function(){return r.current()})},l)}});//# sourceMappingURL=signals.min.js.map | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("@preact/signals-core"),require("react"),require("use-sync-external-store/shim/index.js"),require("react/jsx-runtime"),require("react/jsx-dev-runtime")):"function"==typeof define&&define.amd?define(["exports","@preact/signals-core","react","use-sync-external-store/shim/index.js","react/jsx-runtime","react/jsx-dev-runtime"],n):n((e||self).reactSignals={},e.signalsCore,e.react,e.index_js,e.f,e.l)}(this,function(e,n,r,t,u,i){function f(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var o=/*#__PURE__*/f(r),c=/*#__PURE__*/f(u),a=/*#__PURE__*/f(i),l=[],s=Symbol.for("react.element");Object.defineProperties(n.Signal.prototype,{$$typeof:{configurable:!0,value:s},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var v,p=Symbol.for("react.element");function d(e){if("function"!=typeof e)return e;else return function(r,t){if("string"==typeof r&&t)for(var u in t){var i=t[u];if("children"!==u&&i instanceof n.Signal)t[u]=i.value}return e.call.apply(e,[e,r,t].concat([].slice.call(arguments,2)))}}function b(e){if(v)v();v=e&&e.S()}var g=function(){return b()};Object.defineProperties(n.Signal.prototype,{$$typeof:{configurable:!0,value:p},type:{configurable:!0,value:function(e){return e.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var y=null,j=!1,m=null,x=new Map;function h(e){if(!e)return 1;var n,r=x.get(e);if(void 0!==r)return r;var t=e.useCallback.toString();if(e.useReducer===e.useEffect)n=1;else if(e.useEffect===e.useImperativeHandle)n=32;else if(/Invalid/.test(t))n=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var u=e.useReducer.toString();if(/rerenderReducer/.test(u)||/return\s*\[\w+,/.test(u))n=16;else n=8}else n=4;x.set(e,n);return n}!function(){Object.defineProperty(r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentDispatcher,"current",{get:function(){return m},set:function(e){if(!j){var u=h(m),i=h(e);m=e;if(function(e,n){if(1&e&&28&n)return!0;else if(2&e||2&n)return!1;else if(16&n)return!0;else return!1}(u,i)){j=!0;y=function(){var e=r.useRef();if(null==e.current)e.current=function(){var e,r,t=0,u=n.effect(function(){e=this});e.c=function(){t=t+1|0;if(r)r()};return{updater:e,subscribe:function(e){r=e;return function(){t=t+1|0;r=void 0;u()}},getSnapshot:function(){return t}}}();var u=e.current;t.useSyncExternalStore(u.subscribe,u.getSnapshot,u.getSnapshot);b(u.updater);return g}();j=!1}else if(function(e,n){return Boolean(28&e&&1&n)}(u,i))null==y||y()}else m=e}});!function(){var e=c,n=a;o.createElement=d(o.createElement);n.jsx&&(n.jsx=d(n.jsx));e.jsx&&(e.jsx=d(e.jsx));n.jsxs&&(n.jsxs=d(n.jsxs));e.jsxs&&(e.jsxs=d(e.jsxs));n.jsxDEV&&(n.jsxDEV=d(n.jsxDEV));e.jsxDEV&&(e.jsxDEV=d(e.jsxDEV))}()}();e.Signal=n.Signal;e.batch=n.batch;e.computed=n.computed;e.effect=n.effect;e.signal=n.signal;e.useComputed=function(e){var t=r.useRef(e);return t.current=e,r.useMemo(function(){return n.computed(function(){return t.current()})},l)};e.useSignal=function(e){return r.useMemo(function(){return n.signal(e)},l)};e.useSignalEffect=function(e){var t=r.useRef(e);t.current=e,r.useEffect(function(){return n.effect(function(){return t.current()})},l)}});//# sourceMappingURL=signals.min.js.map |
@@ -1,1 +0,1 @@ | ||
import r,{__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as n,useMemo as e,useRef as t,useEffect as i}from"react";import u from"react/jsx-runtime";import f from"react/jsx-dev-runtime";import{Signal as o,signal as c,computed as a,effect as l}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";import{useSyncExternalStore as s}from"use-sync-external-store/shim/index.js";var v,p=[],m=Symbol.for("react.element");function d(r){if(v)v();v=r&&r.S()}var g=!1,b=null;Object.defineProperty(n.ReactCurrentDispatcher,"current",{get:function(){return b},set:function(r){if(!g){var n=x(b),e=x(r);b=r;if(function(r,n){if(1&r&&28&n)return!0;else if(2&r||2&n)return!1;else if(16&n)return!0;else return!1}(n,e)){g=!0;var t=function(r){var n=r.useRef();if(null==n.current)n.current=function(){var r,n,e=0,t=l(function(){r=this});r.c=function(){e=e+1|0;if(n)n()};return{updater:r,subscribe:function(r){n=r;return function(){e=e+1|0;n=void 0;t()}},getSnapshot:function(){return e}}}();var e=n.current;s(e.subscribe,e.getSnapshot,e.getSnapshot);return e}(r);g=!1;d(t.updater)}else if(function(r,n){return Boolean(28&r&&1&n)}(n,e))d()}else b=r}});var h=new Map;function x(r){if(!r)return 1;var n,e=h.get(r);if(void 0!==e)return e;var t=r.useCallback.toString();if(r.useReducer===r.useEffect)n=1;else if(r.useEffect===r.useImperativeHandle)n=32;else if(/Invalid/.test(t))n=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var i=r.useReducer.toString();if(/rerenderReducer/.test(i)||/return\s*\[\w+,/.test(i))n=16;else n=8}else n=4;h.set(r,n);return n}function y(r){if("function"!=typeof r)return r;else return function(n,e){if("string"==typeof n&&e)for(var t in e){var i=e[t];if("children"!==t&&i instanceof o)e[t]=i.value}return r.call.apply(r,[r,n,e].concat([].slice.call(arguments,2)))}}var j=u,S=f;r.createElement=y(r.createElement);S.jsx&&(S.jsx=y(S.jsx));j.jsx&&(j.jsx=y(j.jsx));S.jsxs&&(S.jsxs=y(S.jsxs));j.jsxs&&(j.jsxs=y(j.jsxs));S.jsxDEV&&(S.jsxDEV=y(S.jsxDEV));j.jsxDEV&&(j.jsxDEV=y(j.jsxDEV));Object.defineProperties(o.prototype,{$$typeof:{configurable:!0,value:m},type:{configurable:!0,value:function(r){return r.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});function useSignal(r){return e(function(){return c(r)},p)}function useComputed(r){var n=t(r);n.current=r;return e(function(){return a(function(){return n.current()})},p)}function useSignalEffect(r){var n=t(r);n.current=r;i(function(){return l(function(){return n.current()})},p)}export{useComputed,useSignal,useSignalEffect};//# sourceMappingURL=signals.module.js.map | ||
import{Signal as n,signal as e,computed as r,effect as t}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";import u,{useMemo as i,useRef as f,useEffect as o,__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as c}from"react";import{useSyncExternalStore as a}from"use-sync-external-store/shim/index.js";import l from"react/jsx-runtime";import s from"react/jsx-dev-runtime";var v=[],p=Symbol.for("react.element");function m(n){return i(function(){return e(n)},v)}function g(n){var e=f(n);return e.current=n,i(function(){return r(function(){return e.current()})},v)}function b(n){var e=f(n);e.current=n,o(function(){return t(function(){return e.current()})},v)}Object.defineProperties(n.prototype,{$$typeof:{configurable:!0,value:p},type:{configurable:!0,value:function(n){return n.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var d,y=Symbol.for("react.element");function h(e){if("function"!=typeof e)return e;else return function(r,t){if("string"==typeof r&&t)for(var u in t){var i=t[u];if("children"!==u&&i instanceof n)t[u]=i.value}return e.call.apply(e,[e,r,t].concat([].slice.call(arguments,2)))}}function j(n){if(d)d();d=n&&n.S()}var x=function(){return j()};Object.defineProperties(n.prototype,{$$typeof:{configurable:!0,value:y},type:{configurable:!0,value:function(n){return n.data.value}},props:{configurable:!0,get:function(){return{data:this}}},ref:{configurable:!0,value:null}});var S=null,$=!1,O=null,w=new Map;function C(n){if(!n)return 1;var e,r=w.get(n);if(void 0!==r)return r;var t=n.useCallback.toString();if(n.useReducer===n.useEffect)e=1;else if(n.useEffect===n.useImperativeHandle)e=32;else if(/Invalid/.test(t))e=2;else if(/updateCallback/.test(t)||/\[0\]/.test(t)&&/\[1\]/.test(t)){var u=n.useReducer.toString();if(/rerenderReducer/.test(u)||/return\s*\[\w+,/.test(u))e=16;else e=8}else e=4;w.set(n,e);return e}!function(){Object.defineProperty(c.ReactCurrentDispatcher,"current",{get:function(){return O},set:function(n){if(!$){var e=C(O),r=C(n);O=n;if(function(n,e){if(1&n&&28&e)return!0;else if(2&n||2&e)return!1;else if(16&e)return!0;else return!1}(e,r)){$=!0;S=function(){var n=f();if(null==n.current)n.current=function(){var n,e,r=0,u=t(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;u()}},getSnapshot:function(){return r}}}();var e=n.current;a(e.subscribe,e.getSnapshot,e.getSnapshot);j(e.updater);return x}();$=!1}else if(function(n,e){return Boolean(28&n&&1&e)}(e,r))null==S||S()}else O=n}});!function(){var n=l,e=s;u.createElement=h(u.createElement);e.jsx&&(e.jsx=h(e.jsx));n.jsx&&(n.jsx=h(n.jsx));e.jsxs&&(e.jsxs=h(e.jsxs));n.jsxs&&(n.jsxs=h(n.jsxs));e.jsxDEV&&(e.jsxDEV=h(e.jsxDEV));n.jsxDEV&&(n.jsxDEV=h(n.jsxDEV))}()}();export{g as useComputed,m as useSignal,b as useSignalEffect};//# sourceMappingURL=signals.module.js.map |
{ | ||
"name": "@preact/signals-react", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"license": "MIT", | ||
@@ -31,10 +31,16 @@ "description": "Manage state with style in React", | ||
"browser": "./dist/signals.module.js", | ||
"umd": "./dist/signals.umd.js", | ||
"import": "./dist/signals.mjs", | ||
"require": "./dist/signals.js" | ||
} | ||
}, | ||
"./runtime": { | ||
"types": "./runtime/dist/index.d.ts", | ||
"browser": "./runtime/dist/runtime.module.js", | ||
"import": "./runtime/dist/runtime.mjs", | ||
"require": "./runtime/dist/runtime.js" | ||
}, | ||
"./runtime/package.json": "./runtime/package.json" | ||
}, | ||
"mangle": "../../mangle.json", | ||
"dependencies": { | ||
"@preact/signals-core": "^1.3.0", | ||
"@preact/signals-core": "^1.3.1", | ||
"use-sync-external-store": "^1.2.0" | ||
@@ -41,0 +47,0 @@ }, |
507
src/index.ts
import { | ||
useRef, | ||
useMemo, | ||
useEffect, | ||
// @ts-ignore-next-line | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED as ReactInternals, | ||
type ReactElement, | ||
} from "react"; | ||
import React from "react"; | ||
import jsxRuntime from "react/jsx-runtime"; | ||
import jsxRuntimeDev from "react/jsx-dev-runtime"; | ||
import { | ||
signal, | ||
@@ -21,446 +9,18 @@ computed, | ||
} from "@preact/signals-core"; | ||
import { useSyncExternalStore } from "use-sync-external-store/shim/index.js"; | ||
import type { Effect, JsxRuntimeModule } from "./internal"; | ||
import type { ReactElement } from "react"; | ||
import { useSignal, useComputed, useSignalEffect } from "../runtime"; | ||
import { installAutoSignalTracking } from "../runtime/src/auto"; | ||
export { signal, computed, batch, effect, Signal, type ReadonlySignal }; | ||
export { | ||
signal, | ||
computed, | ||
batch, | ||
effect, | ||
Signal, | ||
type ReadonlySignal, | ||
useSignal, | ||
useComputed, | ||
useSignalEffect, | ||
}; | ||
const Empty = [] as const; | ||
const ReactElemType = Symbol.for("react.element"); // https://github.com/facebook/react/blob/346c7d4c43a0717302d446da9e7423a8e28d8996/packages/shared/ReactSymbols.js#L15 | ||
interface ReactDispatcher { | ||
useRef: typeof React.useRef; | ||
useCallback: typeof React.useCallback; | ||
useReducer: typeof React.useReducer; | ||
useSyncExternalStore: typeof React.useSyncExternalStore; | ||
useEffect: typeof React.useEffect; | ||
useImperativeHandle: typeof React.useImperativeHandle; | ||
} | ||
let finishUpdate: (() => void) | undefined; | ||
function setCurrentUpdater(updater?: Effect) { | ||
// end tracking for the current update: | ||
if (finishUpdate) finishUpdate(); | ||
// start tracking the new update: | ||
finishUpdate = updater && updater._start(); | ||
} | ||
interface EffectStore { | ||
updater: Effect; | ||
subscribe(onStoreChange: () => void): () => void; | ||
getSnapshot(): number; | ||
} | ||
/** | ||
* A redux-like store whose store value is a positive 32bit integer (a 'version'). | ||
* | ||
* React subscribes to this store and gets a snapshot of the current 'version', | ||
* whenever the 'version' changes, we tell React it's time to update the component (call 'onStoreChange'). | ||
* | ||
* How we achieve this is by creating a binding with an 'effect', when the `effect._callback' is called, | ||
* we update our store version and tell React to re-render the component ([1] We don't really care when/how React does it). | ||
* | ||
* [1] | ||
* @see https://react.dev/reference/react/useSyncExternalStore | ||
* @see https://github.com/reactjs/rfcs/blob/main/text/0214-use-sync-external-store.md | ||
*/ | ||
function createEffectStore(): EffectStore { | ||
let updater!: Effect; | ||
let version = 0; | ||
let onChangeNotifyReact: (() => void) | undefined; | ||
let unsubscribe = effect(function (this: Effect) { | ||
updater = this; | ||
}); | ||
updater._callback = function () { | ||
version = (version + 1) | 0; | ||
if (onChangeNotifyReact) onChangeNotifyReact(); | ||
}; | ||
return { | ||
updater, | ||
subscribe(onStoreChange) { | ||
onChangeNotifyReact = onStoreChange; | ||
return function () { | ||
/** | ||
* Rotate to next version when unsubscribing to ensure that components are re-run | ||
* when subscribing again. | ||
* | ||
* In StrictMode, 'memo'-ed components seem to keep a stale snapshot version, so | ||
* don't re-run after subscribing again if the version is the same as last time. | ||
* | ||
* Because we unsubscribe from the effect, the version may not change. We simply | ||
* set a new initial version in case of stale snapshots here. | ||
*/ | ||
version = (version + 1) | 0; | ||
onChangeNotifyReact = undefined; | ||
unsubscribe(); | ||
}; | ||
}, | ||
getSnapshot() { | ||
return version; | ||
}, | ||
}; | ||
} | ||
/** | ||
* Custom hook to create the effect to track signals used during render and | ||
* subscribe to changes to rerender the component when the signals change | ||
*/ | ||
function usePreactSignalStore(nextDispatcher: ReactDispatcher): EffectStore { | ||
const storeRef = nextDispatcher.useRef<EffectStore>(); | ||
if (storeRef.current == null) { | ||
storeRef.current = createEffectStore(); | ||
} | ||
const store = storeRef.current; | ||
useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot); | ||
return store; | ||
} | ||
// In order for signals to work in React, we need to observe what signals a | ||
// component uses while rendering. To do this, we need to know when a component | ||
// is rendering. To do this, we watch the transition of the | ||
// ReactCurrentDispatcher to know when a component is rerendering. | ||
// | ||
// To track when we are entering and exiting a component render (i.e. before and | ||
// after React renders a component), we track how the dispatcher changes. | ||
// Outside of a component rendering, the dispatcher is set to an instance that | ||
// errors or warns when any hooks are called. This behavior is prevents hooks | ||
// from being used outside of components. Right before React renders a | ||
// component, the dispatcher is set to an instance that doesn't warn or error | ||
// and contains the implementations of all hooks. Right after React finishes | ||
// rendering a component, the dispatcher is set to the erroring one again. This | ||
// erroring dispatcher is called the `ContextOnlyDispatcher` in React's source. | ||
// | ||
// So, we watch the getter and setter on `ReactCurrentDispatcher.current` to | ||
// monitor the changes to the current ReactDispatcher. When the dispatcher | ||
// changes from the ContextOnlyDispatcher to a "valid" dispatcher, we assume we | ||
// are entering a component render. At this point, we setup our | ||
// auto-subscriptions for any signals used in the component. We do this by | ||
// creating an Signal effect and manually starting the Signal effect. We use | ||
// `useSyncExternalStore` to trigger rerenders on the component when any signals | ||
// it uses changes. | ||
// | ||
// When the dispatcher changes from a valid dispatcher back to the | ||
// ContextOnlyDispatcher, we assume we are exiting a component render. At this | ||
// point we stop the effect. | ||
// | ||
// Some additional complexities to be aware of: | ||
// - If a component calls `setState` while rendering, React will re-render the | ||
// component immediately. Before triggering the re-render, React will change | ||
// the dispatcher to the HooksDispatcherOnRerender. When we transition to this | ||
// rerendering adapter, we need to re-trigger our hooks to keep the order of | ||
// hooks the same for every render of a component. | ||
// | ||
// - In development, useReducer, useState, and useMemo change the dispatcher to | ||
// a different warning dispatcher (not ContextOnlyDispatcher) before invoking | ||
// the reducer and resets it right after. | ||
// | ||
// The useSyncExternalStore shim will use some of these hooks when we invoke | ||
// it while entering a component render. We need to prevent this dispatcher | ||
// change caused by these hooks from re-triggering our entering logic (it | ||
// would cause an infinite loop if we did not). We do this by using a lock to | ||
// prevent the setter from running while we are in the setter. | ||
// | ||
// When a Component's function body invokes useReducer, useState, or useMemo, | ||
// this change in dispatcher should not signal that we are entering or exiting | ||
// a component render. We ignore this change by detecting these dispatchers as | ||
// different from ContextOnlyDispatcher and other valid dispatchers. | ||
// | ||
// - The `use` hook will change the dispatcher to from a valid update dispatcher | ||
// to a valid mount dispatcher in some cases. Similarly to useReducer | ||
// mentioned above, we should not signal that we are exiting a component | ||
// during this change. Because these other valid dispatchers do not pass the | ||
// ContextOnlyDispatcher check, they do not affect our logic. | ||
// | ||
// - When server rendering, React does not change the dispatcher before and | ||
// after each component render. It sets it once for before the first render | ||
// and once for after the last render. This means that we will not be able to | ||
// detect when we are entering or exiting a component render. This is fine | ||
// because we don't need to detect this for server rendering. A component | ||
// can't trigger async rerenders in SSR so we don't need to track signals. | ||
// | ||
// If a component updates a signal value while rendering during SSR, we will | ||
// not rerender the component because the signal value will synchronously | ||
// change so all reads of the signal further down the tree will see the new | ||
// value. | ||
/* | ||
Below is a state machine definition for transitions between the various | ||
dispatchers in React's prod build. (It does not include dev time warning | ||
dispatchers which are just always ignored). | ||
ENTER and EXIT suffixes indicates whether this ReactCurrentDispatcher transition | ||
signals we are entering or exiting a component render, or if it doesn't signal a | ||
change in the component rendering lifecyle (NOOP). | ||
```js | ||
// Paste this into https://stately.ai/viz to visualize the state machine. | ||
import { createMachine } from "xstate"; | ||
// ENTER, EXIT, NOOP suffixes indicates whether this ReactCurrentDispatcher | ||
// transition signals we are entering or exiting a component render, or | ||
// if it doesn't signal a change in the component rendering lifecyle (NOOP). | ||
const dispatcherMachinePROD = createMachine({ | ||
id: "ReactCurrentDispatcher_PROD", | ||
initial: "null", | ||
states: { | ||
null: { | ||
on: { | ||
pushDispatcher: "ContextOnlyDispatcher", | ||
}, | ||
}, | ||
ContextOnlyDispatcher: { | ||
on: { | ||
renderWithHooks_Mount_ENTER: "HooksDispatcherOnMount", | ||
renderWithHooks_Update_ENTER: "HooksDispatcherOnUpdate", | ||
pushDispatcher_NOOP: "ContextOnlyDispatcher", | ||
popDispatcher_NOOP: "ContextOnlyDispatcher", | ||
}, | ||
}, | ||
HooksDispatcherOnMount: { | ||
on: { | ||
renderWithHooksAgain_ENTER: "HooksDispatcherOnRerender", | ||
resetHooksAfterThrow_EXIT: "ContextOnlyDispatcher", | ||
finishRenderingHooks_EXIT: "ContextOnlyDispatcher", | ||
}, | ||
}, | ||
HooksDispatcherOnUpdate: { | ||
on: { | ||
renderWithHooksAgain_ENTER: "HooksDispatcherOnRerender", | ||
resetHooksAfterThrow_EXIT: "ContextOnlyDispatcher", | ||
finishRenderingHooks_EXIT: "ContextOnlyDispatcher", | ||
use_ResumeSuspensedMount_NOOP: "HooksDispatcherOnMount", | ||
}, | ||
}, | ||
HooksDispatcherOnRerender: { | ||
on: { | ||
renderWithHooksAgain_ENTER: "HooksDispatcherOnRerender", | ||
resetHooksAfterThrow_EXIT: "ContextOnlyDispatcher", | ||
finishRenderingHooks_EXIT: "ContextOnlyDispatcher", | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
*/ | ||
let lock = false; | ||
let currentDispatcher: ReactDispatcher | null = null; | ||
Object.defineProperty(ReactInternals.ReactCurrentDispatcher, "current", { | ||
get() { | ||
return currentDispatcher; | ||
}, | ||
set(nextDispatcher: ReactDispatcher) { | ||
if (lock) { | ||
currentDispatcher = nextDispatcher; | ||
return; | ||
} | ||
const currentDispatcherType = getDispatcherType(currentDispatcher); | ||
const nextDispatcherType = getDispatcherType(nextDispatcher); | ||
// Update the current dispatcher now so the hooks inside of the | ||
// useSyncExternalStore shim get the right dispatcher. | ||
currentDispatcher = nextDispatcher; | ||
if (isEnteringComponentRender(currentDispatcherType, nextDispatcherType)) { | ||
lock = true; | ||
const store = usePreactSignalStore(nextDispatcher); | ||
lock = false; | ||
setCurrentUpdater(store.updater); | ||
} else if ( | ||
isExitingComponentRender(currentDispatcherType, nextDispatcherType) | ||
) { | ||
setCurrentUpdater(); | ||
} | ||
}, | ||
}); | ||
type DispatcherType = number; | ||
const ContextOnlyDispatcherType = 1 << 0; | ||
const WarningDispatcherType = 1 << 1; | ||
const MountDispatcherType = 1 << 2; | ||
const UpdateDispatcherType = 1 << 3; | ||
const RerenderDispatcherType = 1 << 4; | ||
const ServerDispatcherType = 1 << 5; | ||
const BrowserClientDispatcherType = | ||
MountDispatcherType | UpdateDispatcherType | RerenderDispatcherType; | ||
const dispatcherTypeCache = new Map<ReactDispatcher, DispatcherType>(); | ||
function getDispatcherType(dispatcher: ReactDispatcher | null): DispatcherType { | ||
// Treat null the same as the ContextOnlyDispatcher. | ||
if (!dispatcher) return ContextOnlyDispatcherType; | ||
const cached = dispatcherTypeCache.get(dispatcher); | ||
if (cached !== undefined) return cached; | ||
// The ContextOnlyDispatcher sets all the hook implementations to a function | ||
// that takes no arguments and throws and error. This dispatcher is the only | ||
// dispatcher where useReducer and useEffect will have the same | ||
// implementation. | ||
let type: DispatcherType; | ||
const useCallbackImpl = dispatcher.useCallback.toString(); | ||
if (dispatcher.useReducer === dispatcher.useEffect) { | ||
type = ContextOnlyDispatcherType; | ||
// @ts-expect-error When server rendering, useEffect and useImperativeHandle | ||
// are both set to noop functions and so have the same implementation. | ||
} else if (dispatcher.useEffect === dispatcher.useImperativeHandle) { | ||
type = ServerDispatcherType; | ||
} else if (/Invalid/.test(useCallbackImpl)) { | ||
// We first check for warning dispatchers because they would also pass some | ||
// of the checks below. | ||
type = WarningDispatcherType; | ||
} else if ( | ||
// The development mount dispatcher invokes a function called | ||
// `mountCallback` whereas the development update/re-render dispatcher | ||
// invokes a function called `updateCallback`. Use that difference to | ||
// determine if we are in a mount or update-like dispatcher in development. | ||
// The production mount dispatcher defines an array of the form [callback, | ||
// deps] whereas update/re-render dispatchers read the array using array | ||
// indices (e.g. `[0]` and `[1]`). Use those differences to determine if we | ||
// are in a mount or update-like dispatcher in production. | ||
/updateCallback/.test(useCallbackImpl) || | ||
(/\[0\]/.test(useCallbackImpl) && /\[1\]/.test(useCallbackImpl)) | ||
) { | ||
// The update and rerender dispatchers have different implementations for | ||
// useReducer. We'll check it's implementation to determine if this is the | ||
// rerender or update dispatcher. | ||
let useReducerImpl = dispatcher.useReducer.toString(); | ||
if ( | ||
// The development rerender dispatcher invokes a function called | ||
// `rerenderReducer` whereas the update dispatcher invokes a function | ||
// called `updateReducer`. The production rerender dispatcher returns an | ||
// array of the form `[state, dispatch]` whereas the update dispatcher | ||
// returns an array of `[fiber.memoizedState, dispatch]` so we check the | ||
// return statement in the implementation of useReducer to differentiate | ||
// between the two. | ||
/rerenderReducer/.test(useReducerImpl) || | ||
/return\s*\[\w+,/.test(useReducerImpl) | ||
) { | ||
type = RerenderDispatcherType; | ||
} else { | ||
type = UpdateDispatcherType; | ||
} | ||
} else { | ||
type = MountDispatcherType; | ||
} | ||
dispatcherTypeCache.set(dispatcher, type); | ||
return type; | ||
} | ||
function isEnteringComponentRender( | ||
currentDispatcherType: DispatcherType, | ||
nextDispatcherType: DispatcherType | ||
): boolean { | ||
if ( | ||
currentDispatcherType & ContextOnlyDispatcherType && | ||
nextDispatcherType & BrowserClientDispatcherType | ||
) { | ||
// ## Mount or update (ContextOnlyDispatcher -> ValidDispatcher (Mount or Update)) | ||
// | ||
// If the current dispatcher is the ContextOnlyDispatcher and the next | ||
// dispatcher is a valid dispatcher, we are entering a component render. | ||
return true; | ||
} else if ( | ||
currentDispatcherType & WarningDispatcherType || | ||
nextDispatcherType & WarningDispatcherType | ||
) { | ||
// ## Warning dispatcher | ||
// | ||
// If the current dispatcher or next dispatcher is an warning dispatcher, | ||
// we are not entering a component render. The current warning dispatchers | ||
// are used to warn when hooks are nested improperly and do not indicate | ||
// entering a new component render. | ||
return false; | ||
} else if (nextDispatcherType & RerenderDispatcherType) { | ||
// Any transition into the rerender dispatcher is the beginning of a | ||
// component render, so we should invoke our hooks. Details below. | ||
// | ||
// ## In-place rerendering (e.g. Mount -> Rerender) | ||
// | ||
// If we are transitioning from the mount, update, or rerender dispatcher to | ||
// the rerender dispatcher (e.g. HooksDispatcherOnMount to | ||
// HooksDispatcherOnRerender), then this component is rerendering due to | ||
// calling setState inside of its function body. We are re-entering a | ||
// component's render method and so we should re-invoke our hooks. | ||
return true; | ||
} else { | ||
// ## Resuming suspended mount edge case (Update -> Mount) | ||
// | ||
// If we are transitioning from the update dispatcher to the mount | ||
// dispatcher, then this component is using the `use` hook and is resuming | ||
// from a mount. We should not re-invoke our hooks in this situation since | ||
// we are not entering a new component render, but instead continuing a | ||
// previous render. | ||
// | ||
// ## Other transitions | ||
// | ||
// For example, Mount -> Mount, Update -> Update, Mount -> Update, any | ||
// transition in and out of invalid dispatchers. | ||
// | ||
// There is no known transition for the following transitions so we default | ||
// to not triggering a re-enter of the component. | ||
// - HooksDispatcherOnMount -> HooksDispatcherOnMount | ||
// - HooksDispatcherOnMount -> HooksDispatcherOnUpdate | ||
// - HooksDispatcherOnUpdate -> HooksDispatcherOnUpdate | ||
return false; | ||
} | ||
} | ||
/** | ||
* We are exiting a component render if the current dispatcher is a valid | ||
* dispatcher and the next dispatcher is the ContextOnlyDispatcher. | ||
*/ | ||
function isExitingComponentRender( | ||
currentDispatcherType: DispatcherType, | ||
nextDispatcherType: DispatcherType | ||
): boolean { | ||
return Boolean( | ||
currentDispatcherType & BrowserClientDispatcherType && | ||
nextDispatcherType & ContextOnlyDispatcherType | ||
); | ||
} | ||
function WrapJsx<T>(jsx: T): T { | ||
if (typeof jsx !== "function") return jsx; | ||
return function (type: any, props: any, ...rest: any[]) { | ||
if (typeof type === "string" && props) { | ||
for (let i in props) { | ||
let v = props[i]; | ||
if (i !== "children" && v instanceof Signal) { | ||
props[i] = v.value; | ||
} | ||
} | ||
} | ||
return jsx.call(jsx, type, props, ...rest); | ||
} as any as T; | ||
} | ||
const JsxPro: JsxRuntimeModule = jsxRuntime; | ||
const JsxDev: JsxRuntimeModule = jsxRuntimeDev; | ||
/** | ||
* createElement _may_ be called by jsx runtime as a fallback in certain cases, | ||
* so we need to wrap it regardless. | ||
* | ||
* The jsx exports depend on the `NODE_ENV` var to ensure the users' bundler doesn't | ||
* include both, so one of them will be set with `undefined` values. | ||
*/ | ||
React.createElement = WrapJsx(React.createElement); | ||
JsxDev.jsx && /* */ (JsxDev.jsx = WrapJsx(JsxDev.jsx)); | ||
JsxPro.jsx && /* */ (JsxPro.jsx = WrapJsx(JsxPro.jsx)); | ||
JsxDev.jsxs && /* */ (JsxDev.jsxs = WrapJsx(JsxDev.jsxs)); | ||
JsxPro.jsxs && /* */ (JsxPro.jsxs = WrapJsx(JsxPro.jsxs)); | ||
JsxDev.jsxDEV && /**/ (JsxDev.jsxDEV = WrapJsx(JsxDev.jsxDEV)); | ||
JsxPro.jsxDEV && /**/ (JsxPro.jsxDEV = WrapJsx(JsxPro.jsxDEV)); | ||
declare module "@preact/signals-core" { | ||
@@ -472,39 +32,2 @@ // @ts-ignore internal Signal is viewed as function | ||
/** | ||
* A wrapper component that renders a Signal's value directly as a Text node. | ||
*/ | ||
function Text({ data }: { data: Signal }) { | ||
return data.value; | ||
} | ||
// Decorate Signals so React renders them as <Text> components. | ||
Object.defineProperties(Signal.prototype, { | ||
$$typeof: { configurable: true, value: ReactElemType }, | ||
type: { configurable: true, value: Text }, | ||
props: { | ||
configurable: true, | ||
get() { | ||
return { data: this }; | ||
}, | ||
}, | ||
ref: { configurable: true, value: null }, | ||
}); | ||
export function useSignal<T>(value: T) { | ||
return useMemo(() => signal<T>(value), Empty); | ||
} | ||
export function useComputed<T>(compute: () => T) { | ||
const $compute = useRef(compute); | ||
$compute.current = compute; | ||
return useMemo(() => computed<T>(() => $compute.current()), Empty); | ||
} | ||
export function useSignalEffect(cb: () => void | (() => void)) { | ||
const callback = useRef(cb); | ||
callback.current = cb; | ||
useEffect(() => { | ||
return effect(() => callback.current()); | ||
}, Empty); | ||
} | ||
installAutoSignalTracking(); |
@@ -115,3 +115,4 @@ import React from "react"; | ||
if (errorSpy.firstCall.args[0].toString().includes("%s")) { | ||
message = consoleFormat(...errorSpy.firstCall.args); | ||
const firstArg = errorSpy.firstCall.args[0]; | ||
message = consoleFormat(firstArg, ...errorSpy.firstCall.args.slice(1)); | ||
} else { | ||
@@ -118,0 +119,0 @@ message = errorSpy.firstCall.args.join(" "); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
293127
34
1554
0
0
4
Updated@preact/signals-core@^1.3.1