Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@preact/signals

Package Overview
Dependencies
Maintainers
10
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@preact/signals - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

test/exports.test.tsx

13

CHANGELOG.md
# @preact/signals
## 1.0.4
### Patch Changes
- [#147](https://github.com/preactjs/signals/pull/147) [`3556499`](https://github.com/preactjs/signals/commit/355649903b766630b62cdd0f90a35d3eafa99fa9) Thanks [@developit](https://github.com/developit)! - Improve performance when rendering Signals as Text in Preact.
* [#148](https://github.com/preactjs/signals/pull/148) [`b948745`](https://github.com/preactjs/signals/commit/b948745de7b5b60a20ce3bdc5ee72d47d47f38ec) Thanks [@marvinhagemeister](https://github.com/marvinhagemeister)! - Move `types` field in `package.json` to the top of the entry list to ensure that TypeScript always finds it.
- [#153](https://github.com/preactjs/signals/pull/153) [`0da9ce3`](https://github.com/preactjs/signals/commit/0da9ce3c6f57cef67c3e84f0d829421aee8defff) Thanks [@developit](https://github.com/developit)! - Optimize the performance of prop bindings in Preact
- Updated dependencies [[`f2ba3d6`](https://github.com/preactjs/signals/commit/f2ba3d657bf8169c6ba1d47c0827aa18cfe1c947), [`160ea77`](https://github.com/preactjs/signals/commit/160ea7791f3adb55c562f5990e0b4848d8491a38), [`4385ea8`](https://github.com/preactjs/signals/commit/4385ea8c8358a154d8b789685bb061658ce1153f), [`b948745`](https://github.com/preactjs/signals/commit/b948745de7b5b60a20ce3bdc5ee72d47d47f38ec), [`00a59c6`](https://github.com/preactjs/signals/commit/00a59c6475bd4542fb934474d82d1e242b2ac870)]:
- @preact/signals-core@1.1.1
## 1.0.3

@@ -4,0 +17,0 @@

3

dist/signals.js

@@ -1,2 +0,1 @@

var n,r,t,e=require("preact"),i=require("preact/hooks"),u=require("@preact/signals-core"),o=new WeakSet,f=new WeakSet,c=new WeakSet;function a(n,r){e.options[n]=r.bind(null,e.options[n]||function(){})}var v=new WeakMap;function s(n){t&&t(!0,!0),r=n,t=n&&n._()}function l(n){var r=u.signal(void 0);return r._c=!0,r._u=n,r}function b(n){var r=v.get(n);if(r)r.__.length=0;else{var t=[];(r=l(function(){for(var r=n.__e,e=0;e<t.length;e++){var i=t[e],u=i.t,o=i.i._v;if(!r)return;u in r?r[u]=o:o?r.setAttribute(u,o):r.removeAttribute(u)}})).__=t,v.set(n,r)}return r}function p(n,r,t){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(p):n instanceof u.Signal&&(t[r]=e.createElement(_,{data:n})))}function _(n){var t=this,e=n.data,o=h(e);o.value=e;var f=i.useMemo(function(){for(var n=t.__v;n=n.__;)if(n.__c){c.add(n.__c);break}return r._u=function(){t.base.data=f._v},u.computed(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function h(n){return i.useMemo(function(){return u.signal(n)},[])}_.displayName="_st",a("__b",function(n,r){if("string"==typeof r.type){var t,e=r.props;for(var i in e){var o=e[i];"children"===i?p(o,"children",e):o instanceof u.Signal&&function(){t||(t=b(r)),t.__.push({t:i,i:o});var n=t._u;if(o._u){var u=o._u;o._u=function(){n(),u()}}else o._u=n;e[i]=o.peek()}()}s(t)}n(r)}),a("__r",function(r,t){var e,i=t.__c;i&&(o.delete(i),void 0===(e=v.get(i))&&(e=l(function(){o.add(i),i.setState({})}),v.set(i,e))),n=i,s(e),r(t)}),a("__e",function(r,t,e,i){s(),n=void 0,r(t,e,i)}),a("diffed",function(r,t){s(),n=void 0,r(t)}),a("unmount",function(n,r){var t=r.__c||r,e=v.get(t);if(e){v.delete(t);var i=e._d;i&&(i.forEach(function(n){return n._s.delete(e)}),i.clear())}n(r)}),a("__h",function(n,r,t,e){e<3&&f.add(r),n(r,t,e)}),e.Component.prototype.shouldComponentUpdate=function(n,r){var t,e=v.get(this);if(!(e&&0!==(null==(t=e._d)?void 0:t.size)||c.has(this)))return!0;if(o.has(this))return!0;if(f.has(this))return!0;for(var i in r)return!0;for(var u in n)if("__source"!==u&&n[u]!==this.props[u])return!0;for(var a in this.props)if(!(a in n))return!0;return!1},Object.defineProperty(exports,"Signal",{enumerable:!0,get:function(){return u.Signal}}),Object.defineProperty(exports,"batch",{enumerable:!0,get:function(){return u.batch}}),Object.defineProperty(exports,"computed",{enumerable:!0,get:function(){return u.computed}}),Object.defineProperty(exports,"effect",{enumerable:!0,get:function(){return u.effect}}),Object.defineProperty(exports,"signal",{enumerable:!0,get:function(){return u.signal}}),exports.useComputed=function(r){var t=i.useRef(r);return t.current=r,c.add(n),i.useMemo(function(){return u.computed(function(){return t.current()})},[])},exports.useSignal=h;
//# sourceMappingURL=signals.js.map
var r,n,t,i=require("preact"),e=require("preact/hooks"),o=require("@preact/signals-core"),u=new WeakSet,f=new WeakSet,a=new WeakSet;function c(r,n){i.options[r]=n.bind(null,i.options[r]||function(){})}var v=new WeakMap;function s(r){t&&t(!0,!0),n=r,t=r&&r._()}function p(r){var n=o.signal(void 0);return n._u=r,n}function l(r){var t=this,i=r.data,u=h(i);u.value=i;var f=e.useMemo(function(){for(var r=t.__v;r=r.__;)if(r.__c){a.add(r.__c);break}return n._u=function(){t.base.data=f._v},o.computed(function(){var r=u.value.value;return 0===r?0:!0===r?"":r||""})},[]);return f.value}function _(r){var n={__proto__:null},t=p(function(i){var e=t.__;for(var u in e)if("children"!==u){var f=e[u];if(f instanceof o.Signal){var a=f.value,c=n[u];n[u]=a,!0===i||c===a||(u in r?r[u]=a:a?r.setAttribute(u,a):r.removeAttribute(u))}}});return t}function h(r){return e.useMemo(function(){return o.signal(r)},[])}l.displayName="_st",Object.defineProperties(o.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:l},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),c("__b",function(r,n){if("string"==typeof n.type){var t,i=n.props;for(var e in i)if("children"!==e){var u=i[e];u instanceof o.Signal&&(t||(n.__np=t={}),t[e]=u,i[e]=u.peek())}}r(n)}),c("__r",function(n,t){var i,e=t.__c;e&&(u.delete(e),void 0===(i=v.get(e))&&(i=p(function(){u.add(e),e.setState({})}),v.set(e,i))),r=e,s(i),n(t)}),c("__e",function(n,t,i,e){s(),r=void 0,n(t,i,e)}),c("diffed",function(n,t){var i,e;if(s(),r=void 0,"string"==typeof t.type&&(i=t.__e)){var o=t.__np;o&&((e=i._u)||(e=_(i),i._u=e),e.__=o,s(e),e._u(!0))}n(t)}),c("unmount",function(r,n){var t=n.__c,i=t&&v.get(t);if(i&&(v.delete(t),i._()(!0,!0)),"string"==typeof n.type){var e=n.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}r(n)}),c("__h",function(r,n,t,i){i<3&&f.add(n),r(n,t,i)}),i.Component.prototype.shouldComponentUpdate=function(r,n){var t,i=v.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||a.has(this)))return!0;if(u.has(this))return!0;if(f.has(this))return!0;for(var e in n)return!0;for(var o in r)if("__source"!==o&&r[o]!==this.props[o])return!0;for(var c in this.props)if(!(c in r))return!0;return!1},exports.Signal=o.Signal,exports.batch=o.batch,exports.computed=o.computed,exports.effect=o.effect,exports.signal=o.signal,exports.useComputed=function(n){var t=e.useRef(n);return t.current=n,a.add(r),e.useMemo(function(){return o.computed(function(){return t.current()})},[])},exports.useSignal=h;//# sourceMappingURL=signals.js.map

@@ -1,2 +0,1 @@

!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],e):e((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,e,r,t){var i,u,o,f=new WeakSet,c=new WeakSet,a=new WeakSet;function s(n,r){e.options[n]=r.bind(null,e.options[n]||function(){})}var v=new WeakMap;function l(n){o&&o(!0,!0),u=n,o=n&&n._()}function d(n){var e=t.signal(void 0);return e._c=!0,e._u=n,e}function b(n){var e=v.get(n);if(e)e.__.length=0;else{var r=[];(e=d(function(){for(var e=n.__e,t=0;t<r.length;t++){var i=r[t],u=i.t,o=i.i._v;if(!e)return;u in e?e[u]=o:o?e.setAttribute(u,o):e.removeAttribute(u)}})).__=r,v.set(n,e)}return e}function p(n,r,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(p):n instanceof t.Signal&&(i[r]=e.createElement(h,{data:n})))}function h(n){var e=this,i=n.data,o=g(i);o.value=i;var f=r.useMemo(function(){for(var n=e.__v;n=n.__;)if(n.__c){a.add(n.__c);break}return u._u=function(){e.base.data=f._v},t.computed(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function g(n){return r.useMemo(function(){return t.signal(n)},[])}h.displayName="_st",s("__b",function(n,e){if("string"==typeof e.type){var r,i=e.props;for(var u in i){var o=i[u];"children"===u?p(o,"children",i):o instanceof t.Signal&&function(){r||(r=b(e)),r.__.push({t:u,i:o});var n=r._u;if(o._u){var t=o._u;o._u=function(){n(),t()}}else o._u=n;i[u]=o.peek()}()}l(r)}n(e)}),s("__r",function(n,e){var r,t=e.__c;t&&(f.delete(t),void 0===(r=v.get(t))&&(r=d(function(){f.add(t),t.setState({})}),v.set(t,r))),i=t,l(r),n(e)}),s("__e",function(n,e,r,t){l(),i=void 0,n(e,r,t)}),s("diffed",function(n,e){l(),i=void 0,n(e)}),s("unmount",function(n,e){var r=e.__c||e,t=v.get(r);if(t){v.delete(r);var i=t._d;i&&(i.forEach(function(n){return n._s.delete(t)}),i.clear())}n(e)}),s("__h",function(n,e,r,t){t<3&&c.add(e),n(e,r,t)}),e.Component.prototype.shouldComponentUpdate=function(n,e){var r,t=v.get(this);if(!(t&&0!==(null==(r=t._d)?void 0:r.size)||a.has(this)))return!0;if(f.has(this))return!0;if(c.has(this))return!0;for(var i in e)return!0;for(var u in n)if("__source"!==u&&n[u]!==this.props[u])return!0;for(var o in this.props)if(!(o in n))return!0;return!1},Object.defineProperty(n,"Signal",{enumerable:!0,get:function(){return t.Signal}}),Object.defineProperty(n,"batch",{enumerable:!0,get:function(){return t.batch}}),Object.defineProperty(n,"computed",{enumerable:!0,get:function(){return t.computed}}),Object.defineProperty(n,"effect",{enumerable:!0,get:function(){return t.effect}}),Object.defineProperty(n,"signal",{enumerable:!0,get:function(){return t.signal}}),n.useComputed=function(n){var e=r.useRef(n);return e.current=n,a.add(i),r.useMemo(function(){return t.computed(function(){return e.current()})},[])},n.useSignal=g});
//# sourceMappingURL=signals.min.js.map
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("preact"),require("preact/hooks"),require("@preact/signals-core")):"function"==typeof define&&define.amd?define(["exports","preact","preact/hooks","@preact/signals-core"],r):r((n||self).preactSignals={},n.preact,n.hooks,n.signalsCore)}(this,function(n,r,t,i){var e,o,f,u=new WeakSet,a=new WeakSet,c=new WeakSet;function v(n,t){r.options[n]=t.bind(null,r.options[n]||function(){})}var s=new WeakMap;function l(n){f&&f(!0,!0),o=n,f=n&&n._()}function p(n){var r=i.signal(void 0);return r._u=n,r}function d(n){var r=this,e=n.data,f=h(e);f.value=e;var u=t.useMemo(function(){for(var n=r.__v;n=n.__;)if(n.__c){c.add(n.__c);break}return o._u=function(){r.base.data=u._v},i.computed(function(){var n=f.value.value;return 0===n?0:!0===n?"":n||""})},[]);return u.value}function _(n){var r={__proto__:null},t=p(function(e){var o=t.__;for(var f in o)if("children"!==f){var u=o[f];if(u instanceof i.Signal){var a=u.value,c=r[f];r[f]=a,!0===e||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function h(n){return t.useMemo(function(){return i.signal(n)},[])}d.displayName="_st",Object.defineProperties(i.Signal.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),v("__b",function(n,r){if("string"==typeof r.type){var t,e=r.props;for(var o in e)if("children"!==o){var f=e[o];f instanceof i.Signal&&(t||(r.__np=t={}),t[o]=f,e[o]=f.peek())}}n(r)}),v("__r",function(n,r){var t,i=r.__c;i&&(u.delete(i),void 0===(t=s.get(i))&&(t=p(function(){u.add(i),i.setState({})}),s.set(i,t))),e=i,l(t),n(r)}),v("__e",function(n,r,t,i){l(),e=void 0,n(r,t,i)}),v("diffed",function(n,r){var t,i;if(l(),e=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=_(t),t._u=i),i.__=o,l(i),i._u(!0))}n(r)}),v("unmount",function(n,r){var t=r.__c,i=t&&s.get(t);if(i&&(s.delete(t),i._()(!0,!0)),"string"==typeof r.type){var e=r.__e,o=e._u;o&&(o._()(!0,!0),e._u=null)}n(r)}),v("__h",function(n,r,t,i){i<3&&a.add(r),n(r,t,i)}),r.Component.prototype.shouldComponentUpdate=function(n,r){var t,i=s.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||c.has(this)))return!0;if(u.has(this))return!0;if(a.has(this))return!0;for(var e in r)return!0;for(var o in n)if("__source"!==o&&n[o]!==this.props[o])return!0;for(var f in this.props)if(!(f in n))return!0;return!1},n.Signal=i.Signal,n.batch=i.batch,n.computed=i.computed,n.effect=i.effect,n.signal=i.signal,n.useComputed=function(n){var r=t.useRef(n);return r.current=n,c.add(e),t.useMemo(function(){return i.computed(function(){return r.current()})},[])},n.useSignal=h});//# sourceMappingURL=signals.min.js.map

@@ -1,2 +0,1 @@

import{Component as n,options as r,createElement as t}from"preact";import{useMemo as i,useRef as o}from"preact/hooks";import{Signal as e,signal as f,computed as u}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var a,c,v,s=new WeakSet,l=new WeakSet,p=new WeakSet;function _(n,t){r[n]=t.bind(null,r[n]||function(){})}var h=new WeakMap;function d(n){v&&v(!0,!0),c=n,v=n&&n._()}function m(n){var r=f(void 0);return r._c=!0,r._u=n,r}function k(n){var r=h.get(n);if(r)r.__.length=0;else{var t=[];(r=m(function(){for(var r=n.__e,i=0;i<t.length;i++){var o=t[i],e=o.t,f=o.i._v;if(!r)return;e in r?r[e]=f:f?r.setAttribute(e,f):r.removeAttribute(e)}})).__=t,h.set(n,r)}return r}function g(n,r,i){"object"!=typeof n||null==n||(Array.isArray(n)?n.forEach(g):n instanceof e&&(i[r]=t(b,{data:n})))}function b(n){var r=this,t=n.data,o=w(t);o.value=t;var e=i(function(){for(var n=r.__v;n=n.__;)if(n.__c){p.add(n.__c);break}return c._u=function(){r.base.data=e._v},u(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return e.value}function w(n){return i(function(){return f(n)},[])}function y(n){var r=o(n);return r.current=n,p.add(a),i(function(){return u(function(){return r.current()})},[])}b.displayName="_st",_("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var o in i){var f=i[o];"children"===o?g(f,"children",i):f instanceof e&&function(){t||(t=k(r)),t.__.push({t:o,i:f});var n=t._u;if(f._u){var e=f._u;f._u=function(){n(),e()}}else f._u=n;i[o]=f.peek()}()}d(t)}n(r)}),_("__r",function(n,r){var t,i=r.__c;i&&(s.delete(i),void 0===(t=h.get(i))&&(t=m(function(){s.add(i),i.setState({})}),h.set(i,t))),a=i,d(t),n(r)}),_("__e",function(n,r,t,i){d(),a=void 0,n(r,t,i)}),_("diffed",function(n,r){d(),a=void 0,n(r)}),_("unmount",function(n,r){var t=r.__c||r,i=h.get(t);if(i){h.delete(t);var o=i._d;o&&(o.forEach(function(n){return n._s.delete(i)}),o.clear())}n(r)}),_("__h",function(n,r,t,i){i<3&&l.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=h.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||p.has(this)))return!0;if(s.has(this))return!0;if(l.has(this))return!0;for(var o in r)return!0;for(var e in n)if("__source"!==e&&n[e]!==this.props[e])return!0;for(var f in this.props)if(!(f in n))return!0;return!1};export{y as useComputed,w as useSignal};
//# sourceMappingURL=signals.module.js.map
import{Component as n,options as r}from"preact";import{useMemo as t,useRef as i}from"preact/hooks";import{Signal as o,computed as e,signal as f}from"@preact/signals-core";export{Signal,batch,computed,effect,signal}from"@preact/signals-core";var u,a,c,v=new WeakSet,s=new WeakSet,l=new WeakSet;function p(n,t){r[n]=t.bind(null,r[n]||function(){})}var _=new WeakMap;function h(n){c&&c(!0,!0),a=n,c=n&&n._()}function g(n){var r=f(void 0);return r._u=n,r}function d(n){var r=this,i=n.data,o=m(i);o.value=i;var f=t(function(){for(var n=r.__v;n=n.__;)if(n.__c){l.add(n.__c);break}return a._u=function(){r.base.data=f._v},e(function(){var n=o.value.value;return 0===n?0:!0===n?"":n||""})},[]);return f.value}function b(n){var r={__proto__:null},t=g(function(i){var e=t.__;for(var f in e)if("children"!==f){var u=e[f];if(u instanceof o){var a=u.value,c=r[f];r[f]=a,!0===i||c===a||(f in n?n[f]=a:a?n.setAttribute(f,a):n.removeAttribute(f))}}});return t}function m(n){return t(function(){return f(n)},[])}function k(n){var r=i(n);return r.current=n,l.add(u),t(function(){return e(function(){return r.current()})},[])}d.displayName="_st",Object.defineProperties(o.prototype,{constructor:{configurable:!0},type:{configurable:!0,value:d},props:{configurable:!0,get:function(){return{data:this}}},__b:{configurable:!0,value:1}}),p("__b",function(n,r){if("string"==typeof r.type){var t,i=r.props;for(var e in i)if("children"!==e){var f=i[e];f instanceof o&&(t||(r.__np=t={}),t[e]=f,i[e]=f.peek())}}n(r)}),p("__r",function(n,r){var t,i=r.__c;i&&(v.delete(i),void 0===(t=_.get(i))&&(t=g(function(){v.add(i),i.setState({})}),_.set(i,t))),u=i,h(t),n(r)}),p("__e",function(n,r,t,i){h(),u=void 0,n(r,t,i)}),p("diffed",function(n,r){var t,i;if(h(),u=void 0,"string"==typeof r.type&&(t=r.__e)){var o=r.__np;o&&((i=t._u)||(i=b(t),t._u=i),i.__=o,h(i),i._u(!0))}n(r)}),p("unmount",function(n,r){var t=r.__c,i=t&&_.get(t);if(i&&(_.delete(t),i._()(!0,!0)),"string"==typeof r.type){var o=r.__e,e=o._u;e&&(e._()(!0,!0),o._u=null)}n(r)}),p("__h",function(n,r,t,i){i<3&&s.add(r),n(r,t,i)}),n.prototype.shouldComponentUpdate=function(n,r){var t,i=_.get(this);if(!(i&&0!==(null==(t=i._d)?void 0:t.size)||l.has(this)))return!0;if(v.has(this))return!0;if(s.has(this))return!0;for(var o in r)return!0;for(var e in n)if("__source"!==e&&n[e]!==this.props[e])return!0;for(var f in this.props)if(!(f in n))return!0;return!1};export{k as useComputed,m as useSignal};//# sourceMappingURL=signals.module.js.map
{
"name": "@preact/signals",
"version": "1.0.3",
"version": "1.0.4",
"license": "MIT",

@@ -12,3 +12,4 @@ "description": "",

"type": "git",
"url": "https://github.com/preactjs/signals"
"url": "https://github.com/preactjs/signals",
"directory": "packages/preact"
},

@@ -29,7 +30,7 @@ "bugs": "https://github.com/preactjs/signals/issues",

".": {
"types": "./dist/signals.d.ts",
"browser": "./dist/signals.module.js",
"umd": "./dist/signals.umd.js",
"import": "./dist/signals.mjs",
"require": "./dist/signals.js",
"types": "./dist/signals.d.ts"
"require": "./dist/signals.js"
}

@@ -39,3 +40,3 @@ },

"dependencies": {
"@preact/signals-core": "^1.0.1"
"@preact/signals-core": "^1.1.1"
},

@@ -42,0 +43,0 @@ "peerDependencies": {

@@ -1,2 +0,2 @@

import { options, Component, createElement } from "preact";
import { options, Component } from "preact";
import { useRef, useMemo } from "preact/hooks";

@@ -52,3 +52,2 @@ import {

const s = signal(undefined) as Updater;
s._canActivate = true;
s._updater = updater;

@@ -58,32 +57,2 @@ return s;

// Get a (cached) Signal property updater for an element VNode
function getElementUpdater(vnode: VNode) {
let updater = updaterForComponent.get(vnode) as ElementUpdater;
if (!updater) {
let signalProps: Array<{ _key: string; _signal: Signal }> = [];
updater = createUpdater(() => {
let dom = vnode.__e as Element;
for (let i = 0; i < signalProps.length; i++) {
let { _key: prop, _signal: signal } = signalProps[i];
let value = signal._value;
if (!dom) return;
if (prop in dom) {
// @ts-ignore-next-line silly
dom[prop] = value;
} else if (value) {
dom.setAttribute(prop, value);
} else {
dom.removeAttribute(prop);
}
}
}) as ElementUpdater;
updater._props = signalProps;
updaterForComponent.set(vnode, updater);
} else {
updater._props.length = 0;
}
return updater;
}
/** @todo This may be needed for complex prop value detection. */

@@ -98,14 +67,2 @@ // function isSignalValue(value: any): value is Signal {

/** Convert Signals within (nested) props.children into Text components */
function childToSignal<T>(child: any, i: keyof T, arr: T) {
if (typeof child !== "object" || child == null) {
// can't be a signal
} else if (Array.isArray(child)) {
child.forEach(childToSignal);
} else if (child instanceof Signal) {
// @ts-ignore-next-line yes, arr can accept VNodes:
arr[i] = createElement(Text, { data: child });
}
}
/**

@@ -149,33 +106,33 @@ * A wrapper component that renders a Signal directly as a Text node.

Object.defineProperties(Signal.prototype, {
constructor: { configurable: true },
type: { configurable: true, value: Text },
props: {
configurable: true,
get() {
return { data: this };
},
},
// Setting a VNode's _depth to 1 forces Preact to clone it before modifying:
// https://github.com/preactjs/preact/blob/d7a433ee8463a7dc23a05111bb47de9ec729ad4d/src/diff/children.js#L77
// @todo remove this for Preact 11
__b: { configurable: true, value: 1 },
});
/** Inject low-level property/attribute bindings for Signals into Preact's diff */
hook(OptionsTypes.DIFF, (old, vnode) => {
if (typeof vnode.type === "string") {
// let orig = vnode.__o || vnode;
let signalProps: Record<string, any> | undefined;
let props = vnode.props;
let updater;
for (let i in props) {
if (i === "children") continue;
for (let i in props) {
let value = props[i];
if (i === "children") {
childToSignal(value, "children", props);
} else if (value instanceof Signal) {
// first Signal prop triggers creation/cleanup of the updater:
if (!updater) updater = getElementUpdater(vnode);
// track which props are Signals for precise updates:
updater._props.push({ _key: i, _signal: value });
let newUpdater = updater._updater;
if (value._updater) {
let oldUpdater = value._updater;
value._updater = () => {
newUpdater();
oldUpdater();
};
} else {
value._updater = newUpdater;
}
if (value instanceof Signal) {
if (!signalProps) vnode.__np = signalProps = {};
signalProps[i] = value;
props[i] = value.peek();
}
}
setCurrentUpdater(updater);
}

@@ -220,15 +177,73 @@

currentComponent = undefined;
let dom: Element;
let updater: ElementUpdater;
// vnode._dom is undefined during string rendering,
// so we use this to skip prop subscriptions during SSR.
if (typeof vnode.type === "string" && (dom = vnode.__e as Element)) {
let props = vnode.__np;
if (props) {
// @ts-ignore-next
updater = dom._updater;
if (!updater) {
updater = createElementUpdater(dom);
// @ts-ignore-next
dom._updater = updater;
}
updater!._props = props;
setCurrentUpdater(updater);
// @ts-ignore-next we're adding an argument here
updater._updater(true);
}
}
old(vnode);
});
// per-element updater for 1+ signal bindings
function createElementUpdater(dom: Element) {
const cache: Record<string, any> = { __proto__: null };
const updater = createUpdater((skip?: boolean) => {
const props = updater._props;
for (let prop in props) {
if (prop === "children") continue;
let signal = props[prop];
if (signal instanceof Signal) {
let value = signal.value;
let cached = cache[prop];
cache[prop] = value;
if (skip === true || cached === value) {
// this is just a subscribe run, not an update
} else if (prop in dom) {
// @ts-ignore-next-line silly
dom[prop] = value;
} else if (value) {
dom.setAttribute(prop, value);
} else {
dom.removeAttribute(prop);
}
}
}
}) as ElementUpdater;
return updater;
}
/** Unsubscribe from Signals when unmounting components/vnodes */
hook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {
let thing = vnode.__c || vnode;
const updater = updaterForComponent.get(thing);
let component = vnode.__c;
const updater = component && updaterForComponent.get(component);
if (updater) {
updaterForComponent.delete(thing);
const signals = updater._deps;
if (signals) {
signals.forEach(signal => signal._subs.delete(updater));
signals.clear();
updaterForComponent.delete(component);
updater._setCurrent()(true, true);
}
if (typeof vnode.type === "string") {
const dom = vnode.__e as Element;
// @ts-ignore-next
const updater = dom._updater;
if (updater) {
updater._setCurrent()(true, true);
// @ts-ignore-next
dom._updater = null;
}

@@ -285,2 +300,3 @@ }

if (hasHookState.has(this)) return true;
// @ts-ignore
for (let i in state) return true;

@@ -287,0 +303,0 @@

@@ -11,2 +11,4 @@ import { Component } from "preact";

__e?: Element | Text;
/** Props that had Signal values before diffing (used after diffing to subscribe) */
__np?: Record<string, any> | null;
}

@@ -22,3 +24,3 @@

export interface ElementUpdater extends Updater {
_props: Array<{ _key: string, _signal: Signal }>;
_props: Record<string, any>;
}

@@ -25,0 +27,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc