UNPKG

g2o-reactive

Version:

Wrapper around signal-polyfill to support different module formats

55 lines (54 loc) 10.3 kB
var e=Object.defineProperty,r=(r,o,t)=>(((r,o,t)=>{o in r?e(r,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[o]=t})(r,"symbol"!=typeof o?o+"":o,t),t),o=(e,r)=>{if(Object(r)!==r)throw TypeError('Cannot use the "in" operator on this value');return e.has(r)},t=(e,r,o)=>{if(r.has(e))throw TypeError("Cannot add the same private member more than once");r instanceof WeakSet?r.add(e):r.set(e,o)},n=(e,r,o)=>(((e,r,o)=>{if(!r.has(e))throw TypeError("Cannot "+o)})(e,r,"access private method"),o) /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */;function u(e,r){return Object.is(e,r)} /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */let i=null,c=!1,d=1;const s=Symbol("SIGNAL");function l(e){const r=i;return i=e,r}const a={version:0,lastCleanEpoch:0,dirty:!1,producerNode:void 0,producerLastReadVersion:void 0,producerIndexOfThis:void 0,nextProducerIndex:0,liveConsumerNode:void 0,liveConsumerIndexOfThis:void 0,consumerAllowSignalWrites:!1,consumerIsAlwaysLive:!1,producerMustRecompute:()=>!1,producerRecomputeValue:()=>{},consumerMarkedDirty:()=>{},consumerOnSignalRead:()=>{}};function p(e){if(c)throw new Error(typeof ngDevMode<"u"&&ngDevMode?"Assertion error: signal read during notification phase":"");if(null===i)return;i.consumerOnSignalRead(e);const r=i.nextProducerIndex++;if(y(i),r<i.producerNode.length&&i.producerNode[r]!==e&&g(i)){m(i.producerNode[r],i.producerIndexOfThis[r])}i.producerNode[r]!==e&&(i.producerNode[r]=e,i.producerIndexOfThis[r]=g(i)?v(e,i,r):0),i.producerLastReadVersion[r]=e.version}function h(e){if((!g(e)||e.dirty)&&(e.dirty||e.lastCleanEpoch!==d)){if(!e.producerMustRecompute(e)&&!function(e){y(e);for(let r=0;r<e.producerNode.length;r++){const o=e.producerNode[r],t=e.producerLastReadVersion[r];if(t!==o.version||(h(o),t!==o.version))return!0}return!1}(e))return e.dirty=!1,void(e.lastCleanEpoch=d);e.producerRecomputeValue(e),e.dirty=!1,e.lastCleanEpoch=d}}function f(e){if(void 0===e.liveConsumerNode)return;const r=c;c=!0;try{for(const r of e.liveConsumerNode)r.dirty||w(r)}finally{c=r}}function w(e){var r;e.dirty=!0,f(e),null==(r=e.consumerMarkedDirty)||r.call(e.wrapper??e)}function v(e,r,o){var t;if(N(e),y(e),0===e.liveConsumerNode.length){null==(t=e.watched)||t.call(e.wrapper);for(let r=0;r<e.producerNode.length;r++)e.producerIndexOfThis[r]=v(e.producerNode[r],e,r)}return e.liveConsumerIndexOfThis.push(o),e.liveConsumerNode.push(r)-1}function m(e,r){var o;if(N(e),y(e),typeof ngDevMode<"u"&&ngDevMode&&r>=e.liveConsumerNode.length)throw new Error(`Assertion error: active consumer index ${r} is out of bounds of ${e.liveConsumerNode.length} consumers)`);if(1===e.liveConsumerNode.length){null==(o=e.unwatched)||o.call(e.wrapper);for(let r=0;r<e.producerNode.length;r++)m(e.producerNode[r],e.producerIndexOfThis[r])}const t=e.liveConsumerNode.length-1;if(e.liveConsumerNode[r]=e.liveConsumerNode[t],e.liveConsumerIndexOfThis[r]=e.liveConsumerIndexOfThis[t],e.liveConsumerNode.length--,e.liveConsumerIndexOfThis.length--,r<e.liveConsumerNode.length){const o=e.liveConsumerIndexOfThis[r],t=e.liveConsumerNode[r];y(t),t.producerIndexOfThis[o]=r}}function g(e){var r;return e.consumerIsAlwaysLive||((null==(r=null==e?void 0:e.liveConsumerNode)?void 0:r.length)??0)>0}function y(e){e.producerNode??(e.producerNode=[]),e.producerIndexOfThis??(e.producerIndexOfThis=[]),e.producerLastReadVersion??(e.producerLastReadVersion=[])}function N(e){e.liveConsumerNode??(e.liveConsumerNode=[]),e.liveConsumerIndexOfThis??(e.liveConsumerIndexOfThis=[]) /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */}function C(e){if(h(e),p(e),e.value===S)throw e.error;return e.value}const T=Symbol("UNSET"),x=Symbol("COMPUTING"),S=Symbol("ERRORED"),I={...a,value:T,dirty:!0,error:null,equal:u,producerMustRecompute:e=>e.value===T||e.value===x,producerRecomputeValue(e){if(e.value===x)throw new Error("Detected cycle in computations.");const r=e.value;e.value=x;const o=function(e){return e&&(e.nextProducerIndex=0),l(e)}(e);let t,n=!1;try{t=e.computation.call(e.wrapper),n=r!==T&&r!==S&&e.equal.call(e.wrapper,r,t)}catch(r){t=S,e.error=r}finally{!function(e,r){if(l(r),e&&void 0!==e.producerNode&&void 0!==e.producerIndexOfThis&&void 0!==e.producerLastReadVersion){if(g(e))for(let r=e.nextProducerIndex;r<e.producerNode.length;r++)m(e.producerNode[r],e.producerIndexOfThis[r]);for(;e.producerNode.length>e.nextProducerIndex;)e.producerNode.pop(),e.producerLastReadVersion.pop(),e.producerIndexOfThis.pop()}}(e,o)}n?e.value=r:(e.value=t,e.version++)}}; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */let O=function(){throw new Error};function b(){return p(this),this.value}function E(e,r){!1!==(null==i?void 0:i.consumerAllowSignalWrites)||O(),e.equal.call(e.wrapper,e.value,r)||(e.value=r,function(e){e.version++,d++,f(e) /** * @license * Copyright 2024 Bloomberg Finance L.P. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */}(e))}const W={...a,equal:u,value:void 0};const R=Symbol("node");let k,M,L;var P;function A(e,r={}){return new P.Computed(e,r)}(e=>{var u,d,h,f;u=R,d=new WeakSet,k=e=>o(d,e),e.State=class{constructor(o,n={}){t(this,d),r(this,u);const i= /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ function(e){const r=Object.create(W);r.value=e;const o=()=>(p(r),r.value);return o[s]=r,o}(o)[s];if(this[R]=i,i.wrapper=this,n){const r=n.equals;r&&(i.equal=r),i.watched=n[e.subtle.watched],i.unwatched=n[e.subtle.unwatched]}}get(){if(!k(this))throw new TypeError("Wrong receiver type for Signal.State.prototype.get");return b.call(this[R])}set(e){if(!k(this))throw new TypeError("Wrong receiver type for Signal.State.prototype.set");if(c)throw new Error("Writes to signals not permitted during Watcher callback");E(this[R],e)}};h=R,f=new WeakSet,M=e=>o(f,e),e.Computed=class{constructor(o,n){t(this,f),r(this,h);const u=function(e){const r=Object.create(I);r.computation=e;const o=()=>C(r);return o[s]=r,o}(o)[s];if(u.consumerAllowSignalWrites=!0,this[R]=u,u.wrapper=this,n){const r=n.equals;r&&(u.equal=r),u.watched=n[e.subtle.watched],u.unwatched=n[e.subtle.unwatched]}}get(){if(!M(this))throw new TypeError("Wrong receiver type for Signal.Computed.prototype.get");return C(this[R])}},(e=>{var u,c,d,s;e.untrack=function(e){let r,o=null;try{o=l(null),r=e()}finally{l(o)}return r},e.introspectSources=function(e){var r;if(!M(e)&&!L(e))throw new TypeError("Called introspectSources without a Computed or Watcher argument");return(null==(r=e[R].producerNode)?void 0:r.map((e=>e.wrapper)))??[]},e.introspectSinks=function(e){var r;if(!M(e)&&!k(e))throw new TypeError("Called introspectSinks without a Signal argument");return(null==(r=e[R].liveConsumerNode)?void 0:r.map((e=>e.wrapper)))??[]},e.hasSinks=function(e){if(!M(e)&&!k(e))throw new TypeError("Called hasSinks without a Signal argument");const r=e[R].liveConsumerNode;return!!r&&r.length>0},e.hasSources=function(e){if(!M(e)&&!L(e))throw new TypeError("Called hasSources without a Computed or Watcher argument");const r=e[R].producerNode;return!!r&&r.length>0};u=R,c=new WeakSet,d=new WeakSet,s=function(e){for(const r of e)if(!M(r)&&!k(r))throw new TypeError("Called watch/unwatch without a Computed or State argument")},L=e=>o(c,e),e.Watcher=class{constructor(e){t(this,c),t(this,d),r(this,u);let o=Object.create(a);o.wrapper=this,o.consumerMarkedDirty=e,o.consumerIsAlwaysLive=!0,o.consumerAllowSignalWrites=!1,o.producerNode=[],this[R]=o}watch(...e){if(!L(this))throw new TypeError("Called unwatch without Watcher receiver");n(this,d,s).call(this,e);const r=this[R];r.dirty=!1;const o=l(r);for(const r of e)p(r[R]);l(o)}unwatch(...e){if(!L(this))throw new TypeError("Called unwatch without Watcher receiver");n(this,d,s).call(this,e);const r=this[R];y(r);let o=[];for(let t=0;t<r.producerNode.length;t++)e.includes(r.producerNode[t].wrapper)&&(m(r.producerNode[t],r.producerIndexOfThis[t]),o.push(t));for(const e of o){const o=r.producerNode.length-1;if(r.producerNode[e]=r.producerNode[o],r.producerIndexOfThis[e]=r.producerIndexOfThis[o],r.producerNode.length--,r.producerIndexOfThis.length--,r.nextProducerIndex--,e<r.producerNode.length){const o=r.producerIndexOfThis[e],t=r.producerNode[e];N(t),t.liveConsumerIndexOfThis[o]=e}}}getPending(){if(!L(this))throw new TypeError("Called getPending without Watcher receiver");return this[R].producerNode.filter((e=>e.dirty)).map((e=>e.wrapper))}},e.currentComputed=function(){var e;return null==(e=i)?void 0:e.wrapper},e.watched=Symbol("watched"),e.unwatched=Symbol("unwatched")})(e.subtle||(e.subtle={}))})(P||(P={}));let V=!0;const q=new P.subtle.Watcher((()=>{V&&(V=!1,queueMicrotask(D))}));function D(){V=!0;for(const e of q.getPending())e.get();q.watch()}function j(e){let r;const o=new P.Computed((()=>{"function"==typeof r&&r(),r=e()}));return q.watch(o),o.get(),t=()=>{q.unwatch(o),"function"==typeof r&&r()},{dispose:()=>{t()}};var t}function G(e,r={}){return new P.State(e,r)}function U(e,r={}){return new P.State(e,r)}function $(e){return P.subtle.untrack(e)}export{A as computed,j as effect,U as signal,G as state,$ as untrack}; //# sourceMappingURL=index.min.js.map