UNPKG

@connectv/core

Version:

agent-based reactive programming library for typescript/javascript

2 lines (1 loc) 27.6 kB
var connective=function(t,e,s,r){"use strict";r=r&&r.hasOwnProperty("default")?r.default:r;class i{track(t){return this._sub||(this._sub=new e.Subscription),this._sub.add(t),t}untrack(t){return this._sub&&this._sub.remove(t),this}get tracking(){return!!this._sub}clear(){return this._sub&&(this._sub.unsubscribe(),this._sub=void 0),this}}function n(t){return!!t.bind&&"function"==typeof t.bind}class u extends Error{constructor(){super("A group of pins does not have an observable.")}}class o extends i{from(...t){return this.entries.from(...t)}to(...t){return this.exits.to(...t)}serialFrom(...t){return this.entries.serialFrom(...t)}serialTo(...t){return this.exits.serialTo(...t)}get observable(){return this.exits.observable}subscribe(t,e,s){return this.exits.subscribe(t,e,s)}}class c extends o{constructor(t){super(),this.factory=t;let[e,s]=t();this.entries=e instanceof a?e:h(...e),this.exits=s instanceof a?s:h(...s)}}class a{constructor(t){this.pins=t}get observable(){throw new u}from(...t){return t.forEach(t=>this.pins.forEach(e=>e.from(t))),p(...t)}to(...t){return t.forEach(t=>this.pins.forEach(e=>e.to(t))),l(...t)}serialFrom(...t){t.filter(t=>t instanceof o).forEach(t=>{for(let e=0;e<Math.min(this.pins.length,t.exits.pins.length);e++)this.pins[e].from(t.exits.pins[e])});let e=t.filter(t=>!(t instanceof o));for(let t=0;t<Math.min(this.pins.length,e.length);t++)this.pins[t].from(e[t]);return p(...t)}serialTo(...t){t.filter(t=>t instanceof o).forEach(t=>{for(let e=0;e<Math.min(this.pins.length,t.entries.pins.length);e++)this.pins[e].to(t.entries.pins[e])});let e=t.filter(t=>!(t instanceof o));for(let t=0;t<Math.min(this.pins.length,e.length);t++)this.pins[t].to(e[t]);return l(...t)}clear(){return this.pins.forEach(t=>t.clear()),this}bind(){return this.pins.forEach(t=>{n(t)&&t.bind()}),this}subscribe(t,s,r){return this.pins.reduce((e,i)=>(e.add(i.subscribe(t,s,r)),e),new e.Subscription)}}function h(...t){return new a(t)}function l(...t){if(1==t.length){let e=t[0];return e instanceof o?e.exits:e}return h(...t.reduce((t,e)=>e instanceof o?t.concat(e.exits.pins):t.concat([e]),[]))}function p(...t){if(1==t.length){let e=t[0];return e instanceof o?e.entries:e}return h(...t.reduce((t,e)=>e instanceof o?t.concat(e.entries.pins):t.concat([e]),[]))}class d extends i{to(...t){return t.forEach(t=>t.from(this)),l(...t)}from(...t){return t.forEach(t=>{t instanceof a?t.pins.forEach(t=>this.connect(t)):t instanceof o?t.exits.pins.forEach(t=>this.connect(t)):this.connect(t)}),p(...t)}serialTo(...t){return t.forEach(t=>{t instanceof o?t.entries.pins.length>0&&t.entries.pins[0].from(this):t.from(this)}),l(...t)}serialFrom(...t){return t.forEach(t=>{t instanceof o?t.exits.pins.length>0&&this.connect(t.exits.pins[0]):this.connect(t)}),l(...t)}subscribe(t,e,r){return e||r?this.track(this.observable.pipe(s.map(t=>t.value)).subscribe(t,e,r)):this.track(this.observable.pipe(s.map(t=>t.value)).subscribe(t))}}class f extends Error{constructor(){super("Attempted to modify pin after it was locked.\nCheck the following link for more info:\nhttps://connective.dev/docs/pin#subscribing-and-binding")}}class _ extends Error{constructor(){super("Unresolved pin observable.")}}class b extends d{constructor(){super(),this._resolving=!1,this._deference_connected=!1,this._inbound=[]}connect(t){if(this.locked)throw new f;return this._inbound.includes(t)||this._inbound.push(t),this}get observable(){if(this.shouldResolve(this._inbound,this._observable)){if(this._resolving)return this._deferred||(this._deferred=new e.Subject),this._deferred;if(this._resolving=!0,this._observable=this.resolve(this._inbound),this._deferred){let t=this._observable;this._observable=e.defer(()=>(this._deference_connected||(this.track(t.subscribe(this._deferred)),this._deference_connected=!0),t))}this._resolving=!1}if(!this._observable)throw new _;return this._observable}clear(){return this._inbound.length=0,this._observable=void 0,this._deference_connected=!1,this._deferred&&(this._deferred.complete(),this._deferred=void 0),super.clear()}get locked(){return this.isLocked(this._observable)}get connected(){return this.isConnected()}isConnected(){return this._inbound.length>0}}class g extends b{isLocked(t){return void 0!==t}shouldResolve(t,e){return void 0===e}resolve(t){return 1==t.length?t[0].observable:e.merge(...t.map(t=>t.observable))}}function v(){return new g}class m extends i{constructor(t=(()=>new g)){super(),this.factory=t,this._pins={}}get(t){if(!(t in this._pins)){let e=this.factory(t);return this._pins[t]=e,this._subject&&this._subject.next([t,e]),e}return this._pins[t]}instantiated(t){return t in this._pins}get pins(){return Object.values(this._pins)}get entries(){return Object.entries(this._pins)}subscribe(t){return this._subject||(this._subject=new e.Subject),this.entries.forEach(e=>t(...e)),this.track(this._subject.subscribe(e=>t(...e)))}clear(){return this.pins.forEach(t=>t.clear()),this._pins={},this._subject&&(this._subject.complete(),this._subject=void 0),super.clear()}}function w(t){return void 0!==t&&void 0!==t.outputs&&void 0!==t.outputs.length&&(void 0===t.inputs||void 0!==t.inputs.length)}function k(t){return void 0!==t&&"function"==typeof t.in&&"function"==typeof t.out&&t.inputs instanceof m&&t.outputs instanceof m&&void 0!==t.signature&&w(t.signature)}class x extends Error{constructor(t,e){super(`Input ${t} not in signature {inputs: ${e.inputs}}.\nRead this for more information:\nhttps://connective.dev/docs/agent#signature`),this.input=t,this.signature=e}}class y extends Error{constructor(t,e){super(`Output ${t} not in signature {outputs: ${e.outputs}}.\nRead this for more information:\nhttps://connective.dev/docs/agent#signature`),this.output=t,this.signature=e}}class E extends Error{constructor(t){super(`${t.constructor?t.constructor.name:t} is not a properly defined PartialFlow.\nFor more information, follow this link:\nhttps://connective.dev/docs/agent#implicit-connection`)}}class O extends o{constructor(t){super(),this.signature=t,this._inputs=new m(t=>this.createInput(t)),this._outputs=new m(t=>this.createOutput(t))}in(t){return this._inputs.get(t.toString())}out(t){return this._outputs.get(t.toString())}get entries(){if(!this._entries){let t=this.createEntries();this._entries=t instanceof a?t:h(...t)}return this._entries}get exits(){if(!this._exits){let t=this.createExits();this._exits=t instanceof a?t:h(...t)}return this._exits}get inputs(){return this._inputs}get outputs(){return this._outputs}clear(){return this._inputs.clear(),this._outputs.clear(),super.clear()}checkInput(t){if(!this.signature.inputs||!this.signature.inputs.includes(t))throw new x(t,this.signature)}checkOutput(t){if(!this.signature.outputs.includes(t))throw new y(t,this.signature)}createInput(t){return this.checkInput(t),new g}createOutput(t){return this.checkOutput(t),new g}createEntries(){throw new E(this)}createExits(){throw new E(this)}}class j extends Error{constructor(t){super(`No child with name ${t} is defined.`)}}class S extends Error{constructor(t){super(`Child ${t} is not an Agent.`)}}class q extends Error{constructor(t){super(`Child ${t} is not a Pin.`)}}class M extends O{constructor(t){super(t),this.init()}init(){this.build(),this.wire()}add(t,e){if(this._children||(this._children={}),!e)return this.add(`${Object.keys(this._children).length}`,t);let s=t;return this._children[s]=e,n(e)&&this.toBind(e),e}child(t){if("string"!=typeof t)return this.child(t.toString());if(this._children&&t in this._children)return this._children[t];throw new j(t)}pin(t){let e=this.child(t);if(e instanceof O)throw new q(t.toString());return e}agent(t){let e=this.child(t);if(!(e instanceof O))throw new S(t.toString());return e}toBind(t){return this._bindables||(this._bindables=[]),this._bindables.push(t),this}bind(){return this._bindables&&this._bindables.forEach(t=>t.bind()),this}clear(){return this._children&&(Object.values(this._children).forEach(t=>t.clear()),this._children=void 0),this._bindables&&(this._bindables=void 0),super.clear()}}class I{constructor(t){this.values=t}}const C={};class P{constructor(t,e={}){this.value=t,this.context=e}static from(t,e=C){return new P(e===C?t.map(t=>t.value):e,t.reduce((t,e)=>(Object.entries(e.context).forEach(([e,s])=>{if(e in t){if(t[e]==s)return t;t[e]instanceof I?s instanceof I?t[e]=new I(t[e].values.concat(s.values.filter(s=>!t[e].values.includes(s)))):t[e].values.includes(s)||t[e].values.push(s):s instanceof I?s.values.includes(t[e])?t[e]=s:t[e]=new I([t[e]].concat(s.values)):t[e]=new I([t[e],s])}else t[e]=s}),t),{}))}fork(t){return new P(t,this.context)}}function A(t,e){return new P(t,e)}const F={};class L extends g{constructor(t=F){super(),this.val=t}resolve(t){if(this.val instanceof m){let t=this.val.entries;return 0==t.length?e.of(A()):e.zip(...t.map(t=>t[1].observable)).pipe(s.map(e=>P.from(e,t.reduce((t,s,r)=>(t[s[0]]=e[r].value,t),{}))))}if(0==t.length)return e.of(A(this.val));{let r=e.zip(...t.map(t=>t.observable));return this.val!==F?r.pipe(s.map(t=>P.from(t,this.val))):r.pipe(s.map(t=>P.from(t)))}}}function N(t){return new L(t)}class $ extends g{constructor(t){super(),this.pinmap=t,t&&this.track(t.subscribe((t,e)=>e.to(this)))}resolve(t){if(this.pinmap){let t=this.pinmap.entries;return 0==t.length?e.of(A()):e.combineLatest(...t.map(t=>t[1].observable)).pipe(s.map(e=>P.from(e,t.reduce((t,s,r)=>(t[s[0]]=e[r].value,t),{}))))}return e.combineLatest(...t.map(t=>t.observable)).pipe(s.map(t=>P.from(t)))}}function K(...t){let e=t.map(t=>t instanceof m?new $(t):t);return 0==e.length?new $:1==e.length?e[0]instanceof $?e[0]:e[0].to(new $):h(...e).to(new $)}class R extends Error{constructor(t,e){super(t instanceof Error?t.message:t),this.emission=e,t instanceof Error?this.original=t:this.original=new Error(t)}get message(){return this.original.message}get stack(){return this.original.stack}}class B extends g{constructor(t){super(),this.pipes=t}resolve(t){return this.pipes.reduce((t,e)=>t.pipe(e),1==t.length?t[0].observable:e.merge(...t.map(t=>t.observable)))}}function T(...t){return new B(t)}class D extends B{constructor(t){super(t.length<=1?[s.filter(e=>{try{return t(e.value)}catch(t){throw new R(t,e)}})]:[s.mergeMap(r=>new e.Observable(e=>{t(r.value,t=>{e.next(t),e.complete()},t=>{e.error(new R(t,r))},r.context)}).pipe(s.filter(t=>!!t),s.map(t=>r))),s.share()]),this.filter=t}}function G(t){return new D(t)}function U(){return new D(()=>!1)}class z extends B{constructor(t){super(t.length<=1?[s.map(e=>{try{return e.fork(t(e.value))}catch(t){throw new R(t,e)}})]:[s.mergeMap(s=>new e.Observable(e=>{t(s.value,t=>{e.next(s.fork(t)),e.complete()},t=>{e.error(new R(t,s))},s.context)})),s.share()]),this.map=t}}function J(t){return new z(t)}class W extends Error{constructor(t){super(`Following inputs are missing from provided data: ${t}.\nRead this for more information:\nhttps://connective.dev/docs/node#optional`),this.missing=t}}class H extends O{constructor(t){super(t),this._control_required=!0,this._control=N(),this._res=K(this.inputs,this.control.to(J(()=>this._control_required=!1))).to(G(()=>!this._control_required)).to(J((e,s,r,i)=>{this._control.connected&&(this._control_required=!0),t.required&&t.required.some(t=>!(e&&e[0]&&t in e[0]))?r(new W(t.required.filter(t=>!(e&&e[0]&&t in e[0])))):this.run(e[0],(t,e)=>{this.signature.outputs.includes(t)?s({out:t,data:e}):r(new y(t,this.signature))},r,i)}))}get control(){return this._control}createOutput(t){return this.checkOutput(t),this._res.to(G(e=>e.out==t)).to(J(t=>t.data))}createEntries(){return(this.signature.inputs||[]).map(t=>this.in(t))}createExits(){return this.signature.outputs.map(t=>this.out(t))}clear(){return this.control.clear(),super.clear()}}class V extends H{constructor(t,e){super(t),this._run=e}run(t,e,s,r){this._run.apply(this,[t,e,s,r])}}class Q extends H{constructor(t,e){super({inputs:"function"==typeof t?[]:t,required:"function"==typeof t?[]:t,outputs:["result"]}),this.func=e||t}run(t,e,s,r){let i=this.signature.inputs?this.signature.inputs.map(e=>t[e]):[];try{let t=this.func.apply(void 0,i.concat(r));"function"==typeof t?t.apply(void 0,[t=>e("result",t),s]):e("result",t)}catch(t){s(t)}}get result(){return this.out("result")}}function X(t,e){if(e)return new Q(t,e);{let e=t;return new Q(Array.apply(0,{length:e.length}).map((t,e)=>e.toString()),e)}}class Y extends O{constructor(){super({inputs:["value"],outputs:["value"]}),this._control=new L}get input(){return this.in("value")}get output(){return this.out("value")}get control(){return this._control}createOutput(t){return this.checkOutput(t),h(this.control,this.input).to(new L).to(G(([t,e])=>t.every(t=>!!t))).to(J(([t,e])=>e))}createEntries(){return[this.input]}createExits(){return[this.output]}clear(){return this.control.clear(),super.clear()}}class Z extends O{constructor(t){super(t.signature),this.core=t,this._control_required=!0,this._control=N(),this._pack=K(this.inputs,this._control.to(J(()=>this._control_required=!1))).to(G(()=>!this._control_required)).to(J(t=>(this._control.connected&&(this._control_required=!0),t[0]))),this.track(t.inputs.subscribe((t,e)=>{this._pack.to(J(e=>e[t])).to(e)})),this.track(t.outputs.subscribe((t,e)=>{e.to(this.out(t))}))}get control(){return this._control}createInput(t){return this.core.in(t),super.createInput(t)}createOutput(t){return this.core.out(t),super.createOutput(t)}clear(){return this._control.clear(),this._pack.clear(),this.core.clear(),super.clear()}}class tt extends b{constructor(t=new e.Subject){super(),this._subject=t}send(t,e){this.emit(A(t,e))}emit(t){this._subject.next(t)}clear(){return this._subject.complete(),this._subject=new e.Subject,super.clear()}isConnected(){return this.tracking||super.isConnected()}resolve(t){return t.forEach(t=>{this.track(t.observable.subscribe(this._subject))}),t.length=0,this._subject}isLocked(){return!1}shouldResolve(t,e){return t.length>0||!e}}function et(t){return new tt(t)}class st extends O{proxy(t){let s=new e.Subscription(()=>{this.untrack(s)});return this.inputs.entries.forEach(e=>t.in(e[0]).from(e[1])),this.outputs.entries.forEach(e=>{s.add(t.out(e[0]).observable.subscribe(t=>{e[1].emit(t)}))}),this.track(s)}createOutput(t){return this.checkOutput(t),new tt}}const rt={};class it extends O{constructor(t=rt,s){super({inputs:["value"],outputs:["value"]}),this.initial=rt,t!=rt||s?s?(this.initial=t,this.compare=s):"function"==typeof t?this.compare=t:(this.initial=t,this.compare=r):(this.initial=rt,this.compare=r),this._subject=new e.BehaviorSubject(A(this.initial)),this._injector=et()}get input(){return this.in("value")}get output(){return this.out("value")}get value(){return this._subject.value.value!==rt?this._subject.value.value:void 0}set value(t){this._injector.send(t)}bind(){return this.track(this.output.observable.subscribe()),this}clear(){return this._subject.complete(),super.clear()}createOutput(t){return this.checkOutput(t),h(this.input,this._injector).to(G(t=>!this.compare(t,this.value))).to(et(this._subject)).to(G(t=>t!==rt))}createEntries(){return[this.input]}createExits(){return[this.output]}}class nt extends O{constructor(...t){super({inputs:["target"],outputs:t.map((t,e)=>e.toString())}),this.cases=t}get target(){return this.in("target")}case(t){return this.out(t)}createOutput(t){this.checkOutput(t);let e=this.cases[t];return this.target.to(G("function"==typeof e?e:t=>e===t))}}class ut extends O{constructor(){super({inputs:["input"],outputs:["output","error"]}),this._err=et(),this._gate=this.input.to(T(s.tap(null,t=>{!function(t){return t instanceof Error&&t.original instanceof Error&&t.emission instanceof P}(t)?this._err.send(t):this._err.emit(A(t,t.emission.context))}),s.retry(),s.share()))}createOutput(t){return this.checkOutput(t),"error"==t?h(this._err,this._gate.to(U())).to(v()):this._gate}createEntries(){return[this.input]}createExits(){return[this.output,this.error]}clear(){return this._err.clear(),super.clear()}get input(){return this.in("input")}get output(){return this.out("output")}get error(){return this.out("error")}}function ot(t,e){return{accepts:(t,s)=>void 0===e||s.length<e,complete:e=>e.length>=t}}const ct=ot(0),at=ot(1);class ht extends O{constructor(t){super({inputs:t.map((t,e)=>e.toString()),outputs:["out"]}),this._head=0,this._control=new L,this._relay=new g,this.tokens=t.map(t=>"number"==typeof t?function(t){return ot(t,t)}(t):"+"===t?at:"*"===t?ct:t),t.forEach((t,e)=>{this.in(e).to(T(s.map(t=>(this._take(t,e,!0),t)))).to(this._relay)}),this.reset()}_take(t,e,s=!1){e==this._head?this.tokens[e].accepts(t,this._seq[e])?this._seq[e].push(t):(this.reset(),s&&this._take(t,e)):e<this._head?(this.reset(),this._take(t,e)):this._seek(e)?this._take(t,e,s):(this.reset(),this._seek(e)&&this._take(t,e,s))}_seek(t){for(let e=this._head;e<t;e++)if(!this.tokens[e].complete(this._seq[e]))return!1;return this._head=t,!0}get _complete(){return this._seq.every((t,e)=>this.tokens[e].complete(t))}reset(){return this._seq=this.tokens.map(t=>[]),this._head=0,this}createOutput(t){return this.checkOutput(t),h(this._control.to(J(()=>this.reset())).to(U()),this._relay.to(G(()=>this._complete))).to(T(s.map(()=>{let t=this._seq.map(t=>1==t.length?t[0].value:t.map(t=>t.value));return P.from(this._seq.reduce((t,e)=>t.concat(e),[]),1==this.tokens.length?t[0]:t)}),s.share()))}createEntries(){return(this.signature.inputs||[]).map(t=>this.in(t))}createExits(){return[this.output]}clear(){return this.reset(),this._control.clear(),this._relay.clear(),super.clear()}get control(){return this._control}get output(){return this.out("out")}}class lt extends O{constructor(t,e=!0){super({inputs:t,outputs:["output"]}),this.keys=t,this.pop=e,this._cache={},this._inject=new tt}_receive(t,e){e.context.__fork?e.context.__fork instanceof I?e.context.__fork.values.forEach(s=>this._fill(s,t,e)):this._fill(e.context.__fork,t,e):this._fill([],t,e)}_cache_key(t){return t.join(";")}_fill(t,e,s){let r=this._cache_key(t),i=this._cache[r]=this._cache[r]||{};i[e]=s,this._complete(i)&&this._emit(i,t)}_emit(t,e){delete this._cache[this._cache_key(e)];let s=P.from(Object.values(t),Object.entries(t).reduce((t,e)=>(t[e[0]]=e[1].value,t),{}));s.context.__fork=this.pop?e.slice(0,-1):[...e],this._inject.emit(s)}_complete(t){return Object.values(t).length==this.keys.length}createOutput(t){return this.checkOutput(t),h(h(...this.keys.map(t=>this.in(t).to(T(s.tap(e=>this._receive(t,e)))))).to(U()),this._inject).to(v())}createEntries(){return this.keys.map(t=>this.in(t))}createExits(){return[this.output]}get output(){return this.out("output")}clear(){return this._inject.clear(),this._cache={},this}}function pt(t){return N(t)}function dt(t,s,r,i){return J((n,u,o,c)=>{let a=t(),h={},l=new e.Subscription,p=()=>{Object.values(h).forEach(t=>t.clear()),a.clear(),l.unsubscribe(),r&&r(l)};n&&Object.keys(n).forEach(t=>a.in(t).from(h[t]=et()));let d=a.signature.outputs||[];i&&(d=i()),d.forEach(t=>{l.add(a.out(t).subscribe(e=>{p(),u({label:t,value:e})},t=>{p(),o(t)}))}),s&&s(l),n&&Object.entries(n).forEach(([t,e])=>h[t].emit(A(e,c)))})}class ft extends O{constructor(t,s){super(s||t.signature||t().clear().signature),this.ref=t,this._all_subs=new e.Subscription,this._control_required=!0,this._control=new L,this._relay=K(N(this.inputs),this._control.to(J(()=>this._control_required=!1))).to(G(()=>!this._control_required)).to(J(t=>(this._control.connected&&(this._control_required=!0),t[0]))).to(dt(this.ref,t=>this._all_subs.add(t),t=>this._all_subs.remove(t),()=>this.outputs.entries.map(([t,e])=>t)))}createOutput(t){return this.checkOutput(t),this._relay.to(G(e=>e.label==t)).to(J(t=>t.value))}createEntries(){return(this.signature.inputs||[]).map(t=>this.in(t))}createExits(){return this.signature.outputs.map(t=>this.out(t))}get control(){return this._control}clear(){return this._relay.clear(),this._control.clear(),this._all_subs.unsubscribe(),super.clear()}}class _t extends O{constructor(t){super({inputs:["value"],outputs:["pass","fail"]}),this.predicate=t,t.length<=1?this.core=this.input.to(J(e=>[e,t(e)])):this.core=this.input.to(J((e,s,r,i)=>t(e,t=>s([e,t]),r,i)))}get input(){return this.in("value")}get pass(){return this.out("pass")}get fail(){return this.out("fail")}createOutput(t){return this.checkOutput(t),"pass"==t?this.core.to(G(([t,e])=>e)).to(J(([t,e])=>t)):this.core.to(G(([t,e])=>!e)).to(J(([t,e])=>t))}createEntries(){return[this.input]}createExits(){return[this.pass,this.fail]}}class bt extends M{constructor(t,e){super(e),this.factory=t,[this.inpins,this.outpins]=this.factory((...t)=>t.forEach(t=>this.add(t)))}init(){}wire(){}build(){}createInput(t){return this.inpins[t]}createOutput(t){return this.outpins[t]}createEntries(){return Object.values(this.inpins)}createExits(){return Object.values(this.outpins)}}class gt extends B{constructor(t=(()=>{})){super([s.tap(e=>t(e.value,e.context))]),this.func=t,this._bound=!1}get bound(){return this._bound}bind(){return this._bound||(this._bound=!0,this.track(this.subscribe())),this}}function vt(t){return new gt(t)}const mt="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",wt=10;function kt(t=wt,e=mt){let s="";for(let r=0;r<t;r++)s+=e[Math.floor(Math.random()*e.length)];return s}class xt extends O{constructor(t,e,s){super(s||{inputs:["value"],outputs:["value"]}),this.bound=!1,this.reemit=new tt,this.downPropageteKey=kt(),t instanceof it?this.state=t:(this.accessor=t,this.state=new it(this.accessor.initial,e),this.accessor.get.to(J((t,e,s,r)=>{r[this.downPropageteKey]=!0,e(t)})).to(this).to(G((t,e,s,r)=>{const i=r[this.downPropageteKey];delete r[this.downPropageteKey],e(!i)})).to(this.accessor.set))}sub(t,e){let s=!1,r=this;return(e||((t,e)=>new xt(t,e)))({initial:(r.value||[])[t],get:r.output.to(J(e=>(e||[])[t])),set:vt((e,i)=>{try{r.value||(r.value=[]),this.accessor?r.value=Array.isArray(r.value)?Object.assign([],r.value,{[t]:e}):Object.assign({},r.value,{[t]:e}):(r.value[t]=e,s?this.reemit.emit(A(e,i)):s=!0)}catch(t){}}),bind(t){return t(this.set.subscribe())}},this.state.compare)}get value(){return this.state.value}set value(t){this.state.value=t}get compare(){return this.state.compare}get input(){return this.in("value")}get output(){return this.out("value")}bind(){return this.bound||(this.accessor?this.accessor.bind(t=>this.track(t)):this.track(this.output.subscribe()),this.bound=!0),this}createOutput(t){return this.checkOutput(t),h(this.input.to(this.state),this.reemit.to(J(()=>this.value)))}createEntries(){return[this.input]}createExits(){return[this.output]}}class yt extends xt{constructor(t,e,s){super(t,s,{inputs:["value"],outputs:["value","changes"]}),this.keyfunc=e,this._keyMap={}}key(t,e){let s=!1,r=this;return(e||((t,e)=>new xt(t,e)))({initial:(r._keyMap[t]||{item:void 0}).item||(Object.values(this.value)||[]).find(e=>this.keyfunc(e)==t),get:h(r.changes,r.reemit).to(J(()=>(r._keyMap[t]||{item:void 0}).item)),set:vt((e,i)=>{try{let n=r._keyMap[t];n&&(n.item=e,r.accessor?r.value=Array.isArray(r.value)?Object.assign([],r.value,{[n.index]:e}):Object.assign({},r.value,{[n.index]:e}):(r.value[n.index]=e,s?this.reemit.emit(A(e,i)):s=!0))}catch(t){}}),bind(t){return t(this.set.subscribe())}},this.state.compare)}index(t){let e;return h(pt(e=this._keyMap[t]?this._keyMap[t].index:((Object.entries(this.value)||[]).find(([e,s])=>this.keyfunc(s)==t)||[-1,void 0])[0]),h(this.changes,this.reemit).to(J(()=>(this._keyMap[t]||{index:-1}).index))).to(T(s.distinctUntilKeyChanged("value")))}bind(){return super.bind(),this.track(this.changes.subscribe()),this}get keys(){return Object.keys(this._keyMap)}get changes(){return this.out("changes")}createOutput(t){if("changes"===t){this.output;let t=!0;return this.state.to(J((e,s)=>{const r=function(t,e,s){const r=[],i=[],n=[],u=Object.entries(t).reduce((t,[i,n])=>{const u=s(n);return t[u]={index:i,item:n},u in e||r.push({index:i,item:n}),t},{});return Object.entries(e).forEach(([t,e])=>{if(t in u){const s=u[t];s.index!=e.index&&n.push({oldIndex:e.index,newIndex:s.index,item:e.item})}else i.push(e)}),{changes:{additions:r,deletions:i,moves:n},newKeyMap:u}}(e,this._keyMap,this.keyfunc);this._keyMap=r.newKeyMap;let i=Object.assign({},r.changes,{initial:t});t=!1,s(i)}))}return super.createOutput(t)}}const Et=10;class Ot extends B{constructor(t=Et){super([s.map(e=>{let s=[].concat(e.context.__fork||[]);return s.push(Ot._create_fork_tag(t)),A(e.value,Object.assign({},e.context,{__fork:s}))}),s.share()])}static _create_fork_tag(t=Et){return kt(t)}}const jt={};class St extends B{constructor(t,r=jt){super(t.length<=2?[s.map(e=>this._acc||(this._acc=this._init(e,r),r!==jt)?(this._acc=P.from([this._acc,e],t(this._acc.value,e.value)),this._acc):this._acc)]:[s.mergeMap(s=>new e.Observable(e=>{if(!this._acc&&(this._acc=this._init(s,r),r===jt))return e.next(this._acc),void e.complete();t(this._acc.value,s.value,t=>{this._acc=P.from([this._acc,s],t),e.next(this._acc),e.complete()},t=>{e.error(new R(t,s))},s.context,this._acc.context)})),s.share()]),this.reduce=t,this.start=r,this._acc=void 0}_init(t,e){return e!==jt?t.fork(e):t}}class qt extends B{constructor(){super([s.mergeMap(t=>t.value.map?e.from(t.value.map(e=>t.fork(e))):e.of(t))])}}class Mt extends d{constructor(t){super(),this.observable=t.pipe(s.map(t=>A(t)))}connect(t){throw new f}}return t.Agent=O,t.Check=_t,t.ChildIsNotAgent=S,t.ChildIsNotPin=q,t.ChildNotDefined=j,t.Composition=M,t.Connectible=b,t.Control=L,t.Emission=P,t.EmissionError=R,t.Expr=Q,t.Filter=D,t.Fork=Ot,t.Gate=Y,t.Group=a,t.GroupObservableError=u,t.HandleError=ut,t.InputNotInSignature=x,t.InsufficientInputs=W,t.Invoke=ft,t.Join=lt,t.KeyedDeep=yt,t.Map=z,t.MergedEmissionContextVal=I,t.Node=H,t.NodeWrap=Z,t.OutputNotInSignature=y,t.Pack=$,t.PartialFlow=o,t.Pin=g,t.PinLockedError=f,t.PinMap=m,t.Pipe=B,t.Proxy=st,t.Reduce=St,t.Sequence=ht,t.SimpleDeep=xt,t.Sink=gt,t.Source=tt,t.Spread=qt,t.State=it,t.Switch=nt,t.UnresolvedPinObservableError=_,t._switch=function(...t){return new nt(...t)},t.block=U,t.call=function(t,e){return pt(e).to(dt(t))},t.check=function(t){return new _t(t)},t.composition=function(t,e){let s;if(e)s=t;else{let r=[],i=(e=t)((...t)=>{r=r.concat(t)});s={inputs:Object.keys(i[0]),outputs:Object.keys(i[1])},r.forEach(t=>t.clear())}let r=()=>new bt(e,s);return r.signature=s,r},t.control=N,t.deep=function(t,e){return e?new yt(t,e):new xt(t)},t.emission=A,t.exec=dt,t.expr=X,t.filter=G,t.fork=function(t=Et){return new Ot(t)},t.gate=function(){return new Y},t.group=h,t.handleError=function(){return new ut},t.invoke=function(t,e){return new ft(t,e)},t.isAgentLike=k,t.isBindable=n,t.isClearable=function(t){return!!t.clear&&"function"==typeof t.clear},t.isNodeLike=function(t){return void 0!==t&&t.control instanceof L&&k(t)},t.isPinLike=function(t){return void 0!==t&&"function"==typeof t.from&&"function"==typeof t.to&&"observable"in t&&"function"==typeof t.subscribe},t.isSignature=w,t.join=function(...t){return new lt(t)},t.keyed=function(t){return(e,s)=>new yt(e,t,s)},t.map=J,t.node=function(t,e){return()=>new V(t,e)},t.nodeWrap=function(t){return t instanceof H?t:new Z(t)},t.pack=K,t.partialFlow=function(t){return new c(t)},t.peekJoin=function(...t){return new lt(t,!1)},t.pin=v,t.pipe=T,t.proxy=function(t){return new st(t)},t.reduce=function(t,e=jt){return new St(t,e)},t.sampler=function(){return X(t=>t)},t.sequence=function(...t){return new ht(t)},t.singleton=function(){return function(t){var e;let s=new t;return n(s)&&s.bind(),(e=class extends t{}).instance=s,e}},t.sink=vt,t.source=et,t.spread=function(){return new qt},t.state=function(t,e){return new it(t,e)},t.value=pt,t.wrap=function(t){return new Mt(t)},t}({},rxjs,rxjs.operators,_.isEqual);