Notes/static/js/lib/signals/signals-core.mjs

3 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-06-15 07:24:23 +02:00
function i(){throw new Error("Cycle detected")}const t=1,s=2,n=4,o=8,h=32;function f(){if(u>1){u--;return}let i,t=!1;while(void 0!==c){let n=c;c=void 0;v++;while(void 0!==n){const h=n.o;n.o=void 0;n.f&=~s;if(!(n.f&o)&&y(n))try{n.c()}catch(s){if(!t){i=s;t=!0}}n=h}}v=0;u--;if(t)throw i}function r(i){if(u>0)return i();u++;try{return i()}finally{f()}}let e,c,u=0,v=0,d=0;function l(i){if(void 0===e)return;let t=i.n;if(void 0===t||t.t!==e){t={i:0,S:i,p:void 0,n:e.s,t:e,e:void 0,x:void 0,r:t};e.s=t;i.n=t;if(e.f&h)i.S(t);return t}else if(-1===t.i){t.i=0;if(void 0!==t.p){t.p.n=t.n;if(void 0!==t.n)t.n.p=t.p;t.p=void 0;t.n=e.s;e.s.p=t;e.s=t}return t}}function w(i){this.v=i;this.i=0;this.n=void 0;this.t=void 0}w.prototype.h=function(){return!0};w.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}};w.prototype.U=function(i){const t=i.e,s=i.x;if(void 0!==t){t.x=s;i.e=void 0}if(void 0!==s){s.e=t;i.x=void 0}if(i===this.t)this.t=s};w.prototype.subscribe=function(i){const t=this;return S(function(){const s=t.value,n=this.f&h;this.f&=~h;try{i(s)}finally{this.f|=n}})};w.prototype.valueOf=function(){return this.value};w.prototype.toString=function(){return this.value+""};w.prototype.peek=function(){return this.v};Object.defineProperty(w.prototype,"value",{get(){const i=l(this);if(void 0!==i)i.i=this.i;return this.v},set(t){if(t!==this.v){if(v>100)i();this.v=t;this.i++;d++;u++;try{for(let i=this.t;void 0!==i;i=i.x)i.t.N()}finally{f()}}}});function a(i){return new w(i)}function y(i){for(let 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 _(i){for(let t=i.s;void 0!==t;t=t.n){const i=t.S.n;if(void 0!==i)t.r=i;t.S.n=t;t.i=-1}}function g(i){let t,s=i.s;while(void 0!==s){const i=s.n;if(-1===s.i){s.S.U(s);s.n=void 0}else{if(void 0!==t)t.p=s;s.p=void 0;s.n=t;t=s}s.S.n=s.r;if(void 0!==s.r)s.r=void 0;s=i}i.s=t}function p(i){w.call(this,void 0);this.x=i;this.s=void 0;this.g=d-1;this.f=n}(p.prototype=new w).h=function(){this.f&=~s;if(this.f&t)return!1;if((this.f&(n|h))===h)return!0;this.f&=~n;if(this.g===d)return!0;this.g=d;this.f|=t;if(this.i>0&&!y(this)){this.f&=~t;return!0}const i=e;try{_(this);e=this;const i=this.x();if(16&this.f||this.v!==i||0===this.i){this.v=i;this.f&=-17;this.i++}}catch(i){this.v=i;this.f|=16;this.i++}e=i;g(this);this.f&=~t;return!0};p.prototype.S=function(i){if(void 0===this.t){this.f|=n|h;for(let i=this.s;void 0!==i;i=i.n)i.S.S(i)}w.prototype.S.call(this,i)};p.prototype.U=function(i){w.prototype.U.call(this,i);if(void 0===this.t){this.f&=~h;for(let i=this.s;void 0!==i;i=i.n)i.S.U(i)}};p.prototype.N=function(){if(!(this.f&s)){this.f|=n|s;for(let i=this.t;void 0!==i;i=i.x)i.t.N()}};p.prototype.peek=function(){if(!this.h())i();if(16&this.f)throw this.v;return this.v};Object.defineProperty(p.prototype,"value",{get(){if(this.f&t)i();const s=l(this);this.h();if(void 0!==s)s.i=this.i;if(16&this.f)throw this.v;return this.v}});function b(i){return new p(i)}function x(i){const s=i.u;i.u=void 0;if("function"==typeof s){u++;const n=e;e=void 0;try{s()}catch(s){i.f&=~t;i.f|=o;O(i);throw s}finally{e=n;f()}}}function O(i){for(let t=i.s;void 0!==t;t=t.n)t.S.U(t);i.x=void 0;i.s=void 0;x(i)}function j(i){if(e!==this)throw new Error("Out-of-order effect");g(this);e=i;this.f&=~t;if(this.f&o)O(this);f()}function E(i){this.x=i;this.u=void 0;this.s=void 0;this.o=void 0;this.f=h}E.prototype.c=function(){const i=this.S();try{if(!(this.f&o)&&void 0!==this.x)this.u=this.x()}finally{i()}};E.prototype.S=function(){if(this.f&t)i();this.f|=t;this.f&=~o;x(this);_(this);u++;const s=e;e=this;return j.bind(this,s)};E.prototype.N=function(){if(!(this.f&s)){this.f|=s;this.o=c;c=this}};E.prototype.d=function(){this.f|=o;if(!(this.f&t))O(this)};function S(i){const t=new E(i);t.c();return t.d.bind(t)}export{w as Signal,r as batch,b as computed,S as effect,a as signal};
//# sourceMappingURL=signals-core.mjs.map