nanoeffects 0.9.0 → 0.11.0

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.
Files changed (68) hide show
  1. package/dist/{action-creator-BrZF6hXc.d.ts → action-creator-9e8jesRa.d.ts} +28 -12
  2. package/dist/actions.d.ts +12 -5
  3. package/dist/actions.js +1 -1
  4. package/dist/bus-Cdw5ajKF.js +1 -0
  5. package/dist/context-CJGW6OWj.js +1 -0
  6. package/dist/{context-plugin--HkNN1ha.d.ts → context-plugin-Xibukkbj.d.ts} +1 -1
  7. package/dist/{delay-CIHnOh_L.js → delay-BnBdGPh7.js} +1 -1
  8. package/dist/dev.d.ts +2 -2
  9. package/dist/effects.d.ts +2 -2
  10. package/dist/effects.js +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -1
  13. package/dist/lifecycle-key-8iX8RUVD.js +1 -0
  14. package/dist/match.d.ts +1 -1
  15. package/dist/match.js +1 -1
  16. package/dist/{module-DqKegXHt.d.ts → module-BlkU3i6B.d.ts} +3 -3
  17. package/dist/notrace/{action-creator-DHAEgwuh.d.ts → action-creator-DQTwMBwP.d.ts} +27 -11
  18. package/dist/notrace/actions.d.ts +12 -5
  19. package/dist/notrace/actions.js +1 -1
  20. package/dist/notrace/bus-G2AdGObY.js +1 -0
  21. package/dist/notrace/{context-plugin-BapQkGwQ.d.ts → context-plugin-DlZCkcX9.d.ts} +1 -1
  22. package/dist/notrace/effects.d.ts +2 -2
  23. package/dist/notrace/index.d.ts +2 -2
  24. package/dist/notrace/index.js +1 -1
  25. package/dist/notrace/lifecycle-key-8iX8RUVD.js +1 -0
  26. package/dist/notrace/origin-key-BbWN3gmN.js +1 -0
  27. package/dist/notrace/origin-key-CxK0SkeG.d.ts +10 -0
  28. package/dist/notrace/{plugin-Cxadz6uU.d.ts → plugin-DOYHG78z.d.ts} +2 -2
  29. package/dist/notrace/policies.d.ts +1 -1
  30. package/dist/notrace/policies.js +1 -1
  31. package/dist/notrace/redux.d.ts +4 -3
  32. package/dist/notrace/redux.js +1 -1
  33. package/dist/observable.d.ts +1 -1
  34. package/dist/oop.d.ts +13 -7
  35. package/dist/oop.js +1 -1
  36. package/dist/origin-key-BbWN3gmN.js +1 -0
  37. package/dist/origin-key-CxK0SkeG.d.ts +10 -0
  38. package/dist/{plugin-DkXCvsmB.d.ts → plugin-DHkamZdH.d.ts} +2 -2
  39. package/dist/policies.d.ts +2 -2
  40. package/dist/policies.js +1 -1
  41. package/dist/react.d.ts +15 -8
  42. package/dist/react.js +1 -1
  43. package/dist/redux.d.ts +4 -3
  44. package/dist/redux.js +1 -1
  45. package/dist/scope.d.ts +2 -2
  46. package/dist/scope.js +1 -1
  47. package/dist/signal.d.ts +1 -1
  48. package/dist/signal.js +1 -1
  49. package/dist/trace.d.ts +30 -1
  50. package/dist/trace.js +1 -1
  51. package/dist/{types-KavbvHix.d.ts → types-ByPzv7tw.d.ts} +2 -2
  52. package/dist/{with-policy-DBV9dh2F.js → with-policy-CW50Edgk.js} +1 -1
  53. package/dist/{with-policy-CqUS-VWW.d.ts → with-policy-DpCvSZRk.d.ts} +1 -1
  54. package/docs/actions.md +10 -3
  55. package/docs/migrating-from-effector.md +1 -1
  56. package/docs/oop.md +2 -0
  57. package/docs/package.md +1 -1
  58. package/docs/plugins.md +16 -1
  59. package/docs/scope.md +1 -1
  60. package/docs/stores.md +5 -1
  61. package/docs/tracing.md +121 -1
  62. package/llms-full.txt +158 -10
  63. package/llms.txt +1 -1
  64. package/package.json +3 -3
  65. package/dist/bus-CkTHUAvq.js +0 -1
  66. package/dist/context-COgyNlrC.js +0 -1
  67. package/dist/notrace/bus-BmCTo8BG.js +0 -1
  68. package/dist/onAbort-BgMfspK4.js +0 -1
@@ -1,4 +1,9 @@
1
1
  //#region src/core/types.d.ts
2
+ /** The third argument of `on` and `once`: read by `tracePlugin`, ignored without it. */
3
+ type ListenerOptions = {
4
+ /** Names the listener in the trace: each call of it is a task with this `type`. */
5
+ traceId?: string;
6
+ };
2
7
  type PayloadOf<P> = undefined extends P ? [payload?: P] : [payload: P];
3
8
  type EventMap = Record<string, unknown>;
4
9
  type Keys<E> = keyof E & string;
@@ -7,12 +12,17 @@ type EffectName = "every" | "latest" | "leading" | "debounce" | "throttle" | "ru
7
12
  type Phase = "start" | "done" | "error" | "cancel" | "drop";
8
13
  type TraceEvent<E = Record<string, unknown>> = { [K in Keys<E>]: {
9
14
  id: number;
10
- parent?: number;
15
+ /** Id of the task that started this one, if it has one. */
16
+ parentId?: number;
11
17
  type: K;
12
18
  effect: EffectName;
13
19
  phase: Phase;
14
20
  payload: E[K];
15
21
  error?: unknown;
22
+ /** The flow the task belongs to: its own, or the nearest ancestor’s; absent without one. */
23
+ traceGroupId?: string;
24
+ /** The id of the root task of the chain; absent on the root itself. */
25
+ rootId?: number;
16
26
  }; }[Keys<E>];
17
27
  /**
18
28
  * The contract of every trace logger: receives each task phase of a bus.
@@ -35,11 +45,17 @@ declare const anyEventType = "*";
35
45
  * `task` makes for each task.
36
46
  */
37
47
  interface Tracer<Ref extends object = object> {
38
- /** Registers a task and returns its handle; `parent` is the handle of its parent task. */
48
+ /**
49
+ * Registers a task and returns its handle; `parent` is the handle of its parent task.
50
+ * `traceGroupId` names the flow the task belongs to; a task without one has its parent’s.
51
+ */
39
52
  task(event: {
40
53
  type: string;
41
54
  payload?: unknown;
55
+ traceGroupId?: string;
42
56
  }, parent?: Ref): Ref;
57
+ /** Gives the task another `type`, for a task that is named after it was made (a named listener). A tracer may leave it out. */
58
+ rename?(ref: Ref, type: string): void;
43
59
  /** Reports a phase of a task. */
44
60
  report(ref: Ref, effect: EffectName, phase: Phase, error?: unknown): void;
45
61
  /** Returns `settle(phase, error?)` that reports only the first phase it gets. */
@@ -117,23 +133,23 @@ type Emit<E extends EventMap> = {
117
133
  (event: EventLike<E>, scope?: EmitScope<E>): void;
118
134
  };
119
135
  type On<E extends EventMap> = {
120
- (event: AnyEvent, fn: Task<EventPayload<E>, E>): UnsubscribeCallback;
121
- <K extends EventKey<E>>(event: K, fn: Task<E[K], E>): UnsubscribeCallback;
136
+ (event: AnyEvent, fn: Task<EventPayload<E>, E>, options?: ListenerOptions): UnsubscribeCallback;
137
+ <K extends EventKey<E>>(event: K, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
122
138
  /** An action creator without a payload type: the payload comes from the event map. */
123
139
  /** The creators and commands that carry a scope come first: the handler sees their fields. */
124
- <K extends EventKey<E>, F>(creator: FreeActionCreator<K, F> & WithScope<F>, fn: Task<E[K], WithFields<E, F>>): UnsubscribeCallback;
125
- <P, F>(creator: ActionCreator<P, string, PayloadOf<P>, F> & WithScope<F>, fn: Task<P, WithFields<E, F>>): UnsubscribeCallback;
126
- <P, F>(command: Command<P, unknown> & WithScope<F>, fn: Task<P, WithFields<E, F>>): UnsubscribeCallback;
127
- <K extends EventKey<E>>(creator: FreeActionCreator<K>, fn: Task<E[K], E>): UnsubscribeCallback;
140
+ <K extends EventKey<E>, F>(creator: FreeActionCreator<K, F> & WithScope<F>, fn: Task<E[K], WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
141
+ <P, F>(creator: ActionCreator<P, string, PayloadOf<P>, F> & WithScope<F>, fn: Task<P, WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
142
+ <P, F>(command: Command<P, unknown> & WithScope<F>, fn: Task<P, WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
143
+ <K extends EventKey<E>>(creator: FreeActionCreator<K>, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
128
144
  /** An action creator: the payload comes from it, so an event map is not needed. */
129
- <P>(creator: ActionCreator<P>, fn: Task<P, E>): UnsubscribeCallback;
145
+ <P>(creator: ActionCreator<P>, fn: Task<P, E>, options?: ListenerOptions): UnsubscribeCallback;
130
146
  /** A command: the payload comes from it too. */
131
- <P>(command: Command<P, unknown>, fn: Task<P, E>): UnsubscribeCallback;
147
+ <P>(command: Command<P, unknown>, fn: Task<P, E>, options?: ListenerOptions): UnsubscribeCallback;
132
148
  <K extends EventKey<E>, Event extends {
133
149
  type: K;
134
150
  }>(event: Event & {
135
151
  type: K;
136
- }, fn: Task<E[K], E>): UnsubscribeCallback;
152
+ }, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
137
153
  };
138
154
  type Off<E extends EventMap> = {
139
155
  /** Removes every listener of every event (including `"*"`). */
@@ -384,4 +400,4 @@ interface ActionCreator<P = void, T extends string = string, A extends unknown[]
384
400
  toString(): T;
385
401
  }
386
402
  //#endregion
387
- export { HandlerRef as A, EventMap as B, Emit as C, EventPayload as D, EventLike as E, commandCall as F, Trace as G, PayloadOf as H, createBus as I, UnsubscribeCallback as J, TraceEvent as K, Tracer as L, On as M, WithContext as N, EventsOf as O, WithType as P, anyEventType as R, ContextOption as S, EventKey as T, Phase as U, Keys as V, Task as W, Command as _, EmitScope as a, Context as b, WithScope as c, Effects as d, Signaled as f, BusOptions as g, Bus as h, WithPayload as i, Off as j, Handler as k, BoundForm as l, AnyEvent as m, Free as n, Scope as o, Action as p, TraceLogger as q, FreeActionCreator as r, WithFields as s, ActionCreator as t, Effect as u, CommandCall as v, Emittable as w, ContextOf as x, Commands as y, EffectName as z };
403
+ export { HandlerRef as A, EventMap as B, Emit as C, EventPayload as D, EventLike as E, commandCall as F, Task as G, ListenerOptions as H, createBus as I, TraceLogger as J, Trace as K, Tracer as L, On as M, WithContext as N, EventsOf as O, WithType as P, anyEventType as R, ContextOption as S, EventKey as T, PayloadOf as U, Keys as V, Phase as W, UnsubscribeCallback as Y, Command as _, EmitScope as a, Context as b, WithScope as c, Effects as d, Signaled as f, BusOptions as g, Bus as h, WithPayload as i, Off as j, Handler as k, BoundForm as l, AnyEvent as m, Free as n, Scope as o, Action as p, TraceEvent as q, FreeActionCreator as r, WithFields as s, ActionCreator as t, Effect as u, CommandCall as v, Emittable as w, ContextOf as x, Commands as y, EffectName as z };
package/dist/actions.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { B as EventMap, H as PayloadOf, T as EventKey, W as Task, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator } from "./action-creator-BrZF6hXc.js";
2
- import { r as Scoped, t as FieldsOfScope } from "./types-KavbvHix.js";
3
- import { a as createModule, c as CarryingCalls, d as commandsPlugin, f as createCommand, l as MadeCommand, n as ModuleApi, o as modulePlugin, r as ModuleContext, s as Binding, t as Module, u as bindHandler } from "./module-DqKegXHt.js";
1
+ import { B as EventMap, G as Task, T as EventKey, U as PayloadOf, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator } from "./action-creator-9e8jesRa.js";
2
+ import { r as Scoped, t as FieldsOfScope } from "./types-ByPzv7tw.js";
3
+ import { a as createModule, c as CarryingCalls, d as commandsPlugin, f as createCommand, l as MadeCommand, n as ModuleApi, o as modulePlugin, r as ModuleContext, s as Binding, t as Module, u as bindHandler } from "./module-BlkU3i6B.js";
4
+ import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
4
5
  //#region src/actions/action.d.ts
5
6
  type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCreator<T, F> & WithScope<F> : ActionCreator<P, T, PayloadOf<P>, F> & WithScope<F>;
6
7
  /**
@@ -52,8 +53,8 @@ type EmitterApi<E extends EventMap> = {
52
53
  */
53
54
  emitter: {
54
55
  /** A creator without a payload type: the payload comes from the event map. */
55
- <K extends EventKey<E>>(creator: FreeActionCreator<K>): (...payload: PayloadOf<E[K]>) => void;
56
- <P, T extends string, A extends unknown[]>(creator: ActionCreator<P, T, A>): (...args: A) => void;
56
+ <K extends EventKey<E>>(creator: FreeActionCreator<K>, options?: OriginOptions): (...payload: PayloadOf<E[K]>) => void;
57
+ <P, T extends string, A extends unknown[]>(creator: ActionCreator<P, T, A>, options?: OriginOptions): (...args: A) => void;
57
58
  };
58
59
  };
59
60
  /**
@@ -63,6 +64,9 @@ type EmitterApi<E extends EventMap> = {
63
64
  * The creator stays a plain `createAction`, so `bus.emit(creator(value))` and
64
65
  * `bus.on(creator, …)` work as before. Returns the same bus.
65
66
  *
67
+ * With a tracer on the bus, `{ traceId }` names this callback in the trace as the
68
+ * root of the chain it starts, and `{ traceGroupId }` names the flow it belongs to.
69
+ *
66
70
  * @example
67
71
  * ```ts
68
72
  * import { createBus } from "nanoeffects";
@@ -74,6 +78,9 @@ type EmitterApi<E extends EventMap> = {
74
78
  *
75
79
  * // nanostores: the store calls it with the new value
76
80
  * $name.listen(emitNameChanged);
81
+ *
82
+ * // the trace says which store began a chain
83
+ * $cart.listen(bus.emitter(cartChanged, { traceId: "cartStore" }));
77
84
  * ```
78
85
  */
79
86
  export declare const emitterPlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus<E> & EmitterApi<E>;
package/dist/actions.js CHANGED
@@ -1 +1 @@
1
- import{n as e,t}from"./onAbort-BgMfspK4.js";import{i as n,n as r,s as i}from"./context-COgyNlrC.js";import{t as a}from"./event-0RIBGYqS.js";import{i as o}from"./bus-CkTHUAvq.js";import{t as s}from"./scope-key-B7l7mUS5.js";const c=(e,t)=>(t&&Object.defineProperty(e,s,{value:t}),e),l=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),u=e=>Reflect.get(e,s);function d(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(c((...t)=>c({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var f=class extends Error{};function p(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new f(`No handler for command "${e}"`);return t(r,i)}return c(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=c(a,n);return s}const m=(e,t)=>[e,t],h=e=>typeof Reflect.get(e??{},`handle`)==`function`,g=(e=[])=>t=>{let n=t.commands;if(h(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},_=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},v=e=>typeof e==`string`?d(e):e,y=(e,t,n,r)=>{let i=u(t);e.emit(Reflect.apply(t,null,[n]),r&&i?l(r,i):r)},b=(t,n,r)=>{let a=t;a[i]=!0;try{y(t,n,t.signal.reason,r)}catch(t){e(`cancel listener`,t)}a[i]=!1},x=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof f)throw e;return Promise.reject(e)}},S=(e,t,n={},r)=>{let i=p(e,t),a=t=>v(n[t]??`${e}/${t}`),s=a(`pending`),l=a(`success`),u=a(`fail`),d=a(`cancel`),f=(e,t,n)=>{let i=n;return x(()=>(y(i,s,t,r),Reflect.apply(e,null,[t,n]))).then(e=>(i.signal.aborted?b(i,d,r):y(i,l,e,r),e),e=>{i.signal.aborted?b(i,d,r):y(i,u,e,r)})},m=e=>(t,n)=>f(e,t,n);function h(n,a){return typeof a==`object`?t?f(i,n,a):Reflect.apply(i,null,[n,a]):c(o(_,e,n),r)}let g=Object.assign(h,{type:e,match:i.match,wrap:m,pending:s,success:l,fail:u,cancel:d}),_=c(g,r);return _},C=e=>typeof e==`object`&&!!e,w=e=>{if(typeof e==`string`)return e;if(C(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},T=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(w(n),w(t[0]))&&(r===void 0||Object.is(r,t[1]))},E=e=>{let t={on:e.on,once:e.once,off:e.off},n=[],r=!0,i=(t,n)=>Reflect.apply(t,e,n),a=(e,t)=>(...a)=>{if(!r)return i(t,a);let o={kind:e,args:a,dropped:!1};return n.push(o),()=>{o.live?o.live():o.dropped=!0}},o=e=>(...t)=>{if(!r)return i(e,t);for(let e of n)e.kind!==`off`&&T(t,e.args)&&(e.dropped=!0);n.push({kind:`off`,args:t,dropped:!1})},s=a(`on`,t.on),c=a(`once`,t.once),l=o(t.off);return e.on=s,e.once=c,e.off=l,{release:()=>{r=!1,e.on===s&&(e.on=t.on),e.once===c&&(e.once=t.once),e.off===l&&(e.off=t.off)},replay:()=>{for(let e of n.splice(0))if(!e.dropped){if(e.kind===`off`)i(t.off,e.args);else{let n=e.kind===`on`?t.on:t.once;e.live=i(n,e.args)}}}}};function D(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const O=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,k=(e,t,n)=>{n&&e.applyScope?.(t,n)},A=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],j=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},M=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},N=(e=[])=>i=>{let a=i.commands??g()(i),o={handle:(...e)=>{if(!a.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(a.handle,null,e)}},s=e=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:c=>{let l=r.get(c);if(l)return l;let u=a.get(c.name);u&&r.get(u)?.();let d=n(e,c.name),f=d.ctx,p=s(f),m=()=>{r.delete(c),a.get(c.name)===c&&a.delete(c.name),f.abort(),d.unlink()};r.set(c,m),a.set(c.name,c);let h=c.scope,g=O(i,h),_=(e,n)=>{let r=Reflect.apply(o.handle,null,A(g,e,n));return t(f,r),r},v=(...e)=>{let t=e.map(e=>typeof e==`function`?_(e,e):p.mount(e));return()=>{for(let e of t)e()}},y=M(f),b=j(f,g);try{k(i,f,h),c.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:b}))}catch(e){throw m(),e}return m},unmount:e=>r.get(e)?.(),isMounted:e=>r.has(e)}},c=E(i),l=i.emit,u,d=(t=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let n=r({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:n,apps:s(n.ctx)},t)for(let t of e)u.apps.mount(t);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,i,e));i.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:D};return i.app=p,p};export{m as bindHandler,g as commandsPlugin,d as createAction,p as createCommand,S as createJob,D as createModule,_ as emitterPlugin,N as modulePlugin};
1
+ import{d as e,i as t,l as n,n as r,u as i}from"./context-CJGW6OWj.js";import{t as a}from"./event-0RIBGYqS.js";import{i as o}from"./bus-Cdw5ajKF.js";import{n as s,t as c}from"./lifecycle-key-8iX8RUVD.js";import{t as l}from"./origin-key-BbWN3gmN.js";import{t as u}from"./scope-key-B7l7mUS5.js";const d=(e,t)=>(t&&Object.defineProperty(e,u,{value:t}),e),f=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),p=e=>Reflect.get(e,u);function m(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(d((...t)=>d({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var h=class extends Error{};function g(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new h(`No handler for command "${e}"`);return t(r,i)}return d(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=d(a,n);return s}const _=(e,t)=>[e,t],v=e=>typeof Reflect.get(e??{},`handle`)==`function`,y=(e=[])=>t=>{let n=t.commands;if(v(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},b=()=>e=>{function t(t,n){return(...r)=>{let i=t(...r),a=Reflect.get(e,l);a?a(i,n):e.emit(i)}}return Object.assign(e,{emitter:t})},x=e=>typeof e==`string`?m(e):e,S=(e,t,n,r)=>{let i=p(t);e.emit(Reflect.apply(t,null,[n]),r&&i?f(r,i):r)},C=(t,r,i,a)=>{let o=t;o[n]=!0;try{S(t,r,i,a)}catch(t){e(`cancel listener`,t)}o[n]=!1},w=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof h)throw e;return Promise.reject(e)}},T=(t,n,r={},a)=>{let l=g(t,n),u=e=>x(r[e]??`${t}/${e}`),f=u(`pending`),p=u(`success`),m=u(`fail`),h=u(`cancel`),_=(t,n,r,o)=>{let s=r,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},g=e=>{l===`started`&&(d(),C(s,h,e,a))},_=()=>{l===`idle`&&(l=`started`,u=i(s,g),S(s,f,n,a))},v=e=>{l===`started`&&(d(),s.signal.aborted?C(s,h,s.signal.reason,a):`error`in e?S(s,m,e.error,a):S(s,p,e.value,a))};return Reflect.set(s,c,{start:_,cancel:g,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),w(()=>(o||_(),Reflect.apply(t,null,[n,r]))).then(e=>(v({value:e}),e),t=>{if(l===`idle`)try{_()}catch(t){e(`pending listener`,t)}v({error:t})})},v=e=>{let t=Reflect.get(e,s)===!0;return(n,r)=>_(e,n,r,t)};function y(e,r){return typeof r==`object`?n?_(l,e,r,!1):Reflect.apply(l,null,[e,r]):d(o(T,t,e),a)}let b=Object.assign(y,{type:t,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:h}),T=d(b,a);return T},E=e=>typeof e==`object`&&!!e,D=e=>{if(typeof e==`string`)return e;if(E(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},O=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(D(n),D(t[0]))&&(r===void 0||Object.is(r,t[1]))},k=e=>{let t={on:e.on,once:e.once,off:e.off},n=[],r=!0,i=(t,n)=>Reflect.apply(t,e,n),a=(e,t)=>(...a)=>{if(!r)return i(t,a);let o={kind:e,args:a,dropped:!1};return n.push(o),()=>{o.live?o.live():o.dropped=!0}},o=e=>(...t)=>{if(!r)return i(e,t);for(let e of n)e.kind!==`off`&&O(t,e.args)&&(e.dropped=!0);n.push({kind:`off`,args:t,dropped:!1})},s=a(`on`,t.on),c=a(`once`,t.once),l=o(t.off);return e.on=s,e.once=c,e.off=l,{release:()=>{r=!1,e.on===s&&(e.on=t.on),e.once===c&&(e.once=t.once),e.off===l&&(e.off=t.off)},replay:()=>{for(let e of n.splice(0))if(!e.dropped){if(e.kind===`off`)i(t.off,e.args);else{let n=e.kind===`on`?t.on:t.once;e.live=i(n,e.args)}}}}};function A(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const j=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,M=(e,t,n)=>{n&&e.applyScope?.(t,n)},N=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],P=(e,t)=>{let n=e.on;return(...e)=>{let[r,i,...a]=e;return Reflect.apply(n,null,[r,t(i),...a])}},F=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},I=(e=[])=>n=>{let a=n.commands??y()(n),o={handle:(...e)=>{if(!a.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(a.handle,null,e)}},s=e=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:c=>{let l=r.get(c);if(l)return l;let u=a.get(c.name);u&&r.get(u)?.();let d=t(e,c.name),f=d.ctx,p=s(f),m=()=>{r.delete(c),a.get(c.name)===c&&a.delete(c.name),f.abort(),d.unlink()};r.set(c,m),a.set(c.name,c);let h=c.scope,g=j(n,h),_=(e,t)=>{let n=Reflect.apply(o.handle,null,N(g,e,t));return i(f,n),n},v=(...e)=>{let t=e.map(e=>typeof e==`function`?_(e,e):p.mount(e));return()=>{for(let e of t)e()}},y=F(f),b=P(f,g);try{M(n,f,h),c.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:b}))}catch(e){throw m(),e}return m},unmount:e=>r.get(e)?.(),isMounted:e=>r.has(e)}},c=k(n),l=n.emit,u,d=(t=!0)=>{if(u)return u;c.release(),n.emit===f&&(n.emit=l);let i=r({event:{type:`app`,payload:void 0},bus:n,tracer:n.trace});if(u={root:i,apps:s(i.ctx)},t)for(let t of e)u.apps.mount(t);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,n,e));n.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:A};return n.app=p,p};export{_ as bindHandler,y as commandsPlugin,m as createAction,g as createCommand,T as createJob,A as createModule,b as emitterPlugin,I as modulePlugin};
@@ -0,0 +1 @@
1
+ import{d as e,n as t,o as n,r}from"./context-CJGW6OWj.js";import{n as i}from"./promise-CSLpzwSR.js";import{n as a,t as o}from"./event-0RIBGYqS.js";const s=`*`,c=()=>{let e={},t={},n=n=>(t[n]=void 0,e[n]??=/* @__PURE__ */ new Map,e[n]),r=(e,t)=>{let r=n(e),i=r.get(t)??0;i>1?r.set(t,i-1):t?r.delete(t):r.clear()};return{get:n=>(t[n]??=[...e[n]??[]].flatMap(([e,t])=>Array(t).fill(e)),t[n]),add:(e,t)=>{let i=n(e);return i.set(t,(i.get(t)??0)+1),()=>{r(e,t)}},remove:r,clear:()=>{e={},t={}}}},l=Symbol(),u=(e,t,n)=>{let r={type:t,payload:n};return Object.defineProperty(r,l,{value:e,enumerable:!1}),r},d=e=>{if(typeof e==`function`)return e;if(typeof e==`object`&&e&&l in e)return e[l]},f=(e,t,n)=>Reflect.apply(e,null,[t,n]),p=(s={})=>{let l=c(),u=(e,t)=>l.add(o(e),t),p=(e,t)=>{let n=u(e,(e,r)=>(n(),t(e,r)));return n},m=(e,t)=>{e===void 0?l.clear():l.remove(o(e),t)},h=(n,a)=>{let{onError:o}=s,{ctx:c}=t({event:a,bus:g,tracer:s.trace,parent:{ref:r()}});if(!o){n(a.payload,c);return}let l=t=>{try{o(t,a)}catch(t){e(`onError failed`,t)}};try{let e=n(a.payload,c);i(e)&&Promise.resolve(e).catch(l)}catch(e){l(e)}},g={on:u,once:p,off:m,emit:(e,i)=>{let c=o(e),u=a(e,i),p={type:c,payload:u},m=d(e),_=[...l.get(c),...l.get(`*`)];_.length===0&&!m&&s.onUnhandled?.(p);for(let e of _)h(e,p);if(m){let{ctx:e}=t({event:p,bus:g,tracer:s.trace,parent:{ref:r()}}),i=g.commands?.run??f;return n(e,`command`,()=>i(m,u,e))}},use:e=>e(g),ctx:s.context,trace:s.trace};return g};export{s as a,u as i,d as n,p as r,l as t};
@@ -0,0 +1 @@
1
+ const e=(e,t)=>{globalThis.console?.error(`[nanoeffects] ${e}`,t)},t=()=>{},n=/* @__PURE__ */ new WeakMap,r=t=>{let r=n.get(t);if(r)return r;let i=/* @__PURE__ */ new Set;return n.set(t,i),t.addEventListener(`abort`,()=>{let n=[...i];i.clear();for(let{fn:r}of n)try{r(t.reason)}catch(t){e(`abort callback failed`,t)}},{once:!0}),i},i=(e,n)=>{let{signal:i}=e;if(!i)return t;if(i.aborted)return n(i.reason),t;let a=r(i),o={fn:n};return a.add(o),()=>{a.delete(o)}},a=e=>{let t=new AbortController;return{controller:t,unlink:i({signal:e},e=>t.abort(e))}},o=Symbol(),s=typeof __NANOEFFECTS_TRACE__>`u`||__NANOEFFECTS_TRACE__,c=/* @__PURE__ */ new WeakMap;let l;const u=()=>l,d=()=>{},f=(e,t,n,r)=>{let i=s&&c.get(e);i&&i.tracer.report(i.ref,t,n,r)},p=(e,t)=>{let n=s&&c.get(e);n&&n.tracer.rename?.(n.ref,t)},m=(e,t)=>{let n=s&&c.get(e);return n?n.tracer.settler(n.ref,t):d},h=(e,t,n)=>{let r=s&&c.get(e);return r?r.tracer.run(r.ref,t,e.signal,n):n()},g=(e,t)=>typeof e==`function`?e(t):e,_=({event:e,bus:t,tracer:n,parent:r})=>{let{controller:u,unlink:f}=a(r?.signal),{signal:p}=u,m=s&&n?{tracer:n,ref:n.task(e,r?.ref)}:void 0,v=(...e)=>{if(p.aborted&&!x[o])return;let n=t?.emit,r=l;l=m?.ref;try{return n?.(...e)}finally{l=r}},y=(...e)=>{if(p.aborted)return d;let n=t?.on,r=n?.(...e);if(!r)return d;let a=i({signal:p},r);return()=>{a(),r()}},b=async(r,...i)=>{p.throwIfAborted();let a=i[0],o=_({event:{type:e.type,payload:a},bus:t,tracer:n,parent:{signal:p,ref:m?.ref}});try{return await h(o.ctx,`run`,()=>r(a,o.ctx))}finally{o.unlink()}},x={...g(t?.ctx,e),type:e.type,payload:e.payload,signal:p,abort:e=>u.abort(e),emit:v,on:y,off:t?.off??d,run:b};return t?.addContext?.(x),m&&c.set(x,m),{ctx:x,unlink:f}},v=(e,t)=>{let n=s?c.get(e):void 0;return _({event:{type:t??e.type??``,payload:e.payload},bus:{emit:e.emit,on:e.on,off:e.off,ctx:e},tracer:n?.tracer,parent:{signal:e.signal,ref:n?.ref}})};export{p as a,s as c,e as d,v as i,o as l,_ as n,h as o,u as r,f as s,m as t,i as u};
@@ -1,4 +1,4 @@
1
- import { B as EventMap, N as WithContext, h as Bus, x as ContextOf } from "./action-creator-BrZF6hXc.js";
1
+ import { B as EventMap, N as WithContext, h as Bus, x as ContextOf } from "./action-creator-9e8jesRa.js";
2
2
  //#region src/core/context-plugin.d.ts
3
3
  /** What a function that makes the fields of a task gets to see. */
4
4
  type ContextTask = {
@@ -1 +1 @@
1
- import{t as e}from"./onAbort-BgMfspK4.js";const t=2**31-1,n=(e,n)=>{let r={id:null};if(n===1/0)return r;let i=n=>{r.id=setTimeout(()=>{n>t?i(n-t):(r.id=null,e())},Math.min(n,t))};return i(n>0?n:0),r},r=e=>{e.id!==null&&(clearTimeout(e.id),e.id=null)},i=(t,i)=>new Promise((a,o)=>{let s=n(()=>{c(),a()},typeof i==`function`?i():i),c=e(t,e=>{r(s),o(e)})});export{r as n,n as r,i as t};
1
+ import{u as e}from"./context-CJGW6OWj.js";const t=2**31-1,n=(e,n)=>{let r={id:null};if(n===1/0)return r;let i=n=>{r.id=setTimeout(()=>{n>t?i(n-t):(r.id=null,e())},Math.min(n,t))};return i(n>0?n:0),r},r=e=>{e.id!==null&&(clearTimeout(e.id),e.id=null)},i=(t,i)=>new Promise((a,o)=>{let s=n(()=>{c(),a()},typeof i==`function`?i():i),c=e(t,e=>{r(s),o(e)})});export{r as n,n as r,i as t};
package/dist/dev.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as EventMap, J as UnsubscribeCallback, h as Bus } from "./action-creator-BrZF6hXc.js";
2
- import { i as WithApp, n as ModuleApi } from "./module-DqKegXHt.js";
1
+ import { B as EventMap, Y as UnsubscribeCallback, h as Bus } from "./action-creator-9e8jesRa.js";
2
+ import { i as WithApp, n as ModuleApi } from "./module-BlkU3i6B.js";
3
3
  //#region src/dev/types.d.ts
4
4
  /** The parts of what `modulePlugin` returned that the diagnostics report. */
5
5
  type ReportedApp<E extends EventMap> = Pick<ModuleApi<E>, "mount" | "unmount" | "isMounted">;
package/dist/effects.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as EventMap, W as Task, u as Effect } from "./action-creator-BrZF6hXc.js";
2
- import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-DkXCvsmB.js";
1
+ import { B as EventMap, G as Task, u as Effect } from "./action-creator-9e8jesRa.js";
2
+ import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-DHkamZdH.js";
3
3
  //#region src/utils/onAbort.d.ts
4
4
  export declare const onAbort: (ctx: {
5
5
  signal?: AbortSignal | undefined;
package/dist/effects.js CHANGED
@@ -1 +1 @@
1
- import{n as e,t}from"./onAbort-BgMfspK4.js";import{a as n,i as r,o as i,t as a}from"./context-COgyNlrC.js";import{n as o,r as s,t as c}from"./delay-CIHnOh_L.js";const l=(e,n)=>n?.signal?new Promise((r,i)=>{let a=t(n,i);e.then(r,i).finally(a)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=r(n??{}),d=a(c,e);i(c,e,`start`),t(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,t)=>{let{ctx:i,unlink:a}=r(e);try{return await n(i,`run`,()=>t(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,t)=>{let i=Object.entries(t).map(([t,i])=>{let a=r(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:t,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(n,r)=>n.signal.aborted?Promise.reject(n.signal.reason):new Promise((i,a)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=t(n,e=>{s=`cancelled`;for(let e of o.reverse())c(e);a(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{r({resolve:u(i),reject:u(a),onCancel:d})}catch(e){u(a)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)t in e||Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,n,r,i)=>{if(e.signal.aborted)return Promise.resolve();let a=i.leading??!1,c=i.waitForFn??!1;return u(`poll`,e,e=>new Promise((i,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof r==`function`?r(e):r)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let t=u++;d(e,e=>n(e,t)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};t(e,p),a?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,n,r={}){return u(`take`,e,i=>new Promise((a,o)=>{let{predicate:s}=r,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)};c.push(e.on(n,e=>{(!s||s(e))&&(l(),a(e))})),c.push(t(i,u)),c.push(t({signal:r.signal},u))}))}const M=(e,t,i)=>u(`timeout`,e,async e=>{let a=r(e),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${t}ms`,`TimeoutError`)),t);try{return await l(Promise.resolve(n(a.ctx,`run`,()=>i(a.ctx))),a.ctx)}finally{o(c),a.unlink()}});function N(e,t,n){if(typeof e==`number`){let n=t;return(t,r)=>M(r,e,p(n,t))}let r=t,i=n;return M(e,r,e=>i({signal:e.signal,abort:e.abort}))}export{_ as all,y as allSettled,x as backoff,S as cancellable,c as delay,C as effectsPlugin,t as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
1
+ import{d as e,i as t,o as n,s as r,t as i,u as a}from"./context-CJGW6OWj.js";import{n as o,r as s,t as c}from"./delay-BnBdGPh7.js";const l=(e,t)=>t?.signal?new Promise((n,r)=>{let i=a(t,r);e.then(n,r).finally(i)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=t(n??{}),d=i(c,e);r(c,e,`start`),a(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,r)=>{let{ctx:i,unlink:a}=t(e);try{return await n(i,`run`,()=>r(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,r)=>{let i=Object.entries(r).map(([r,i])=>{let a=t(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:r,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(t,n)=>t.signal.aborted?Promise.reject(t.signal.reason):new Promise((r,i)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=a(t,e=>{s=`cancelled`;for(let e of o.reverse())c(e);i(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{n({resolve:u(r),reject:u(i),onCancel:d})}catch(e){u(i)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)t in e||Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,t,n,r)=>{if(e.signal.aborted)return Promise.resolve();let i=r.leading??!1,c=r.waitForFn??!1;return u(`poll`,e,e=>new Promise((r,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof n==`function`?n(e):n)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let n=u++;d(e,e=>t(e,n)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};a(e,p),i?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,t,n={}){return u(`take`,e,r=>new Promise((i,o)=>{let{predicate:s}=n,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)};c.push(e.on(t,e=>{(!s||s(e))&&(l(),i(e))})),c.push(a(r,u)),c.push(a({signal:n.signal},u))}))}const M=(e,r,i)=>u(`timeout`,e,async e=>{let a=t(e),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${r}ms`,`TimeoutError`)),r);try{return await l(Promise.resolve(n(a.ctx,`run`,()=>i(a.ctx))),a.ctx)}finally{o(c),a.unlink()}});function N(e,t,n){if(typeof e==`number`){let n=t;return(t,r)=>M(r,e,p(n,t))}let r=t,i=n;return M(e,r,e=>i({signal:e.signal,abort:e.abort}))}export{_ as all,y as allSettled,x as backoff,S as cancellable,c as delay,C as effectsPlugin,a as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { A as HandlerRef, B as EventMap, C as Emit, D as EventPayload, E as EventLike, F as commandCall, G as Trace, H as PayloadOf, I as createBus, J as UnsubscribeCallback, K as TraceEvent, L as Tracer, M as On, N as WithContext, O as EventsOf, P as WithType, R as anyEventType, S as ContextOption, T as EventKey, U as Phase, V as Keys, W as Task, _ as Command, b as Context, g as BusOptions, h as Bus, i as WithPayload, j as Off, k as Handler, m as AnyEvent, p as Action, q as TraceLogger, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as EffectName } from "./action-creator-BrZF6hXc.js";
2
- import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin--HkNN1ha.js";
1
+ import { A as HandlerRef, B as EventMap, C as Emit, D as EventPayload, E as EventLike, F as commandCall, G as Task, I as createBus, J as TraceLogger, K as Trace, L as Tracer, M as On, N as WithContext, O as EventsOf, P as WithType, R as anyEventType, S as ContextOption, T as EventKey, U as PayloadOf, V as Keys, W as Phase, Y as UnsubscribeCallback, _ as Command, b as Context, g as BusOptions, h as Bus, i as WithPayload, j as Off, k as Handler, m as AnyEvent, p as Action, q as TraceEvent, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as EffectName } from "./action-creator-9e8jesRa.js";
2
+ import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin-Xibukkbj.js";
3
3
  export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type ContextTask, type EffectName, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type NoClash, type Off, type On, type PayloadOf, type Phase, type Task, type Trace, type TraceEvent, type TraceLogger, type Tracer, type UnsubscribeCallback, type WithContext, type WithPayload, type WithType, anyEventType, commandCall, contextPlugin, createBus };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{a as e,r as t,t as n}from"./bus-CkTHUAvq.js";const r=e=>typeof e==`function`,i=e=>({type:Reflect.get(e,`type`),payload:Reflect.get(e,`payload`)}),a=e=>t=>{let n=t.addContext;return t.addContext=t=>{n?.(t);let a=r(e)?e(i(t)):e;for(let[e,n]of Object.entries(a))e in t||Reflect.set(t,e,n)},t};export{e as anyEventType,n as commandCall,a as contextPlugin,t as createBus};
1
+ import{a as e,r as t,t as n}from"./bus-Cdw5ajKF.js";const r=e=>typeof e==`function`,i=e=>({type:Reflect.get(e,`type`),payload:Reflect.get(e,`payload`)}),a=e=>t=>{let n=t.addContext;return t.addContext=t=>{n?.(t);let a=r(e)?e(i(t)):e;for(let[e,n]of Object.entries(a))e in t||Reflect.set(t,e,n)},t};export{e as anyEventType,n as commandCall,a as contextPlugin,t as createBus};
@@ -0,0 +1 @@
1
+ const e=Symbol(),t=Symbol();export{t as n,e as t};
package/dist/match.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { B as EventMap, D as EventPayload, J as UnsubscribeCallback, W as Task, h as Bus, p as Action } from "./action-creator-BrZF6hXc.js";
1
+ import { B as EventMap, D as EventPayload, G as Task, Y as UnsubscribeCallback, h as Bus, p as Action } from "./action-creator-9e8jesRa.js";
2
2
  //#region src/match/plugin.d.ts
3
3
  /** A function over the action of an event: `{ type, payload }`. */
4
4
  type Predicate<E extends EventMap> = (action: Action<E>) => boolean;
package/dist/match.js CHANGED
@@ -1 +1 @@
1
- const e=e=>typeof e==`function`&&typeof Reflect.get(e,`type`)!=`string`,t=e=>e,n=()=>n=>{let{on:r,once:i,off:a}=n,o=/* @__PURE__ */ new Map,s=(e,t,i)=>{let a=Reflect.apply(r,n,[`*`,(n,r)=>{if(e({type:Reflect.get(r,`type`),payload:n}))return i&&c(),t(n,r)}]),s={handler:t,remove:c};o.set(e,[...o.get(e)??[],s]);function c(){a();let t=(o.get(e)??[]).filter(e=>e!==s);t.length>0?o.set(e,t):o.delete(e)}return c},c=(t,a,o)=>e(t)?s(t,a,o):Reflect.apply(o?i:r,n,[t,a]);return Object.assign(n,t({on:(e,t)=>c(e,t,!1),once:(e,t)=>c(e,t,!0),off:(t,r)=>{if(r===void 0&&(t===void 0||t===`*`)&&o.clear(),!e(t))return Reflect.apply(a,n,[t,r]);let i=o.get(t)??[],s=i.filter(e=>e.handler===r),c=r===void 0?i:s.slice(0,1);for(let e of c)e.remove()}}))};export{n as matchPlugin};
1
+ const e=e=>typeof e==`function`&&typeof Reflect.get(e,`type`)!=`string`,t=e=>e,n=()=>n=>{let{on:r,once:i,off:a}=n,o=/* @__PURE__ */ new Map,s=(e,t,i)=>{let a=Reflect.apply(r,n,[`*`,(n,r)=>{if(e({type:Reflect.get(r,`type`),payload:n}))return i&&c(),t(n,r)}]),s={handler:t,remove:c};o.set(e,[...o.get(e)??[],s]);function c(){a();let t=(o.get(e)??[]).filter(e=>e!==s);t.length>0?o.set(e,t):o.delete(e)}return c},c=(t,a,o,c)=>e(t)?s(t,a,o):Reflect.apply(o?i:r,n,[t,a,c]);return Object.assign(n,t({on:(e,t,n)=>c(e,t,!1,n),once:(e,t,n)=>c(e,t,!0,n),off:(t,r)=>{if(r===void 0&&(t===void 0||t===`*`)&&o.clear(),!e(t))return Reflect.apply(a,n,[t,r]);let i=o.get(t)??[],s=i.filter(e=>e.handler===r),c=r===void 0?i:s.slice(0,1);for(let e of c)e.remove()}}))};export{n as matchPlugin};
@@ -1,5 +1,5 @@
1
- import { B as EventMap, H as PayloadOf, J as UnsubscribeCallback, W as Task, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, v as CommandCall, y as Commands } from "./action-creator-BrZF6hXc.js";
2
- import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-KavbvHix.js";
1
+ import { B as EventMap, G as Task, U as PayloadOf, Y as UnsubscribeCallback, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, v as CommandCall, y as Commands } from "./action-creator-9e8jesRa.js";
2
+ import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-ByPzv7tw.js";
3
3
  //#region src/actions/command.d.ts
4
4
  /** What `createCommand` returns: a command that also tells whether an action is of its type. */
5
5
  type MadeCommand<P, R, T extends string> = Command<P, R> & {
@@ -53,7 +53,7 @@ type ScopedCommand<P, R, T extends string, F> = ((payload: P) => CommandCall<P,
53
53
  * ```
54
54
  */
55
55
  declare function createCommand<P = void, R = void, T extends string = string, E extends EventMap = EventMap>(type: T, handler?: Task<P, E, R>): MadeCommand<P, R, T>;
56
- declare function createCommand<P, R, T extends string, S extends Scope = Record<never, never>>(type: T, handler: Task<P, Scoped<EventMap, S>, R>, scope: S): ScopedCommand<P, R, T, FieldsOfScope<EventMap, S>>;
56
+ declare function createCommand<P, R, T extends string, S extends Scope = Record<never, never>>(type: T, handler: Task<P, Scoped<EventMap, S>, R> | undefined, scope: S): ScopedCommand<P, R, T, FieldsOfScope<EventMap, S>>;
57
57
  /** A command (or its type) with the handler to bind to it; `bindHandler` makes one. */
58
58
  type Binding<E extends EventMap = EventMap> = readonly [Command<never, unknown> | string, Task<never, E, unknown>];
59
59
  /**
@@ -1,4 +1,9 @@
1
1
  //#region src/core/types.d.ts
2
+ /** The third argument of `on` and `once`: read by `tracePlugin`, ignored without it. */
3
+ type ListenerOptions = {
4
+ /** Names the listener in the trace: each call of it is a task with this `type`. */
5
+ traceId?: string;
6
+ };
2
7
  type PayloadOf<P> = undefined extends P ? [payload?: P] : [payload: P];
3
8
  type EventMap = Record<string, unknown>;
4
9
  type Keys<E> = keyof E & string;
@@ -7,12 +12,17 @@ type EffectName = "every" | "latest" | "leading" | "debounce" | "throttle" | "ru
7
12
  type Phase = "start" | "done" | "error" | "cancel" | "drop";
8
13
  type TraceEvent<E = Record<string, unknown>> = { [K in Keys<E>]: {
9
14
  id: number;
10
- parent?: number;
15
+ /** Id of the task that started this one, if it has one. */
16
+ parentId?: number;
11
17
  type: K;
12
18
  effect: EffectName;
13
19
  phase: Phase;
14
20
  payload: E[K];
15
21
  error?: unknown;
22
+ /** The flow the task belongs to: its own, or the nearest ancestor’s; absent without one. */
23
+ traceGroupId?: string;
24
+ /** The id of the root task of the chain; absent on the root itself. */
25
+ rootId?: number;
16
26
  }; }[Keys<E>];
17
27
  /**
18
28
  * The contract of every trace logger: receives each task phase of a bus.
@@ -35,11 +45,17 @@ declare const anyEventType = "*";
35
45
  * `task` makes for each task.
36
46
  */
37
47
  interface Tracer<Ref extends object = object> {
38
- /** Registers a task and returns its handle; `parent` is the handle of its parent task. */
48
+ /**
49
+ * Registers a task and returns its handle; `parent` is the handle of its parent task.
50
+ * `traceGroupId` names the flow the task belongs to; a task without one has its parent’s.
51
+ */
39
52
  task(event: {
40
53
  type: string;
41
54
  payload?: unknown;
55
+ traceGroupId?: string;
42
56
  }, parent?: Ref): Ref;
57
+ /** Gives the task another `type`, for a task that is named after it was made (a named listener). A tracer may leave it out. */
58
+ rename?(ref: Ref, type: string): void;
43
59
  /** Reports a phase of a task. */
44
60
  report(ref: Ref, effect: EffectName, phase: Phase, error?: unknown): void;
45
61
  /** Returns `settle(phase, error?)` that reports only the first phase it gets. */
@@ -117,23 +133,23 @@ type Emit<E extends EventMap> = {
117
133
  (event: EventLike<E>, scope?: EmitScope<E>): void;
118
134
  };
119
135
  type On<E extends EventMap> = {
120
- (event: AnyEvent, fn: Task<EventPayload<E>, E>): UnsubscribeCallback;
121
- <K extends EventKey<E>>(event: K, fn: Task<E[K], E>): UnsubscribeCallback;
136
+ (event: AnyEvent, fn: Task<EventPayload<E>, E>, options?: ListenerOptions): UnsubscribeCallback;
137
+ <K extends EventKey<E>>(event: K, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
122
138
  /** An action creator without a payload type: the payload comes from the event map. */
123
139
  /** The creators and commands that carry a scope come first: the handler sees their fields. */
124
- <K extends EventKey<E>, F>(creator: FreeActionCreator<K, F> & WithScope<F>, fn: Task<E[K], WithFields<E, F>>): UnsubscribeCallback;
125
- <P, F>(creator: ActionCreator<P, string, PayloadOf<P>, F> & WithScope<F>, fn: Task<P, WithFields<E, F>>): UnsubscribeCallback;
126
- <P, F>(command: Command<P, unknown> & WithScope<F>, fn: Task<P, WithFields<E, F>>): UnsubscribeCallback;
127
- <K extends EventKey<E>>(creator: FreeActionCreator<K>, fn: Task<E[K], E>): UnsubscribeCallback;
140
+ <K extends EventKey<E>, F>(creator: FreeActionCreator<K, F> & WithScope<F>, fn: Task<E[K], WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
141
+ <P, F>(creator: ActionCreator<P, string, PayloadOf<P>, F> & WithScope<F>, fn: Task<P, WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
142
+ <P, F>(command: Command<P, unknown> & WithScope<F>, fn: Task<P, WithFields<E, F>>, options?: ListenerOptions): UnsubscribeCallback;
143
+ <K extends EventKey<E>>(creator: FreeActionCreator<K>, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
128
144
  /** An action creator: the payload comes from it, so an event map is not needed. */
129
- <P>(creator: ActionCreator<P>, fn: Task<P, E>): UnsubscribeCallback;
145
+ <P>(creator: ActionCreator<P>, fn: Task<P, E>, options?: ListenerOptions): UnsubscribeCallback;
130
146
  /** A command: the payload comes from it too. */
131
- <P>(command: Command<P, unknown>, fn: Task<P, E>): UnsubscribeCallback;
147
+ <P>(command: Command<P, unknown>, fn: Task<P, E>, options?: ListenerOptions): UnsubscribeCallback;
132
148
  <K extends EventKey<E>, Event extends {
133
149
  type: K;
134
150
  }>(event: Event & {
135
151
  type: K;
136
- }, fn: Task<E[K], E>): UnsubscribeCallback;
152
+ }, fn: Task<E[K], E>, options?: ListenerOptions): UnsubscribeCallback;
137
153
  };
138
154
  type Off<E extends EventMap> = {
139
155
  /** Removes every listener of every event (including `"*"`). */
@@ -1,5 +1,6 @@
1
- import { B as PayloadOf, C as EventKey, H as Task, K as UnsubscribeCallback, R as EventMap, _ as Commands, a as Scope, g as CommandCall, h as Command, n as Free, o as WithFields, p as Bus, r as FreeActionCreator, s as WithScope, t as ActionCreator, u as Effects, v as Context } from "./action-creator-DHAEgwuh.js";
2
- import { t as BoundOf } from "./plugin-Cxadz6uU.js";
1
+ import { B as PayloadOf, C as EventKey, H as Task, K as UnsubscribeCallback, R as EventMap, _ as Commands, a as Scope, g as CommandCall, h as Command, n as Free, o as WithFields, p as Bus, r as FreeActionCreator, s as WithScope, t as ActionCreator, u as Effects, v as Context } from "./action-creator-DQTwMBwP.js";
2
+ import { t as BoundOf } from "./plugin-DOYHG78z.js";
3
+ import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
3
4
  //#region src/scope/types.d.ts
4
5
  /**
5
6
  * The slots `scopePlugin` fills for `modulePlugin`. They are not on `Bus`: a bus
@@ -117,7 +118,7 @@ type ScopedCommand<P, R, T extends string, F> = ((payload: P) => CommandCall<P,
117
118
  * ```
118
119
  */
119
120
  export declare function createCommand<P = void, R = void, T extends string = string, E extends EventMap = EventMap>(type: T, handler?: Task<P, E, R>): MadeCommand<P, R, T>;
120
- export declare function createCommand<P, R, T extends string, S extends Scope = Record<never, never>>(type: T, handler: Task<P, Scoped<EventMap, S>, R>, scope: S): ScopedCommand<P, R, T, FieldsOfScope<EventMap, S>>;
121
+ export declare function createCommand<P, R, T extends string, S extends Scope = Record<never, never>>(type: T, handler: Task<P, Scoped<EventMap, S>, R> | undefined, scope: S): ScopedCommand<P, R, T, FieldsOfScope<EventMap, S>>;
121
122
  /** A command (or its type) with the handler to bind to it; `bindHandler` makes one. */
122
123
  type Binding<E extends EventMap = EventMap> = readonly [Command<never, unknown> | string, Task<never, E, unknown>];
123
124
  /**
@@ -169,8 +170,8 @@ type EmitterApi<E extends EventMap> = {
169
170
  */
170
171
  emitter: {
171
172
  /** A creator without a payload type: the payload comes from the event map. */
172
- <K extends EventKey<E>>(creator: FreeActionCreator<K>): (...payload: PayloadOf<E[K]>) => void;
173
- <P, T extends string, A extends unknown[]>(creator: ActionCreator<P, T, A>): (...args: A) => void;
173
+ <K extends EventKey<E>>(creator: FreeActionCreator<K>, options?: OriginOptions): (...payload: PayloadOf<E[K]>) => void;
174
+ <P, T extends string, A extends unknown[]>(creator: ActionCreator<P, T, A>, options?: OriginOptions): (...args: A) => void;
174
175
  };
175
176
  };
176
177
  /**
@@ -180,6 +181,9 @@ type EmitterApi<E extends EventMap> = {
180
181
  * The creator stays a plain `createAction`, so `bus.emit(creator(value))` and
181
182
  * `bus.on(creator, …)` work as before. Returns the same bus.
182
183
  *
184
+ * With a tracer on the bus, `{ traceId }` names this callback in the trace as the
185
+ * root of the chain it starts, and `{ traceGroupId }` names the flow it belongs to.
186
+ *
183
187
  * @example
184
188
  * ```ts
185
189
  * import { createBus } from "nanoeffects";
@@ -191,6 +195,9 @@ type EmitterApi<E extends EventMap> = {
191
195
  *
192
196
  * // nanostores: the store calls it with the new value
193
197
  * $name.listen(emitNameChanged);
198
+ *
199
+ * // the trace says which store began a chain
200
+ * $cart.listen(bus.emitter(cartChanged, { traceId: "cartStore" }));
194
201
  * ```
195
202
  */
196
203
  export declare const emitterPlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus<E> & EmitterApi<E>;
@@ -1 +1 @@
1
- import{c as e,i as t,l as n,n as r,s as i}from"./context-yYU3_9lq.js";import{a,r as o}from"./bus-BmCTo8BG.js";const s=Symbol.for(`nanoeffects.scope`),c=(e,t)=>(t&&Object.defineProperty(e,s,{value:t}),e),l=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),u=e=>Reflect.get(e,s);function d(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(c((...t)=>c({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var f=class extends Error{};function p(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new f(`No handler for command "${e}"`);return t(r,i)}return c(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=c(a,n);return s}const m=(e,t)=>[e,t],h=e=>typeof Reflect.get(e??{},`handle`)==`function`,g=(e=[])=>t=>{let n=t.commands;if(h(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},_=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},v=e=>typeof e==`string`?d(e):e,y=(e,t,n,r)=>{let i=u(t);e.emit(Reflect.apply(t,null,[n]),r&&i?l(r,i):r)},b=(e,t,r)=>{let a=e;a[i]=!0;try{y(e,t,e.signal.reason,r)}catch(e){n(`cancel listener`,e)}a[i]=!1},x=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof f)throw e;return Promise.reject(e)}},S=(e,t,n={},r)=>{let i=p(e,t),a=t=>v(n[t]??`${e}/${t}`),s=a(`pending`),l=a(`success`),u=a(`fail`),d=a(`cancel`),f=(e,t,n)=>{let i=n;return x(()=>(y(i,s,t,r),Reflect.apply(e,null,[t,n]))).then(e=>(i.signal.aborted?b(i,d,r):y(i,l,e,r),e),e=>{i.signal.aborted?b(i,d,r):y(i,u,e,r)})},m=e=>(t,n)=>f(e,t,n);function h(n,a){return typeof a==`object`?t?f(i,n,a):Reflect.apply(i,null,[n,a]):c(o(_,e,n),r)}let g=Object.assign(h,{type:e,match:i.match,wrap:m,pending:s,success:l,fail:u,cancel:d}),_=c(g,r);return _},C=e=>typeof e==`object`&&!!e,w=e=>{if(typeof e==`string`)return e;if(C(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},T=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(w(n),w(t[0]))&&(r===void 0||Object.is(r,t[1]))},E=e=>{let t={on:e.on,once:e.once,off:e.off},n=[],r=!0,i=(t,n)=>Reflect.apply(t,e,n),a=(e,t)=>(...a)=>{if(!r)return i(t,a);let o={kind:e,args:a,dropped:!1};return n.push(o),()=>{o.live?o.live():o.dropped=!0}},o=e=>(...t)=>{if(!r)return i(e,t);for(let e of n)e.kind!==`off`&&T(t,e.args)&&(e.dropped=!0);n.push({kind:`off`,args:t,dropped:!1})},s=a(`on`,t.on),c=a(`once`,t.once),l=o(t.off);return e.on=s,e.once=c,e.off=l,{release:()=>{r=!1,e.on===s&&(e.on=t.on),e.once===c&&(e.once=t.once),e.off===l&&(e.off=t.off)},replay:()=>{for(let e of n.splice(0))if(!e.dropped){if(e.kind===`off`)i(t.off,e.args);else{let n=e.kind===`on`?t.on:t.once;e.live=i(n,e.args)}}}}};function D(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const O=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,k=(e,t,n)=>{n&&e.applyScope?.(t,n)},A=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],j=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},M=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},N=(n=[])=>i=>{let a=i.commands??g()(i),o={handle:(...e)=>{if(!a.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(a.handle,null,e)}},s=n=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:c=>{let l=r.get(c);if(l)return l;let u=a.get(c.name);u&&r.get(u)?.();let d=t(n,c.name),f=d.ctx,p=s(f),m=()=>{r.delete(c),a.get(c.name)===c&&a.delete(c.name),f.abort(),d.unlink()};r.set(c,m),a.set(c.name,c);let h=c.scope,g=O(i,h),_=(t,n)=>{let r=Reflect.apply(o.handle,null,A(g,t,n));return e(f,r),r},v=(...e)=>{let t=e.map(e=>typeof e==`function`?_(e,e):p.mount(e));return()=>{for(let e of t)e()}},y=M(f),b=j(f,g);try{k(i,f,h),c.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:b}))}catch(e){throw m(),e}return m},unmount:e=>r.get(e)?.(),isMounted:e=>r.has(e)}},c=E(i),l=i.emit,u,d=(e=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let t=r({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:t,apps:s(t.ctx)},e)for(let e of n)u.apps.mount(e);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,i,e));i.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:D};return i.app=p,p};export{m as bindHandler,g as commandsPlugin,d as createAction,p as createCommand,S as createJob,D as createModule,_ as emitterPlugin,N as modulePlugin};
1
+ import{c as e,i as t,l as n,n as r,s as i}from"./context-yYU3_9lq.js";import{a,r as o}from"./bus-G2AdGObY.js";import{n as s,t as c}from"./lifecycle-key-8iX8RUVD.js";import{t as l}from"./origin-key-BbWN3gmN.js";const u=Symbol.for(`nanoeffects.scope`),d=(e,t)=>(t&&Object.defineProperty(e,u,{value:t}),e),f=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),p=e=>Reflect.get(e,u);function m(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(d((...t)=>d({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var h=class extends Error{};function g(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new h(`No handler for command "${e}"`);return t(r,i)}return d(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=d(a,n);return s}const _=(e,t)=>[e,t],v=e=>typeof Reflect.get(e??{},`handle`)==`function`,y=(e=[])=>t=>{let n=t.commands;if(v(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},b=()=>e=>{function t(t,n){return(...r)=>{let i=t(...r),a=Reflect.get(e,l);a?a(i,n):e.emit(i)}}return Object.assign(e,{emitter:t})},x=e=>typeof e==`string`?m(e):e,S=(e,t,n,r)=>{let i=p(t);e.emit(Reflect.apply(t,null,[n]),r&&i?f(r,i):r)},C=(e,t,r,a)=>{let o=e;o[i]=!0;try{S(e,t,r,a)}catch(e){n(`cancel listener`,e)}o[i]=!1},w=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof h)throw e;return Promise.reject(e)}},T=(t,r,i={},a)=>{let l=g(t,r),u=e=>x(i[e]??`${t}/${e}`),f=u(`pending`),p=u(`success`),m=u(`fail`),h=u(`cancel`),_=(t,r,i,o)=>{let s=i,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},g=e=>{l===`started`&&(d(),C(s,h,e,a))},_=()=>{l===`idle`&&(l=`started`,u=e(s,g),S(s,f,r,a))},v=e=>{l===`started`&&(d(),s.signal.aborted?C(s,h,s.signal.reason,a):`error`in e?S(s,m,e.error,a):S(s,p,e.value,a))};return Reflect.set(s,c,{start:_,cancel:g,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),w(()=>(o||_(),Reflect.apply(t,null,[r,i]))).then(e=>(v({value:e}),e),e=>{if(l===`idle`)try{_()}catch(e){n(`pending listener`,e)}v({error:e})})},v=e=>{let t=Reflect.get(e,s)===!0;return(n,r)=>_(e,n,r,t)};function y(e,n){return typeof n==`object`?r?_(l,e,n,!1):Reflect.apply(l,null,[e,n]):d(o(T,t,e),a)}let b=Object.assign(y,{type:t,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:h}),T=d(b,a);return T},E=e=>typeof e==`object`&&!!e,D=e=>{if(typeof e==`string`)return e;if(E(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},O=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(D(n),D(t[0]))&&(r===void 0||Object.is(r,t[1]))},k=e=>{let t={on:e.on,once:e.once,off:e.off},n=[],r=!0,i=(t,n)=>Reflect.apply(t,e,n),a=(e,t)=>(...a)=>{if(!r)return i(t,a);let o={kind:e,args:a,dropped:!1};return n.push(o),()=>{o.live?o.live():o.dropped=!0}},o=e=>(...t)=>{if(!r)return i(e,t);for(let e of n)e.kind!==`off`&&O(t,e.args)&&(e.dropped=!0);n.push({kind:`off`,args:t,dropped:!1})},s=a(`on`,t.on),c=a(`once`,t.once),l=o(t.off);return e.on=s,e.once=c,e.off=l,{release:()=>{r=!1,e.on===s&&(e.on=t.on),e.once===c&&(e.once=t.once),e.off===l&&(e.off=t.off)},replay:()=>{for(let e of n.splice(0))if(!e.dropped){if(e.kind===`off`)i(t.off,e.args);else{let n=e.kind===`on`?t.on:t.once;e.live=i(n,e.args)}}}}};function A(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const j=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,M=(e,t,n)=>{n&&e.applyScope?.(t,n)},N=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],P=(e,t)=>{let n=e.on;return(...e)=>{let[r,i,...a]=e;return Reflect.apply(n,null,[r,t(i),...a])}},F=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},I=(n=[])=>i=>{let a=i.commands??y()(i),o={handle:(...e)=>{if(!a.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(a.handle,null,e)}},s=n=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:c=>{let l=r.get(c);if(l)return l;let u=a.get(c.name);u&&r.get(u)?.();let d=t(n,c.name),f=d.ctx,p=s(f),m=()=>{r.delete(c),a.get(c.name)===c&&a.delete(c.name),f.abort(),d.unlink()};r.set(c,m),a.set(c.name,c);let h=c.scope,g=j(i,h),_=(t,n)=>{let r=Reflect.apply(o.handle,null,N(g,t,n));return e(f,r),r},v=(...e)=>{let t=e.map(e=>typeof e==`function`?_(e,e):p.mount(e));return()=>{for(let e of t)e()}},y=F(f),b=P(f,g);try{M(i,f,h),c.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:b}))}catch(e){throw m(),e}return m},unmount:e=>r.get(e)?.(),isMounted:e=>r.has(e)}},c=k(i),l=i.emit,u,d=(e=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let t=r({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:t,apps:s(t.ctx)},e)for(let e of n)u.apps.mount(e);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,i,e));i.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:A};return i.app=p,p};export{_ as bindHandler,y as commandsPlugin,m as createAction,g as createCommand,T as createJob,A as createModule,b as emitterPlugin,I as modulePlugin};
@@ -0,0 +1 @@
1
+ import{a as e,l as t,n,r}from"./context-yYU3_9lq.js";import{n as i}from"./promise-CSLpzwSR.js";const a=e=>typeof e==`string`?e:e.type,o=(e,t)=>typeof e==`object`?e.payload:t,s=`*`,c=()=>{let e={},t={},n=n=>(t[n]=void 0,e[n]??=/* @__PURE__ */ new Map,e[n]),r=(e,t)=>{let r=n(e),i=r.get(t)??0;i>1?r.set(t,i-1):t?r.delete(t):r.clear()};return{get:n=>(t[n]??=[...e[n]??[]].flatMap(([e,t])=>Array(t).fill(e)),t[n]),add:(e,t)=>{let i=n(e);return i.set(t,(i.get(t)??0)+1),()=>{r(e,t)}},remove:r,clear:()=>{e={},t={}}}},l=Symbol(),u=(e,t,n)=>{let r={type:t,payload:n};return Object.defineProperty(r,l,{value:e,enumerable:!1}),r},d=e=>{if(typeof e==`function`)return e;if(typeof e==`object`&&e&&l in e)return e[l]},f=(e,t,n)=>Reflect.apply(e,null,[t,n]),p=(s={})=>{let l=c(),u=(e,t)=>l.add(a(e),t),p=(e,t)=>{let n=u(e,(e,r)=>(n(),t(e,r)));return n},m=(e,t)=>{e===void 0?l.clear():l.remove(a(e),t)},h=(e,a)=>{let{onError:o}=s,{ctx:c}=n({event:a,bus:g,tracer:s.trace,parent:{ref:r()}});if(!o){e(a.payload,c);return}let l=e=>{try{o(e,a)}catch(e){t(`onError failed`,e)}};try{let t=e(a.payload,c);i(t)&&Promise.resolve(t).catch(l)}catch(e){l(e)}},g={on:u,once:p,off:m,emit:(t,i)=>{let c=a(t),u=o(t,i),p={type:c,payload:u},m=d(t),_=[...l.get(c),...l.get(`*`)];_.length===0&&!m&&s.onUnhandled?.(p);for(let e of _)h(e,p);if(m){let{ctx:t}=n({event:p,bus:g,tracer:s.trace,parent:{ref:r()}}),i=g.commands?.run??f;return e(t,`command`,()=>i(m,u,t))}},use:e=>e(g),ctx:s.context,trace:s.trace};return g};export{a,s as i,p as n,u as r,l as t};
@@ -1,4 +1,4 @@
1
- import { R as EventMap, j as WithContext, p as Bus, y as ContextOf } from "./action-creator-DHAEgwuh.js";
1
+ import { R as EventMap, j as WithContext, p as Bus, y as ContextOf } from "./action-creator-DQTwMBwP.js";
2
2
  //#region src/core/context-plugin.d.ts
3
3
  /** What a function that makes the fields of a task gets to see. */
4
4
  type ContextTask = {
@@ -1,5 +1,5 @@
1
- import { H as Task, R as EventMap, l as Effect } from "./action-creator-DHAEgwuh.js";
2
- import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-Cxadz6uU.js";
1
+ import { H as Task, R as EventMap, l as Effect } from "./action-creator-DQTwMBwP.js";
2
+ import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-DOYHG78z.js";
3
3
  //#region src/utils/onAbort.d.ts
4
4
  export declare const onAbort: (ctx: {
5
5
  signal?: AbortSignal | undefined;
@@ -1,3 +1,3 @@
1
- import { A as On, B as PayloadOf, C as EventKey, D as Handler, E as EventsOf, F as Tracer, G as TraceLogger, H as Task, I as anyEventType, K as UnsubscribeCallback, L as EffectName, M as WithType, N as commandCall, O as HandlerRef, P as createBus, R as EventMap, S as Emittable, T as EventPayload, U as Trace, V as Phase, W as TraceEvent, _ as Commands, b as ContextOption, d as Action, f as AnyEvent, g as CommandCall, h as Command, i as WithPayload, j as WithContext, k as Off, m as BusOptions, p as Bus, v as Context, w as EventLike, x as Emit, y as ContextOf, z as Keys } from "./action-creator-DHAEgwuh.js";
2
- import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin-BapQkGwQ.js";
1
+ import { A as On, B as PayloadOf, C as EventKey, D as Handler, E as EventsOf, F as Tracer, G as TraceLogger, H as Task, I as anyEventType, K as UnsubscribeCallback, L as EffectName, M as WithType, N as commandCall, O as HandlerRef, P as createBus, R as EventMap, S as Emittable, T as EventPayload, U as Trace, V as Phase, W as TraceEvent, _ as Commands, b as ContextOption, d as Action, f as AnyEvent, g as CommandCall, h as Command, i as WithPayload, j as WithContext, k as Off, m as BusOptions, p as Bus, v as Context, w as EventLike, x as Emit, y as ContextOf, z as Keys } from "./action-creator-DQTwMBwP.js";
2
+ import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin-DlZCkcX9.js";
3
3
  export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type ContextTask, type EffectName, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type NoClash, type Off, type On, type PayloadOf, type Phase, type Task, type Trace, type TraceEvent, type TraceLogger, type Tracer, type UnsubscribeCallback, type WithContext, type WithPayload, type WithType, anyEventType, commandCall, contextPlugin, createBus };
@@ -1 +1 @@
1
- import{i as e,n as t,t as n}from"./bus-BmCTo8BG.js";const r=e=>typeof e==`function`,i=e=>({type:Reflect.get(e,`type`),payload:Reflect.get(e,`payload`)}),a=e=>t=>{let n=t.addContext;return t.addContext=t=>{n?.(t);let a=r(e)?e(i(t)):e;for(let[e,n]of Object.entries(a))e in t||Reflect.set(t,e,n)},t};export{e as anyEventType,n as commandCall,a as contextPlugin,t as createBus};
1
+ import{i as e,n as t,t as n}from"./bus-G2AdGObY.js";const r=e=>typeof e==`function`,i=e=>({type:Reflect.get(e,`type`),payload:Reflect.get(e,`payload`)}),a=e=>t=>{let n=t.addContext;return t.addContext=t=>{n?.(t);let a=r(e)?e(i(t)):e;for(let[e,n]of Object.entries(a))e in t||Reflect.set(t,e,n)},t};export{e as anyEventType,n as commandCall,a as contextPlugin,t as createBus};
@@ -0,0 +1 @@
1
+ const e=Symbol(),t=Symbol();export{t as n,e as t};
@@ -0,0 +1 @@
1
+ const e=Symbol.for(`nanoeffects.origin`);export{e as t};
@@ -0,0 +1,10 @@
1
+ //#region src/core/origin-key.d.ts
2
+ /** What an emitter tells the trace about itself. */
3
+ type OriginOptions = {
4
+ /** Names the subscription that emits; it is the `type` of the root task of the trace. */
5
+ traceId?: string;
6
+ /** Names the flow the emit belongs to; every task below it has it too. */
7
+ traceGroupId?: string;
8
+ };
9
+ //#endregion
10
+ export { OriginOptions as t };
@@ -1,5 +1,5 @@
1
- import { R as EventMap, c as BoundForm, j as WithContext, p as Bus, u as Effects, v as Context, z as Keys } from "./action-creator-DHAEgwuh.js";
2
- import { n as NoClash } from "./context-plugin-BapQkGwQ.js";
1
+ import { R as EventMap, c as BoundForm, j as WithContext, p as Bus, u as Effects, v as Context, z as Keys } from "./action-creator-DQTwMBwP.js";
2
+ import { n as NoClash } from "./context-plugin-DlZCkcX9.js";
3
3
  //#region src/utils/delay.d.ts
4
4
  /**
5
5
  * Resolves after `ms`, or rejects early if `ctx.signal` aborts. `ms` can be a
@@ -1,4 +1,4 @@
1
- import { B as PayloadOf, H as Task, R as EventMap } from "./action-creator-DHAEgwuh.js";
1
+ import { B as PayloadOf, H as Task, R as EventMap } from "./action-creator-DQTwMBwP.js";
2
2
  //#region src/policies/keyed.d.ts
3
3
  /** Options shared by every policy that can keep its state per key. */
4
4
  type KeyOption<P> = {
@@ -1 +1 @@
1
- import{a as e,c as t,i as n,n as r,o as i,t as a}from"./context-yYU3_9lq.js";import{n as o,r as s,t as c}from"./delay-BosJKFjg.js";import{n as l,t as u}from"./promise-CSLpzwSR.js";const d=()=>{},f=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},p=(e,t)=>e?f(e,t):t(d),m=(e,t={})=>p(t.key,t=>h(`latest`,(t,n,r)=>(r(),e(t,n)),t)),h=(e,r,o=d)=>{let s=null;return(c,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=a(d,e),m=!1,h=()=>{d.signal.aborted||(m=!0,i(d,e,`start`))};t(d,()=>p(m?`cancel`:`drop`));let g=()=>{f(),s===d&&(s=null,o())},_=e=>{if(!d.signal.aborted)return p(m?`done`:`drop`),e};try{let e=r(c,d,h);return l(e)?Promise.resolve(e).then(_,e=>{if(!d.signal.aborted)throw p(`error`,e),e}).finally(g):(g(),_(e))}catch(e){if(g(),d.signal.aborted)return;throw p(`error`,e),e}}},g=(e,t,n={})=>p(n.key,r=>v(e,t,n,r)),_=(t,n,r,a)=>{let c=a!==d,l=null,u=null;return(d,f)=>{if(f.signal.aborted)return;let p=Date.now(),m=l===null||p-l>=t||p-l<0;return l=p,u!==null&&o(u),u=c?s(a,t):null,r&&m?Promise.resolve(e(f,`debounce`,()=>n(d,f))):(i(f,`debounce`,`drop`),Promise.resolve(void 0))}},v=(n,r,i,a)=>{let o=i.leading??!1,s=i.trailing??!0;if(!s)return _(n,r,o,a);let l=!1,u=0,d=h(`debounce`,([e,i],a,o)=>{let d=u;return t(a,()=>{u===d&&(l=!1)}),c(a,n).then(()=>{if(l=!1,s&&!i)return o(),r(e,a)})},a);return(t,n)=>{if(n.signal.aborted)return;let i=o&&!l;l=!0,u++;let a=d([t,i],n);return i?Promise.resolve(e(n,`debounce`,()=>r(t,n))):a}},y=(n,r={})=>p(r.key,r=>{let a=null;return(o,s)=>{if(s.signal.aborted)return;if(a){i(s,`leading`,`drop`);return}let c={};a=c;let l=()=>{d(),a===c&&(a=null,r())},d=t(s,l);try{return u(e(s,`leading`,()=>n(o,s)),l)}catch(e){throw l(),e}}}),b=(e,t={})=>p(t.key,n=>x(e,t,n)),x=(n,r,a)=>{let o=r.concurrency??1,s=r.limit??1/0,c=[],l=0,u=()=>{for(;l<o;){let e=c.shift();if(!e)return;e.start()}};return(f,p)=>new Promise((m,h)=>{if(p.signal.aborted){m(void 0);return}let g=d,_=()=>{g(),i(p,`queue`,`drop`),m(void 0)},v=()=>{if(g(),p.signal.aborted){_();return}l++;let r=!0,i=()=>{r&&(r=!1,l--,u(),l===0&&c.length===0&&a())},o=t(p,()=>{i(),m(void 0)}),s;try{s=Promise.resolve(e(p,`queue`,()=>n(f,p)))}catch(e){s=Promise.reject(e)}let d=()=>{o(),i()};s.then(e=>{d(),m(p.signal.aborted?void 0:e)},e=>{d(),p.signal.aborted?m(void 0):h(e)})};if(l<o){v();return}if(c.length>=s){if(r.overflow!==`drop-old`||s<=0){_();return}c.shift()?.drop()}let y={start:v,drop:_};c.push(y),g=t(p,()=>{let e=c.indexOf(y);e>=0&&c.splice(e,1),_()})})},S=(e,t,n={})=>p(n.key,r=>C(e,t,n,r)),C=(t,n,r,a)=>{let c=r.leading??!0,l=r.trailing??!0,u=a!==d,f=null,p=0,m=null,h=null,g=null,_=!1,v=e=>f===null||e-f>=t||e-f<0||e-p>=t,y=e=>i(e,`throttle`,`drop`),b=()=>{let e=g;g=null,e&&(y(e.ctx),e.resolve())},x=(e,t)=>(b(),new Promise((n,r)=>{g={payload:e,ctx:t,resolve:n,reject:r}})),S=(t,r,i)=>{b(),p=i,_=!0;try{return e(r,`throttle`,()=>n(t,r))}finally{_=!1}},C=()=>{u&&(h=s(w,t))},w=()=>{if(h=null,!u||m!==null||g)return;let e=Date.now(),n=Math.max(t-(e-(f??-1/0)),t-(e-p));n<=0?a():h=s(w,n)},T=e=>{m=null;let t=g;if(l&&t&&!t.ctx.signal.aborted){g=null;try{t.resolve(S(t.payload,t.ctx,e))}catch(e){t.reject(e)}}else b();w()},E=()=>{let e=Date.now();v(e)?T(e):m=s(E,Math.min(t-(e-(f??e)),t-(e-p)))},D=()=>{h!==null&&(o(h),h=null)},O=(e,t)=>{if(!l)y(t);else return x(e,t)},k=(e,t,n,r)=>{if(!r)y(t);else if(p=n,C(),!c)y(t);else return S(e,t,n)},A=(e,n,r,i)=>i&&m!==null?(o(m),m=s(E,t),S(e,n,r)):m===null&&(m=s(E,t),i&&(p=r,c))?S(e,n,r):O(e,n);return(e,t)=>{if(t.signal.aborted)return;let n=Date.now(),r=!_&&v(n);return f=n,D(),l?A(e,t,n,r):k(e,t,n,r)}},w=Reflect.get(Symbol,`dispose`),T=e=>{let t=new AbortController,n=async(...n)=>{let i=n[0],{ctx:a,unlink:o}=r({event:{type:`withPolicy`,payload:i},parent:{signal:t.signal}});try{return await e(i,a)}finally{o()}},i=e=>{t.abort(e),t=new AbortController},a=Object.assign(n,{cancel:i});return w&&Reflect.set(a,w,()=>i()),a};export{g as debounce,m as latest,y as leading,b as queue,S as throttle,T as withPolicy};
1
+ import{a as e,c as t,i as n,n as r,o as i,t as a}from"./context-yYU3_9lq.js";import{n as o,r as s,t as c}from"./delay-BosJKFjg.js";import{n as l,t as u}from"./promise-CSLpzwSR.js";import{n as d,t as f}from"./lifecycle-key-8iX8RUVD.js";const p=e=>Reflect.get(e,f),m=e=>{p(e)?.start()},h=(t,n,r)=>(m(t),e(t,n,r)),g=(e,t)=>{i(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,d,!0),e),b=()=>{},x=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},S=(e,t)=>y(e?x(e,t):t(b)),C=(e,t={})=>S(t.key,t=>w(`latest`,(t,n,r)=>(r(),e(t,n)),t)),w=(e,r,o=b)=>{let s=null;return(c,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=a(d,e),h=!1,g=()=>{d.signal.aborted||(h=!0,m(d),i(d,e,`start`))};t(d,e=>{v(d,h,e),p(h?`cancel`:`drop`)});let _=()=>{f(),s===d&&(s=null,o())},y=e=>{if(!d.signal.aborted)return p(h?`done`:`drop`),e};try{let e=r(c,d,g);return l(e)?Promise.resolve(e).then(y,e=>{if(!d.signal.aborted)throw p(`error`,e),e}).finally(_):(_(),y(e))}catch(e){if(_(),d.signal.aborted)return;throw p(`error`,e),e}}},T=(e,t,n={})=>S(n.key,r=>D(e,t,n,r)),E=(e,t,n,r)=>{let i=r!==b,a=null,c=null;return(l,u)=>{if(u.signal.aborted)return;let d=Date.now(),f=a===null||d-a>=e||d-a<0;return a=d,c!==null&&o(c),c=i?s(r,e):null,n&&f?Promise.resolve(h(u,`debounce`,()=>t(l,u))):(g(u,`debounce`),Promise.resolve(void 0))}},D=(e,n,r,i)=>{let a=r.leading??!1,o=r.trailing??!0;if(!o)return E(e,n,a,i);let s=!1,l=0,u=w(`debounce`,([r,i],a,u)=>{let d=l;return t(a,()=>{l===d&&(s=!1)}),c(a,e).then(()=>{if(s=!1,o&&!i)return u(),n(r,a)})},i);return(e,t)=>{if(t.signal.aborted)return;let r=a&&!s;s=!0,l++;let i=u([e,r],t);return r?Promise.resolve(h(t,`debounce`,()=>n(e,t))):i}},O=(e,n={})=>S(n.key,n=>{let r=null;return(i,a)=>{if(a.signal.aborted)return;if(r){g(a,`leading`);return}let o={};r=o;let s=()=>{c(),r===o&&(r=null,n())},c=t(a,s);try{return u(h(a,`leading`,()=>e(i,a)),s)}catch(e){throw s(),e}}}),k=(e,t={})=>S(t.key,n=>A(e,t,n)),A=(e,n,r)=>{let i=n.concurrency??1,a=n.limit??1/0,o=[],s=0,c=()=>{for(;s<i;){let e=o.shift();if(!e)return;e.start()}};return(l,u)=>new Promise((d,f)=>{if(u.signal.aborted){d(void 0);return}let p=b,m=()=>{p(),g(u,`queue`),d(void 0)},v=()=>{if(p(),u.signal.aborted){m();return}s++;let n=!0,i=()=>{n&&(n=!1,s--,c(),s===0&&o.length===0&&r())},a=t(u,()=>{i(),d(void 0)}),g;try{g=Promise.resolve(h(u,`queue`,()=>e(l,u)))}catch(e){g=Promise.reject(e)}let v=()=>{a(),i()};g.then(e=>{_(u,{value:e}),v(),d(u.signal.aborted?void 0:e)},e=>{_(u,{error:e}),v(),u.signal.aborted?d(void 0):f(e)})};if(s<i){v();return}if(o.length>=a){if(n.overflow!==`drop-old`||a<=0){m();return}o.shift()?.drop()}let y={start:v,drop:m};o.push(y),p=t(u,()=>{let e=o.indexOf(y);e>=0&&o.splice(e,1),m()})})},j=(e,t,n={})=>S(n.key,r=>M(e,t,n,r)),M=(e,t,n,r)=>{let i=n.leading??!0,a=n.trailing??!0,c=r!==b,l=null,u=0,d=null,f=null,p=null,m=!1,_=t=>l===null||t-l>=e||t-l<0||t-u>=e,v=e=>g(e,`throttle`),y=()=>{let e=p;p=null,e&&(v(e.ctx),e.resolve())},x=(e,t)=>(y(),new Promise((n,r)=>{p={payload:e,ctx:t,resolve:n,reject:r}})),S=(e,n,r)=>{y(),u=r,m=!0;try{return h(n,`throttle`,()=>t(e,n))}finally{m=!1}},C=()=>{c&&(f=s(w,e))},w=()=>{if(f=null,!c||d!==null||p)return;let t=Date.now(),n=Math.max(e-(t-(l??-1/0)),e-(t-u));n<=0?r():f=s(w,n)},T=e=>{d=null;let t=p;if(a&&t&&!t.ctx.signal.aborted){p=null;try{t.resolve(S(t.payload,t.ctx,e))}catch(e){t.reject(e)}}else y();w()},E=()=>{let t=Date.now();_(t)?T(t):d=s(E,Math.min(e-(t-(l??t)),e-(t-u)))},D=()=>{f!==null&&(o(f),f=null)},O=(e,t)=>{if(!a)v(t);else return x(e,t)},k=(e,t,n,r)=>{if(!r)v(t);else if(u=n,C(),!i)v(t);else return S(e,t,n)},A=(t,n,r,a)=>a&&d!==null?(o(d),d=s(E,e),S(t,n,r)):d===null&&(d=s(E,e),a&&(u=r,i))?S(t,n,r):O(t,n);return(e,t)=>{if(t.signal.aborted)return;let n=Date.now(),r=!m&&_(n);return l=n,D(),a?A(e,t,n,r):k(e,t,n,r)}},N=Reflect.get(Symbol,`dispose`),P=e=>{let t=new AbortController,n=async(...n)=>{let i=n[0],{ctx:a,unlink:o}=r({event:{type:`withPolicy`,payload:i},parent:{signal:t.signal}});try{return await e(i,a)}finally{o()}},i=e=>{t.abort(e),t=new AbortController},a=Object.assign(n,{cancel:i});return N&&Reflect.set(a,N,()=>i()),a};export{T as debounce,C as latest,O as leading,k as queue,j as throttle,P as withPolicy};