@preact/signals-core
Advanced tools
Comparing version 1.5.1 to 1.6.0
# @preact/signals-core | ||
## 1.6.0 | ||
### Minor Changes | ||
- [#525](https://github.com/preactjs/signals/pull/525) [`cb6bdab`](https://github.com/preactjs/signals/commit/cb6bdabbd31b27f8435c7976089fa276da6bfb7a) Thanks [@jviide](https://github.com/jviide)! - Allow setting a signal value inside a computed | ||
### Patch Changes | ||
- [#535](https://github.com/preactjs/signals/pull/535) [`58befba`](https://github.com/preactjs/signals/commit/58befba577d02c5cac5292fda0a599f9708e908b) Thanks [@jviide](https://github.com/jviide)! - Publish packages with provenance statements | ||
- [#529](https://github.com/preactjs/signals/pull/529) [`ec5fe42`](https://github.com/preactjs/signals/commit/ec5fe42850c5dca39da7cf6072558da51cc7fc02) Thanks [@jviide](https://github.com/jviide)! - Document effect cleanups | ||
- [#512](https://github.com/preactjs/signals/pull/512) [`d7f2afa`](https://github.com/preactjs/signals/commit/d7f2afafd7ce0f914cf13d02f87f21ab0c26a74b) Thanks [@jviide](https://github.com/jviide)! - Always reset the evaluation context upon entering an untracked block | ||
- [#531](https://github.com/preactjs/signals/pull/531) [`d17ed0d`](https://github.com/preactjs/signals/commit/d17ed0d2cbc6e57304fa0ed009ecf0a0537fe597) Thanks [@jviide](https://github.com/jviide)! - Add JSDocs for exported core module members | ||
## 1.5.1 | ||
@@ -4,0 +20,0 @@ |
@@ -1,4 +0,26 @@ | ||
declare const identifier: unique symbol; | ||
declare function batch<T>(callback: () => T): T; | ||
declare function untracked<T>(callback: () => T): T; | ||
declare const BRAND_SYMBOL: unique symbol; | ||
/** | ||
* Combine multiple value updates into one "commit" at the end of the provided callback. | ||
* | ||
* Batches can be nested and changes are only flushed once the outermost batch callback | ||
* completes. | ||
* | ||
* Accessing a signal that has been modified within a batch will reflect its updated | ||
* value. | ||
* | ||
* @param fn The callback function. | ||
* @returns The value returned by the callback. | ||
*/ | ||
declare function batch<T>(fn: () => T): T; | ||
/** | ||
* Run a callback function that can access signal values without | ||
* subscribing to the signal updates. | ||
* | ||
* @param fn The callback function. | ||
* @returns The value returned by the callback. | ||
*/ | ||
declare function untracked<T>(fn: () => T): T; | ||
/** | ||
* The base class for plain and computed signals. | ||
*/ | ||
declare class Signal<T = any> { | ||
@@ -11,13 +33,43 @@ constructor(value?: T); | ||
peek(): T; | ||
brand: typeof identifier; | ||
brand: typeof BRAND_SYMBOL; | ||
get value(): T; | ||
set value(value: T); | ||
} | ||
/** | ||
* Create a new plain signal. | ||
* | ||
* @param value The initial value for the signal. | ||
* @returns A new signal. | ||
*/ | ||
declare function signal<T>(value: T): Signal<T>; | ||
/** | ||
* An interface for read-only signals. | ||
*/ | ||
interface ReadonlySignal<T = any> extends Signal<T> { | ||
readonly value: T; | ||
} | ||
declare function computed<T>(compute: () => T): ReadonlySignal<T>; | ||
declare type EffectCleanup = () => unknown; | ||
declare function effect(compute: () => unknown | EffectCleanup): () => void; | ||
export { signal, computed, effect, batch, Signal, ReadonlySignal, untracked, }; | ||
/** | ||
* Create a new signal that is computed based on the values of other signals. | ||
* | ||
* The returned computed signal is read-only, and its value is automatically | ||
* updated when any signals accessed from within the callback function change. | ||
* | ||
* @param fn The effect callback. | ||
* @returns A new read-only signal. | ||
*/ | ||
declare function computed<T>(fn: () => T): ReadonlySignal<T>; | ||
/** | ||
* Create an effect to run arbitrary code in response to signal changes. | ||
* | ||
* An effect tracks which signals are accessed within the given callback | ||
* function `fn`, and re-runs the callback when those signals change. | ||
* | ||
* The callback may return a cleanup function. The cleanup function gets | ||
* run once, either when the callback is next called or when the effect | ||
* gets disposed, whichever happens first. | ||
* | ||
* @param fn The effect callback. | ||
* @returns A function for disposing the effect. | ||
*/ | ||
declare function effect(fn: () => unknown): () => void; | ||
export { signal, computed, effect, batch, untracked, Signal, ReadonlySignal }; |
@@ -1,1 +0,1 @@ | ||
function i(){throw new Error("Cycle detected")}var t=Symbol.for("preact-signals");function r(){if(!(h>1)){var i,t=!1;while(void 0!==s){var r=s;s=void 0;f++;while(void 0!==r){var o=r.o;r.o=void 0;r.f&=-3;if(!(8&r.f)&&c(r))try{r.c()}catch(r){if(!t){i=r;t=!0}}r=o}}f=0;h--;if(t)throw i}else h--}var o=void 0,n=0,s=void 0,h=0,f=0,v=0;function e(i){if(void 0!==o){var t=i.n;if(void 0===t||t.t!==o){t={i:0,S:i,p:o.s,n:void 0,t:o,e:void 0,x:void 0,r:t};if(void 0!==o.s)o.s.n=t;o.s=t;i.n=t;if(32&o.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=o.s;t.n=void 0;o.s.n=t;o.s=t}return t}}}function u(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}u.prototype.brand=t;u.prototype.h=function(){return!0};u.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};u.prototype.U=function(i){if(void 0!==this.t){var t=i.e,r=i.x;if(void 0!==t){t.x=r;i.e=void 0}if(void 0!==r){r.e=t;i.x=void 0}if(i===this.t)this.t=r}};u.prototype.subscribe=function(i){var t=this;return _(function(){var r=t.value,o=32&this.f;this.f&=-33;try{i(r)}finally{this.f|=o}})};u.prototype.valueOf=function(){return this.value};u.prototype.toString=function(){return this.value+""};u.prototype.toJSON=function(){return this.value};u.prototype.peek=function(){return this.v};Object.defineProperty(u.prototype,"value",{get:function(){var i=e(this);if(void 0!==i)i.i=this.i;return this.v},set:function(t){if(o instanceof l)!function(){throw new Error("Computed cannot have side-effects")}();if(t!==this.v){if(f>100)i();this.v=t;this.i++;v++;h++;try{for(var n=this.t;void 0!==n;n=n.x)n.t.N()}finally{r()}}}});function c(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function d(i){for(var t=i.s;void 0!==t;t=t.n){var r=t.S.n;if(void 0!==r)t.r=r;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function a(i){var t=i.s,r=void 0;while(void 0!==t){var o=t.p;if(-1===t.i){t.S.U(t);if(void 0!==o)o.n=t.n;if(void 0!==t.n)t.n.p=o}else r=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=o}i.s=r}function l(i){u.call(this,void 0);this.x=i;this.s=void 0;this.g=v-1;this.f=4}(l.prototype=new u).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===v)return!0;this.g=v;this.f|=1;if(this.i>0&&!c(this)){this.f&=-2;return!0}var i=o;try{d(this);o=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}o=i;a(this);this.f&=-2;return!0};l.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}u.prototype.S.call(this,i)};l.prototype.U=function(i){if(void 0!==this.t){u.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};l.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};l.prototype.peek=function(){if(!this.h())i();if(16&this.f)throw this.v;return this.v};Object.defineProperty(l.prototype,"value",{get:function(){if(1&this.f)i();var t=e(this);this.h();if(void 0!==t)t.i=this.i;if(16&this.f)throw this.v;return this.v}});function y(i){var t=i.u;i.u=void 0;if("function"==typeof t){h++;var n=o;o=void 0;try{t()}catch(t){i.f&=-2;i.f|=8;w(i);throw t}finally{o=n;r()}}}function w(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;y(i)}function p(i){if(o!==this)throw new Error("Out-of-order effect");a(this);o=i;this.f&=-2;if(8&this.f)w(this);r()}function x(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}x.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};x.prototype.S=function(){if(1&this.f)i();this.f|=1;this.f&=-9;y(this);d(this);h++;var t=o;o=this;return p.bind(this,t)};x.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=s;s=this}};x.prototype.d=function(){this.f|=8;if(!(1&this.f))w(this)};function _(i){var t=new x(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}exports.Signal=u;exports.batch=function(i){if(h>0)return i();h++;try{return i()}finally{r()}};exports.computed=function(i){return new l(i)};exports.effect=_;exports.signal=function(i){return new u(i)};exports.untracked=function(i){if(n>0)return i();var t=o;o=void 0;n++;try{return i()}finally{n--;o=t}};//# sourceMappingURL=signals-core.js.map | ||
var i=Symbol.for("preact-signals");function t(){if(!(n>1)){var i,t=!1;while(void 0!==o){var r=o;o=void 0;h++;while(void 0!==r){var s=r.o;r.o=void 0;r.f&=-3;if(!(8&r.f)&&e(r))try{r.c()}catch(r){if(!t){i=r;t=!0}}r=s}}h=0;n--;if(t)throw i}else n--}var r=void 0,o=void 0,n=0,h=0,s=0;function f(i){if(void 0!==r){var t=i.n;if(void 0===t||t.t!==r){t={i:0,S:i,p:r.s,n:void 0,t:r,e:void 0,x:void 0,r:t};if(void 0!==r.s)r.s.n=t;r.s=t;i.n=t;if(32&r.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=r.s;t.n=void 0;r.s.n=t;r.s=t}return t}}}function v(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}v.prototype.brand=i;v.prototype.h=function(){return!0};v.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};v.prototype.U=function(i){if(void 0!==this.t){var t=i.e,r=i.x;if(void 0!==t){t.x=r;i.e=void 0}if(void 0!==r){r.e=t;i.x=void 0}if(i===this.t)this.t=r}};v.prototype.subscribe=function(i){var t=this;return p(function(){var o=t.value,n=r;r=void 0;try{i(o)}finally{r=n}})};v.prototype.valueOf=function(){return this.value};v.prototype.toString=function(){return this.value+""};v.prototype.toJSON=function(){return this.value};v.prototype.peek=function(){var i=r;r=void 0;try{return this.value}finally{r=i}};Object.defineProperty(v.prototype,"value",{get:function(){var i=f(this);if(void 0!==i)i.i=this.i;return this.v},set:function(i){if(i!==this.v){if(h>100)throw new Error("Cycle detected");this.v=i;this.i++;s++;n++;try{for(var r=this.t;void 0!==r;r=r.x)r.t.N()}finally{t()}}}});function e(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function u(i){for(var t=i.s;void 0!==t;t=t.n){var r=t.S.n;if(void 0!==r)t.r=r;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function d(i){var t=i.s,r=void 0;while(void 0!==t){var o=t.p;if(-1===t.i){t.S.U(t);if(void 0!==o)o.n=t.n;if(void 0!==t.n)t.n.p=o}else r=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=o}i.s=r}function c(i){v.call(this,void 0);this.x=i;this.s=void 0;this.g=s-1;this.f=4}(c.prototype=new v).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===s)return!0;this.g=s;this.f|=1;if(this.i>0&&!e(this)){this.f&=-2;return!0}var i=r;try{u(this);r=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}r=i;d(this);this.f&=-2;return!0};c.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}v.prototype.S.call(this,i)};c.prototype.U=function(i){if(void 0!==this.t){v.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};c.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};Object.defineProperty(c.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var i=f(this);this.h();if(void 0!==i)i.i=this.i;if(16&this.f)throw this.v;return this.v}});function a(i){var o=i.u;i.u=void 0;if("function"==typeof o){n++;var h=r;r=void 0;try{o()}catch(t){i.f&=-2;i.f|=8;l(i);throw t}finally{r=h;t()}}}function l(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;a(i)}function y(i){if(r!==this)throw new Error("Out-of-order effect");d(this);r=i;this.f&=-2;if(8&this.f)l(this);t()}function w(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}w.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};w.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1;this.f&=-9;a(this);u(this);n++;var i=r;r=this;return y.bind(this,i)};w.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=o;o=this}};w.prototype.d=function(){this.f|=8;if(!(1&this.f))l(this)};function p(i){var t=new w(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}exports.Signal=v;exports.batch=function(i){if(n>0)return i();n++;try{return i()}finally{t()}};exports.computed=function(i){return new c(i)};exports.effect=p;exports.signal=function(i){return new v(i)};exports.untracked=function(i){var t=r;r=void 0;try{return i()}finally{r=t}};//# sourceMappingURL=signals-core.js.map |
@@ -1,1 +0,1 @@ | ||
!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((i||self).preactSignalsCore={})}(this,function(i){function t(){throw new Error("Cycle detected")}var n=Symbol.for("preact-signals");function o(){if(!(s>1)){var i,t=!1;while(void 0!==h){var n=h;h=void 0;e++;while(void 0!==n){var o=n.o;n.o=void 0;n.f&=-3;if(!(8&n.f)&&c(n))try{n.c()}catch(n){if(!t){i=n;t=!0}}n=o}}e=0;s--;if(t)throw i}else s--}var r=void 0,f=0,h=void 0,s=0,e=0,v=0;function u(i){if(void 0!==r){var t=i.n;if(void 0===t||t.t!==r){t={i:0,S:i,p:r.s,n:void 0,t:r,e:void 0,x:void 0,r:t};if(void 0!==r.s)r.s.n=t;r.s=t;i.n=t;if(32&r.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=r.s;t.n=void 0;r.s.n=t;r.s=t}return t}}}function d(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}d.prototype.brand=n;d.prototype.h=function(){return!0};d.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};d.prototype.U=function(i){if(void 0!==this.t){var t=i.e,n=i.x;if(void 0!==t){t.x=n;i.e=void 0}if(void 0!==n){n.e=t;i.x=void 0}if(i===this.t)this.t=n}};d.prototype.subscribe=function(i){var t=this;return _(function(){var n=t.value,o=32&this.f;this.f&=-33;try{i(n)}finally{this.f|=o}})};d.prototype.valueOf=function(){return this.value};d.prototype.toString=function(){return this.value+""};d.prototype.toJSON=function(){return this.value};d.prototype.peek=function(){return this.v};Object.defineProperty(d.prototype,"value",{get:function(){var i=u(this);if(void 0!==i)i.i=this.i;return this.v},set:function(i){if(r instanceof y)!function(){throw new Error("Computed cannot have side-effects")}();if(i!==this.v){if(e>100)t();this.v=i;this.i++;v++;s++;try{for(var n=this.t;void 0!==n;n=n.x)n.t.N()}finally{o()}}}});function c(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function a(i){for(var t=i.s;void 0!==t;t=t.n){var n=t.S.n;if(void 0!==n)t.r=n;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function l(i){var t=i.s,n=void 0;while(void 0!==t){var o=t.p;if(-1===t.i){t.S.U(t);if(void 0!==o)o.n=t.n;if(void 0!==t.n)t.n.p=o}else n=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=o}i.s=n}function y(i){d.call(this,void 0);this.x=i;this.s=void 0;this.g=v-1;this.f=4}(y.prototype=new d).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===v)return!0;this.g=v;this.f|=1;if(this.i>0&&!c(this)){this.f&=-2;return!0}var i=r;try{a(this);r=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}r=i;l(this);this.f&=-2;return!0};y.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}d.prototype.S.call(this,i)};y.prototype.U=function(i){if(void 0!==this.t){d.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};y.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};y.prototype.peek=function(){if(!this.h())t();if(16&this.f)throw this.v;return this.v};Object.defineProperty(y.prototype,"value",{get:function(){if(1&this.f)t();var i=u(this);this.h();if(void 0!==i)i.i=this.i;if(16&this.f)throw this.v;return this.v}});function w(i){var t=i.u;i.u=void 0;if("function"==typeof t){s++;var n=r;r=void 0;try{t()}catch(t){i.f&=-2;i.f|=8;p(i);throw t}finally{r=n;o()}}}function p(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;w(i)}function b(i){if(r!==this)throw new Error("Out-of-order effect");l(this);r=i;this.f&=-2;if(8&this.f)p(this);o()}function g(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}g.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};g.prototype.S=function(){if(1&this.f)t();this.f|=1;this.f&=-9;w(this);a(this);s++;var i=r;r=this;return b.bind(this,i)};g.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=h;h=this}};g.prototype.d=function(){this.f|=8;if(!(1&this.f))p(this)};function _(i){var t=new g(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}i.Signal=d;i.batch=function(i){if(s>0)return i();s++;try{return i()}finally{o()}};i.computed=function(i){return new y(i)};i.effect=_;i.signal=function(i){return new d(i)};i.untracked=function(i){if(f>0)return i();var t=r;r=void 0;f++;try{return i()}finally{f--;r=t}}});//# sourceMappingURL=signals-core.min.js.map | ||
!function(i,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((i||self).preactSignalsCore={})}(this,function(i){var t=Symbol.for("preact-signals");function o(){if(!(f>1)){var i,t=!1;while(void 0!==n){var o=n;n=void 0;h++;while(void 0!==o){var r=o.o;o.o=void 0;o.f&=-3;if(!(8&o.f)&&u(o))try{o.c()}catch(o){if(!t){i=o;t=!0}}o=r}}h=0;f--;if(t)throw i}else f--}var r=void 0,n=void 0,f=0,h=0,s=0;function e(i){if(void 0!==r){var t=i.n;if(void 0===t||t.t!==r){t={i:0,S:i,p:r.s,n:void 0,t:r,e:void 0,x:void 0,r:t};if(void 0!==r.s)r.s.n=t;r.s=t;i.n=t;if(32&r.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=r.s;t.n=void 0;r.s.n=t;r.s=t}return t}}}function v(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}v.prototype.brand=t;v.prototype.h=function(){return!0};v.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};v.prototype.U=function(i){if(void 0!==this.t){var t=i.e,o=i.x;if(void 0!==t){t.x=o;i.e=void 0}if(void 0!==o){o.e=t;i.x=void 0}if(i===this.t)this.t=o}};v.prototype.subscribe=function(i){var t=this;return b(function(){var o=t.value,n=r;r=void 0;try{i(o)}finally{r=n}})};v.prototype.valueOf=function(){return this.value};v.prototype.toString=function(){return this.value+""};v.prototype.toJSON=function(){return this.value};v.prototype.peek=function(){var i=r;r=void 0;try{return this.value}finally{r=i}};Object.defineProperty(v.prototype,"value",{get:function(){var i=e(this);if(void 0!==i)i.i=this.i;return this.v},set:function(i){if(i!==this.v){if(h>100)throw new Error("Cycle detected");this.v=i;this.i++;s++;f++;try{for(var t=this.t;void 0!==t;t=t.x)t.t.N()}finally{o()}}}});function u(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function d(i){for(var t=i.s;void 0!==t;t=t.n){var o=t.S.n;if(void 0!==o)t.r=o;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function c(i){var t=i.s,o=void 0;while(void 0!==t){var r=t.p;if(-1===t.i){t.S.U(t);if(void 0!==r)r.n=t.n;if(void 0!==t.n)t.n.p=r}else o=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=r}i.s=o}function a(i){v.call(this,void 0);this.x=i;this.s=void 0;this.g=s-1;this.f=4}(a.prototype=new v).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===s)return!0;this.g=s;this.f|=1;if(this.i>0&&!u(this)){this.f&=-2;return!0}var i=r;try{d(this);r=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}r=i;c(this);this.f&=-2;return!0};a.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}v.prototype.S.call(this,i)};a.prototype.U=function(i){if(void 0!==this.t){v.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};a.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};Object.defineProperty(a.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var i=e(this);this.h();if(void 0!==i)i.i=this.i;if(16&this.f)throw this.v;return this.v}});function l(i){var t=i.u;i.u=void 0;if("function"==typeof t){f++;var n=r;r=void 0;try{t()}catch(t){i.f&=-2;i.f|=8;y(i);throw t}finally{r=n;o()}}}function y(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;l(i)}function w(i){if(r!==this)throw new Error("Out-of-order effect");c(this);r=i;this.f&=-2;if(8&this.f)y(this);o()}function p(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}p.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};p.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1;this.f&=-9;l(this);d(this);f++;var i=r;r=this;return w.bind(this,i)};p.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=n;n=this}};p.prototype.d=function(){this.f|=8;if(!(1&this.f))y(this)};function b(i){var t=new p(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}i.Signal=v;i.batch=function(i){if(f>0)return i();f++;try{return i()}finally{o()}};i.computed=function(i){return new a(i)};i.effect=b;i.signal=function(i){return new v(i)};i.untracked=function(i){var t=r;r=void 0;try{return i()}finally{r=t}}});//# sourceMappingURL=signals-core.min.js.map |
@@ -1,1 +0,1 @@ | ||
function i(){throw new Error("Cycle detected")}var t=Symbol.for("preact-signals");function r(){if(!(v>1)){var i,t=!1;while(void 0!==f){var r=f;f=void 0;e++;while(void 0!==r){var n=r.o;r.o=void 0;r.f&=-3;if(!(8&r.f)&&l(r))try{r.c()}catch(r){if(!t){i=r;t=!0}}r=n}}e=0;v--;if(t)throw i}else v--}function n(i){if(v>0)return i();v++;try{return i()}finally{r()}}var o=void 0,h=0;function s(i){if(h>0)return i();var t=o;o=void 0;h++;try{return i()}finally{h--;o=t}}var f=void 0,v=0,e=0,u=0;function c(i){if(void 0!==o){var t=i.n;if(void 0===t||t.t!==o){t={i:0,S:i,p:o.s,n:void 0,t:o,e:void 0,x:void 0,r:t};if(void 0!==o.s)o.s.n=t;o.s=t;i.n=t;if(32&o.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=o.s;t.n=void 0;o.s.n=t;o.s=t}return t}}}function d(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}d.prototype.brand=t;d.prototype.h=function(){return!0};d.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};d.prototype.U=function(i){if(void 0!==this.t){var t=i.e,r=i.x;if(void 0!==t){t.x=r;i.e=void 0}if(void 0!==r){r.e=t;i.x=void 0}if(i===this.t)this.t=r}};d.prototype.subscribe=function(i){var t=this;return O(function(){var r=t.value,n=32&this.f;this.f&=-33;try{i(r)}finally{this.f|=n}})};d.prototype.valueOf=function(){return this.value};d.prototype.toString=function(){return this.value+""};d.prototype.toJSON=function(){return this.value};d.prototype.peek=function(){return this.v};Object.defineProperty(d.prototype,"value",{get:function(){var i=c(this);if(void 0!==i)i.i=this.i;return this.v},set:function(t){if(o instanceof _)!function(){throw new Error("Computed cannot have side-effects")}();if(t!==this.v){if(e>100)i();this.v=t;this.i++;u++;v++;try{for(var n=this.t;void 0!==n;n=n.x)n.t.N()}finally{r()}}}});function a(i){return new d(i)}function l(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function y(i){for(var t=i.s;void 0!==t;t=t.n){var r=t.S.n;if(void 0!==r)t.r=r;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function w(i){var t=i.s,r=void 0;while(void 0!==t){var n=t.p;if(-1===t.i){t.S.U(t);if(void 0!==n)n.n=t.n;if(void 0!==t.n)t.n.p=n}else r=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=n}i.s=r}function _(i){d.call(this,void 0);this.x=i;this.s=void 0;this.g=u-1;this.f=4}(_.prototype=new d).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===u)return!0;this.g=u;this.f|=1;if(this.i>0&&!l(this)){this.f&=-2;return!0}var i=o;try{y(this);o=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}o=i;w(this);this.f&=-2;return!0};_.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}d.prototype.S.call(this,i)};_.prototype.U=function(i){if(void 0!==this.t){d.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};_.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};_.prototype.peek=function(){if(!this.h())i();if(16&this.f)throw this.v;return this.v};Object.defineProperty(_.prototype,"value",{get:function(){if(1&this.f)i();var t=c(this);this.h();if(void 0!==t)t.i=this.i;if(16&this.f)throw this.v;return this.v}});function p(i){return new _(i)}function g(i){var t=i.u;i.u=void 0;if("function"==typeof t){v++;var n=o;o=void 0;try{t()}catch(t){i.f&=-2;i.f|=8;b(i);throw t}finally{o=n;r()}}}function b(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;g(i)}function x(i){if(o!==this)throw new Error("Out-of-order effect");w(this);o=i;this.f&=-2;if(8&this.f)b(this);r()}function E(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}E.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};E.prototype.S=function(){if(1&this.f)i();this.f|=1;this.f&=-9;g(this);y(this);v++;var t=o;o=this;return x.bind(this,t)};E.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=f;f=this}};E.prototype.d=function(){this.f|=8;if(!(1&this.f))b(this)};function O(i){var t=new E(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}export{d as Signal,n as batch,p as computed,O as effect,a as signal,s as untracked};//# sourceMappingURL=signals-core.module.js.map | ||
var i=Symbol.for("preact-signals");function t(){if(!(s>1)){var i,t=!1;while(void 0!==h){var r=h;h=void 0;f++;while(void 0!==r){var o=r.o;r.o=void 0;r.f&=-3;if(!(8&r.f)&&c(r))try{r.c()}catch(r){if(!t){i=r;t=!0}}r=o}}f=0;s--;if(t)throw i}else s--}function r(i){if(s>0)return i();s++;try{return i()}finally{t()}}var o=void 0;function n(i){var t=o;o=void 0;try{return i()}finally{o=t}}var h=void 0,s=0,f=0,v=0;function e(i){if(void 0!==o){var t=i.n;if(void 0===t||t.t!==o){t={i:0,S:i,p:o.s,n:void 0,t:o,e:void 0,x:void 0,r:t};if(void 0!==o.s)o.s.n=t;o.s=t;i.n=t;if(32&o.f)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.n){t.n.p=t.p;if(void 0!==t.p)t.p.n=t.n;t.p=o.s;t.n=void 0;o.s.n=t;o.s=t}return t}}}function u(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}u.prototype.brand=i;u.prototype.h=function(){return!0};u.prototype.S=function(i){if(this.t!==i&&void 0===i.e){i.x=this.t;if(void 0!==this.t)this.t.e=i;this.t=i}};u.prototype.U=function(i){if(void 0!==this.t){var t=i.e,r=i.x;if(void 0!==t){t.x=r;i.e=void 0}if(void 0!==r){r.e=t;i.x=void 0}if(i===this.t)this.t=r}};u.prototype.subscribe=function(i){var t=this;return E(function(){var r=t.value,n=o;o=void 0;try{i(r)}finally{o=n}})};u.prototype.valueOf=function(){return this.value};u.prototype.toString=function(){return this.value+""};u.prototype.toJSON=function(){return this.value};u.prototype.peek=function(){var i=o;o=void 0;try{return this.value}finally{o=i}};Object.defineProperty(u.prototype,"value",{get:function(){var i=e(this);if(void 0!==i)i.i=this.i;return this.v},set:function(i){if(i!==this.v){if(f>100)throw new Error("Cycle detected");this.v=i;this.i++;v++;s++;try{for(var r=this.t;void 0!==r;r=r.x)r.t.N()}finally{t()}}}});function d(i){return new u(i)}function c(i){for(var t=i.s;void 0!==t;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function a(i){for(var t=i.s;void 0!==t;t=t.n){var r=t.S.n;if(void 0!==r)t.r=r;t.S.n=t;t.i=-1;if(void 0===t.n){i.s=t;break}}}function l(i){var t=i.s,r=void 0;while(void 0!==t){var o=t.p;if(-1===t.i){t.S.U(t);if(void 0!==o)o.n=t.n;if(void 0!==t.n)t.n.p=o}else r=t;t.S.n=t.r;if(void 0!==t.r)t.r=void 0;t=o}i.s=r}function y(i){u.call(this,void 0);this.x=i;this.s=void 0;this.g=v-1;this.f=4}(y.prototype=new u).h=function(){this.f&=-3;if(1&this.f)return!1;if(32==(36&this.f))return!0;this.f&=-5;if(this.g===v)return!0;this.g=v;this.f|=1;if(this.i>0&&!c(this)){this.f&=-2;return!0}var i=o;try{a(this);o=this;var t=this.x();if(16&this.f||this.v!==t||0===this.i){this.v=t;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}o=i;l(this);this.f&=-2;return!0};y.prototype.S=function(i){if(void 0===this.t){this.f|=36;for(var t=this.s;void 0!==t;t=t.n)t.S.S(t)}u.prototype.S.call(this,i)};y.prototype.U=function(i){if(void 0!==this.t){u.prototype.U.call(this,i);if(void 0===this.t){this.f&=-33;for(var t=this.s;void 0!==t;t=t.n)t.S.U(t)}}};y.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var i=this.t;void 0!==i;i=i.x)i.t.N()}};Object.defineProperty(y.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var i=e(this);this.h();if(void 0!==i)i.i=this.i;if(16&this.f)throw this.v;return this.v}});function w(i){return new y(i)}function _(i){var r=i.u;i.u=void 0;if("function"==typeof r){s++;var n=o;o=void 0;try{r()}catch(t){i.f&=-2;i.f|=8;g(i);throw t}finally{o=n;t()}}}function g(i){for(var t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;_(i)}function p(i){if(o!==this)throw new Error("Out-of-order effect");l(this);o=i;this.f&=-2;if(8&this.f)g(this);t()}function b(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=32}b.prototype.c=function(){var i=this.S();try{if(8&this.f)return;if(void 0===this.x)return;var t=this.x();if("function"==typeof t)this.u=t}finally{i()}};b.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1;this.f&=-9;_(this);a(this);s++;var i=o;o=this;return p.bind(this,i)};b.prototype.N=function(){if(!(2&this.f)){this.f|=2;this.o=h;h=this}};b.prototype.d=function(){this.f|=8;if(!(1&this.f))g(this)};function E(i){var t=new b(i);try{t.c()}catch(i){t.d();throw i}return t.d.bind(t)}export{u as Signal,r as batch,w as computed,E as effect,d as signal,n as untracked};//# sourceMappingURL=signals-core.module.js.map |
{ | ||
"name": "@preact/signals-core", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"license": "MIT", | ||
@@ -43,3 +43,6 @@ "description": "Manage state with style in every framework", | ||
], | ||
"publishConfig": { | ||
"provenance": true | ||
}, | ||
"scripts": {} | ||
} |
@@ -80,19 +80,2 @@ # Signals | ||
### `untracked(fn)` | ||
In case when you're receiving a callback that can read some signals, but you don't want to subscribe to them, you can use `untracked` to prevent any subscriptions from happening. | ||
```js | ||
const counter = signal(0); | ||
const effectCount = signal(0); | ||
const fn = () => effectCount.value + 1; | ||
effect(() => { | ||
console.log(counter.value); | ||
// Whenever this effect is triggered, run `fn` that gives new value | ||
effectCount.value = untracked(fn); | ||
}); | ||
``` | ||
### `computed(fn)` | ||
@@ -162,2 +145,21 @@ | ||
The effect callback may return a cleanup function. The cleanup function gets run once, either when the effect callback is next called _or_ when the effect gets disposed, whichever happens first. | ||
```js | ||
import { signal, effect } from "@preact/signals-core"; | ||
const count = signal(0); | ||
const dispose = effect(() => { | ||
const c = count.value; | ||
return () => console.log(`cleanup ${c}`); | ||
}); | ||
// Logs: cleanup 0 | ||
count.value = 1; | ||
// Logs: cleanup 1 | ||
dispose(); | ||
``` | ||
### `batch(fn)` | ||
@@ -225,4 +227,21 @@ | ||
### `untracked(fn)` | ||
In case when you're receiving a callback that can read some signals, but you don't want to subscribe to them, you can use `untracked` to prevent any subscriptions from happening. | ||
```js | ||
const counter = signal(0); | ||
const effectCount = signal(0); | ||
const fn = () => effectCount.value + 1; | ||
effect(() => { | ||
console.log(counter.value); | ||
// Whenever this effect is triggered, run `fn` that gives new value | ||
effectCount.value = untracked(fn); | ||
}); | ||
``` | ||
## License | ||
`MIT`, see the [LICENSE](./LICENSE) file. |
192
src/index.ts
@@ -1,10 +0,5 @@ | ||
function cycleDetected(): never { | ||
throw new Error("Cycle detected"); | ||
} | ||
function mutationDetected(): never { | ||
throw new Error("Computed cannot have side-effects"); | ||
} | ||
// An named symbol/brand for detecting Signal instances even when they weren't | ||
// created using the same signals library version. | ||
const BRAND_SYMBOL = Symbol.for("preact-signals"); | ||
const identifier = Symbol.for("preact-signals"); | ||
// Flags for Computed and Effect. | ||
@@ -87,9 +82,21 @@ const RUNNING = 1 << 0; | ||
function batch<T>(callback: () => T): T { | ||
/** | ||
* Combine multiple value updates into one "commit" at the end of the provided callback. | ||
* | ||
* Batches can be nested and changes are only flushed once the outermost batch callback | ||
* completes. | ||
* | ||
* Accessing a signal that has been modified within a batch will reflect its updated | ||
* value. | ||
* | ||
* @param fn The callback function. | ||
* @returns The value returned by the callback. | ||
*/ | ||
function batch<T>(fn: () => T): T { | ||
if (batchDepth > 0) { | ||
return callback(); | ||
return fn(); | ||
} | ||
/*@__INLINE__**/ startBatch(); | ||
try { | ||
return callback(); | ||
return fn(); | ||
} finally { | ||
@@ -103,15 +110,15 @@ endBatch(); | ||
let untrackedDepth = 0; | ||
function untracked<T>(callback: () => T): T { | ||
if (untrackedDepth > 0) { | ||
return callback(); | ||
} | ||
/** | ||
* Run a callback function that can access signal values without | ||
* subscribing to the signal updates. | ||
* | ||
* @param fn The callback function. | ||
* @returns The value returned by the callback. | ||
*/ | ||
function untracked<T>(fn: () => T): T { | ||
const prevContext = evalContext; | ||
evalContext = undefined; | ||
untrackedDepth++; | ||
try { | ||
return callback(); | ||
return fn(); | ||
} finally { | ||
untrackedDepth--; | ||
evalContext = prevContext; | ||
@@ -210,3 +217,12 @@ } | ||
// @ts-ignore internal Signal is viewed as a function | ||
/** | ||
* The base class for plain and computed signals. | ||
*/ | ||
// @ts-ignore: "Cannot redeclare exported variable 'Signal'." | ||
// | ||
// A function with the same name is defined later, so we need to ignore TypeScript's | ||
// warning about a redeclared variable. | ||
// | ||
// The class is declared here, but later implemented with ES5-style prototypes. | ||
// This enables better control of the transpiled output size. | ||
declare class Signal<T = any> { | ||
@@ -250,3 +266,3 @@ /** @internal */ | ||
brand: typeof identifier; | ||
brand: typeof BRAND_SYMBOL; | ||
@@ -258,3 +274,9 @@ get value(): T; | ||
/** @internal */ | ||
// @ts-ignore internal Signal is viewed as function | ||
// @ts-ignore: "Cannot redeclare exported variable 'Signal'." | ||
// | ||
// A class with the same name has already been declared, so we need to ignore | ||
// TypeScript's warning about a redeclared variable. | ||
// | ||
// The previously declared class is implemented here with ES5-style prototypes. | ||
// This enables better control of the transpiled output size. | ||
function Signal(this: Signal, value?: unknown) { | ||
@@ -267,3 +289,3 @@ this._value = value; | ||
Signal.prototype.brand = identifier; | ||
Signal.prototype.brand = BRAND_SYMBOL; | ||
@@ -304,11 +326,11 @@ Signal.prototype._refresh = function () { | ||
Signal.prototype.subscribe = function (fn) { | ||
const signal = this; | ||
return effect(function (this: Effect) { | ||
const value = signal.value; | ||
const flag = this._flags & TRACKING; | ||
this._flags &= ~TRACKING; | ||
return effect(() => { | ||
const value = this.value; | ||
const prevContext = evalContext; | ||
evalContext = undefined; | ||
try { | ||
fn(value); | ||
} finally { | ||
this._flags |= flag; | ||
evalContext = prevContext; | ||
} | ||
@@ -331,7 +353,13 @@ }); | ||
Signal.prototype.peek = function () { | ||
return this._value; | ||
const prevContext = evalContext; | ||
evalContext = undefined; | ||
try { | ||
return this.value; | ||
} finally { | ||
evalContext = prevContext; | ||
} | ||
}; | ||
Object.defineProperty(Signal.prototype, "value", { | ||
get() { | ||
get(this: Signal) { | ||
const node = addDependency(this); | ||
@@ -344,9 +372,5 @@ if (node !== undefined) { | ||
set(this: Signal, value) { | ||
if (evalContext instanceof Computed) { | ||
mutationDetected(); | ||
} | ||
if (value !== this._value) { | ||
if (batchIteration > 100) { | ||
cycleDetected(); | ||
throw new Error("Cycle detected"); | ||
} | ||
@@ -374,2 +398,8 @@ | ||
/** | ||
* Create a new plain signal. | ||
* | ||
* @param value The initial value for the signal. | ||
* @returns A new signal. | ||
*/ | ||
function signal<T>(value: T): Signal<T> { | ||
@@ -491,3 +521,3 @@ return new Signal(value); | ||
declare class Computed<T = any> extends Signal<T> { | ||
_compute: () => T; | ||
_fn: () => T; | ||
_sources?: Node; | ||
@@ -497,3 +527,3 @@ _globalVersion: number; | ||
constructor(compute: () => T); | ||
constructor(fn: () => T); | ||
@@ -504,6 +534,6 @@ _notify(): void; | ||
function Computed(this: Computed, compute: () => unknown) { | ||
function Computed(this: Computed, fn: () => unknown) { | ||
Signal.call(this, undefined); | ||
this._compute = compute; | ||
this._fn = fn; | ||
this._sources = undefined; | ||
@@ -548,3 +578,3 @@ this._globalVersion = globalVersion - 1; | ||
evalContext = this; | ||
const value = this._compute(); | ||
const value = this._fn(); | ||
if ( | ||
@@ -574,3 +604,3 @@ this._flags & HAS_ERROR || | ||
// A computed signal subscribes lazily to its dependencies when the it | ||
// A computed signal subscribes lazily to its dependencies when it | ||
// gets its first subscriber. | ||
@@ -623,16 +653,6 @@ for ( | ||
Computed.prototype.peek = function () { | ||
if (!this._refresh()) { | ||
cycleDetected(); | ||
} | ||
if (this._flags & HAS_ERROR) { | ||
throw this._value; | ||
} | ||
return this._value; | ||
}; | ||
Object.defineProperty(Computed.prototype, "value", { | ||
get() { | ||
get(this: Computed) { | ||
if (this._flags & RUNNING) { | ||
cycleDetected(); | ||
throw new Error("Cycle detected"); | ||
} | ||
@@ -651,2 +671,5 @@ const node = addDependency(this); | ||
/** | ||
* An interface for read-only signals. | ||
*/ | ||
interface ReadonlySignal<T = any> extends Signal<T> { | ||
@@ -656,4 +679,13 @@ readonly value: T; | ||
function computed<T>(compute: () => T): ReadonlySignal<T> { | ||
return new Computed(compute); | ||
/** | ||
* Create a new signal that is computed based on the values of other signals. | ||
* | ||
* The returned computed signal is read-only, and its value is automatically | ||
* updated when any signals accessed from within the callback function change. | ||
* | ||
* @param fn The effect callback. | ||
* @returns A new read-only signal. | ||
*/ | ||
function computed<T>(fn: () => T): ReadonlySignal<T> { | ||
return new Computed(fn); | ||
} | ||
@@ -693,3 +725,3 @@ | ||
} | ||
effect._compute = undefined; | ||
effect._fn = undefined; | ||
effect._sources = undefined; | ||
@@ -714,5 +746,4 @@ | ||
type EffectCleanup = () => unknown; | ||
declare class Effect { | ||
_compute?: () => unknown | EffectCleanup; | ||
_fn?: () => unknown; | ||
_cleanup?: () => unknown; | ||
@@ -723,3 +754,3 @@ _sources?: Node; | ||
constructor(compute: () => unknown | EffectCleanup); | ||
constructor(fn: () => unknown); | ||
@@ -732,4 +763,4 @@ _callback(): void; | ||
function Effect(this: Effect, compute: () => unknown | EffectCleanup) { | ||
this._compute = compute; | ||
function Effect(this: Effect, fn: () => unknown) { | ||
this._fn = fn; | ||
this._cleanup = undefined; | ||
@@ -745,7 +776,7 @@ this._sources = undefined; | ||
if (this._flags & DISPOSED) return; | ||
if (this._compute === undefined) return; | ||
if (this._fn === undefined) return; | ||
const cleanup = this._compute(); | ||
const cleanup = this._fn(); | ||
if (typeof cleanup === "function") { | ||
this._cleanup = cleanup as EffectCleanup; | ||
this._cleanup = cleanup as () => unknown; | ||
} | ||
@@ -759,3 +790,3 @@ } finally { | ||
if (this._flags & RUNNING) { | ||
cycleDetected(); | ||
throw new Error("Cycle detected"); | ||
} | ||
@@ -789,4 +820,17 @@ this._flags |= RUNNING; | ||
function effect(compute: () => unknown | EffectCleanup): () => void { | ||
const effect = new Effect(compute); | ||
/** | ||
* Create an effect to run arbitrary code in response to signal changes. | ||
* | ||
* An effect tracks which signals are accessed within the given callback | ||
* function `fn`, and re-runs the callback when those signals change. | ||
* | ||
* The callback may return a cleanup function. The cleanup function gets | ||
* run once, either when the callback is next called or when the effect | ||
* gets disposed, whichever happens first. | ||
* | ||
* @param fn The effect callback. | ||
* @returns A function for disposing the effect. | ||
*/ | ||
function effect(fn: () => unknown): () => void { | ||
const effect = new Effect(fn); | ||
try { | ||
@@ -803,10 +847,2 @@ effect._callback(); | ||
export { | ||
signal, | ||
computed, | ||
effect, | ||
batch, | ||
Signal, | ||
ReadonlySignal, | ||
untracked, | ||
}; | ||
export { signal, computed, effect, batch, untracked, Signal, ReadonlySignal }; |
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
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
181224
853
245
0