nanoeffects 0.19.1 → 0.21.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 (51) hide show
  1. package/dist/{action-creator-HFA_LZOH.d.ts → action-creator-Bpa8BUS9.d.ts} +18 -7
  2. package/dist/actions.d.ts +3 -3
  3. package/dist/actions.js +1 -1
  4. package/dist/{context-types-BNcs43eY.d.ts → context-types-BKIH5a8s.d.ts} +1 -1
  5. package/dist/context.d.ts +2 -2
  6. package/dist/dev.d.ts +2 -2
  7. package/dist/dev.js +1 -1
  8. package/dist/effects.d.ts +4 -8
  9. package/dist/index.d.ts +2 -2
  10. package/dist/match.d.ts +1 -1
  11. package/dist/{module-DTf73Wdq.d.ts → module-B0KVDNjY.d.ts} +14 -9
  12. package/dist/mount-each-D54UqAhZ.js +1 -0
  13. package/dist/notrace/{action-creator-CbFf398i.d.ts → action-creator-D2CotugT.d.ts} +16 -7
  14. package/dist/notrace/actions.d.ts +14 -9
  15. package/dist/notrace/actions.js +1 -1
  16. package/dist/notrace/effects.d.ts +4 -8
  17. package/dist/notrace/index.d.ts +2 -2
  18. package/dist/notrace/{plugin-C5vIjxhR.d.ts → plugin-C6KA4dX-.d.ts} +1 -1
  19. package/dist/notrace/policies.d.ts +51 -6
  20. package/dist/notrace/policies.js +1 -1
  21. package/dist/notrace/redux.d.ts +1 -1
  22. package/dist/observable.d.ts +1 -1
  23. package/dist/oop.d.ts +1 -1
  24. package/dist/{plugin-Sy8z9IId.d.ts → plugin-D9UTzGRG.d.ts} +2 -2
  25. package/dist/policies.d.ts +52 -7
  26. package/dist/policies.js +1 -1
  27. package/dist/react.d.ts +9 -8
  28. package/dist/redux.d.ts +1 -1
  29. package/dist/scope.d.ts +4 -4
  30. package/dist/signal.d.ts +1 -1
  31. package/dist/state-of-C19H6BoV.d.ts +10 -0
  32. package/dist/trace.d.ts +1 -1
  33. package/dist/{types-DaZT158m.d.ts → types-CbdJKC2z.d.ts} +2 -2
  34. package/dist/vue.d.ts +72 -0
  35. package/dist/vue.js +1 -0
  36. package/dist/{with-policy-C_mX7S54.d.ts → with-policy-BeH8wCkX.d.ts} +1 -1
  37. package/docs/README.md +2 -0
  38. package/docs/actions.md +31 -1
  39. package/docs/chat-example.md +246 -0
  40. package/docs/migrating-from-rxjs.md +58 -1
  41. package/docs/package.md +1 -0
  42. package/docs/plugins.md +3 -3
  43. package/docs/policies.md +27 -0
  44. package/docs/query-libraries.md +2 -0
  45. package/docs/react.md +5 -3
  46. package/docs/starter.md +25 -1
  47. package/docs/typescript.md +14 -0
  48. package/docs/vue.md +96 -0
  49. package/llms-full.txt +517 -10
  50. package/llms.txt +4 -2
  51. package/package.json +25 -4
@@ -75,6 +75,19 @@ type EventPayload<E extends EventMap> = E[EventKey<E>];
75
75
  type WithType<K extends string> = {
76
76
  type: K;
77
77
  };
78
+ /**
79
+ * An object that has the `signal` of a task: the context of a task, `{ signal }`, or the
80
+ * `{ signal, abort }` that a starter of `all` and `race` gets. A function that only needs to
81
+ * stop its request when the task is aborted takes `WithSignal`:
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const search = (query: string, { signal }: WithSignal) => fetch(`/search?q=${query}`, { signal });
86
+ * ```
87
+ */
88
+ type WithSignal = {
89
+ signal: AbortSignal;
90
+ };
78
91
  type HandlerRef<E extends EventMap, Response = unknown> = (action: Action<E>) => Response;
79
92
  type Handler<E extends EventMap, Response = unknown, K extends EventKey<E> = EventKey<E>> = { [P in K]: {
80
93
  type: P;
@@ -278,12 +291,10 @@ type BusOptions<E extends EventMap> = {
278
291
  declare const createBus: <Events extends EventMap>(options?: BusOptions<Events>) => Bus<Events>;
279
292
  //#endregion
280
293
  //#region src/core/effect-types.d.ts
281
- /** What an effect needs of the task it runs for: its signal. A task context fits, and so does `{ signal }`. */
282
- type Signaled = {
283
- signal: AbortSignal;
284
- };
294
+ /** What an effect needs of the task it runs for: its signal. The name `WithSignal` is the one to use; this one stays for `nanoeffects/scope`. */
295
+ type Signaled = WithSignal;
285
296
  /** An effect: a function whose first parameter takes `{ signal }`, like `retry(ctx, fn)`. */
286
- type SignalFirst = (ctx: Signaled, ...args: never[]) => unknown;
297
+ type SignalFirst = (ctx: WithSignal, ...args: never[]) => unknown;
287
298
  /** The effects of a scope or of `effectsPlugin`, by the name they get on the context. */
288
299
  type Effects = Record<string, SignalFirst>;
289
300
  declare const bound: unique symbol;
@@ -299,7 +310,7 @@ declare const bound: unique symbol;
299
310
  * const first: Effect<First> = (ctx, items) => items[0];
300
311
  * ```
301
312
  */
302
- type Effect<F extends (...args: never[]) => unknown> = ((ctx: Signaled, ...args: Parameters<F>) => ReturnType<F>) & {
313
+ type Effect<F extends (...args: never[]) => unknown> = ((ctx: WithSignal, ...args: Parameters<F>) => ReturnType<F>) & {
303
314
  readonly [bound]?: F;
304
315
  };
305
316
  /** The bound form of a plain effect: the first parameter is gone, and so are its generics. */
@@ -400,4 +411,4 @@ interface ActionCreator<P = void, T extends string = string, A extends unknown[]
400
411
  toString(): T;
401
412
  }
402
413
  //#endregion
403
- export { HandlerRef as A, ListenerOptions as B, Emit as C, EventPayload as D, EventLike as E, createBus as F, TraceEvent as G, Phase as H, Tracer as I, TraceLogger as K, EffectName as L, On as M, WithContext as N, EventsOf as O, WithType as P, EventMap as R, ContextOption as S, EventKey as T, Task as U, PayloadOf as V, Trace 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, UnsubscribeCallback 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, Keys as z };
414
+ export { HandlerRef as A, Keys as B, Emit as C, EventPayload as D, EventLike as E, WithType 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, WithSignal as P, EffectName as R, ContextOption as S, EventKey as T, Phase as U, ListenerOptions 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, EventMap as z };
package/dist/actions.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { R as EventMap, T as EventKey, U as Task, V as PayloadOf, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator } from "./action-creator-HFA_LZOH.js";
2
- import { r as Scoped, t as FieldsOfScope } from "./types-DaZT158m.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-DTf73Wdq.js";
1
+ import { 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, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
+ import { r as Scoped, t as FieldsOfScope } from "./types-CbdJKC2z.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-B0KVDNjY.js";
4
4
  import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
5
5
  //#region src/actions/action.d.ts
6
6
  type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCreator<T, F> & WithScope<F> : ActionCreator<P, T, PayloadOf<P>, F> & WithScope<F>;
package/dist/actions.js CHANGED
@@ -1 +1 @@
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{r as o}from"./bus-duCCrqrr.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};
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{r as o}from"./bus-duCCrqrr.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";import{t as d}from"./mount-each-D54UqAhZ.js";const f=(e,t)=>(t&&Object.defineProperty(e,u,{value:t}),e),p=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),m=e=>Reflect.get(e,u);function h(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(f((...t)=>f({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var g=class extends Error{};function _(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new g(`No handler for command "${e}"`);return t(r,i)}return f(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=f(a,n);return s}const v=(e,t)=>[e,t],y=e=>typeof Reflect.get(e??{},`handle`)==`function`,b=(e=[])=>t=>{let n=t.commands;if(y(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},x=()=>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})},S=e=>typeof e==`string`?h(e):e,C=(e,t,n,r)=>{let i=m(t);e.emit(Reflect.apply(t,null,[n]),r&&i?p(r,i):r)},w=(t,r,i,a)=>{let o=t;o[n]=!0;try{C(t,r,i,a)}catch(t){e(`cancel listener`,t)}o[n]=!1},T=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof g)throw e;return Promise.reject(e)}},E=(t,n,r={},a)=>{let l=_(t,n),u=e=>S(r[e]??`${t}/${e}`),d=u(`pending`),p=u(`success`),m=u(`fail`),h=u(`cancel`),g=(t,n,r,o)=>{let s=r,l=`idle`,u=()=>{},f=()=>{l=`closed`,u()},g=e=>{l===`started`&&(f(),w(s,h,e,a))},_=()=>{l===`idle`&&(l=`started`,u=i(s,g),C(s,d,n,a))},v=e=>{l===`started`&&(f(),s.signal.aborted?w(s,h,s.signal.reason,a):`error`in e?C(s,m,e.error,a):C(s,p,e.value,a))};return Reflect.set(s,c,{start:_,cancel:g,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),T(()=>(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)=>g(e,n,r,t)};function y(e,r){return typeof r==`object`?n?g(l,e,r,!1):Reflect.apply(l,null,[e,r]):f(o(x,t,e),a)}let b=Object.assign(y,{type:t,match:l.match,wrap:v,pending:d,success:p,fail:m,cancel:h}),x=f(b,a);return x},D=e=>typeof e==`object`&&!!e,O=e=>{if(typeof e==`string`)return e;if(D(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},k=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(O(n),O(t[0]))&&(r===void 0||Object.is(r,t[1]))},A=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`&&k(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 j(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const M=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,N=(e,t,n)=>{n&&e.applyScope?.(t,n)},P=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],F=(e,t)=>{let n=e.on;return(...e)=>{let[r,i,...a]=e;return Reflect.apply(n,null,[r,t(i),...a])}},I=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},L=(n=[])=>a=>{let o=a.commands??b()(a),s={handle:(...e)=>{if(!o.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(o.handle,null,e)}},c=e=>{let n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Map;return{mount:o=>{let l=n.get(o);if(l)return l;let u=r.get(o.name);u&&n.get(u)?.();let d=t(e,o.name),f=d.ctx,p=c(f),m=()=>{n.delete(o),r.get(o.name)===o&&r.delete(o.name),f.abort(),d.unlink()};n.set(o,m),r.set(o.name,o);let h=o.scope,g=M(a,h),_=(e,t)=>{let n=Reflect.apply(s.handle,null,P(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=I(f),b=F(f,g);try{N(a,f,h),o.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:b}))}catch(e){throw m(),e}return m},unmount:e=>n.get(e)?.(),isMounted:e=>n.has(e)}},l=A(a),u=a.emit,f,p=(e=!0)=>{if(f)return f;l.release(),a.emit===m&&(a.emit=u);let t=r({event:{type:`app`,payload:void 0},bus:a,tracer:a.trace});f={root:t,apps:d(c(t.ctx))};try{e&&f.apps.mount(...n)}finally{l.replay()}return f},m=(...e)=>(p(),Reflect.apply(u,a,e));a.emit=m,n.length>0&&queueMicrotask(()=>{try{p()}catch(t){e(`the modules of the app could not start`,t)}});let h={get commands(){return p(),o},mount:(...e)=>{let{root:t,apps:n}=p();return t.ctx.signal.throwIfAborted(),n.mount(...e)},unmount:(...e)=>p().apps.unmount(...e),isMounted:e=>p().apps.isMounted(e),dispose:()=>p(!1).root.ctx.abort(),createModule:j};return a.app=h,h};export{v as bindHandler,b as commandsPlugin,h as createAction,_ as createCommand,E as createJob,j as createModule,x as emitterPlugin,L as modulePlugin};
@@ -1,4 +1,4 @@
1
- import { R as EventMap, x as ContextOf } from "./action-creator-HFA_LZOH.js";
1
+ import { x as ContextOf, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
2
  //#region src/core/context-types.d.ts
3
3
  /** What a function that makes the fields of a task gets to see. */
4
4
  type ContextTask = {
package/dist/context.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { N as WithContext, R as EventMap, h as Bus } from "./action-creator-HFA_LZOH.js";
2
- import { n as NoClash, t as ContextTask } from "./context-types-BNcs43eY.js";
1
+ import { N as WithContext, h as Bus, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
+ import { n as NoClash, t as ContextTask } from "./context-types-BKIH5a8s.js";
3
3
  //#region src/context/plugin.d.ts
4
4
  /**
5
5
  * Puts `fields` on the context of every task of the bus: a store, a client, a
package/dist/dev.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as EventMap, h as Bus, q as UnsubscribeCallback } from "./action-creator-HFA_LZOH.js";
2
- import { i as WithApp, n as ModuleApi } from "./module-DTf73Wdq.js";
1
+ import { J as UnsubscribeCallback, h as Bus, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
+ import { i as WithApp, n as ModuleApi } from "./module-B0KVDNjY.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/dev.js CHANGED
@@ -1 +1 @@
1
- import{t as e}from"./event-0RIBGYqS.js";const t=`[nanoeffects]`,n=(e={})=>t=>{let{maxListeners:n=10,leaks:r=!0,modules:o=!0,app:s}=e,c=e.log??(e=>console.warn(e)),l=e.log??(e=>console.info(e)),u=[];return r&&u.push(i(t,n,c)),o&&u.push(a(t,s,l)),()=>{for(let e of u.splice(0))e()}},r=t=>typeof t==`function`&&typeof Reflect.get(t,`type`)!=`string`?`*`:e(t),i=(e,n,i)=>{let a=e.on,o=/* @__PURE__ */ new Map,s=/* @__PURE__ */ new Set,c=!0,l=(...l)=>{if(!c)return Reflect.apply(a,e,l);let[u]=l,d=r(u),f=(o.get(d)??0)+1;o.set(d,f),f>n&&!s.has(d)&&(s.add(d),i(`${t} ${f} listeners of "${d}" were added and not unsubscribed (more than ${n}). A file that is reloaded and subscribes again, or an unsubscribe that is never called?`));let p=Reflect.apply(a,e,l),m=!1;return()=>{if(!m){m=!0;let e=(o.get(d)??1)-1;o.set(d,e),e<=n&&s.delete(d)}p()}};return e.on=l,()=>{c=!1,e.on===l&&(e.on=a)}},a=(e,t,n)=>{let r=t??e.app;if(r)return o(r,n);let i,a;return Object.defineProperty(e,"app",{configurable:!0,enumerable:!0,get:()=>i,set:e=>{a?.(),i=e,a=e?o(e,n):void 0}}),()=>{a?.(),Object.defineProperty(e,"app",{configurable:!0,enumerable:!0,writable:!0,value:i})}},o=(e,n)=>{let{mount:r,unmount:i}=e,a=/* @__PURE__ */ new Map;return e.mount=i=>{let o=a.get(i.name);if(o===i&&e.isMounted(i))return r(i);let s=o!==void 0&&e.isMounted(o),c=r(i);return a.set(i.name,i),n(`${t} module "${i.name}" ${s?`replaced`:`mounted`}`),()=>{e.isMounted(i)&&n(`${t} module "${i.name}" unmounted`),c()}},e.unmount=r=>{e.isMounted(r)&&n(`${t} module "${r.name}" unmounted`),i(r)},()=>{e.mount=r,e.unmount=i}};export{n as devPlugin};
1
+ import{t as e}from"./event-0RIBGYqS.js";import{t}from"./mount-each-D54UqAhZ.js";const n=`[nanoeffects]`,r=(e={})=>t=>{let{maxListeners:n=10,leaks:r=!0,modules:i=!0,app:s}=e,c=e.log??(e=>console.warn(e)),l=e.log??(e=>console.info(e)),u=[];return r&&u.push(a(t,n,c)),i&&u.push(o(t,s,l)),()=>{for(let e of u.splice(0))e()}},i=t=>typeof t==`function`&&typeof Reflect.get(t,`type`)!=`string`?`*`:e(t),a=(e,t,r)=>{let a=e.on,o=/* @__PURE__ */ new Map,s=/* @__PURE__ */ new Set,c=!0,l=(...l)=>{if(!c)return Reflect.apply(a,e,l);let[u]=l,d=i(u),f=(o.get(d)??0)+1;o.set(d,f),f>t&&!s.has(d)&&(s.add(d),r(`${n} ${f} listeners of "${d}" were added and not unsubscribed (more than ${t}). A file that is reloaded and subscribes again, or an unsubscribe that is never called?`));let p=Reflect.apply(a,e,l),m=!1;return()=>{if(!m){m=!0;let e=(o.get(d)??1)-1;o.set(d,e),e<=t&&s.delete(d)}p()}};return e.on=l,()=>{c=!1,e.on===l&&(e.on=a)}},o=(e,t,n)=>{let r=t??e.app;if(r)return s(r,n);let i,a;return Object.defineProperty(e,"app",{configurable:!0,enumerable:!0,get:()=>i,set:e=>{a?.(),i=e,a=e?s(e,n):void 0}}),()=>{a?.(),Object.defineProperty(e,"app",{configurable:!0,enumerable:!0,writable:!0,value:i})}},s=(e,r)=>{let{mount:i,unmount:a}=e,o=/* @__PURE__ */ new Map,s=t({mount:t=>{let a=o.get(t.name);if(a===t&&e.isMounted(t))return i(t);let s=a!==void 0&&e.isMounted(a),c=i(t);return o.set(t.name,t),r(`${n} module "${t.name}" ${s?`replaced`:`mounted`}`),()=>{e.isMounted(t)&&r(`${n} module "${t.name}" unmounted`),c()}},unmount:t=>{e.isMounted(t)&&r(`${n} module "${t.name}" unmounted`),a(t)},isMounted:t=>e.isMounted(t)});return e.mount=s.mount,e.unmount=s.unmount,()=>{e.mount=i,e.unmount=a}};export{r as devPlugin};
package/dist/effects.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { R as EventMap, U as Task, u as Effect } from "./action-creator-HFA_LZOH.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-Sy8z9IId.js";
1
+ import { P as WithSignal, W as Task, u as Effect, z as EventMap } from "./action-creator-Bpa8BUS9.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-D9UTzGRG.js";
3
3
  //#region src/utils/onAbort.d.ts
4
4
  export declare const onAbort: (ctx: {
5
5
  signal?: AbortSignal | undefined;
@@ -66,9 +66,7 @@ type CancellableExecutor<T> = (handle: CancellableHandle<T>) => void;
66
66
  * );
67
67
  * ```
68
68
  */
69
- export declare const cancellable: <T = void>(ctx: {
70
- signal: AbortSignal;
71
- }, executor: CancellableExecutor<T>) => Promise<T>;
69
+ export declare const cancellable: <T = void>(ctx: WithSignal, executor: CancellableExecutor<T>) => Promise<T>;
72
70
  //#endregion
73
71
  //#region src/effects/poll.d.ts
74
72
  type PollOptions = {
@@ -108,9 +106,7 @@ type PollOptions = {
108
106
  */
109
107
  type PollMs = number | ((call: number) => number);
110
108
  export declare function poll<P, E extends EventMap, R>(ms: PollMs, task: Task<P, E, R>, options?: PollOptions): Task<P, E, Promise<void>>;
111
- export declare function poll(ctx: {
112
- signal: AbortSignal;
113
- }, fn: (tick: number) => unknown, ms: PollMs, options?: PollOptions): Promise<void>;
109
+ export declare function poll(ctx: WithSignal, fn: (tick: number) => unknown, ms: PollMs, options?: PollOptions): Promise<void>;
114
110
  //#endregion
115
111
  //#region src/effects/retry.d.ts
116
112
  type RetryOptions = {
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { A as HandlerRef, C as Emit, D as EventPayload, E as EventLike, F as createBus, M as On, N as WithContext, O as EventsOf, P as WithType, R as EventMap, S as ContextOption, T as EventKey, U as Task, V as PayloadOf, _ 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 UnsubscribeCallback, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as Keys } from "./action-creator-HFA_LZOH.js";
2
- export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type Off, type On, type PayloadOf, type Task, type UnsubscribeCallback, type WithContext, type WithPayload, type WithType, createBus };
1
+ import { A as HandlerRef, B as Keys, C as Emit, D as EventPayload, E as EventLike, F as WithType, H as PayloadOf, I as createBus, J as UnsubscribeCallback, M as On, N as WithContext, O as EventsOf, P as WithSignal, S as ContextOption, T as EventKey, 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, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
+ export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type Off, type On, type PayloadOf, type Task, type UnsubscribeCallback, type WithContext, type WithPayload, type WithSignal, type WithType, createBus };
package/dist/match.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { D as EventPayload, R as EventMap, U as Task, h as Bus, p as Action, q as UnsubscribeCallback } from "./action-creator-HFA_LZOH.js";
1
+ import { D as EventPayload, J as UnsubscribeCallback, W as Task, h as Bus, p as Action, z as EventMap } from "./action-creator-Bpa8BUS9.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;
@@ -1,5 +1,5 @@
1
- import { R as EventMap, U as Task, V as PayloadOf, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, q as UnsubscribeCallback, v as CommandCall, y as Commands } from "./action-creator-HFA_LZOH.js";
2
- import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-DaZT158m.js";
1
+ import { 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, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
+ import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-CbdJKC2z.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> & {
@@ -159,12 +159,12 @@ type Registry<E extends EventMap> = ReturnType<ReturnType<typeof commandsPlugin<
159
159
  * of what `modulePlugin` returns that `devPlugin` wraps.
160
160
  */
161
161
  interface ModuleHost {
162
- mount(module: {
162
+ mount(...modules: Array<{
163
163
  readonly name: string;
164
- }): UnsubscribeCallback;
165
- unmount(module: {
164
+ }>): UnsubscribeCallback;
165
+ unmount(...modules: Array<{
166
166
  readonly name: string;
167
- }): void;
167
+ }>): void;
168
168
  isMounted(module: {
169
169
  readonly name: string;
170
170
  }): boolean;
@@ -177,9 +177,14 @@ interface WithApp {
177
177
  type ModuleApi<E extends EventMap = EventMap> = {
178
178
  /** The registry of command handlers of the bus: the one it was made with, or the app's own. */
179
179
  commands: Registry<E>;
180
- /** Mounts a module and returns the function that unmounts it; a module that is mounted already is not run again. */
181
- mount(module: Module<E>): UnsubscribeCallback;
182
- unmount(module: Module<E>): void;
180
+ /**
181
+ * Mounts modules, in the order given, and returns the function that unmounts them, last
182
+ * mounted first. A module that is mounted already is not run again. If one of them throws,
183
+ * the ones mounted by this call are unmounted and the error is thrown.
184
+ */
185
+ mount(...modules: Module<E>[]): UnsubscribeCallback;
186
+ /** Unmounts modules, in reverse order of the arguments. */
187
+ unmount(...modules: Module<E>[]): void;
183
188
  isMounted(module: Module<E>): boolean;
184
189
  /** Unmounts every module; a disposed app cannot mount modules. */
185
190
  dispose(): void;
@@ -0,0 +1 @@
1
+ const e=e=>({mount:(...t)=>{let[n]=t;if(t.length===1&&n!==void 0)return e.mount(n);let r=[],i=()=>{for(let e of r.splice(0).reverse())e()};try{for(let n of t)e.isMounted(n)||r.push(e.mount(n))}catch(e){throw i(),e}return i},unmount:(...t)=>{for(let n of[...t].reverse())e.unmount(n)},isMounted:e.isMounted});export{e as t};
@@ -53,6 +53,19 @@ type EventPayload<E extends EventMap> = E[EventKey<E>];
53
53
  type WithType<K extends string> = {
54
54
  type: K;
55
55
  };
56
+ /**
57
+ * An object that has the `signal` of a task: the context of a task, `{ signal }`, or the
58
+ * `{ signal, abort }` that a starter of `all` and `race` gets. A function that only needs to
59
+ * stop its request when the task is aborted takes `WithSignal`:
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * const search = (query: string, { signal }: WithSignal) => fetch(`/search?q=${query}`, { signal });
64
+ * ```
65
+ */
66
+ type WithSignal = {
67
+ signal: AbortSignal;
68
+ };
56
69
  type HandlerRef<E extends EventMap, Response = unknown> = (action: Action<E>) => Response;
57
70
  type Handler<E extends EventMap, Response = unknown, K extends EventKey<E> = EventKey<E>> = { [P in K]: {
58
71
  type: P;
@@ -256,12 +269,8 @@ type BusOptions<E extends EventMap> = {
256
269
  declare const createBus: <Events extends EventMap>(options?: BusOptions<Events>) => Bus<Events>;
257
270
  //#endregion
258
271
  //#region src/core/effect-types.d.ts
259
- /** What an effect needs of the task it runs for: its signal. A task context fits, and so does `{ signal }`. */
260
- type Signaled = {
261
- signal: AbortSignal;
262
- };
263
272
  /** An effect: a function whose first parameter takes `{ signal }`, like `retry(ctx, fn)`. */
264
- type SignalFirst = (ctx: Signaled, ...args: never[]) => unknown;
273
+ type SignalFirst = (ctx: WithSignal, ...args: never[]) => unknown;
265
274
  /** The effects of a scope or of `effectsPlugin`, by the name they get on the context. */
266
275
  type Effects = Record<string, SignalFirst>;
267
276
  declare const bound: unique symbol;
@@ -277,7 +286,7 @@ declare const bound: unique symbol;
277
286
  * const first: Effect<First> = (ctx, items) => items[0];
278
287
  * ```
279
288
  */
280
- type Effect<F extends (...args: never[]) => unknown> = ((ctx: Signaled, ...args: Parameters<F>) => ReturnType<F>) & {
289
+ type Effect<F extends (...args: never[]) => unknown> = ((ctx: WithSignal, ...args: Parameters<F>) => ReturnType<F>) & {
281
290
  readonly [bound]?: F;
282
291
  };
283
292
  /** The bound form of a plain effect: the first parameter is gone, and so are its generics. */
@@ -378,4 +387,4 @@ interface ActionCreator<P = void, T extends string = string, A extends unknown[]
378
387
  toString(): T;
379
388
  }
380
389
  //#endregion
381
- export { On as A, EventKey as C, Handler as D, EventsOf as E, Keys as F, PayloadOf as I, Task as L, WithType as M, createBus as N, HandlerRef as O, EventMap as P, UnsubscribeCallback as R, Emittable as S, EventPayload as T, Commands as _, Scope as a, ContextOption as b, BoundForm as c, Action as d, AnyEvent as f, CommandCall as g, Command as h, WithPayload as i, WithContext as j, Off as k, Effect as l, BusOptions as m, Free as n, WithFields as o, Bus as p, FreeActionCreator as r, WithScope as s, ActionCreator as t, Effects as u, Context as v, EventLike as w, Emit as x, ContextOf as y };
390
+ export { On as A, EventKey as C, Handler as D, EventsOf as E, EventMap as F, Keys as I, PayloadOf as L, WithSignal as M, WithType as N, HandlerRef as O, createBus as P, Task as R, Emittable as S, EventPayload as T, Commands as _, Scope as a, ContextOption as b, BoundForm as c, Action as d, AnyEvent as f, CommandCall as g, Command as h, WithPayload as i, WithContext as j, Off as k, Effect as l, BusOptions as m, Free as n, WithFields as o, Bus as p, FreeActionCreator as r, WithScope as s, ActionCreator as t, Effects as u, Context as v, EventLike as w, Emit as x, ContextOf as y, UnsubscribeCallback as z };
@@ -1,5 +1,5 @@
1
- import { C as EventKey, I as PayloadOf, L as Task, P as EventMap, R as UnsubscribeCallback, _ 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-CbFf398i.js";
2
- import { t as BoundOf } from "./plugin-C5vIjxhR.js";
1
+ import { C as EventKey, F as EventMap, L as PayloadOf, R as Task, _ 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, z as UnsubscribeCallback } from "./action-creator-D2CotugT.js";
2
+ import { t as BoundOf } from "./plugin-C6KA4dX-.js";
3
3
  import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
4
4
  //#region src/scope/types.d.ts
5
5
  /**
@@ -341,12 +341,12 @@ type Registry<E extends EventMap> = ReturnType<ReturnType<typeof commandsPlugin<
341
341
  * of what `modulePlugin` returns that `devPlugin` wraps.
342
342
  */
343
343
  interface ModuleHost {
344
- mount(module: {
344
+ mount(...modules: Array<{
345
345
  readonly name: string;
346
- }): UnsubscribeCallback;
347
- unmount(module: {
346
+ }>): UnsubscribeCallback;
347
+ unmount(...modules: Array<{
348
348
  readonly name: string;
349
- }): void;
349
+ }>): void;
350
350
  isMounted(module: {
351
351
  readonly name: string;
352
352
  }): boolean;
@@ -359,9 +359,14 @@ interface WithApp {
359
359
  type ModuleApi<E extends EventMap = EventMap> = {
360
360
  /** The registry of command handlers of the bus: the one it was made with, or the app's own. */
361
361
  commands: Registry<E>;
362
- /** Mounts a module and returns the function that unmounts it; a module that is mounted already is not run again. */
363
- mount(module: Module<E>): UnsubscribeCallback;
364
- unmount(module: Module<E>): void;
362
+ /**
363
+ * Mounts modules, in the order given, and returns the function that unmounts them, last
364
+ * mounted first. A module that is mounted already is not run again. If one of them throws,
365
+ * the ones mounted by this call are unmounted and the error is thrown.
366
+ */
367
+ mount(...modules: Module<E>[]): UnsubscribeCallback;
368
+ /** Unmounts modules, in reverse order of the arguments. */
369
+ unmount(...modules: Module<E>[]): void;
365
370
  isMounted(module: Module<E>): boolean;
366
371
  /** Unmounts every module; a disposed app cannot mount modules. */
367
372
  dispose(): void;
@@ -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{n as a,r as o}from"./bus-BkUtjPHp.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(a(s,e,r),n)}let o=Object.assign(i,{type:e,match:r}),s=d(o,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 a(e,t){if(t)return i(o(e),t);let n=e.map(([e,t])=>i(o(e),t));return()=>{for(let e of n)e()}}a(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:a};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={},o)=>{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,a)=>{let s=i,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},g=e=>{l===`started`&&(d(),C(s,h,e,o))},_=()=>{l===`idle`&&(l=`started`,u=e(s,g),S(s,f,r,o))},v=e=>{l===`started`&&(d(),s.signal.aborted?C(s,h,s.signal.reason,o):`error`in e?S(s,m,e.error,o):S(s,p,e.value,o))};return Reflect.set(s,c,{start:_,cancel:g,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),w(()=>(a||_(),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(a(T,t,e),o)}let b=Object.assign(y,{type:t,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:h}),T=d(b,o);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};
1
+ import{c as e,i as t,l as n,n as r,s as i}from"./context-yYU3_9lq.js";import{n as a,r as o}from"./bus-BkUtjPHp.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(a(s,e,r),n)}let o=Object.assign(i,{type:e,match:r}),s=d(o,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 a(e,t){if(t)return i(o(e),t);let n=e.map(([e,t])=>i(o(e),t));return()=>{for(let e of n)e()}}a(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:a};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={},o)=>{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,a)=>{let s=i,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},g=e=>{l===`started`&&(d(),C(s,h,e,o))},_=()=>{l===`idle`&&(l=`started`,u=e(s,g),S(s,f,r,o))},v=e=>{l===`started`&&(d(),s.signal.aborted?C(s,h,s.signal.reason,o):`error`in e?S(s,m,e.error,o):S(s,p,e.value,o))};return Reflect.set(s,c,{start:_,cancel:g,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),w(()=>(a||_(),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(a(T,t,e),o)}let b=Object.assign(y,{type:t,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:h}),T=d(b,o);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)}}}}},A=e=>({mount:(...t)=>{let[n]=t;if(t.length===1&&n!==void 0)return e.mount(n);let r=[],i=()=>{for(let e of r.splice(0).reverse())e()};try{for(let n of t)e.isMounted(n)||r.push(e.mount(n))}catch(e){throw i(),e}return i},unmount:(...t)=>{for(let n of[...t].reverse())e.unmount(n)},isMounted:e.isMounted});function j(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const M=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,N=(e,t,n)=>{n&&e.applyScope?.(t,n)},P=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],F=(e,t)=>{let n=e.on;return(...e)=>{let[r,i,...a]=e;return Reflect.apply(n,null,[r,t(i),...a])}},I=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},L=(i=[])=>a=>{let o=a.commands??y()(a),s={handle:(...e)=>{if(!o.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(o.handle,null,e)}},c=n=>{let r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map;return{mount:o=>{let l=r.get(o);if(l)return l;let u=i.get(o.name);u&&r.get(u)?.();let d=t(n,o.name),f=d.ctx,p=c(f),m=()=>{r.delete(o),i.get(o.name)===o&&i.delete(o.name),f.abort(),d.unlink()};r.set(o,m),i.set(o.name,o);let h=o.scope,g=M(a,h),_=(t,n)=>{let r=Reflect.apply(s.handle,null,P(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=I(f),b=F(f,g);try{N(a,f,h),o.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)}},l=k(a),u=a.emit,d,f=(e=!0)=>{if(d)return d;l.release(),a.emit===p&&(a.emit=u);let t=r({event:{type:`app`,payload:void 0},bus:a,tracer:a.trace});d={root:t,apps:A(c(t.ctx))};try{e&&d.apps.mount(...i)}finally{l.replay()}return d},p=(...e)=>(f(),Reflect.apply(u,a,e));a.emit=p,i.length>0&&queueMicrotask(()=>{try{f()}catch(e){n(`the modules of the app could not start`,e)}});let m={get commands(){return f(),o},mount:(...e)=>{let{root:t,apps:n}=f();return t.ctx.signal.throwIfAborted(),n.mount(...e)},unmount:(...e)=>f().apps.unmount(...e),isMounted:e=>f().apps.isMounted(e),dispose:()=>f(!1).root.ctx.abort(),createModule:j};return a.app=m,m};export{_ as bindHandler,y as commandsPlugin,m as createAction,g as createCommand,T as createJob,j as createModule,b as emitterPlugin,L as modulePlugin};
@@ -1,5 +1,5 @@
1
- import { L as Task, P as EventMap, l as Effect } from "./action-creator-CbFf398i.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-C5vIjxhR.js";
1
+ import { F as EventMap, M as WithSignal, R as Task, l as Effect } from "./action-creator-D2CotugT.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-C6KA4dX-.js";
3
3
  //#region src/utils/onAbort.d.ts
4
4
  export declare const onAbort: (ctx: {
5
5
  signal?: AbortSignal | undefined;
@@ -66,9 +66,7 @@ type CancellableExecutor<T> = (handle: CancellableHandle<T>) => void;
66
66
  * );
67
67
  * ```
68
68
  */
69
- export declare const cancellable: <T = void>(ctx: {
70
- signal: AbortSignal;
71
- }, executor: CancellableExecutor<T>) => Promise<T>;
69
+ export declare const cancellable: <T = void>(ctx: WithSignal, executor: CancellableExecutor<T>) => Promise<T>;
72
70
  //#endregion
73
71
  //#region src/effects/poll.d.ts
74
72
  type PollOptions = {
@@ -108,9 +106,7 @@ type PollOptions = {
108
106
  */
109
107
  type PollMs = number | ((call: number) => number);
110
108
  export declare function poll<P, E extends EventMap, R>(ms: PollMs, task: Task<P, E, R>, options?: PollOptions): Task<P, E, Promise<void>>;
111
- export declare function poll(ctx: {
112
- signal: AbortSignal;
113
- }, fn: (tick: number) => unknown, ms: PollMs, options?: PollOptions): Promise<void>;
109
+ export declare function poll(ctx: WithSignal, fn: (tick: number) => unknown, ms: PollMs, options?: PollOptions): Promise<void>;
114
110
  //#endregion
115
111
  //#region src/effects/retry.d.ts
116
112
  type RetryOptions = {
@@ -1,2 +1,2 @@
1
- import { A as On, C as EventKey, D as Handler, E as EventsOf, F as Keys, I as PayloadOf, L as Task, M as WithType, N as createBus, O as HandlerRef, P as EventMap, R as UnsubscribeCallback, S as Emittable, T as EventPayload, _ 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 } from "./action-creator-CbFf398i.js";
2
- export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type Off, type On, type PayloadOf, type Task, type UnsubscribeCallback, type WithContext, type WithPayload, type WithType, createBus };
1
+ import { A as On, C as EventKey, D as Handler, E as EventsOf, F as EventMap, I as Keys, L as PayloadOf, M as WithSignal, N as WithType, O as HandlerRef, P as createBus, R as Task, S as Emittable, T as EventPayload, _ 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 UnsubscribeCallback } from "./action-creator-D2CotugT.js";
2
+ export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type Off, type On, type PayloadOf, type Task, type UnsubscribeCallback, type WithContext, type WithPayload, type WithSignal, type WithType, createBus };
@@ -1,4 +1,4 @@
1
- import { F as Keys, P as EventMap, c as BoundForm, j as WithContext, p as Bus, u as Effects, v as Context, y as ContextOf } from "./action-creator-CbFf398i.js";
1
+ import { F as EventMap, I as Keys, c as BoundForm, j as WithContext, p as Bus, u as Effects, v as Context, y as ContextOf } from "./action-creator-D2CotugT.js";
2
2
  //#region src/core/context-types.d.ts
3
3
  /**
4
4
  * `unknown` when none of the `fields` is already a context field of the bus,
@@ -1,4 +1,4 @@
1
- import { I as PayloadOf, L as Task, P as EventMap } from "./action-creator-CbFf398i.js";
1
+ import { F as EventMap, L as PayloadOf, R as Task } from "./action-creator-D2CotugT.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> = {
@@ -9,6 +9,14 @@ type KeyOption<P> = {
9
9
  */
10
10
  key?: (payload: P) => unknown;
11
11
  };
12
+ /** Options of a policy called without a task: `key` is left out. */
13
+ type Unkeyed<O> = Omit<O, "key"> & {
14
+ key?: undefined;
15
+ };
16
+ /** Options of a policy called without a task, with a `key` that tells the payload type. */
17
+ type Keyed<O, P> = O & {
18
+ key: (payload: P) => unknown;
19
+ };
12
20
  //#endregion
13
21
  //#region src/policies/debounce.d.ts
14
22
  type DebounceResult<R> = Promise<R | undefined> | Promise<Awaited<R> | undefined> | undefined;
@@ -18,6 +26,10 @@ type DebounceOptions<P = never> = KeyOption<P> & {
18
26
  /** Run the last call of a burst after `ms` of quiet. Default `true`. */
19
27
  trailing?: boolean;
20
28
  };
29
+ /** What `debounce(ms)` returns: it turns a task into a `debounce` task. */
30
+ type DebounceWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
31
+ /** What `debounce(ms, { key })` returns: the payload type comes from `key`. */
32
+ type KeyedDebounceWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
21
33
  /**
22
34
  * Waits for `ms` of quiet, then runs; a new call restarts the wait and drops
23
35
  * the previous. With `leading` the first call of a burst runs at once, and the
@@ -38,14 +50,24 @@ type DebounceOptions<P = never> = KeyOption<P> & {
38
50
  * bus.emit("input", "re"); // only the last call runs, 300 ms after it
39
51
  * ```
40
52
  */
41
- export declare const debounce: <P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: DebounceOptions<P>) => Task<P, E, DebounceResult<R>>;
53
+ export declare function debounce<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: DebounceOptions<P>): Task<P, E, DebounceResult<R>>;
54
+ export declare function debounce(ms: number, options?: Unkeyed<DebounceOptions>): DebounceWrapper;
55
+ export declare function debounce<P>(ms: number, options: Keyed<DebounceOptions<P>, P>): KeyedDebounceWrapper<P>;
42
56
  //#endregion
43
57
  //#region src/policies/latest.d.ts
44
58
  type LatestOptions<P = never> = KeyOption<P>;
59
+ type LatestResult<R> = R | Promise<Awaited<R> | undefined> | undefined;
60
+ /** What `latest()` returns: it turns a task into a `latest` task. */
61
+ type LatestWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
62
+ /** What `latest({ key })` returns: the payload type comes from `key`. */
63
+ type KeyedLatestWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
45
64
  /**
46
65
  * Aborts the previous run when a new call arrives; stale results are dropped.
47
66
  * With `options.key` each key has its own latest run.
48
67
  *
68
+ * Called without the task, `latest()` returns a function that takes the task,
69
+ * so `usePolicy(callback, latest())` has no `(task) => ...` to get wrong.
70
+ *
49
71
  * @example
50
72
  * ```ts
51
73
  * bus.on(
@@ -61,12 +83,21 @@ type LatestOptions<P = never> = KeyOption<P>;
61
83
  *
62
84
  * // one latest run per user instead of one for all
63
85
  * bus.on("load", latest(loadUser, { key: (user) => user.id }));
86
+ *
87
+ * // without the task: for a hook that builds the task itself
88
+ * const search = usePolicy(runSearch, latest());
64
89
  * ```
65
90
  */
66
- export declare const latest: <P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LatestOptions<P>) => Task<P, E, R | Promise<Awaited<R> | undefined> | undefined>;
91
+ export declare function latest<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LatestOptions<P>): Task<P, E, LatestResult<R>>;
92
+ export declare function latest(options?: Unkeyed<LatestOptions>): LatestWrapper;
93
+ export declare function latest<P>(options: Keyed<LatestOptions<P>, P>): KeyedLatestWrapper<P>;
67
94
  //#endregion
68
95
  //#region src/policies/leading.d.ts
69
96
  type LeadingOptions<P = never> = KeyOption<P>;
97
+ /** What `leading()` returns: it turns a task into a `leading` task. */
98
+ type LeadingWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
99
+ /** What `leading({ key })` returns: the payload type comes from `key`. */
100
+ type KeyedLeadingWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
70
101
  /**
71
102
  * Drops new calls while the task is still running. The lock is released when
72
103
  * the run settles or when its task is aborted, whichever comes first, so a
@@ -86,7 +117,9 @@ type LeadingOptions<P = never> = KeyOption<P>;
86
117
  * bus.emit("save", draft); // dropped while the first save is in flight
87
118
  * ```
88
119
  */
89
- export declare const leading: <P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LeadingOptions<P>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
120
+ export declare function leading<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LeadingOptions<P>): Task<P, E, R | Promise<Awaited<R>> | undefined>;
121
+ export declare function leading(options?: Unkeyed<LeadingOptions>): LeadingWrapper;
122
+ export declare function leading<P>(options: Keyed<LeadingOptions<P>, P>): KeyedLeadingWrapper<P>;
90
123
  //#endregion
91
124
  //#region src/policies/queue.d.ts
92
125
  type QueueOptions<P = never> = KeyOption<P> & {
@@ -101,6 +134,10 @@ type QueueOptions<P = never> = KeyOption<P> & {
101
134
  */
102
135
  overflow?: "drop-new" | "drop-old";
103
136
  };
137
+ /** What `queue()` returns: it turns a task into a `queue` task. */
138
+ type QueueWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
139
+ /** What `queue({ key })` returns: the payload type comes from `key`. */
140
+ type KeyedQueueWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
104
141
  /**
105
142
  * Runs calls one after another, in the order they arrived: a call starts when
106
143
  * fewer than `concurrency` calls are running. Every call returns a promise with
@@ -131,7 +168,9 @@ type QueueOptions<P = never> = KeyOption<P> & {
131
168
  * bus.emit("save", b); // waits for a, then runs
132
169
  * ```
133
170
  */
134
- export declare const queue: <P, E extends EventMap, R>(fn: Task<P, E, R>, options?: QueueOptions<P>) => Task<P, E, Promise<Awaited<R> | undefined>>;
171
+ export declare function queue<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: QueueOptions<P>): Task<P, E, Promise<Awaited<R> | undefined>>;
172
+ export declare function queue(options?: Unkeyed<QueueOptions>): QueueWrapper;
173
+ export declare function queue<P>(options: Keyed<QueueOptions<P>, P>): KeyedQueueWrapper<P>;
135
174
  //#endregion
136
175
  //#region src/policies/throttle.d.ts
137
176
  type ThrottleOptions<P = never> = KeyOption<P> & {
@@ -140,6 +179,10 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
140
179
  /** Run the latest call made while waiting, at the end of the window. Default `true`. */
141
180
  trailing?: boolean;
142
181
  };
182
+ /** What `throttle(ms)` returns: it turns a task into a `throttle` task. */
183
+ type ThrottleWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
184
+ /** What `throttle(ms, { key })` returns: the payload type comes from `key`. */
185
+ type KeyedThrottleWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
143
186
  /**
144
187
  * At most one run per `ms`, with the semantics of lodash `throttle` (and
145
188
  * patronum): a call made when the throttle is idle, that is, at least `ms`
@@ -166,7 +209,9 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
166
209
  * bus.emit("scroll", 30); // runs once the 200 ms window closes
167
210
  * ```
168
211
  */
169
- export declare const throttle: <P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: ThrottleOptions<P>) => Task<P, E, R | Promise<R | undefined> | undefined>;
212
+ export declare function throttle<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: ThrottleOptions<P>): Task<P, E, R | Promise<R | undefined> | undefined>;
213
+ export declare function throttle(ms: number, options?: Unkeyed<ThrottleOptions>): ThrottleWrapper;
214
+ export declare function throttle<P>(ms: number, options: Keyed<ThrottleOptions<P>, P>): KeyedThrottleWrapper<P>;
170
215
  //#endregion
171
216
  //#region src/policies/with-policy.d.ts
172
217
  /** What `withPolicy` returns: the task as a plain function that can be cancelled. */
@@ -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";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};
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,n)=>typeof e==`function`?n(e,t??{}):t=>n(t,e??{}),S=(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)}},C=(e,t)=>y(e?S(e,t):t(b));function w(e,t){return x(e,t,T)}const T=(e,t)=>C(t.key,t=>E(`latest`,(t,n,r)=>(r(),e(t,n)),t)),E=(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}}};function D(e,t,n){return x(t,n,(t,n)=>O(e,t,n))}const O=(e,t,n)=>C(n.key,r=>A(e,t,n,r)),k=(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))}},A=(e,n,r,i)=>{let a=r.leading??!1,o=r.trailing??!0;if(!o)return k(e,n,a,i);let s=!1,l=0,u=E(`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}};function j(e,t){return x(e,t,M)}const M=(e,n)=>C(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}}});function N(e,t){return x(e,t,P)}const P=(e,t)=>C(t.key,n=>F(e,t,n)),F=(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()})})};function I(e,t,n){return x(t,n,(t,n)=>L(e,t,n))}const L=(e,t,n)=>C(n.key,r=>R(e,t,n,r)),R=(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)}},z=Reflect.get(Symbol,`dispose`),B=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 z&&Reflect.set(a,z,()=>i()),a};export{D as debounce,w as latest,j as leading,N as queue,I as throttle,B as withPolicy};
@@ -1,4 +1,4 @@
1
- import { P as EventMap, p as Bus } from "./action-creator-CbFf398i.js";
1
+ import { F as EventMap, p as Bus } from "./action-creator-D2CotugT.js";
2
2
  import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
3
3
  //#region src/store/redux/middleware.d.ts
4
4
  /** What Redux passes through a middleware: an action is a plain object with a `type`. */
@@ -1,4 +1,4 @@
1
- import { R as EventMap, h as Bus, z as Keys } from "./action-creator-HFA_LZOH.js";
1
+ import { B as Keys, h as Bus, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
2
  //#region src/observable.d.ts
3
3
  declare global {
4
4
  interface SymbolConstructor {
package/dist/oop.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { B as ListenerOptions, R as EventMap, _ as Command, h as Bus, t as ActionCreator } from "./action-creator-HFA_LZOH.js";
1
+ import { V as ListenerOptions, _ as Command, h as Bus, t as ActionCreator, z as EventMap } from "./action-creator-Bpa8BUS9.js";
2
2
  //#region src/oop/types.d.ts
3
3
  type Slots = "ctx" | "commands" | "trace" | "addContext" | "applyScope" | "fitScope" | "app" | "use";
4
4
  type AnyFunction = (...args: never[]) => unknown;