Notes/static/js/lib/signals/signals-core.js
2023-06-15 07:24:23 +02:00

3 lines
3.9 KiB
JavaScript

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