@stacksjs/events 0.75.568 → 0.75.571
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/index.d.ts +7 -0
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -93,6 +93,8 @@ declare const useEvents: Emitter<StacksEvents>;
|
|
|
93
93
|
declare const dispatch: Dispatch;
|
|
94
94
|
declare const dispatchAsync: DispatchAsync;
|
|
95
95
|
declare const dispatchAndCollect: DispatchAndCollect;
|
|
96
|
+
/** `dispatchAndCollect`, answering in place when every listener did (see `emitAndCollectInPlace`). */
|
|
97
|
+
declare const dispatchAndCollectInPlace: DispatchAndCollectInPlace;
|
|
96
98
|
declare const useEvent: Dispatch;
|
|
97
99
|
declare const all: EventHandlerMap<StacksEvents>;
|
|
98
100
|
declare const listen: Listen;
|
|
@@ -113,6 +115,7 @@ export declare interface Emitter<Events extends EventMap> {
|
|
|
113
115
|
(<Key extends keyof Events>(_type: undefined extends Events[Key] ? Key : never) => void)
|
|
114
116
|
emitAsync: <Key extends keyof Events>(_type: Key, _event: Events[Key]) => Promise<unknown[]>
|
|
115
117
|
emitAndCollect: <Key extends keyof Events>(_type: Key, _event: Events[Key]) => Promise<Array<{ ok: true, value: unknown } | { ok: false, error: Error }>>
|
|
118
|
+
emitAndCollectInPlace: <Key extends keyof Events>(_type: Key, _event: Events[Key]) => HandlerOutcome[] | Promise<HandlerOutcome[]>
|
|
116
119
|
removeAllListeners: (_type?: keyof Events | '*') => void
|
|
117
120
|
listenerCount: (_type: keyof Events | '*') => number
|
|
118
121
|
}
|
|
@@ -249,6 +252,8 @@ export type EventHandlerMap<Events extends EventMap> = Map<
|
|
|
249
252
|
keyof Events | '*',
|
|
250
253
|
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>
|
|
251
254
|
>;
|
|
255
|
+
/** What one handler made of an event: what it returned, or what it threw. */
|
|
256
|
+
export type HandlerOutcome = { ok: true, value: unknown } | { ok: false, error: Error }
|
|
252
257
|
/**
|
|
253
258
|
* Explicit priorities (stacksjs/stacks#1878 E-2), per event bucket and then
|
|
254
259
|
* per handler. They used to be written onto the handler function, so one
|
|
@@ -316,10 +321,12 @@ declare type Listen = <Key extends keyof StacksEvents>(_type: Key, _handler: Han
|
|
|
316
321
|
declare type Off = <Key extends keyof StacksEvents>(_type: Key, handler?: Handler<StacksEvents[Key]>) => void;
|
|
317
322
|
declare type DispatchAsync = <Key extends keyof StacksEvents>(_type: Key, _event: StacksEvents[Key]) => Promise<unknown[]>;
|
|
318
323
|
declare type DispatchAndCollect = <Key extends keyof StacksEvents>(_type: Key, _event: StacksEvents[Key]) => Promise<Array<{ ok: true, value: unknown } | { ok: false, error: Error }>>;
|
|
324
|
+
declare type DispatchAndCollectInPlace = <Key extends keyof StacksEvents>(_type: Key, _event: StacksEvents[Key]) => HandlerOutcome[] | Promise<HandlerOutcome[]>;
|
|
319
325
|
export {
|
|
320
326
|
all,
|
|
321
327
|
dispatch,
|
|
322
328
|
dispatchAndCollect,
|
|
329
|
+
dispatchAndCollectInPlace,
|
|
323
330
|
dispatchAsync,
|
|
324
331
|
emitter,
|
|
325
332
|
events,
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
import{existsSync as A,readdirSync as ie,statSync as ae}from"fs";import{extname as ce,join as h}from"path";import M from"process";import{existsSync as re}from"fs";import{isAbsolute as se,resolve as I}from"path";import{fileURLToPath as oe}from"url";function U(e,n){if(e.startsWith("file:"))return oe(e);if(e.startsWith(".")||se(e))return I(n,e);if(e.startsWith("@"))return Bun.resolveSync(e,n);let t=I(n,e);if(re(t))return t;return Bun.resolveSync(e,n)}function de(e){return e}async function G(e={}){let n=e.dir??h(M.cwd(),"app","Listeners"),t=e.extensions??[".ts",".js"],r={warn:e.log?.warn??((a)=>console.warn(a)),error:e.log?.error??((a)=>console.error(a)),info:e.log?.info??((a)=>console.info(a))};if(!A(n))return 0;let s=J(n,t);if(s.length===0)return 0;let o=0;for(let a of s)try{let l=await import(a),p=l?.default??l;if(typeof p==="function")continue;if(!le(p)){r.warn(`[events/discover] ${a}: default export doesn't match ListenerModule shape ({ listensTo, handle }), skipping`);continue}for(let E of ue(p.listensTo)){if(!Y(E,a))continue;j(E,(w)=>p.handle(w,E)),o++}}catch(l){r.error(`[events/discover] failed to import ${a}: ${l instanceof Error?l.message:String(l)}`)}if(o>0)r.info(`[events/discover] registered ${o} listener${o===1?"":"s"} from ${n}`);return o}function J(e,n){let t=[],r=ie(e);for(let s of r){let o=h(e,s),a=ae(o);if(a.isDirectory())t.push(...J(o,n));else if(a.isFile()&&n.includes(ce(s)))t.push(o)}return t}function le(e){let n=e?.listensTo;return!!e&&typeof e==="object"&&(typeof n==="string"||Array.isArray(n))&&typeof e.handle==="function"}function ue(e){return(Array.isArray(e)?e:[e]).map((t)=>String(t).trim()).filter(Boolean)}var F=Symbol.for("stacks.events.claimedListeners"),W=globalThis[F]??(globalThis[F]=new Set);function Y(e,n){let t=`${e}\x00${n}`;if(W.has(t))return!1;return W.add(t),!0}function fe(){W.clear(),delete x()[R],x()[K]=z}var R=Symbol.for("stacks.events.appListenersBoot"),K=Symbol.for("stacks.events.dispatchObserver");function x(){return globalThis}function O(){return x()[R]!==void 0}function ve(e={}){let n=x(),t=n[R];if(t)return t;let r=(async()=>{let s=e.base??M.cwd(),o=await Q(e);if(o===0){let a=await q(s);if(a>0)(e.log?.warn??((p)=>console.warn(p)))(`[events] app/Events.ts declares ${a} listener${a===1?"":"s"}, but none could be registered in this process, so the events they handle will reach nobody. See the errors above.`)}return o})();return n[R]=r,r}async function q(e){let n=["ts","js"].map((t)=>h(e,"app",`Events.${t}`)).find((t)=>A(t));if(!n)return 0;try{let t=await import(n),r=t?.default??t;if(!r||typeof r!=="object")return 0;return Object.values(r).flatMap((s)=>Array.isArray(s)?s:[s]).filter((s)=>typeof s==="string"&&s.trim()!=="").length}catch{return 0}}function z(e){let n=x();if(delete n[K],O())return;let t=M.cwd();q(t).then((r)=>{if(r===0||O())return;console.warn(`[events] "${e}" was dispatched, but this process never registered the application's listeners, so nothing handles it or any later event (app/Events.ts declares ${r}). An entry point that runs application code must call injectGlobalAutoImports() from @stacksjs/server, or bootAppListeners() from @stacksjs/events, before its first dispatch.`)})}if(!x()[K]&&!O())x()[K]=z;async function Q(e={}){let n=e.base??M.cwd(),t={warn:e.log?.warn??((s)=>console.warn(s)),error:e.log?.error??((s)=>console.error(s)),info:e.log?.info??((s)=>console.info(s))},r=0;if(r+=await ye(n,t),r+=await G({...e,dir:e.dir??h(n,"app","Listeners"),log:{...t,info:()=>{}}}),r>0)t.info(`[events] registered ${r} listener${r===1?"":"s"}`);return r}async function ye(e,n){let t=["ts","js"].map((o)=>h(e,"app",`Events.${o}`)).find((o)=>A(o));if(!t)return 0;let r;try{let o=await import(t);r=o?.default??o}catch(o){return n.error(`[events] failed to read ${t}: ${o instanceof Error?o.message:String(o)}`),0}if(!r||typeof r!=="object")return 0;let s=0;for(let[o,a]of Object.entries(r))for(let l of Array.isArray(a)?a:[a]){if(typeof l!=="string"||!l.trim())continue;let p=await ke(e,l.trim());if(!p){n.warn(`[events] ${o}: no listener or action called ${l}`);continue}if(!Y(o,p.id))continue;let{handle:E,validations:w}=p;j(o,(T)=>(pe(o,l,T,w,n),E(T,o))),s++}return s}function pe(e,n,t,r,s){if(!r||typeof t!=="object"||t===null)return;let o=t,a=[];for(let[l,p]of Object.entries(r)){let E=p?.rule,w=E?.validate;if(typeof w!=="function")continue;try{if(!w.call(E,o[l]).valid)a.push(l)}catch{a.push(l)}}if(a.length)s.warn(`[events] ${e}: payload does not match what ${n} declares (${a.join(", ")})`)}var C=new Map;async function V(e,n,t){let r=`${e}:${n}`,s=C.get(r);if(s)return s;let o=(async()=>{let a=h(e,"storage","framework","auto-imports"),l=h(a,`${n}.ts`);if(!A(l))return null;let E=(await import(l))[t];if(!E)throw Error(`Invalid ${t} registry: ${l}`);return E})();return C.set(r,o),o}async function ge(e,n){let t=await V(e,"listeners","listeners"),r=await V(e,"actions","actions");if(!t&&!r)return null;let s=t?.[n]??r?.[`Actions/${n}`];return s?U(s,h(e,"storage/framework/auto-imports")):null}function me(){C.clear()}function Ee(e){let n=[h(e,"app"),h(e,"storage","framework","defaults","app")];try{let t=Bun.resolveSync("@stacksjs/defaults/package.json",e);n.push(h(t.slice(0,t.lastIndexOf("/")),"app"))}catch{}return n.filter((t)=>A(t))}async function ke(e,n){let t=[],r=await ge(e,n);if(r)t.push(r);for(let s of Ee(e))for(let o of["Listeners","Actions"])for(let a of["ts","js"])t.push(h(s,o,`${n}.${a}`));for(let s of t){if(!A(s))continue;try{let o=await import(s),a=o?.default??o,l=a?.handle;if(typeof l!=="function")continue;return{id:s,handle:l.bind(a),validations:a?.validations}}catch{continue}}return null}var X=Symbol.for("stacks.events.handler.error");function H(e,n){let t=n.slice(),r=e.get(n);if(t.length<2||!r)return t;return t.map((s,o)=>({h:s,i:o,p:r.get(s)??0})).sort((s,o)=>o.p-s.p||s.i-o.i).map((s)=>s.h)}function he(e){return new RegExp(`^${e.replace(/[\\^$+?()[\]{}|]/g,"\\$&").replace(/\*/g,".*")}$`)}function S(e,n,t){console.error(`[Events] ${e} for '${String(n)}':`,t)}function L(e){return!!e&&typeof e.catch==="function"}function B(e){let n=e??new Map,t=new WeakMap,r=new WeakMap,s=(i,v,d)=>{let f=r.get(d),y=f?.key===i?f.re:void 0;if(!y)y=he(i),r.set(d,{key:i,re:y});return y.test(v)};function o(i,v,d){let f=n.get(i);if(f)f.push(v);else n.set(i,f=[v]);if(d?.priority!==void 0&&Number.isFinite(d.priority)){let y=t.get(f);if(!y)t.set(f,y=new Map);y.set(v,d.priority)}}function a(i,v){let d=(...f)=>(l(i,d),v(...f));d[X]=v,o(i,d)}function l(i,v){let d=n.get(i);if(!d)return;if(!v){n.set(i,[]);return}let f=t.get(d);for(let y=d.length-1;y>=0;y--){let k=d[y];if(k===v||k?.[X]===v){if(d.splice(y,1),!d.includes(k))f?.delete(k)}}}function p(i){if(i===void 0)n.clear();else n.delete(i)}function E(i){return n.get(i)?.length??0}function w(i,v){let d=n.get(i),f=n.get("*"),y=d?H(t,d):void 0,k=f?H(t,f):void 0;if(y)for(let m of y)try{let c=m(v);if(L(c))c.catch((u)=>S("Async handler error",i,u))}catch(c){S("Handler error",i,c)}let g=String(i);if(n.forEach((m,c)=>{let u=String(c);if(u===g||u==="*"||!u.includes("*"))return;if(s(u,g,m))for(let _ of H(t,m))try{let P=_(i,v);if(L(P))P.catch((te)=>S(`Async pattern handler '${u}' error`,i,te))}catch(P){S(`Pattern handler '${u}' error`,i,P)}}),k)for(let m of k)try{let c=m(i,v);if(L(c))c.catch((u)=>S("Async wildcard handler error",i,u))}catch(c){S("Wildcard handler error",i,c)}}async function T(i,v){let d=[],f=async(g,m)=>{if(!g)return;for(let c of H(t,g))try{let u=m?c(i,v):c(v);d.push(L(u)?await u:u)}catch(u){S("Awaited handler error",i,u),d.push(void 0)}};await f(n.get(i),!1);let y=String(i),k=[];n.forEach((g,m)=>{let c=String(m);if(c===y||c==="*"||!c.includes("*"))return;if(s(c,y,g))k.push(c)});for(let g of k)await f(n.get(g),!0);return await f(n.get("*"),!0),d}async function ne(i,v){let d=[],f=async(g,m)=>{if(!g)return;for(let c of H(t,g))try{let u=m?c(i,v):c(v),_=L(u)?await u:u;d.push({ok:!0,value:_})}catch(u){let _=u instanceof Error?u:Error(String(u));d.push({ok:!1,error:_})}};await f(n.get(i),!1);let y=String(i),k=[];n.forEach((g,m)=>{let c=String(m);if(c===y||c==="*"||!c.includes("*"))return;if(s(c,y,g))k.push(c)});for(let g of k)await f(n.get(g),!0);return await f(n.get("*"),!0),d}return{all:n,on:o,once:a,off:l,emit:w,emitAsync:T,emitAndCollect:ne,removeAllListeners:p,listenerCount:E}}var Oe=B,Ge=B;function we(e,n){let t=(r)=>`${n}:${r}`;return{on(r,s,o){e.on(t(r),s,o)},once(r,s){e.once(t(r),s)},off(r,s){e.off(t(r),s)},emit(r,s){e.emit(t(r),s)},emitAsync(r,s){return e.emitAsync(t(r),s)},emitAndCollect(r,s){return e.emitAndCollect(t(r),s)},listenerCount(r){return e.listenerCount(t(r))}}}function Me(e){return e}var Z=Symbol.for("stacks.events.emitter"),ee=globalThis,N=ee[Z]??(ee[Z]=B()),b=N,We=N,be=Symbol.for("stacks.events.dispatchObserver");function D(e){let n=globalThis[be];if(n)n(String(e))}var xe=(e,n)=>(D(e),b.emit(e,n)),Ce=(e,n)=>(D(e),b.emitAsync(e,n)),je=(e,n)=>(D(e),b.emitAndCollect(e,n)),Be=xe,{all:Ne,on:j,on:De,once:Ie,off:Ue}=b;function Fe(e){return we(N,e)}export{Ne as all,O as appListenersBooted,ve as bootAppListeners,B as createEmitter,Ge as default,Me as defineEvents,de as defineListener,G as discoverListeners,xe as dispatch,je as dispatchAndCollect,Ce as dispatchAsync,b as emitter,N as events,j as listen,Oe as mitt,Ue as off,Ie as once,Q as registerAppListeners,fe as resetListenerRegistry,me as resetNameRegistries,we as scope,Fe as scopedEvents,Be as useEvent,We as useEvents,De as useListen};
|
|
2
|
+
import{existsSync as P,readdirSync as ae,statSync as ce}from"fs";import{extname as de,join as w}from"path";import M from"process";import{existsSync as se}from"fs";import{isAbsolute as oe,resolve as N}from"path";import{fileURLToPath as ie}from"url";function U(e,n){if(e.startsWith("file:"))return ie(e);if(e.startsWith(".")||oe(e))return N(n,e);if(e.startsWith("@"))return Bun.resolveSync(e,n);let t=N(n,e);if(se(t))return t;return Bun.resolveSync(e,n)}function le(e){return e}async function G(e={}){let n=e.dir??w(M.cwd(),"app","Listeners"),t=e.extensions??[".ts",".js"],s={warn:e.log?.warn??((c)=>console.warn(c)),error:e.log?.error??((c)=>console.error(c)),info:e.log?.info??((c)=>console.info(c))};if(!P(n))return 0;let r=q(n,t);if(r.length===0)return 0;let o=0;for(let c of r)try{let u=await import(c),E=u?.default??u;if(typeof E==="function")continue;if(!ue(E)){s.warn(`[events/discover] ${c}: default export doesn't match ListenerModule shape ({ listensTo, handle }), skipping`);continue}for(let h of fe(E.listensTo)){if(!J(h,c))continue;I(h,(b)=>E.handle(b,h)),o++}}catch(u){s.error(`[events/discover] failed to import ${c}: ${u instanceof Error?u.message:String(u)}`)}if(o>0)s.info(`[events/discover] registered ${o} listener${o===1?"":"s"} from ${n}`);return o}function q(e,n){let t=[],s=ae(e);for(let r of s){let o=w(e,r),c=ce(o);if(c.isDirectory())t.push(...q(o,n));else if(c.isFile()&&n.includes(de(r)))t.push(o)}return t}function ue(e){let n=e?.listensTo;return!!e&&typeof e==="object"&&(typeof n==="string"||Array.isArray(n))&&typeof e.handle==="function"}function fe(e){return(Array.isArray(e)?e:[e]).map((t)=>String(t).trim()).filter(Boolean)}var F=Symbol.for("stacks.events.claimedListeners"),W=globalThis[F]??(globalThis[F]=new Set);function J(e,n){let t=`${e}\x00${n}`;if(W.has(t))return!1;return W.add(t),!0}function ye(){W.clear(),delete A()[K],A()[O]=z}var K=Symbol.for("stacks.events.appListenersBoot"),O=Symbol.for("stacks.events.dispatchObserver");function A(){return globalThis}function R(){return A()[K]!==void 0}function ve(e={}){let n=A(),t=n[K];if(t)return t;let s=(async()=>{let r=e.base??M.cwd(),o=await Q(e);if(o===0){let c=await Y(r);if(c>0)(e.log?.warn??((E)=>console.warn(E)))(`[events] app/Events.ts declares ${c} listener${c===1?"":"s"}, but none could be registered in this process, so the events they handle will reach nobody. See the errors above.`)}return o})();return n[K]=s,s}async function Y(e){let n=["ts","js"].map((t)=>w(e,"app",`Events.${t}`)).find((t)=>P(t));if(!n)return 0;try{let t=await import(n),s=t?.default??t;if(!s||typeof s!=="object")return 0;return Object.values(s).flatMap((r)=>Array.isArray(r)?r:[r]).filter((r)=>typeof r==="string"&&r.trim()!=="").length}catch{return 0}}function z(e){let n=A();if(delete n[O],R())return;let t=M.cwd();Y(t).then((s)=>{if(s===0||R())return;console.warn(`[events] "${e}" was dispatched, but this process never registered the application's listeners, so nothing handles it or any later event (app/Events.ts declares ${s}). An entry point that runs application code must call injectGlobalAutoImports() from @stacksjs/server, or bootAppListeners() from @stacksjs/events, before its first dispatch.`)})}if(!A()[O]&&!R())A()[O]=z;async function Q(e={}){let n=e.base??M.cwd(),t={warn:e.log?.warn??((r)=>console.warn(r)),error:e.log?.error??((r)=>console.error(r)),info:e.log?.info??((r)=>console.info(r))},s=0;if(s+=await pe(n,t),s+=await G({...e,dir:e.dir??w(n,"app","Listeners"),log:{...t,info:()=>{}}}),s>0)t.info(`[events] registered ${s} listener${s===1?"":"s"}`);return s}async function pe(e,n){let t=["ts","js"].map((o)=>w(e,"app",`Events.${o}`)).find((o)=>P(o));if(!t)return 0;let s;try{let o=await import(t);s=o?.default??o}catch(o){return n.error(`[events] failed to read ${t}: ${o instanceof Error?o.message:String(o)}`),0}if(!s||typeof s!=="object")return 0;let r=0;for(let[o,c]of Object.entries(s))for(let u of Array.isArray(c)?c:[c]){if(typeof u!=="string"||!u.trim())continue;let E=await he(e,u.trim());if(!E){n.warn(`[events] ${o}: no listener or action called ${u}`);continue}if(!J(o,E.id))continue;let{handle:h,validations:b}=E;I(o,(T)=>(ge(o,u,T,b,n),h(T,o))),r++}return r}function ge(e,n,t,s,r){if(!s||typeof t!=="object"||t===null)return;let o=t,c=[];for(let[u,E]of Object.entries(s)){let h=E?.rule,b=h?.validate;if(typeof b!=="function")continue;try{if(!b.call(h,o[u]).valid)c.push(u)}catch{c.push(u)}}if(c.length)r.warn(`[events] ${e}: payload does not match what ${n} declares (${c.join(", ")})`)}var j=new Map;async function V(e,n,t){let s=`${e}:${n}`,r=j.get(s);if(r)return r;let o=(async()=>{let c=w(e,"storage","framework","auto-imports"),u=w(c,`${n}.ts`);if(!P(u))return null;let h=(await import(u))[t];if(!h)throw Error(`Invalid ${t} registry: ${u}`);return h})();return j.set(s,o),o}async function me(e,n){let t=await V(e,"listeners","listeners"),s=await V(e,"actions","actions");if(!t&&!s)return null;let r=t?.[n]??s?.[`Actions/${n}`];return r?U(r,w(e,"storage/framework/auto-imports")):null}function Ee(){j.clear()}function ke(e){let n=[w(e,"app"),w(e,"storage","framework","defaults","app")];try{let t=Bun.resolveSync("@stacksjs/defaults/package.json",e);n.push(w(t.slice(0,t.lastIndexOf("/")),"app"))}catch{}return n.filter((t)=>P(t))}async function he(e,n){let t=[],s=await me(e,n);if(s)t.push(s);for(let r of ke(e))for(let o of["Listeners","Actions"])for(let c of["ts","js"])t.push(w(r,o,`${n}.${c}`));for(let r of t){if(!P(r))continue;try{let o=await import(r),c=o?.default??o,u=c?.handle;if(typeof u!=="function")continue;return{id:r,handle:u.bind(c),validations:c?.validations}}catch{continue}}return null}var X=Symbol.for("stacks.events.handler.error");function L(e,n){let t=n.slice(),s=e.get(n);if(t.length<2||!s)return t;return t.map((r,o)=>({h:r,i:o,p:s.get(r)??0})).sort((r,o)=>o.p-r.p||r.i-o.i).map((r)=>r.h)}function we(e){return new RegExp(`^${e.replace(/[\\^$+?()[\]{}|]/g,"\\$&").replace(/\*/g,".*")}$`)}function H(e,n,t){console.error(`[Events] ${e} for '${String(n)}':`,t)}function _(e){return!!e&&typeof e.catch==="function"}function B(e){let n=e??new Map,t=new WeakMap,s=new WeakMap,r=(a,y,l)=>{let f=s.get(l),v=f?.key===a?f.re:void 0;if(!v)v=we(a),s.set(l,{key:a,re:v});return v.test(y)};function o(a,y,l){let f=n.get(a);if(f)f.push(y);else n.set(a,f=[y]);if(l?.priority!==void 0&&Number.isFinite(l.priority)){let v=t.get(f);if(!v)t.set(f,v=new Map);v.set(y,l.priority)}}function c(a,y){let l=(...f)=>(u(a,l),y(...f));l[X]=y,o(a,l)}function u(a,y){let l=n.get(a);if(!l)return;if(!y){n.set(a,[]);return}let f=t.get(l);for(let v=l.length-1;v>=0;v--){let p=l[v];if(p===y||p?.[X]===y){if(l.splice(v,1),!l.includes(p))f?.delete(p)}}}function E(a){if(a===void 0)n.clear();else n.delete(a)}function h(a){return n.get(a)?.length??0}function b(a,y){let l=n.get(a),f=n.get("*"),v=l?L(t,l):void 0,p=f?L(t,f):void 0;if(v)for(let k of v)try{let i=k(y);if(_(i))i.catch((d)=>H("Async handler error",a,d))}catch(i){H("Handler error",a,i)}let g=String(a);if(n.forEach((k,i)=>{let d=String(i);if(d===g||d==="*"||!d.includes("*"))return;if(r(d,g,k))for(let m of L(t,k))try{let S=m(a,y);if(_(S))S.catch((re)=>H(`Async pattern handler '${d}' error`,a,re))}catch(S){H(`Pattern handler '${d}' error`,a,S)}}),p)for(let k of p)try{let i=k(a,y);if(_(i))i.catch((d)=>H("Async wildcard handler error",a,d))}catch(i){H("Wildcard handler error",a,i)}}async function T(a,y){let l=[],f=async(g,k)=>{if(!g)return;for(let i of L(t,g))try{let d=k?i(a,y):i(y);l.push(_(d)?await d:d)}catch(d){H("Awaited handler error",a,d),l.push(void 0)}};await f(n.get(a),!1);let v=String(a),p=[];n.forEach((g,k)=>{let i=String(k);if(i===v||i==="*"||!i.includes("*"))return;if(r(i,v,g))p.push(i)});for(let g of p)await f(n.get(g),!0);return await f(n.get("*"),!0),l}async function ne(a,y){let l=[],f=async(g,k)=>{if(!g)return;for(let i of L(t,g))try{let d=k?i(a,y):i(y),m=_(d)?await d:d;l.push({ok:!0,value:m})}catch(d){let m=d instanceof Error?d:Error(String(d));l.push({ok:!1,error:m})}};await f(n.get(a),!1);let v=String(a),p=[];n.forEach((g,k)=>{let i=String(k);if(i===v||i==="*"||!i.includes("*"))return;if(r(i,v,g))p.push(i)});for(let g of p)await f(n.get(g),!0);return await f(n.get("*"),!0),l}function te(a,y){let l=[],f=(i,d)=>{if(!i)return;for(let m of L(t,i))l.push(d?()=>m(a,y):()=>m(y))};f(n.get(a),!1);let v=String(a);n.forEach((i,d)=>{let m=String(d);if(m===v||m==="*"||!m.includes("*"))return;if(r(m,v,i))f(i,!0)}),f(n.get("*"),!0);let p=[],g=(i)=>({ok:!1,error:i instanceof Error?i:Error(String(i))}),k=async(i,d)=>{try{p.push({ok:!0,value:await d})}catch(m){p.push(g(m))}for(let m=i+1;m<l.length;m++)try{let S=l[m]();p.push({ok:!0,value:_(S)?await S:S})}catch(S){p.push(g(S))}return p};for(let i=0;i<l.length;i++)try{let d=l[i]();if(_(d))return k(i,d);p.push({ok:!0,value:d})}catch(d){p.push(g(d))}return p}return{all:n,on:o,once:c,off:u,emit:b,emitAsync:T,emitAndCollect:ne,emitAndCollectInPlace:te,removeAllListeners:E,listenerCount:h}}var Me=B,Je=B;function Se(e,n){let t=(s)=>`${n}:${s}`;return{on(s,r,o){e.on(t(s),r,o)},once(s,r){e.once(t(s),r)},off(s,r){e.off(t(s),r)},emit(s,r){e.emit(t(s),r)},emitAsync(s,r){return e.emitAsync(t(s),r)},emitAndCollect(s,r){return e.emitAndCollect(t(s),r)},listenerCount(s){return e.listenerCount(t(s))}}}function Ce(e){return e}var Z=Symbol.for("stacks.events.emitter"),ee=globalThis,D=ee[Z]??(ee[Z]=B()),x=D,We=D,xe=Symbol.for("stacks.events.dispatchObserver");function C(e){let n=globalThis[xe];if(n)n(String(e))}var be=(e,n)=>(C(e),x.emit(e,n)),je=(e,n)=>(C(e),x.emitAsync(e,n)),Ie=(e,n)=>(C(e),x.emitAndCollect(e,n)),Be=(e,n)=>(C(e),x.emitAndCollectInPlace(e,n)),De=be,{all:Ne,on:I,on:Ue,once:Fe,off:Ve}=x;function Ge(e){return Se(D,e)}export{Ne as all,R as appListenersBooted,ve as bootAppListeners,B as createEmitter,Je as default,Ce as defineEvents,le as defineListener,G as discoverListeners,be as dispatch,Ie as dispatchAndCollect,Be as dispatchAndCollectInPlace,je as dispatchAsync,x as emitter,D as events,I as listen,Me as mitt,Ve as off,Fe as once,Q as registerAppListeners,ye as resetListenerRegistry,Ee as resetNameRegistries,Se as scope,Ge as scopedEvents,De as useEvent,We as useEvents,Ue as useListen};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/events",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.571",
|
|
6
6
|
"description": "Functional event emitting.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -63,6 +63,6 @@
|
|
|
63
63
|
"better-dx": "^0.2.24"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@stacksjs/path": "0.75.
|
|
66
|
+
"@stacksjs/path": "0.75.571"
|
|
67
67
|
}
|
|
68
68
|
}
|