@hub3js/core 0.59.1-next.1 → 0.59.1-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/hub/mod.d.ts CHANGED
@@ -6,4 +6,6 @@ export { Hub } from './hub.js';
6
6
  export type { Store, State, ProviderMetadata } from './store/mod.js';
7
7
  export { createStore, guessProviderStateSelector, namespaceStateSelector, } from './store/mod.js';
8
8
  export { generateStoreId } from './helpers.js';
9
+ export type { CanEagerConnect } from './namespaces/types.js';
10
+ export type { FunctionWithContext } from './types.js';
9
11
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/hub/mod.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,OAAO,EACP,UAAU,EACV,iBAAiB,EACjB,OAAO,GACR,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,EACL,WAAW,EACX,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/hub/mod.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,OAAO,EACP,UAAU,EACV,iBAAiB,EACjB,OAAO,GACR,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,EACL,WAAW,EACX,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,YAAY,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC"}
package/dist/mod.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { Actions, RegisteredNamespaces, Store, State, ProviderMetadata, Subscriber, SubscriberCleanUp, Context, } from './hub/mod.js';
1
+ export type { Actions, RegisteredNamespaces, Store, State, ProviderMetadata, Subscriber, SubscriberCleanUp, Context, CanEagerConnect, FunctionWithContext, } from './hub/mod.js';
2
2
  export { Hub, Provider, Namespace, createStore, guessProviderStateSelector, namespaceStateSelector, } from './hub/mod.js';
3
3
  export type { ProxiedNamespace, FindProxiedNamespace } from './builders/mod.js';
4
4
  export { NamespaceBuilder, ProviderBuilder, ActionBuilder, } from './builders/mod.js';
package/dist/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,OAAO,EACP,oBAAoB,EACpB,KAAK,EACL,KAAK,EACL,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,OAAO,GACR,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,GAAG,EACH,QAAQ,EACR,SAAS,EACT,WAAW,EACX,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,cAAc,CAAC;AAEtB,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,aAAa,GACd,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,OAAO,EACP,oBAAoB,EACpB,KAAK,EACL,KAAK,EACL,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,OAAO,EACP,eAAe,EACf,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,GAAG,EACH,QAAQ,EACR,SAAS,EACT,WAAW,EACX,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,cAAc,CAAC;AAEtB,YAAY,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,aAAa,GACd,MAAM,mBAAmB,CAAC"}
package/dist/mod.js CHANGED
@@ -1,2 +1,2 @@
1
- var D=Object.defineProperty;var s=(a,e)=>D(a,"name",{value:e,configurable:!0});function T(a){return a.constructor.name==="AsyncFunction"}s(T,"isAsync");function N(a,e){return`${a}$$${e}`}s(N,"generateStoreId");var l=s(a=>`Couldn't find "${a}" action. Are you sure you've added the action?`,"ACTION_NOT_FOUND_ERROR"),E=s(a=>`An error occurred during running ${a}`,"OR_ELSE_ACTION_FAILED_ERROR"),P=s(a=>`An error occurred during running before for "${a}" action`,"BEFORE_ACTION_FAILED_ERROR"),C="For setup store, you should set `store` first.";var u=class{static{s(this,"Namespace")}namespaceId;providerId;#e;#r=new Map;#n=new Map;#t=new Map;#o=new Map;#s=!1;#i;#c;constructor(e,t,r){let{configs:n,actions:o}=r;this.namespaceId=e,this.providerId=t,this.#c=n||new Map,this.#e=o,r.store&&this.store(r.store)}init(){if(this.#s)return;let e=this.#e.get("init");e&&e(this.#a()),this.#s=!0}state(){let e=this.#i;if(!e)throw new Error("You need to set your store using `.store` method first.");let t=this.#m();return[s(o=>{let i=e.getState().namespaces.getNamespaceData(t);return o?i[o]:i},"getState"),s((o,i)=>{e.getState().namespaces.updateStatus(t,o,i)},"setState")]}store(e){return this.#i&&console.warn("You've already set an store for your Namespace. Old store will be replaced by the new one."),this.#i=e,this.#l(),this}and_then(e,t){let r=this.#r.get(e)||[];return this.#r.set(e,r.concat(t)),this}or_else(e,t){let r=this.#n.get(e)||[];return this.#n.set(e,r.concat(t)),this}after(e,t,r){let n=this.#o.get(e)||[],o={hook:t,options:{context:r?.context}};return this.#o.set(e,n.concat(o)),this}before(e,t,r){let n=this.#t.get(e)||[],o={hook:t,options:{context:r?.context}};return this.#t.set(e,n.concat(o)),this}has(e){return!!this.#e.get(e)}run(e,...t){let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));return T(r)?this.#v(e,t):this.#d(e,t)}#d(e,t){try{this.#u(e)}catch(i){throw this.#p(e),new Error(P(e.toString()),{cause:i})}let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));let n=this.#a(),o;try{o=r(n,...t),o=this.#h(e,o)}catch(i){o=this.#f(e,i)}finally{this.#p(e)}return o}async#v(e,t){try{this.#u(e)}catch(o){throw this.#p(e),new Error(P(e.toString()),{cause:o})}let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));let n=this.#a();return await r(n,...t).then(o=>this.#h(e,o)).catch(o=>this.#f(e,o)).finally(()=>this.#p(e))}#p(e){let t=this.#o.get(e);t&&t.forEach(r=>{let n=r.options?.context||this.#a();r.hook(n)})}#u(e){let t=this.#t.get(e);t&&t.forEach(r=>{let n=r.options?.context||this.#a();r.hook(n)})}#h(e,t){let r=this.#r.get(e);if(r){let n=this.#a();t=r.reduce((o,i)=>i(n,o),t)}return t}#f(e,t){let r=this.#n.get(e);if(r)try{let n=this.#a();return r.reduce((o,i)=>i(n,o),t)}catch(n){if(n instanceof Error)throw n.cause=t,n;let o=E(`${e.toString()} for ${this.namespaceId} namespace.`),i=new Error(String(n),{cause:t});throw new Error(o,{cause:i})}else throw t}#l(){let e=this.#i;if(!e)throw new Error(C);let t=this.#m();e.getState().namespaces.addNamespace(t,{namespaceId:this.namespaceId,providerId:this.providerId})}#m(){return N(this.providerId,this.namespaceId)}#a(){return{state:this.state.bind(this),action:this.run.bind(this)}}};var h=class{static{s(this,"Provider")}id;version="1.0";#e;#r=!1;#n={};#t;#o;constructor(e,t,r,n){this.id=e,this.#o=r,this.#n=n?.extendInternalActions||{},this.#e=t,n?.store&&(this.#t=n.store,this.#i())}init(){if(this.#r)return;let e=this.#n.init;e&&e(this.#c()),this.#r=!0}state(){let e=this.#t;if(!e)throw new Error(`Any store detected for ${this.id}. You need to set your store using '.store' method first.`);return[s(n=>{let o=e.getState().providers.guessNamespacesState(this.id);if(!n)return o;switch(n){case"installed":case"connected":case"connecting":return o[n];default:throw new Error("Unhandled state for provider")}},"getState"),s((n,o)=>{if(n==="installed")return e.getState().providers.updateStatus(this.id,n,o);throw new Error(`Unhandled state update for provider. (provider id: ${this.id}, state name: ${n})`)},"setState")]}store(e){return this.#t&&console.warn("You've already set an store for your Provider. Old store will be replaced by the new one."),this.#t=e,this.#i(),this}info(){let e=this.#t;return e?{metadata:e.getState().providers.list[this.id].config.metadata}:this.#o}getAll(){return this.#e}get(e){return this.#e.get(e)}findByNamespace(e){let t;return this.#e.forEach(r=>{r.namespaceId===e&&(t=r)}),t}before(e,t){return this.#s("before",e,t),this}after(e,t){return this.#s("after",e,t),this}#s(e,t,r){let n={state:this.state.bind(this)};return this.#e.forEach(o=>{if(e==="after")o.after(t,r,{context:n});else if(e==="before")o.before(t,r,{context:n});else throw new Error(`Your hook name is invalid: ${e}`)}),this}#i(){let e=this.#t;if(!e)throw new Error("For setup store, you should set `store` first.");e.getState().providers.addProvider(this.id,this.#o),this.#e.forEach(t=>{t.store(e)})}#c(){return{state:this.state.bind(this)}}};var S=class{static{s(this,"Hub")}#e=new Map;#r;constructor(e){this.#r=e??{}}init(){this.runAll("init")}runAll(e){let t=[],r=this.#e.values();for(let n of r){let o={id:n.id,provider:void 0,namespaces:[]},i=n[e];typeof i=="function"&&(o.provider=i.call(n));let c=n.getAll().values();for(let p of c){let f=p[e];if(typeof f=="function"){let j=f();o.namespaces.push(j)}}t.push(o)}return t}add(e,t){return this.#r.store&&t.store(this.#r.store),this.#e.set(e,t),this}remove(e){if(!this.#e.get(e))throw new Error(`Provider not found: No provider exists with ID "${e}"`);return this.#r.store?.getState().providers.removeProvider(e),this.#e.delete(e),this}get(e){return this.#e.get(e)}getAll(){return this.#e}state(){let e=this.runAll("state"),t={};return e.forEach(r=>{let n=[];r.namespaces.forEach(i=>{let[c]=i;n.push(c())});let[o]=r.provider;t[r.id]={...o()||{},namespaces:n}}),t}};function d(a,e){let t=a.namespaces.list,r=Object.keys(t).filter(c=>t[c].info.providerId===e),n=!!a.providers.list[e]?.data.installed,o=r.length>0?r.some(c=>t[c].data.connected):!1,i=r.length>0?r.some(c=>t[c].data.connecting):!1;return{installed:n,connected:o,connecting:i}}s(d,"guessProviderStateSelector");function v(a,e){return a.namespaces.list[e].data}s(v,"namespaceStateSelector");import{createStore as M}from"zustand/vanilla";function k(a){let e=s(t=>r=>{let n=s((o,i,c)=>{for(let p of o)r(p,i,c)},"executeListener");return t.subscribe((o,i)=>{let c=W(o,i),f=[...A(o),...c];n(f,o,i)}),{flushEvents:s(()=>{let o=t.getState(),i=A(o);n(i,o,o)},"flushEvents")}},"extendedSubscribe");return new Proxy(a,{get:s(function(t,r,n){return r==="subscribe"?e(t):Reflect.get(t,r,n)},"get")})}s(k,"extend");function A(a){return[...a.providers.events,...a.namespaces.events]}s(A,"tryConsumeEvents");function W(a,e){let t=[];for(let r of Object.keys(a.providers.list)){let n=d(a,r),o=d(e,r);if(o.connecting!==n.connecting){let i={type:"provider_connecting",provider:r,value:n.connecting};t.push(i)}if(!o.connected&&n.connected){let i={type:"provider_connected",provider:r};t.push(i)}if(o.connected&&!n.connected){let i={type:"provider_disconnected",provider:r};t.push(i)}}return t}s(W,"getEventsLike");var R=s(()=>({config:{}}),"hubStore");import{produce as I}from"immer";var m=class{static{s(this,"ConsumableEvents")}#e=[];push(e){this.#e.push(e)}[Symbol.iterator](){return{next:s(()=>this.#e.length==0?{done:!0,value:void 0}:{done:!1,value:this.#e.shift()},"next")}}};var K=s((a,e)=>({events:new m,list:{},addNamespace:s((t,r)=>{let o={data:{accounts:null,network:null,connected:!1,connecting:!1,connectArgs:null},error:"",info:r};a(I(i=>{i.namespaces.list[t]=o}))},"addNamespace"),updateStatus:s((t,r,n)=>{let o=e().namespaces.list[t];if(!o)throw new Error(`No namespace with '${t}' found.`);e().namespaces._produceEventsWhenUpdatingStatus(o,t,r,n),a(I(i=>{i.namespaces.list[t].data[r]=n}))},"updateStatus"),getNamespaceData(t){return v(e(),t)},_produceEventsWhenUpdatingStatus:s((t,r,n,o)=>{if(n==="accounts")if(Object.is(o,null)||Array.isArray(o)&&o.length===0){if(e().namespaces.list[r].data.connected){let p={type:"namespace_disconnected",provider:t.info.providerId,namespace:t.info.namespaceId};e().namespaces.events.push(p)}}else{let c=e().namespaces.list[r].data.accounts;if(c){if(!([...c].sort().toString()===o.sort().toString())){let f={type:"namespace_account_switched",provider:t.info.providerId,namespace:t.info.namespaceId,previousAccounts:c,accounts:o};e().namespaces.events.push(f)}}else{let p={type:"namespace_connected",provider:t.info.providerId,namespace:t.info.namespaceId,accounts:o};e().namespaces.events.push(p)}}else if(n==="network"){let i=e().namespaces.list[r].data.network,c={type:"namespace_network_switched",provider:t.info.providerId,namespace:t.info.namespaceId,network:o,previousNetwork:i};e().namespaces.events.push(c)}},"_produceEventsWhenUpdatingStatus")}),"namespacesStore");import{produce as b}from"immer";var O=s((a,e)=>({events:new m,list:{},addProvider:s((t,r)=>{let n={data:{installed:!1},error:"",config:r};a(b(o=>{o.providers.list[t]=n}))},"addProvider"),removeProvider:s(t=>{a(b(r=>{delete r.providers.list[t]}))},"removeProvider"),updateStatus:s((t,r,n)=>{let o=e().providers.list[t];if(!o)throw new Error(`No namespace namespace with '${t}' found.`);e().providers._produceEventsWhenUpdatingStatus(o,t,r,n),a(b(i=>{i.providers.list[t].data[r]=n}))},"updateStatus"),guessNamespacesState:s(t=>d(e(),t),"guessNamespacesState"),_produceEventsWhenUpdatingStatus:s((t,r,n,o)=>{if(n==="installed"){let i={type:"provider_detected",provider:r};e().providers.events.push(i)}},"_produceEventsWhenUpdatingStatus")}),"providersStore");var w=s(()=>{let a=M((...e)=>({hub:R(...e),providers:O(...e),namespaces:K(...e)}));return k(a)},"createStore");var _=["init","state","after","before","and_then","or_else","store"],F=["string","number"],y=class{static{s(this,"NamespaceBuilder")}#e;#r;#n=new Map;#t=[];#o;constructor(e,t){this.#e=e,this.#r=t,this.#o={}}config(e,t){return this.#o[e]=t,this}action(e,t){return Array.isArray(e)?(e.forEach(([r,n])=>{this.#n.set(r,n)}),this):typeof e=="object"&&e?.actionName?(this.#t.push(e),this):(t&&this.#n.set(e,t),this)}build(){if(this.#s(this.#o))return this.#d(this.#o);throw new Error("You namespace config isn't valid.")}#s(e){return!0}#i(e){this.#t.forEach(t=>{t.after.forEach(r=>{r.map(n=>{e.after(t.actionName,n)})}),t.before.forEach(r=>{r.map(n=>{e.before(t.actionName,n)})}),t.and.forEach(r=>{r.map(n=>{e.and_then(t.actionName,n)})}),t.or.forEach(r=>{r.map(n=>{e.or_else(t.actionName,n)})})})}#c(){this.#t.forEach(e=>{this.#n.set(e.actionName,e.action)})}#d(e){this.#c();let t=new u(this.#e,this.#r,{configs:e,actions:this.#n});return this.#i(t),new Proxy(t,{has:s((n,o)=>{if(typeof o!="string")throw new Error("You can use string as your property on Namespace instance.");return _.includes(o)||F.includes(typeof t[o])?!0:t.has(o)},"has"),get:s((n,o)=>{if(typeof o!="string")throw new Error("You can use string as your property on Namespace instance.");let i=t[o];return _.includes(o)?i.bind(t):F.includes(typeof i)?i:t.run.bind(t,o)},"get"),set:s(()=>{throw new Error("You can not set anything on this object.")},"set")})}};var g=class{static{s(this,"ProviderBuilder")}#e;#r=new Map;#n={};#t={};#o;constructor(e,t){this.#e=e,this.#o=t||{}}add(e,t){return this.#o.store&&t.store(this.#o.store),this.#r.set(e,t),this}config(e,t){return this.#t[e]=t,this}init(e){return this.#n.init=e,this}build(){if(this.#s(this.#t))return new h(this.#e,this.#r,this.#t,{extendInternalActions:this.#n,store:this.#o.store});throw new Error("You need to set all required configs.")}#s(e){return!!e.metadata}};var x=class{static{s(this,"ActionBuilder")}name;#e=new Map;#r=new Map;#n=new Map;#t=new Map;#o;constructor(e){this.name=e}and(e){return this.#e.has(this.name)||this.#e.set(this.name,[]),this.#e.get(this.name)?.push(e),this}or(e){return this.#r.has(this.name)||this.#r.set(this.name,[]),this.#r.get(this.name)?.push(e),this}before(e){return this.#t.has(this.name)||this.#t.set(this.name,[]),this.#t.get(this.name)?.push(e),this}after(e){return this.#n.has(this.name)||this.#n.set(this.name,[]),this.#n.get(this.name)?.push(e),this}action(e){return this.#o=e,this}build(){if(!this.#o)throw new Error("Your action builder should includes an action.");return{actionName:this.name,action:this.#o,before:this.#t,after:this.#n,and:this.#e,or:this.#r}}};export{x as ActionBuilder,S as Hub,u as Namespace,y as NamespaceBuilder,h as Provider,g as ProviderBuilder,w as createStore,d as guessProviderStateSelector,v as namespaceStateSelector};
1
+ var D=Object.defineProperty;var s=(a,e)=>D(a,"name",{value:e,configurable:!0});function C(a){return a.constructor.name==="AsyncFunction"}s(C,"isAsync");function N(a,e){return`${a}$$${e}`}s(N,"generateStoreId");var l=s(a=>`Couldn't find "${a}" action. Are you sure you've added the action?`,"ACTION_NOT_FOUND_ERROR"),E=s(a=>`An error occurred during running ${a}`,"OR_ELSE_ACTION_FAILED_ERROR"),P=s(a=>`An error occurred during running before for "${a}" action`,"BEFORE_ACTION_FAILED_ERROR"),T="For setup store, you should set `store` first.";var u=class{static{s(this,"Namespace")}namespaceId;providerId;#e;#r=new Map;#n=new Map;#t=new Map;#o=new Map;#s=!1;#i;#c;constructor(e,t,r){let{configs:n,actions:o}=r;this.namespaceId=e,this.providerId=t,this.#c=n||new Map,this.#e=o,r.store&&this.store(r.store)}init(){if(this.#s)return;let e=this.#e.get("init");e&&e(this.#a()),this.#s=!0}state(){let e=this.#i;if(!e)throw new Error("You need to set your store using `.store` method first.");let t=this.#m();return[s(o=>{let i=e.getState().namespaces.getNamespaceData(t);return o?i[o]:i},"getState"),s((o,i)=>{e.getState().namespaces.updateStatus(t,o,i)},"setState")]}store(e){return this.#i&&console.warn("You've already set an store for your Namespace. Old store will be replaced by the new one."),this.#i=e,this.#l(),this}and_then(e,t){let r=this.#r.get(e)||[];return this.#r.set(e,r.concat(t)),this}or_else(e,t){let r=this.#n.get(e)||[];return this.#n.set(e,r.concat(t)),this}after(e,t,r){let n=this.#o.get(e)||[],o={hook:t,options:{context:r?.context}};return this.#o.set(e,n.concat(o)),this}before(e,t,r){let n=this.#t.get(e)||[],o={hook:t,options:{context:r?.context}};return this.#t.set(e,n.concat(o)),this}has(e){return!!this.#e.get(e)}run(e,...t){let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));return C(r)?this.#v(e,t):this.#d(e,t)}#d(e,t){try{this.#u(e)}catch(i){throw this.#p(e),new Error(P(e.toString()),{cause:i})}let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));let n=this.#a(),o;try{o=r(n,...t),o=this.#h(e,o)}catch(i){o=this.#f(e,i)}finally{this.#p(e)}return o}async#v(e,t){try{this.#u(e)}catch(o){throw this.#p(e),new Error(P(e.toString()),{cause:o})}let r=this.#e.get(e);if(!r)throw new Error(l(e.toString()));let n=this.#a();return await r(n,...t).then(o=>this.#h(e,o)).catch(o=>this.#f(e,o)).finally(()=>this.#p(e))}#p(e){let t=this.#o.get(e);t&&t.forEach(r=>{let n=r.options?.context||this.#a();r.hook(n)})}#u(e){let t=this.#t.get(e);t&&t.forEach(r=>{let n=r.options?.context||this.#a();r.hook(n)})}#h(e,t){let r=this.#r.get(e);if(r){let n=this.#a();t=r.reduce((o,i)=>i(n,o),t)}return t}#f(e,t){let r=this.#n.get(e);if(r)try{let n=this.#a();return r.reduce((o,i)=>i(n,o),t)}catch(n){if(n instanceof Error)throw n.cause=t,n;let o=E(`${e.toString()} for ${this.namespaceId} namespace.`),i=new Error(String(n),{cause:t});throw new Error(o,{cause:i})}else throw t}#l(){let e=this.#i;if(!e)throw new Error(T);let t=this.#m();e.getState().namespaces.addNamespace(t,{namespaceId:this.namespaceId,providerId:this.providerId})}#m(){return N(this.providerId,this.namespaceId)}#a(){return{state:this.state.bind(this),action:this.run.bind(this)}}};var h=class{static{s(this,"Provider")}id;version="1.0";#e;#r=!1;#n={};#t;#o;constructor(e,t,r,n){this.id=e,this.#o=r,this.#n=n?.extendInternalActions||{},this.#e=t,n?.store&&(this.#t=n.store,this.#i())}init(){if(this.#r)return;let e=this.#n.init;e&&e(this.#c()),this.#r=!0}state(){let e=this.#t;if(!e)throw new Error(`Any store detected for ${this.id}. You need to set your store using '.store' method first.`);return[s(n=>{let o=e.getState().providers.guessNamespacesState(this.id);if(!n)return o;switch(n){case"installed":case"connected":case"connecting":return o[n];default:throw new Error("Unhandled state for provider")}},"getState"),s((n,o)=>{if(n==="installed")return e.getState().providers.updateStatus(this.id,n,o);throw new Error(`Unhandled state update for provider. (provider id: ${this.id}, state name: ${n})`)},"setState")]}store(e){return this.#t&&console.warn("You've already set an store for your Provider. Old store will be replaced by the new one."),this.#t=e,this.#i(),this}info(){let e=this.#t;return e?{metadata:e.getState().providers.list[this.id].config.metadata}:this.#o}getAll(){return this.#e}get(e){return this.#e.get(e)}findByNamespace(e){let t;return this.#e.forEach(r=>{r.namespaceId===e&&(t=r)}),t}before(e,t){return this.#s("before",e,t),this}after(e,t){return this.#s("after",e,t),this}#s(e,t,r){let n={state:this.state.bind(this)};return this.#e.forEach(o=>{if(e==="after")o.after(t,r,{context:n});else if(e==="before")o.before(t,r,{context:n});else throw new Error(`Your hook name is invalid: ${e}`)}),this}#i(){let e=this.#t;if(!e)throw new Error("For setup store, you should set `store` first.");e.getState().providers.addProvider(this.id,this.#o),this.#e.forEach(t=>{t.store(e)})}#c(){return{state:this.state.bind(this)}}};var S=class{static{s(this,"Hub")}#e=new Map;#r;constructor(e){this.#r=e??{}}init(){this.runAll("init")}runAll(e){let t=[],r=this.#e.values();for(let n of r){let o={id:n.id,provider:void 0,namespaces:[]},i=n[e];typeof i=="function"&&(o.provider=i.call(n));let c=n.getAll().values();for(let p of c){let f=p[e];if(typeof f=="function"){let j=f();o.namespaces.push(j)}}t.push(o)}return t}add(e,t){return this.#r.store&&t.store(this.#r.store),this.#e.set(e,t),this}remove(e){if(!this.#e.get(e))throw new Error(`Provider not found: No provider exists with ID "${e}"`);return this.#r.store?.getState().providers.removeProvider(e),this.#e.delete(e),this}get(e){return this.#e.get(e)}getAll(){return this.#e}state(){let e=this.runAll("state"),t={};return e.forEach(r=>{let n=[];r.namespaces.forEach(i=>{let[c]=i;n.push(c())});let[o]=r.provider;t[r.id]={...o()||{},namespaces:n}}),t}};function d(a,e){let t=a.namespaces.list,r=Object.keys(t).filter(c=>t[c].info.providerId===e),n=!!a.providers.list[e]?.data.installed,o=r.length>0?r.some(c=>t[c].data.connected):!1,i=r.length>0?r.some(c=>t[c].data.connecting):!1;return{installed:n,connected:o,connecting:i}}s(d,"guessProviderStateSelector");function v(a,e){return a.namespaces.list[e].data}s(v,"namespaceStateSelector");import{createStore as M}from"zustand/vanilla";function k(a){let e=s(t=>r=>{let n=s((o,i,c)=>{for(let p of o)r(p,i,c)},"executeListener");return t.subscribe((o,i)=>{let c=W(o,i),f=[...A(o),...c];n(f,o,i)}),{flushEvents:s(()=>{let o=t.getState(),i=A(o);n(i,o,o)},"flushEvents")}},"extendedSubscribe");return new Proxy(a,{get:s(function(t,r,n){return r==="subscribe"?e(t):Reflect.get(t,r,n)},"get")})}s(k,"extend");function A(a){return[...a.providers.events,...a.namespaces.events]}s(A,"tryConsumeEvents");function W(a,e){let t=[];for(let r of Object.keys(a.providers.list)){let n=d(a,r),o=d(e,r);if(o.connecting!==n.connecting){let i={type:"provider_connecting",provider:r,value:n.connecting};t.push(i)}if(!o.connected&&n.connected){let i={type:"provider_connected",provider:r};t.push(i)}if(o.connected&&!n.connected){let i={type:"provider_disconnected",provider:r};t.push(i)}}return t}s(W,"getEventsLike");var R=s(()=>({config:{}}),"hubStore");import{produce as I}from"immer";var m=class{static{s(this,"ConsumableEvents")}#e=[];push(e){this.#e.push(e)}[Symbol.iterator](){return{next:s(()=>this.#e.length==0?{done:!0,value:void 0}:{done:!1,value:this.#e.shift()},"next")}}};var K=s((a,e)=>({events:new m,list:{},addNamespace:s((t,r)=>{let o={data:{accounts:null,network:null,connected:!1,connecting:!1,connectArgs:null},error:"",info:r};a(I(i=>{i.namespaces.list[t]=o}))},"addNamespace"),updateStatus:s((t,r,n)=>{let o=e().namespaces.list[t];if(!o)throw new Error(`No namespace with '${t}' found.`);e().namespaces._produceEventsWhenUpdatingStatus(o,t,r,n),a(I(i=>{i.namespaces.list[t].data[r]=n}))},"updateStatus"),getNamespaceData(t){return v(e(),t)},_produceEventsWhenUpdatingStatus:s((t,r,n,o)=>{if(n==="accounts")if(Object.is(o,null)||Array.isArray(o)&&o.length===0){if(e().namespaces.list[r].data.connected){let p={type:"namespace_disconnected",provider:t.info.providerId,namespace:t.info.namespaceId};e().namespaces.events.push(p)}}else{let c=e().namespaces.list[r].data.accounts;if(c){if(!([...c].sort().toString()===o.sort().toString())){let f={type:"namespace_account_switched",provider:t.info.providerId,namespace:t.info.namespaceId,previousAccounts:c,accounts:o};e().namespaces.events.push(f)}}else{let p={type:"namespace_connected",provider:t.info.providerId,namespace:t.info.namespaceId,accounts:o};e().namespaces.events.push(p)}}else if(n==="network"){let i=e().namespaces.list[r].data.network,c={type:"namespace_network_switched",provider:t.info.providerId,namespace:t.info.namespaceId,network:o,previousNetwork:i};e().namespaces.events.push(c)}},"_produceEventsWhenUpdatingStatus")}),"namespacesStore");import{produce as b}from"immer";var O=s((a,e)=>({events:new m,list:{},addProvider:s((t,r)=>{let n={data:{installed:!1},error:"",config:r};a(b(o=>{o.providers.list[t]=n}))},"addProvider"),removeProvider:s(t=>{a(b(r=>{delete r.providers.list[t]}))},"removeProvider"),updateStatus:s((t,r,n)=>{let o=e().providers.list[t];if(!o)throw new Error(`No namespace namespace with '${t}' found.`);e().providers._produceEventsWhenUpdatingStatus(o,t,r,n),a(b(i=>{i.providers.list[t].data[r]=n}))},"updateStatus"),guessNamespacesState:s(t=>d(e(),t),"guessNamespacesState"),_produceEventsWhenUpdatingStatus:s((t,r,n,o)=>{if(n==="installed"){let i={type:"provider_detected",provider:r};e().providers.events.push(i)}},"_produceEventsWhenUpdatingStatus")}),"providersStore");var w=s(()=>{let a=M((...e)=>({hub:R(...e),providers:O(...e),namespaces:K(...e)}));return k(a)},"createStore");var F=["init","state","after","before","and_then","or_else","store"],_=["string","number"],y=class{static{s(this,"NamespaceBuilder")}#e;#r;#n=new Map;#t=[];#o;constructor(e,t){this.#e=e,this.#r=t,this.#o={}}config(e,t){return this.#o[e]=t,this}action(e,t){return Array.isArray(e)?(e.forEach(([r,n])=>{this.#n.set(r,n)}),this):typeof e=="object"&&e?.actionName?(this.#t.push(e),this):(t&&this.#n.set(e,t),this)}build(){if(this.#s(this.#o))return this.#d(this.#o);throw new Error("You namespace config isn't valid.")}#s(e){return!0}#i(e){this.#t.forEach(t=>{t.after.forEach(r=>{r.map(n=>{e.after(t.actionName,n)})}),t.before.forEach(r=>{r.map(n=>{e.before(t.actionName,n)})}),t.and.forEach(r=>{r.map(n=>{e.and_then(t.actionName,n)})}),t.or.forEach(r=>{r.map(n=>{e.or_else(t.actionName,n)})})})}#c(){this.#t.forEach(e=>{this.#n.set(e.actionName,e.action)})}#d(e){this.#c();let t=new u(this.#e,this.#r,{configs:e,actions:this.#n});return this.#i(t),new Proxy(t,{has:s((n,o)=>{if(typeof o!="string")throw new Error("You can use string as your property on Namespace instance.");return F.includes(o)||_.includes(typeof t[o])?!0:t.has(o)},"has"),get:s((n,o)=>{if(typeof o!="string")throw new Error("You can use string as your property on Namespace instance.");let i=t[o];return F.includes(o)?i.bind(t):_.includes(typeof i)?i:t.run.bind(t,o)},"get"),set:s(()=>{throw new Error("You can not set anything on this object.")},"set")})}};var g=class{static{s(this,"ProviderBuilder")}#e;#r=new Map;#n={};#t={};#o;constructor(e,t){this.#e=e,this.#o=t||{}}add(e,t){return this.#o.store&&t.store(this.#o.store),this.#r.set(e,t),this}config(e,t){return this.#t[e]=t,this}init(e){return this.#n.init=e,this}build(){if(this.#s(this.#t))return new h(this.#e,this.#r,this.#t,{extendInternalActions:this.#n,store:this.#o.store});throw new Error("You need to set all required configs.")}#s(e){return!!e.metadata}};var x=class{static{s(this,"ActionBuilder")}name;#e=new Map;#r=new Map;#n=new Map;#t=new Map;#o;constructor(e){this.name=e}and(e){return this.#e.has(this.name)||this.#e.set(this.name,[]),this.#e.get(this.name)?.push(e),this}or(e){return this.#r.has(this.name)||this.#r.set(this.name,[]),this.#r.get(this.name)?.push(e),this}before(e){return this.#t.has(this.name)||this.#t.set(this.name,[]),this.#t.get(this.name)?.push(e),this}after(e){return this.#n.has(this.name)||this.#n.set(this.name,[]),this.#n.get(this.name)?.push(e),this}action(e){return this.#o=e,this}build(){if(!this.#o)throw new Error("Your action builder should includes an action.");return{actionName:this.name,action:this.#o,before:this.#t,after:this.#n,and:this.#e,or:this.#r}}};export{x as ActionBuilder,S as Hub,u as Namespace,y as NamespaceBuilder,h as Provider,g as ProviderBuilder,w as createStore,d as guessProviderStateSelector,v as namespaceStateSelector};
2
2
  //# sourceMappingURL=mod.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hub3js/core",
3
- "version": "0.59.1-next.1",
3
+ "version": "0.59.1-next.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/mod.ts",
package/src/hub/mod.ts CHANGED
@@ -17,3 +17,5 @@ export {
17
17
  namespaceStateSelector,
18
18
  } from './store/mod.js';
19
19
  export { generateStoreId } from './helpers.js';
20
+ export type { CanEagerConnect } from './namespaces/types.js';
21
+ export type { FunctionWithContext } from './types.js';
package/src/mod.ts CHANGED
@@ -7,6 +7,8 @@ export type {
7
7
  Subscriber,
8
8
  SubscriberCleanUp,
9
9
  Context,
10
+ CanEagerConnect,
11
+ FunctionWithContext,
10
12
  } from './hub/mod.js';
11
13
  export {
12
14
  Hub,