UNPKG

statesio

Version:
1 lines 3.9 kB
(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{State:()=>o,StateGroup:()=>c});var i,s,r,n,h=function(t,e,i,s,r){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?r.call(t,i):r?r.value=i:e.set(t,i),i},a=function(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)};class o{constructor(t,e){this.valid=!0,this.changeListeners=[],this.listeners=[],this.hook=()=>this.invalidate(),i.set(this,void 0),this.cache=e,h(this,i,t||"","f")}setName(t){return h(this,i,t,"f"),this}getName(){return a(this,i,"f")}subscribe(t){return-1===this.changeListeners.indexOf(t)&&this.changeListeners.push(t),{listener:t,destroy:()=>{this.removeSubscriber(t)}}}removeSubscriber(t){let e=this.changeListeners.indexOf(t);return-1!==e&&this.changeListeners.splice(e,1),this}clearSubscribers(){for(let t in this.changeListeners)this.removeSubscriber(this.changeListeners[0]);return this}listen(t){return-1===this.listeners.indexOf(t)&&this.listeners.push(t),{listener:t,destroy:()=>{this.removeSubscriber(t)}}}removeListener(t){let e=this.listeners.indexOf(t);return-1!==e&&this.listeners.splice(e,1),this}clearListeners(){for(let t in this.listeners)this.removeSubscriber(this.listeners[0]);return this}isValid(){return this.valid}validate(){this.valid||(this.valid=!0,this.onValidate())}invalidate(){if(this.valid){this.valid=!1,this.onInvalidate();for(let t of this.listeners)t()}for(let t of this.changeListeners)t(this.get())}onInvalidate(){}onValidate(){}connect(t){return t.setConnection(this),this}setConnection(t){var e;return null===(e=this.incoming)||void 0===e||e.removeSubscriber(this.hook),this.incoming=void 0,t&&(this.incoming=t,t.subscribe(this.hook)),this.invalidate(),this}isConnected(){return!!this.incoming}getIncoming(){return this.incoming}set(t,e=!0){return this.cache=t,e?this.invalidate():this.valid=!1,this}get(){return this.valid||(this.cache=this.incoming?this.incoming.get():this.cache,this.validate()),this.cache}}i=new WeakMap;class c extends o{constructor(t,e){super(t),s.set(this,[]),r.set(this,((...t)=>{if(this.incoming)return this.incoming.get();let e={};for(const t of this.getStates())e[t.getName()]=t.get();return e})),n.set(this,a(this,r,"f")),this.setComputeFn(e)}addState(...t){return t.forEach((t=>{t.subscribe(this.hook),a(this,s,"f").push(t),this[t.getName()]=t})),this.invalidate(),this}removeState(...t){return t.forEach((t=>{let e=a(this,s,"f").indexOf(t);e>-1&&this.removeStateAt(e)})),this}removeStateAt(t){return delete this[a(this,s,"f")[t].getName()],a(this,s,"f")[t].removeSubscriber(this.hook),a(this,s,"f").splice(t,1),this.invalidate(),this}clearStates(){for(let t in a(this,s,"f"))this.removeStateAt(0);return this}getStates(){return a(this,s,"f")}set(t,e=!0){return Object.keys(t).forEach((e=>{this[e].set(t[e],!1)})),e?this.invalidate():this.valid=!1,this}get(){return this.valid||(this.cache=this.compute(...a(this,s,"f").map((t=>t.get()))),this.validate()),this.cache}compute(...t){return a(this,n,"f").call(this,...t)}setComputeFn(t){return h(this,n,t||a(this,r,"f"),"f"),this.invalidate(),this}}s=new WeakMap,r=new WeakMap,n=new WeakMap,window.statesio=e})();