/* esm.sh - @assistant-ui/core@0.3.31/dist/subscribable/subscribable */
import{notifyEventListeners as g}from"../utils/notify-event-listeners.mjs";import{shallowEqual as p}from"../../../../../store@0.3.22/X-ZEBhc3Npc3RhbnQtdWkvdGFwQDAuOS4yNCxyZWFjdEAxOS4zLjE/es2022/client.mjs";var u=Symbol("skip-update"),b=(t,...e)=>{let s=[];for(let i of t)try{i(...e)}catch(o){s.push(o)}if(s.length===1)throw s[0];if(s.length>1){for(let i of s)console.error(i);throw new AggregateError(s)}},h=t=>{b(t)},a=(t,e)=>{try{t()}catch(s){console.error("[assistant-ui] Subscription rollback cleanup threw",s)}throw e},S=(t,e)=>t===void 0||e===void 0?t===e:p(t,e),_=class{_subscribers=new Set;subscribe(t){return this._subscribers.add(t),()=>this._subscribers.delete(t)}waitForUpdate(){return new Promise(t=>{let e=this.subscribe(()=>{e(),t()})})}_notifySubscribers(){b(this._subscribers)}},v=class extends _{_state;constructor(t){super(),this._state=t,this.subscribe=this.subscribe.bind(this),this.getState=this.getState.bind(this),this.getServerSnapshot=()=>t}getState(){return this._state}getServerSnapshot;setState(t){Object.is(t,this._state)||(this._state=t,this._notifySubscribers())}},c=class{_subscriptions=new Set;_connection;get isConnected(){return!!this._connection}notifySubscribers(t,e){if(e){g(this._subscriptions,t,e);return}b(this._subscriptions,t)}_updateConnection(){if(this._subscriptions.size>0){if(this._connection)return;this._connection=this._connect()}else{let t=this._connection;this._connection=void 0,t?.()}}subscribe(t){this._subscriptions.add(t);try{this._updateConnection()}catch(e){throw this._subscriptions.delete(t),e}return()=>{this._subscriptions.delete(t),this._updateConnection()}}},y=class extends c{get path(){return this.binding.path}binding;constructor(t){super(),this.binding=t;let e=t.getState();if(e===u)throw new Error("Entry not available in the store");this._previousState=e}_previousState;getState=()=>(this.isConnected||this._syncState(),this._previousState);_syncState(){let t=this.binding.getState();return t===u||S(t,this._previousState)?!1:(this._previousState=t,!0)}_connect(){let t=()=>{this._syncState()&&this.notifySubscribers()},e=this.binding.subscribe(t);try{return this._syncState(),e}catch(s){throw a(e,s)}}},w=class extends c{get path(){return this.binding.path}binding;constructor(t){super(),this.binding=t}_previousStateDirty=!0;_previousState;getState=()=>{if(!this.isConnected||this._previousStateDirty){let t=this.binding.getState();t!==u&&(this._previousState===void 0||!S(t,this._previousState))&&(this._previousState=t),this._previousStateDirty=!1}if(this._previousState===void 0)throw new Error("Entry not available in the store");return this._previousState};_connect(){let t=()=>{this._previousStateDirty=!0,this.notifySubscribers()},e=this.binding.subscribe(t);return this._previousStateDirty=!0,e}},m=class extends c{get path(){return this.binding.path}binding;constructor(t){super(),this.binding=t}getState(){return this.binding.getState()}outerSubscribe(t){return this.binding.subscribe(t)}_connect(){let t=()=>{this.notifySubscribers()},e=this.binding.getState(),s=e?.subscribe(t),i=()=>{let n=this.binding.getState();if(n===e)return;e=n;let r=s;s=void 0;try{r?.()}finally{s=n?.subscribe(t),t()}},o;try{o=this.outerSubscribe(i)}catch(n){throw a(()=>s?.(),n)}return()=>h([()=>o(),()=>s?.()])}},E=class extends c{config;constructor(t){super(),this.config=t}getState(){return this.config.binding.getState()}outerSubscribe(t){return this.config.binding.subscribe(t)}_connect(){let t=`Runtime event "${this.config.event}"`,e=r=>{this.notifySubscribers(r,t)},s=this.config.binding.getState(),i=s?.unstable_on(this.config.event,e),o=()=>{let r=this.config.binding.getState();if(r===s)return;s=r;let l=i;i=void 0;try{l?.()}finally{i=r?.unstable_on(this.config.event,e),this.config.notifyOnRebind&&e({})}},n;try{n=this.outerSubscribe(o)}catch(r){throw a(()=>i?.(),r)}return()=>h([()=>n(),()=>i?.()])}};export{c as BaseSubject,_ as BaseSubscribable,E as EventSubscriptionSubject,w as LazyMemoizeSubject,m as NestedSubscriptionSubject,u as SKIP_UPDATE,y as ShallowMemoizeSubject,v as WritableSubscribable,b as notifySubscribers,h as runCleanups};
//# sourceMappingURL=subscribable.mjs.map