nanoeffects 0.7.0 → 0.9.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 (76) hide show
  1. package/README.md +2 -2
  2. package/dist/{action-creator-Bd4TBZgl.d.ts → action-creator-BrZF6hXc.d.ts} +0 -4
  3. package/dist/actions.d.ts +41 -25
  4. package/dist/actions.js +1 -1
  5. package/dist/bus-CkTHUAvq.js +1 -0
  6. package/dist/context-COgyNlrC.js +1 -0
  7. package/dist/{context-plugin-BVNKqkIx.d.ts → context-plugin--HkNN1ha.d.ts} +1 -1
  8. package/dist/dev.d.ts +9 -8
  9. package/dist/dev.js +1 -1
  10. package/dist/effects.d.ts +2 -2
  11. package/dist/effects.js +1 -1
  12. package/dist/index.d.ts +2 -2
  13. package/dist/index.js +1 -1
  14. package/dist/match.d.ts +50 -0
  15. package/dist/match.js +1 -0
  16. package/dist/{module-DZ5ngUrM.d.ts → module-DqKegXHt.d.ts} +43 -18
  17. package/dist/notrace/{action-creator-D-y_-xRS.d.ts → action-creator-DHAEgwuh.d.ts} +0 -4
  18. package/dist/notrace/actions.d.ts +90 -39
  19. package/dist/notrace/actions.js +1 -1
  20. package/dist/notrace/bus-BmCTo8BG.js +1 -0
  21. package/dist/notrace/{context-plugin-DDfsaErA.d.ts → context-plugin-BapQkGwQ.d.ts} +1 -1
  22. package/dist/notrace/context-yYU3_9lq.js +1 -0
  23. package/dist/notrace/{delay-B_W1tnkS.js → delay-BosJKFjg.js} +1 -1
  24. package/dist/notrace/effects.d.ts +2 -2
  25. package/dist/notrace/effects.js +1 -1
  26. package/dist/notrace/index.d.ts +2 -2
  27. package/dist/notrace/index.js +1 -1
  28. package/dist/notrace/{plugin-CDAngHwg.d.ts → plugin-Cxadz6uU.d.ts} +2 -2
  29. package/dist/notrace/policies.d.ts +30 -2
  30. package/dist/notrace/policies.js +1 -1
  31. package/dist/notrace/redux.d.ts +1 -1
  32. package/dist/observable.d.ts +1 -1
  33. package/dist/oop.d.ts +131 -0
  34. package/dist/oop.js +1 -0
  35. package/dist/{plugin-DQNPBcxx.d.ts → plugin-DkXCvsmB.d.ts} +2 -2
  36. package/dist/policies.d.ts +3 -2
  37. package/dist/policies.js +1 -1
  38. package/dist/react.d.ts +33 -1
  39. package/dist/react.js +1 -1
  40. package/dist/redux.d.ts +1 -1
  41. package/dist/scope.d.ts +5 -6
  42. package/dist/scope.js +1 -1
  43. package/dist/signal.d.ts +51 -0
  44. package/dist/signal.js +1 -0
  45. package/dist/trace.d.ts +1 -1
  46. package/dist/{types-BJ0fzV1p.d.ts → types-KavbvHix.d.ts} +13 -3
  47. package/dist/with-policy-CqUS-VWW.d.ts +30 -0
  48. package/dist/with-policy-DBV9dh2F.js +1 -0
  49. package/docs/README.md +7 -0
  50. package/docs/actions.md +152 -39
  51. package/docs/cancel-stale-requests.md +111 -0
  52. package/docs/debounce-and-abort.md +106 -0
  53. package/docs/guide.md +143 -19
  54. package/docs/hmr.md +6 -5
  55. package/docs/migrating-from-effector.md +51 -18
  56. package/docs/migrating-from-mitt.md +97 -0
  57. package/docs/migrating-from-rxjs.md +17 -6
  58. package/docs/motivation.md +41 -0
  59. package/docs/observable.md +124 -10
  60. package/docs/oop.md +286 -0
  61. package/docs/package.md +6 -2
  62. package/docs/plugins.md +247 -51
  63. package/docs/policies.md +33 -0
  64. package/docs/prevent-double-submit.md +128 -0
  65. package/docs/query-libraries.md +124 -0
  66. package/docs/react.md +61 -5
  67. package/docs/scope.md +60 -22
  68. package/docs/stores.md +263 -109
  69. package/llms-full.txt +2153 -355
  70. package/llms.txt +10 -3
  71. package/package.json +43 -4
  72. package/skills/nanoeffects/SKILL.md +52 -0
  73. package/dist/bus-BAs9x7IQ.js +0 -1
  74. package/dist/context-CK0ZomWt.js +0 -1
  75. package/dist/notrace/bus-Bdk0YyFQ.js +0 -1
  76. package/dist/notrace/context-CctVmHLC.js +0 -1
@@ -1,6 +1,16 @@
1
- import { B as PayloadOf, C as EventKey, H as Task, K as UnsubscribeCallback, R as EventMap, 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-D-y_-xRS.js";
2
- import { t as BoundOf } from "./plugin-CDAngHwg.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-DHAEgwuh.js";
2
+ import { t as BoundOf } from "./plugin-Cxadz6uU.js";
3
3
  //#region src/scope/types.d.ts
4
+ /**
5
+ * The slots `scopePlugin` fills for `modulePlugin`. They are not on `Bus`: a bus
6
+ * has them in its type once the plugin returned it.
7
+ */
8
+ interface ScopeSlots {
9
+ /** Puts the fields of a module's scope on the context of one of its tasks. Without it a module's scope is ignored. */
10
+ applyScope?: ((ctx: object, scope: Scope) => void) | undefined;
11
+ /** Wraps a handler so it runs with the fields of a module's scope. */
12
+ fitScope?: ((scope: Scope, fn: unknown) => unknown) | undefined;
13
+ }
4
14
  type BoundEffects<E extends EventMap, S extends Scope> = S extends {
5
15
  $effects: infer H extends Effects;
6
16
  } ? BoundOf<H, E> : unknown;
@@ -120,13 +130,21 @@ type Binding<E extends EventMap = EventMap> = readonly [Command<never, unknown>
120
130
  * ```
121
131
  */
122
132
  export declare const bindHandler: <P, R, E extends EventMap>(command: Command<P, R>, fn: Task<P, E, R>) => Binding<E>;
133
+ /** The registry of handlers that `commandsPlugin` returns: `run` for the bus, `handle` to bind more. */
134
+ interface Registry$1<E extends EventMap> extends Commands {
135
+ handle<P, R>(command: Command<P, R>, fn: Task<P, E, R>): UnsubscribeCallback;
136
+ handle(type: string, fn: Task<never, E, unknown>): UnsubscribeCallback;
137
+ handle(bindings: readonly Binding<E>[]): UnsubscribeCallback;
138
+ }
123
139
  /**
124
140
  * The plugin that gives a bus the handlers bound to commands:
125
141
  * `bus.use(commandsPlugin(bindings))` makes the registry, sets it as the
126
142
  * commands of the bus and returns it. Each bus has its own, so each test can
127
143
  * have its own implementation. `handle` binds more later and returns the
128
- * function that unbinds. A second `commandsPlugin` on the same bus replaces
129
- * the registry of that bus.
144
+ * function that unbinds. A second `commandsPlugin` on the same bus binds its
145
+ * list in the registry that is there and returns it, so the order of the
146
+ * plugins does not matter. A `commands` that is not such a registry (a custom
147
+ * runner) is replaced.
130
148
  *
131
149
  * What the bus runs for a command: the handler bound last, else the one the
132
150
  * command was created with, else it throws `No handler for command "..."`.
@@ -140,14 +158,7 @@ export declare const bindHandler: <P, R, E extends EventMap>(command: Command<P,
140
158
  * unbind(); // the command's own handler is back
141
159
  * ```
142
160
  */
143
- export declare const commandsPlugin: <E extends EventMap>(bindings?: readonly Binding<E>[]) => (bus: Bus<E>) => {
144
- run: (command: Command, payload: unknown, ctx: unknown) => unknown;
145
- handle: {
146
- <P, R>(command: Command<P, R>, fn: Task<P, E, R>): UnsubscribeCallback;
147
- (type: string, fn: Task<never, E, unknown>): UnsubscribeCallback;
148
- (bindings: readonly Binding<E>[]): UnsubscribeCallback;
149
- };
150
- };
161
+ export declare const commandsPlugin: <E extends EventMap>(bindings?: readonly Binding<E>[]) => (bus: Bus<E>) => Registry$1<E>;
151
162
  //#endregion
152
163
  //#region src/actions/emitter.d.ts
153
164
  /** What `emitterPlugin` adds to the bus. */
@@ -193,56 +204,72 @@ type Creator = {
193
204
  readonly type: string;
194
205
  };
195
206
  type Outcome = string | Creator;
196
- /** What `createJob` returns: the command of the job, with its two actions as properties. */
197
- type Job<P, R, T extends string, S extends Outcome, F extends Outcome> = MadeCommand<P, Promise<Awaited<R> | undefined>, T> & {
207
+ /** What `createJob` returns: the command of the job, with its four actions as properties. */
208
+ type Job<P, R, T extends string, S extends Outcome, F extends Outcome, PE extends Outcome = `${T}/pending`, CE extends Outcome = `${T}/cancel`> = MadeCommand<P, Promise<Awaited<R> | undefined>, T> & {
209
+ /** Made from a string with the payload of the call as its payload, or the creator that was given. */
210
+ readonly pending: PE extends string ? ActionCreator<P, PE> : PE;
198
211
  /** Made from a string with the result as its payload, or the creator that was given. */
199
212
  readonly success: S extends string ? ActionCreator<Awaited<R>, S> : S;
200
213
  /** Made from a string with the error as its payload, or the creator that was given. */
201
214
  readonly fail: F extends string ? ActionCreator<unknown, F> : F;
215
+ /** Made from a string with the abort reason as its payload, or the creator that was given. */
216
+ readonly cancel: CE extends string ? ActionCreator<unknown, CE> : CE;
202
217
  };
203
218
  /**
204
219
  * Creates a job: a command whose outcome is sent on as actions. When the
205
- * handler succeeds, `job.success(result)` is emitted on the bus, and when it
206
- * fails, `job.fail(error)`. It is made for the case where nobody waits for
207
- * the result and the bus carries it on, like `createAsyncThunk` or an effector
208
- * effect. It returns the command, with the two actions on it: `job.success` and
209
- * `job.fail`.
220
+ * handler starts, `job.pending(payload)` is emitted on the bus. When it
221
+ * succeeds, `job.success(result)` follows, and when it fails, `job.fail(error)`.
222
+ * When the task is aborted after it started, `job.cancel(reason)` follows
223
+ * instead. After `pending`, exactly one of the three closes the call. A job is
224
+ * made for the case where nobody waits for the result and the bus carries it
225
+ * on, like `createAsyncThunk` or an effector effect. It returns the command,
226
+ * with the four actions on it.
210
227
  *
211
228
  * A scope as the last argument, `{ cache, $effects: { race } }`, gives the handler
212
229
  * and the listeners of the outcome those context fields; see `scopePlugin`.
213
230
  *
214
- * `success` and `fail` are a type string, from which the job makes an action
215
- * creator with `createAction`, or an action creator that exists already, which
216
- * is used and returned as it is. A missing one is made with the type
217
- * `<type>/success` or `<type>/fail`.
231
+ * `pending`, `success`, `fail` and `cancel` are a type string, from which the
232
+ * job makes an action creator with `createAction`, or an action creator that
233
+ * exists already, which is used and returned as it is. A missing one is made
234
+ * with the type `<type>/pending`, `<type>/success`, `<type>/fail` or
235
+ * `<type>/cancel`.
218
236
  *
219
237
  * `bus.emit(job(payload))` returns a promise of the result. An error of the
220
238
  * handler always counts as handled: it goes to `job.fail` and the promise
221
239
  * resolves with `undefined`; a command is the tool for a result that must reject.
222
240
  * The actions are emitted as the task that ran the job, also for a handler bound
223
- * with `commands.handle`, and are skipped once the task is aborted. A missing
224
- * handler is not an outcome: `emit` throws `MissingHandlerError`. Calling the
225
- * job builds `{ type, payload }` like a command; it does not run the handler.
241
+ * with `commands.handle`. Once the task is aborted, `success` and `fail` are
242
+ * skipped and `cancel` is sent when the handler settles (a handler that never
243
+ * settles sends nothing). A call that does not start, because a policy drops it
244
+ * or it is aborted while it waits in a `queue`, sends none of the four. A
245
+ * missing handler is not an outcome: `emit` throws `MissingHandlerError`.
246
+ * Calling the job builds `{ type, payload }` like a command; it does not run
247
+ * the handler.
226
248
  *
227
249
  * @example
228
250
  * ```ts
229
- * const loadUser = createJob("user/load", fetchUser, {
230
- * success: "user/loaded",
231
- * fail: "user/failed",
232
- * });
251
+ * const loadUser = createJob("user/load", fetchUser);
252
+ * let loading = false;
233
253
  *
234
- * bus.on(loadUser.success, (user) => render(user));
235
- * bus.emit(loadUser("1")); // loadUser.success(user) or loadUser.fail(error) comes later
254
+ * bus.on(loadUser.pending, () => { loading = true; });
255
+ * bus.on(loadUser.success, (user) => { loading = false; render(user); });
256
+ * bus.on(loadUser.fail, () => { loading = false; });
257
+ * bus.on(loadUser.cancel, () => { loading = false; });
258
+ * bus.emit(loadUser("1")); // pending now, then success, fail or cancel
236
259
  * ```
237
260
  */
238
- export declare const createJob: <P = void, R = void, T extends string = string, E extends EventMap = EventMap, S extends Outcome = `${T}/success`, F extends Outcome = `${T}/fail`, X extends Scope = Record<never, never>>(type: T, handler?: Task<P, Scoped<E, X>, R>, outcomes?: {
261
+ export declare const createJob: <P = void, R = void, T extends string = string, E extends EventMap = EventMap, S extends Outcome = `${T}/success`, F extends Outcome = `${T}/fail`, PE extends Outcome = `${T}/pending`, CE extends Outcome = `${T}/cancel`, X extends Scope = Record<never, never>>(type: T, handler?: Task<P, Scoped<E, X>, R>, outcomes?: {
262
+ /** Gets the payload of the call when the handler starts: a type to make an action creator of, or an action creator. */
263
+ pending?: PE;
239
264
  /** Gets the result when the handler succeeds: a type to make an action creator of, or an action creator. */
240
265
  success?: S;
241
266
  /** Gets the error when the handler fails: a type to make an action creator of, or an action creator. */
242
267
  fail?: F;
268
+ /** Gets the abort reason when the task is aborted before the handler settles: a type to make an action creator of, or an action creator. */
269
+ cancel?: CE;
243
270
  },
244
271
  /** Context values and `$effects` for the handler and for the listeners of the outcome; see `scopePlugin`. */
245
- scope?: X) => CarryingCalls<Job<P, R, T, S, F>, P, Promise<Awaited<R> | undefined>, FieldsOfScope<E, X>>;
272
+ scope?: X) => CarryingCalls<Job<P, R, T, S, F, PE, CE>, P, Promise<Awaited<R> | undefined>, FieldsOfScope<E, X>>;
246
273
  //#endregion
247
274
  //#region src/actions/module.d.ts
248
275
  /**
@@ -302,6 +329,25 @@ type Module<E extends EventMap = EventMap> = {
302
329
  export declare function createModule<E extends EventMap = EventMap>(name: string, setup: (m: ModuleContext<E>) => void): Module<E>;
303
330
  export declare function createModule<E extends EventMap, S extends Scope = Record<never, never>>(name: string, setup: (m: ModuleContext<Scoped<E, S>>) => void, scope: S): Module<E>;
304
331
  type Registry<E extends EventMap> = ReturnType<ReturnType<typeof commandsPlugin<E>>>;
332
+ /**
333
+ * The modules of an app as the plugins that report on them see them: the part
334
+ * of what `modulePlugin` returns that `devPlugin` wraps.
335
+ */
336
+ interface ModuleHost {
337
+ mount(module: {
338
+ readonly name: string;
339
+ }): UnsubscribeCallback;
340
+ unmount(module: {
341
+ readonly name: string;
342
+ }): void;
343
+ isMounted(module: {
344
+ readonly name: string;
345
+ }): boolean;
346
+ }
347
+ /** The slot `modulePlugin` fills; a bus has it in its type once the plugin returned it. */
348
+ interface WithApp {
349
+ app?: ModuleHost | undefined;
350
+ }
305
351
  /** What `modulePlugin` returns. */
306
352
  type ModuleApi<E extends EventMap = EventMap> = {
307
353
  /** The registry of command handlers of the bus: the one it was made with, or the app's own. */
@@ -322,10 +368,15 @@ type ModuleApi<E extends EventMap = EventMap> = {
322
368
  };
323
369
  /**
324
370
  * The plugin that assembles an app from modules: `bus.use(modulePlugin(modules))`
325
- * mounts `modules` at once, in the order given, and returns `mount`, `unmount`,
326
- * `isMounted`, `dispose` and `commands` for the modules that come and go later. The bus runs the commands that the
327
- * modules bind: if `bus.use(commandsPlugin())` was called before, that registry is
328
- * used, otherwise the app attaches its own, as `app.commands`.
371
+ * returns `mount`, `unmount`, `isMounted`, `dispose` and `commands` for the
372
+ * modules that come and go, and mounts `modules`, in the order given, on the
373
+ * first use of the app or of the bus: the first `bus.emit`, or the first call
374
+ * of one of its methods. By then every plugin is on the bus, so the plugins can
375
+ * be used in any order. The `on`, `once` and `off` calls made in between are
376
+ * held and made right after the modules, so the listeners of a module stay
377
+ * ahead of them. The bus runs the commands that the modules bind: the registry
378
+ * that the bus has is used (a later `commandsPlugin` binds in it), otherwise the
379
+ * app attaches its own, as `app.commands`. The app is also `bus.app`.
329
380
  *
330
381
  * A module is known by its `name`: mounting a module whose name is taken
331
382
  * replaces the mounted one, which is unmounted first. That is what hot reload
@@ -354,6 +405,6 @@ type ModuleApi<E extends EventMap = EventMap> = {
354
405
  * app.dispose();
355
406
  * ```
356
407
  */
357
- export declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E>) => ModuleApi<E>;
408
+ export declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E> & ScopeSlots & WithApp) => ModuleApi<E>;
358
409
  //#endregion
359
410
  export type { ActionCreator, Binding, EmitterApi, FreeActionCreator, Job, Module, ModuleApi, ModuleContext };
@@ -1 +1 @@
1
- import{i as e,n as t,s as n}from"./context-CctVmHLC.js";import{a as r,r as i}from"./bus-Bdk0YyFQ.js";const a=Symbol.for(`nanoeffects.scope`),o=(e,t)=>(t&&Object.defineProperty(e,a,{value:t}),e),s=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),c=e=>Reflect.get(e,a);function l(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(o((...t)=>o({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var u=class extends Error{};function d(e,t,n){let r=t=>t.type===e;function a(r,a){if(typeof a==`object`){if(!t)throw new u(`No handler for command "${e}"`);return t(r,a)}return o(i(c,e,r),n)}let s=Object.assign(a,{type:e,match:r}),c=o(s,n);return c}const f=(e,t)=>[e,t],p=(e=[])=>t=>{let n=/* @__PURE__ */ new Map,i=(e,t)=>{let r={fn:t};return n.set(e,[...n.get(e)??[],r]),()=>{let t=(n.get(e)??[]).filter(e=>e!==r);t.length>0?n.set(e,t):n.delete(e)}};function a(e,t){if(t)return i(r(e),t);let n=e.map(([e,t])=>i(r(e),t));return()=>{for(let e of n)e()}}a(e);let o={run:(e,t,r)=>{let i=(n.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,r])},handle:a};return t.commands=o,o},m=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},h=e=>typeof e==`string`?l(e):e,g=(e,t,n,r)=>{let i=c(t);e.emit(Reflect.apply(t,null,[n]),r&&i?s(r,i):r)},_=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof u)throw e;return Promise.reject(e)}},v=(e,t,n={},r)=>{let a=d(e,t),s=h(n.success??`${e}/success`),c=h(n.fail??`${e}/fail`),l=(e,t,n)=>{let i=n;return _(()=>Reflect.apply(e,null,[t,n])).then(e=>(g(i,s,e,r),e),e=>{g(i,c,e,r)})},u=e=>(t,n)=>l(e,t,n);function f(t,n){return typeof n==`object`?l(a,t,n):o(i(m,e,t),r)}let p=Object.assign(f,{type:e,match:a.match,wrap:u,success:s,fail:c}),m=o(p,r);return m};function y(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const b=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,x=(e,t,n)=>{n&&e.applyScope?.(t,n)},S=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],C=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},w=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},T=(r=[])=>i=>{let a=i.commands??p()(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=t({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace}),c=t=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:s=>{let l=r.get(s);if(l)return l;let u=a.get(s.name);u&&r.get(u)?.();let d=e(t,s.name),f=d.ctx,p=c(f),m=()=>{r.delete(s),a.get(s.name)===s&&a.delete(s.name),f.abort(),d.unlink()};r.set(s,m),a.set(s.name,s);let h=s.scope,g=b(i,h),_=(e,t)=>{let r=Reflect.apply(o.handle,null,S(g,e,t));return n(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=w(f),T=C(f,g);try{x(i,f,h),s.setup(Object.assign(f,{handle:_,use:v,spawn:y,on:T}))}catch(e){throw m(),e}return m},unmount:e=>r.get(e)?.(),isMounted:e=>r.has(e)}},l=c(s.ctx);for(let e of r)l.mount(e);return{commands:a,mount:e=>(s.ctx.signal.throwIfAborted(),l.mount(e)),unmount:l.unmount,isMounted:l.isMounted,dispose:()=>s.ctx.abort(),createModule:y}};export{f as bindHandler,p as commandsPlugin,l as createAction,d as createCommand,v as createJob,y as createModule,m as emitterPlugin,T 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-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};
@@ -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:()=>{for(let t of Reflect.ownKeys(e))delete e[t];for(let e of Reflect.ownKeys(t))delete t[e]}}},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-D-y_-xRS.js";
1
+ import { R as EventMap, j as WithContext, p as Bus, y as ContextOf } from "./action-creator-DHAEgwuh.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 = {
@@ -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();let s;const c=()=>s,l=()=>{},u=(e,t,n,r)=>{},d=(e,t)=>l,f=(e,t,n)=>n(),p=(e,t)=>typeof e==`function`?e(t):e,m=({event:e,bus:t,tracer:n,parent:r})=>{let{controller:c,unlink:u}=a(r?.signal),{signal:d}=c,h=(...e)=>{if(d.aborted&&!v[o])return;let n=t?.emit,r=s;s=void 0;try{return n?.(...e)}finally{s=r}},g=(...e)=>{if(d.aborted)return l;let n=t?.on,r=n?.(...e);if(!r)return l;let a=i({signal:d},r);return()=>{a(),r()}},_=async(r,...i)=>{d.throwIfAborted();let a=i[0],o=m({event:{type:e.type,payload:a},bus:t,tracer:n,parent:{signal:d,ref:void 0}});try{return await f(o.ctx,`run`,()=>r(a,o.ctx))}finally{o.unlink()}},v={...p(t?.ctx,e),type:e.type,payload:e.payload,signal:d,abort:e=>c.abort(e),emit:h,on:g,off:t?.off??l,run:_};return t?.addContext?.(v),{ctx:v,unlink:u}},h=(e,t)=>m({event:{type:t??e.type??``,payload:e.payload},bus:{emit:e.emit,on:e.on,off:e.off,ctx:e},tracer:void 0,parent:{signal:e.signal,ref:void 0}});export{f as a,i as c,h as i,e as l,m as n,u as o,c as r,o as s,d as t};
@@ -1 +1 @@
1
- import{s as e}from"./context-CctVmHLC.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{c as e}from"./context-yYU3_9lq.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,5 +1,5 @@
1
- import { H as Task, R as EventMap, l as Effect } from "./action-creator-D-y_-xRS.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-CDAngHwg.js";
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";
3
3
  //#region src/utils/onAbort.d.ts
4
4
  export declare const onAbort: (ctx: {
5
5
  signal?: AbortSignal | undefined;
@@ -1 +1 @@
1
- import{a as e,c as t,i as n,o as r,s as i,t as a}from"./context-CctVmHLC.js";import{n as o,r as s,t as c}from"./delay-B_W1tnkS.js";const l=(e,t)=>t?.signal?new Promise((n,r)=>{let a=i(t,r);e.then(n,r).finally(a)}):e,u=async(e,t,o,s)=>{t?.signal?.throwIfAborted();let{ctx:c,unlink:u}=n(t??{}),d=a(c,e);r(c,e,`start`),i(c,()=>d(`cancel`));try{let e=await l(o(c),t);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(t,r)=>{let{ctx:i,unlink:a}=n(t);try{return await e(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=(t,r)=>{let i=Object.entries(r).map(([r,i])=>{let a=n(t),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:r,task:new Promise(t=>t(e(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=(e,n)=>e.signal.aborted?Promise.reject(e.signal.reason):new Promise((r,a)=>{let o=[],s=`pending`,c=e=>{try{e()}catch(e){t(`cancel cleanup failed`,e)}},l=i(e,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{n({resolve:u(r),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)Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,t,n,r)=>{if(e.signal.aborted)return Promise.resolve();let a=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)})};i(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,t,n={}){return u(`take`,e,r=>new Promise((a,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(),a(e))})),c.push(i(r,u)),c.push(i({signal:n.signal},u))}))}const M=(t,r,i)=>u(`timeout`,t,async t=>{let a=n(t),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${r}ms`,`TimeoutError`)),r);try{return await l(Promise.resolve(e(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,i as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
1
+ import{a as e,c as t,i as n,l 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";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,r,o,s)=>{r?.signal?.throwIfAborted();let{ctx:c,unlink:u}=n(r??{}),d=a(c,e);i(c,e,`start`),t(c,()=>d(`cancel`));try{let e=await l(o(c),r);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(t,r)=>{let{ctx:i,unlink:a}=n(t);try{return await e(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=(t,r)=>{let i=Object.entries(r).map(([r,i])=>{let a=n(t),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:r,task:new Promise(t=>t(e(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=(e,n)=>e.signal.aborted?Promise.reject(e.signal.reason):new Promise((i,a)=>{let o=[],s=`pending`,c=e=>{try{e()}catch(e){r(`cancel cleanup failed`,e)}},l=t(e,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{n({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=(t,r,i)=>u(`timeout`,t,async t=>{let a=n(t),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${r}ms`,`TimeoutError`)),r);try{return await l(Promise.resolve(e(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,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-D-y_-xRS.js";
2
- import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin-DDfsaErA.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-DHAEgwuh.js";
2
+ import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin-BapQkGwQ.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-Bdk0YyFQ.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-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,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-D-y_-xRS.js";
2
- import { n as NoClash } from "./context-plugin-DDfsaErA.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-DHAEgwuh.js";
2
+ import { n as NoClash } from "./context-plugin-BapQkGwQ.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 { H as Task, R as EventMap } from "./action-creator-D-y_-xRS.js";
1
+ import { B as PayloadOf, H as Task, R as EventMap } from "./action-creator-DHAEgwuh.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> = {
@@ -168,4 +168,32 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
168
168
  */
169
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>;
170
170
  //#endregion
171
- export type { DebounceOptions, LatestOptions, LeadingOptions, QueueOptions, ThrottleOptions };
171
+ //#region src/policies/with-policy.d.ts
172
+ /** What `withPolicy` returns: the task as a plain function that can be cancelled. */
173
+ type PolicyFunction<P, R> = {
174
+ (...args: PayloadOf<P>): Promise<Awaited<R> | undefined>;
175
+ /** Aborts every call that has not finished. Calls made after it work as before. */
176
+ cancel: (reason?: unknown) => void;
177
+ /** Same as `cancel`; present where `Symbol.dispose` exists, for `using`. */
178
+ [Symbol.dispose]?: () => void;
179
+ };
180
+ /**
181
+ * Makes a policy (or any task) a plain function that needs no bus and no
182
+ * context. The returned function keeps the state of the policy between calls,
183
+ * so a debounced one waits across calls. `cancel()` aborts what is pending or
184
+ * running; calls after it work again. The callback keeps the `(payload, ctx)`
185
+ * shape; `ctx.signal`, `ctx.abort` and `ctx.run` work, `ctx.emit` and `ctx.on`
186
+ * do nothing because there is no bus.
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * const onChange = withPolicy(debounce(300, (query: string) => search(query)));
191
+ *
192
+ * onChange("re");
193
+ * onChange("redux"); // only this one runs, 300 ms after it
194
+ * onChange.cancel(); // or nothing runs at all
195
+ * ```
196
+ */
197
+ export declare const withPolicy: <P, R>(task: Task<P, EventMap, R>) => PolicyFunction<P, R>;
198
+ //#endregion
199
+ export type { DebounceOptions, LatestOptions, LeadingOptions, PolicyFunction, QueueOptions, ThrottleOptions };
@@ -1 +1 @@
1
- import{a as e,i as t,o as n,s as r,t as i}from"./context-CctVmHLC.js";import{n as a,r as o,t as s}from"./delay-B_W1tnkS.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";const u=()=>{},d=(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)}},f=(e,t)=>e?d(e,t):t(u),p=(e,t={})=>f(t.key,t=>m(`latest`,(t,n,r)=>(r(),e(t,n)),t)),m=(e,a,o=u)=>{let s=null;return(l,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=t(u);s=d;let p=i(d,e),m=!1,h=()=>{d.signal.aborted||(m=!0,n(d,e,`start`))};r(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=a(l,d,h);return c(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}}},h=(e,t,n={})=>f(n.key,r=>_(e,t,n,r)),g=(t,r,i,s)=>{let c=s!==u,l=null,d=null;return(u,f)=>{if(f.signal.aborted)return;let p=Date.now(),m=l===null||p-l>=t||p-l<0;return l=p,d!==null&&a(d),d=c?o(s,t):null,i&&m?Promise.resolve(e(f,`debounce`,()=>r(u,f))):(n(f,`debounce`,`drop`),Promise.resolve(void 0))}},_=(t,n,i,a)=>{let o=i.leading??!1,c=i.trailing??!0;if(!c)return g(t,n,o,a);let l=!1,u=0,d=m(`debounce`,([e,i],a,o)=>{let d=u;return r(a,()=>{u===d&&(l=!1)}),s(a,t).then(()=>{if(l=!1,c&&!i)return o(),n(e,a)})},a);return(t,r)=>{if(r.signal.aborted)return;let i=o&&!l;l=!0,u++;let a=d([t,i],r);return i?Promise.resolve(e(r,`debounce`,()=>n(t,r))):a}},v=(t,i={})=>f(i.key,i=>{let a=null;return(o,s)=>{if(s.signal.aborted)return;if(a){n(s,`leading`,`drop`);return}let c={};a=c;let u=()=>{d(),a===c&&(a=null,i())},d=r(s,u);try{return l(e(s,`leading`,()=>t(o,s)),u)}catch(e){throw u(),e}}}),y=(e,t={})=>f(t.key,n=>b(e,t,n)),b=(t,i,a)=>{let o=i.concurrency??1,s=i.limit??1/0,c=[],l=0,d=()=>{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=u,_=()=>{g(),n(p,`queue`,`drop`),m(void 0)},v=()=>{if(g(),p.signal.aborted){_();return}l++;let n=!0,i=()=>{n&&(n=!1,l--,d(),l===0&&c.length===0&&a())},o=r(p,()=>{i(),m(void 0)}),s;try{s=Promise.resolve(e(p,`queue`,()=>t(f,p)))}catch(e){s=Promise.reject(e)}let u=()=>{o(),i()};s.then(e=>{u(),m(p.signal.aborted?void 0:e)},e=>{u(),p.signal.aborted?m(void 0):h(e)})};if(l<o){v();return}if(c.length>=s){if(i.overflow!==`drop-old`||s<=0){_();return}c.shift()?.drop()}let y={start:v,drop:_};c.push(y),g=r(p,()=>{let e=c.indexOf(y);e>=0&&c.splice(e,1),_()})})},x=(e,t,n={})=>f(n.key,r=>S(e,t,n,r)),S=(t,r,i,s)=>{let c=i.leading??!0,l=i.trailing??!0,d=s!==u,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=>n(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,n,i)=>{b(),p=i,_=!0;try{return e(n,`throttle`,()=>r(t,n))}finally{_=!1}},C=()=>{d&&(h=o(w,t))},w=()=>{if(h=null,!d||m!==null||g)return;let e=Date.now(),n=Math.max(t-(e-(f??-1/0)),t-(e-p));n<=0?s():h=o(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=o(E,Math.min(t-(e-(f??e)),t-(e-p)))},D=()=>{h!==null&&(a(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?(a(m),m=o(E,t),S(e,n,r)):m===null&&(m=o(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)}};export{h as debounce,p as latest,v as leading,y as queue,x as throttle};
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,4 +1,4 @@
1
- import { R as EventMap, p as Bus } from "./action-creator-D-y_-xRS.js";
1
+ import { R as EventMap, p as Bus } from "./action-creator-DHAEgwuh.js";
2
2
  //#region src/store/redux/middleware.d.ts
3
3
  /** What Redux passes through a middleware: an action is a plain object with a `type`. */
4
4
  type ReduxAction = {
@@ -1,4 +1,4 @@
1
- import { B as EventMap, V as Keys, h as Bus } from "./action-creator-Bd4TBZgl.js";
1
+ import { B as EventMap, V as Keys, h as Bus } from "./action-creator-BrZF6hXc.js";
2
2
  //#region src/observable.d.ts
3
3
  declare global {
4
4
  interface SymbolConstructor {
package/dist/oop.d.ts ADDED
@@ -0,0 +1,131 @@
1
+ import { B as EventMap, _ as Command, h as Bus, t as ActionCreator } from "./action-creator-BrZF6hXc.js";
2
+ //#region src/oop/types.d.ts
3
+ type Slots = "ctx" | "commands" | "trace" | "addContext" | "applyScope" | "fitScope" | "app" | "use";
4
+ type AnyFunction = (...args: never[]) => unknown;
5
+ /**
6
+ * The methods an instance of a class from `extendBus` gets: the function
7
+ * members of the bus (`on`, `once`, `off`, `emit`, and those of the plugins used
8
+ * before `extendBus`), `dispose`, and the bus itself as `bus`. A member of the
9
+ * class with the same name is kept and not replaced.
10
+ */
11
+ type BusMethods<B> = { [K in keyof B as K extends Slots ? never : B[K] extends AnyFunction ? K : never]: B[K]; } & {
12
+ /** Removes every subscription made through `on` and `once` of this instance. */
13
+ dispose(): void;
14
+ [Symbol.dispose](): void;
15
+ readonly bus: B;
16
+ };
17
+ /** Any class that `extendBus` can extend. */
18
+ type Constructor = new (...args: never[]) => object;
19
+ /**
20
+ * The class `extendBus` returns: it takes the arguments of `C`, its instances
21
+ * are instances of `C` with the methods of `B` that `C` does not have, and its
22
+ * static members are those of `C` plus the bus.
23
+ */
24
+ type Extended<C extends Constructor, B> = {
25
+ new (...args: ConstructorParameters<C>): InstanceType<C> & Omit<BusMethods<B>, keyof InstanceType<C>>;
26
+ readonly bus: B;
27
+ readonly prototype: InstanceType<C>;
28
+ } & Omit<C, "prototype">;
29
+ /** A wrapper for the handler of `@On` and `@Once`, such as `latest`. */
30
+ type Wrap = (handler: (payload: unknown, ctx: unknown) => unknown) => unknown;
31
+ type Handler<P> = (payload: P, ctx: never) => unknown;
32
+ /**
33
+ * What `@On(event)` and `@Once(event)` return: a method decorator of the
34
+ * language and of `experimentalDecorators`. The payload of the method is
35
+ * checked against the event when the event is an action creator or a command.
36
+ */
37
+ interface MethodDecorator<P> {
38
+ (value: Handler<P>, context: ClassMethodDecoratorContext): void;
39
+ (target: object, key: string | symbol, descriptor: TypedPropertyDescriptor<Handler<P>>): void;
40
+ }
41
+ /** The events a decorator takes. */
42
+ type EventOf<P> = ActionCreator<P> | Command<P, unknown>;
43
+ //#endregion
44
+ //#region src/oop/decorators.d.ts
45
+ /**
46
+ * A class decorator that does what `extendBus` does: `@WithBus(bus) class A {}`
47
+ * replaces `A` by `extendBus(bus, A)`, and `@WithBus() class A {}` by
48
+ * `extendBus(A)`. It works for the decorators of the language and for
49
+ * `experimentalDecorators`. TypeScript does not change the type of a decorated
50
+ * class, so declare what it gets next to it:
51
+ * `interface A extends BusMethods<typeof bus> {}`.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * interface SearchService extends BusMethods<typeof bus> {}
56
+ *
57
+ * @WithBus(bus)
58
+ * class SearchService {}
59
+ * ```
60
+ */
61
+ export declare function WithBus<B extends object>(bus: B): <C extends Constructor>(Base: C) => Extended<C, B>;
62
+ export declare function WithBus(): <C extends Constructor>(Base: C) => Extended<C, Bus<EventMap>>;
63
+ /**
64
+ * Makes a method of a class from `extendBus` or `@WithBus` a handler of `event`
65
+ * for each instance, with `this` bound to the instance, from the construction
66
+ * until `dispose()`. `wrap` takes the bound handler and returns the one to
67
+ * subscribe, such as `latest`. An event emitted inside the constructor of the
68
+ * base class does not reach it: the subscription is made right after.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * class SearchServiceBase {
73
+ * @On(queryChanged, latest) // a new query aborts the one before it
74
+ * async search(query: string, { signal }: Context) {}
75
+ * }
76
+ * ```
77
+ */
78
+ export declare function On<P>(event: EventOf<P>, wrap?: Wrap): MethodDecorator<P>;
79
+ export declare function On(event: string, wrap?: Wrap): MethodDecorator<never>;
80
+ /** Like `On`, but the handler runs at most once for an instance. */
81
+ export declare function Once<P>(event: EventOf<P>, wrap?: Wrap): MethodDecorator<P>;
82
+ export declare function Once(event: string, wrap?: Wrap): MethodDecorator<never>;
83
+ //#endregion
84
+ //#region src/oop/extend.d.ts
85
+ /**
86
+ * Makes a class from `Base` whose instances have the methods of a bus, with the
87
+ * subscriptions made through an instance ended by `dispose()`. `new Extended(…)`
88
+ * runs the constructor of `Base`; the class has the name of `Base`, and its
89
+ * instances are instances of `Base`. The members of `Base` win: a method called
90
+ * `emit` or `dispose` in `Base` is kept, and `disposeBus(this)` ends the
91
+ * subscriptions from there.
92
+ *
93
+ * Without a bus, `extendBus(Base)` makes one for the class (`Extended.bus`),
94
+ * shared by its instances. The methods of the plugins used on `bus` before this
95
+ * call are on the instances too; the ones of plugins used later are not.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * class SearchServiceBase {
100
+ * constructor(private api: Api) {
101
+ * this.on("query/changed", (query) => this.load(query)); // ends with dispose()
102
+ * }
103
+ * load(query: string) {}
104
+ * }
105
+ *
106
+ * export const SearchService = extendBus(bus, SearchServiceBase);
107
+ * const service = new SearchService(api);
108
+ * service.dispose(); // its subscriptions are gone
109
+ * ```
110
+ */
111
+ export declare function extendBus<C extends Constructor>(Base: C): Extended<C, Bus<EventMap>>;
112
+ export declare function extendBus<B extends object, C extends Constructor>(bus: B, Base: C): Extended<C, B>;
113
+ //#endregion
114
+ //#region src/oop/subscriptions.d.ts
115
+ /**
116
+ * Removes every subscription an instance made through `on` and `once`, and does
117
+ * nothing for any other value. A class with a `dispose` of its own calls it from
118
+ * there.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * class SearchService {
123
+ * dispose() {
124
+ * disposeBus(this); // the subscriptions of this instance are removed
125
+ * }
126
+ * }
127
+ * ```
128
+ */
129
+ export declare const disposeBus: (instance: unknown) => void;
130
+ //#endregion
131
+ export type { BusMethods, Extended, MethodDecorator };
package/dist/oop.js ADDED
@@ -0,0 +1 @@
1
+ import{r as e}from"./bus-CkTHUAvq.js";const t=/* @__PURE__ */ new WeakMap,n=e=>typeof e==`function`,r=e=>Reflect.ownKeys(e).flatMap(t=>{let r=Reflect.getOwnPropertyDescriptor(e,t)?.value;return n(r)?[{key:t,value:r}]:[]}),i=/* @__PURE__ */ new WeakMap,a=e=>{let n=i.get(e);if(n)return n;let a=/* @__PURE__ */ new Set,o=[];for(let n=e;n&&n!==Object.prototype;n=Reflect.getPrototypeOf(n))for(let{key:e,value:i}of r(n)){if(e===`constructor`||a.has(e))continue;a.add(e);let n=t.get(i);n&&o.push({key:e,method:i,spec:n})}return i.set(e,o),o},o=/* @__PURE__ */ new WeakMap,s=e=>typeof e==`object`&&!!e||typeof e==`function`,c=e=>{let t=o.get(e);if(t)return t;let n=/* @__PURE__ */ new Set;return o.set(e,n),n},l=e=>{if(typeof e==`string`)return e;if(s(e)){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},u=(e,t,n)=>Reflect.apply(Reflect.get(e,t),e,n),d=(e,t,n,r)=>{let[i,a,...o]=r,s=c(e),l={event:i,handler:a,off:()=>{}},d=u(t,n,[i,n===`once`?(e,t)=>(s.delete(l),typeof a==`function`?Reflect.apply(a,null,[e,t]):void 0):a,...o]),f=typeof d==`function`?()=>d():()=>{};return l.off=f,s.add(l),()=>{s.delete(l),f()}},f=(e,t,n)=>{let[r,i]=n,a=c(e);r===void 0&&a.clear();for(let e of[...a])Object.is(l(e.event),l(r))&&(i===void 0||e.handler===i)&&(a.delete(e),e.off());return u(t,`off`,n)},p=e=>{if(!s(e))return;let t=o.get(e);if(t){o.delete(e);for(let e of t)e.off()}},m=/* @__PURE__ */ new Set([`on`,`once`,`off`,`emit`,`ctx`,`commands`,`trace`,`addContext`,`applyScope`,`fitScope`,`app`,`use`]),h=Reflect.get(Symbol,`dispose`),g=e=>typeof e==`function`,_=(e,t,n,r)=>{n in t||Object.defineProperty(e,n,{configurable:!0,enumerable:!1,...r})},v=/* @__PURE__ */ new WeakMap,y=(e,t)=>{let n=v.get(e);if(n)return n;let r={on:(...n)=>d(e,t,`on`,n),once:(...n)=>d(e,t,`once`,n),off:(...n)=>f(e,t,n)};return v.set(e,r),r},b=(e,t)=>(...n)=>Reflect.apply(Reflect.get(e,t),e,n),x=(e,t)=>{for(let{method:n,spec:r}of a(Reflect.getPrototypeOf(e)??{})){let i=(t,r)=>Reflect.apply(n,e,[t,r]),a=r.wrap?r.wrap(i):i;d(e,t,r.once?`once`:`on`,[r.event,a])}},S=(e,t,n)=>{for(let r of[`on`,`once`,`off`])_(e,t,r,{get(){return y(this,n)[r]}});_(e,t,`emit`,{value:b(n,`emit`)});for(let[r,i]of Object.entries(n))typeof i==`function`&&!m.has(r)&&_(e,t,r,{value:b(n,r)});let r=function(){p(this)};_(e,t,`dispose`,{value:r}),h&&_(e,t,h,{value:r}),_(e,t,`bus`,{get:()=>n})},C=(e,t)=>{let n=function(...r){let i=Reflect.construct(t,r,new.target??n);return x(i,e),i};return Object.setPrototypeOf(n,t),n.prototype=Object.create(t.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),Object.defineProperty(n,"name",{value:t.name,configurable:!0}),`bus`in t||Object.defineProperty(n,"bus",{value:e,configurable:!0}),S(n.prototype,t.prototype,e),n};function w(t,n){if(g(t))return C(e(),t);if(!n)throw TypeError(`extendBus: pass the class to extend`);return C(t,n)}function T(e){return t=>e?w(e,t):w(t)}const E=e=>{let[t,n,r]=e;return typeof n==`string`||typeof n==`symbol`?Reflect.get(Object(r),`value`):t},D=(e,n,r)=>((...i)=>{let a=E(i);typeof a==`function`&&t.set(a,{event:e,wrap:n,once:r})});function O(e,t){return D(e,t,!1)}function k(e,t){return D(e,t,!0)}export{O as On,k as Once,T as WithBus,p as disposeBus,w as extendBus};
@@ -1,5 +1,5 @@
1
- import { B as EventMap, N as WithContext, V as Keys, b as Context, d as Effects, h as Bus, l as BoundForm } from "./action-creator-Bd4TBZgl.js";
2
- import { n as NoClash } from "./context-plugin-BVNKqkIx.js";
1
+ import { B as EventMap, N as WithContext, V as Keys, b as Context, d as Effects, h as Bus, l as BoundForm } from "./action-creator-BrZF6hXc.js";
2
+ import { n as NoClash } from "./context-plugin--HkNN1ha.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,5 @@
1
- import { B as EventMap, W as Task } from "./action-creator-Bd4TBZgl.js";
1
+ import { B as EventMap, W as Task } from "./action-creator-BrZF6hXc.js";
2
+ import { n as withPolicy, t as PolicyFunction } from "./with-policy-CqUS-VWW.js";
2
3
  //#region src/policies/keyed.d.ts
3
4
  /** Options shared by every policy that can keep its state per key. */
4
5
  type KeyOption<P> = {
@@ -168,4 +169,4 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
168
169
  */
169
170
  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>;
170
171
  //#endregion
171
- export type { DebounceOptions, LatestOptions, LeadingOptions, QueueOptions, ThrottleOptions };
172
+ export { type DebounceOptions, type LatestOptions, type LeadingOptions, type PolicyFunction, type QueueOptions, type ThrottleOptions, withPolicy };
package/dist/policies.js CHANGED
@@ -1 +1 @@
1
- import{t as e}from"./onAbort-BgMfspK4.js";import{a as t,i as n,o as r,t as i}from"./context-CK0ZomWt.js";import{n as a,r as o,t as s}from"./delay-CIHnOh_L.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";const u=()=>{},d=(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)}},f=(e,t)=>e?d(e,t):t(u),p=(e,t={})=>f(t.key,t=>m(`latest`,(t,n,r)=>(r(),e(t,n)),t)),m=(t,a,o=u)=>{let s=null;return(l,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=i(d,t),m=!1,h=()=>{d.signal.aborted||(m=!0,r(d,t,`start`))};e(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=a(l,d,h);return c(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}}},h=(e,t,n={})=>f(n.key,r=>_(e,t,n,r)),g=(e,n,i,s)=>{let c=s!==u,l=null,d=null;return(u,f)=>{if(f.signal.aborted)return;let p=Date.now(),m=l===null||p-l>=e||p-l<0;return l=p,d!==null&&a(d),d=c?o(s,e):null,i&&m?Promise.resolve(t(f,`debounce`,()=>n(u,f))):(r(f,`debounce`,`drop`),Promise.resolve(void 0))}},_=(n,r,i,a)=>{let o=i.leading??!1,c=i.trailing??!0;if(!c)return g(n,r,o,a);let l=!1,u=0,d=m(`debounce`,([t,i],a,o)=>{let d=u;return e(a,()=>{u===d&&(l=!1)}),s(a,n).then(()=>{if(l=!1,c&&!i)return o(),r(t,a)})},a);return(e,n)=>{if(n.signal.aborted)return;let i=o&&!l;l=!0,u++;let a=d([e,i],n);return i?Promise.resolve(t(n,`debounce`,()=>r(e,n))):a}},v=(n,i={})=>f(i.key,i=>{let a=null;return(o,s)=>{if(s.signal.aborted)return;if(a){r(s,`leading`,`drop`);return}let c={};a=c;let u=()=>{d(),a===c&&(a=null,i())},d=e(s,u);try{return l(t(s,`leading`,()=>n(o,s)),u)}catch(e){throw u(),e}}}),y=(e,t={})=>f(t.key,n=>b(e,t,n)),b=(n,i,a)=>{let o=i.concurrency??1,s=i.limit??1/0,c=[],l=0,d=()=>{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=u,_=()=>{g(),r(p,`queue`,`drop`),m(void 0)},v=()=>{if(g(),p.signal.aborted){_();return}l++;let r=!0,i=()=>{r&&(r=!1,l--,d(),l===0&&c.length===0&&a())},o=e(p,()=>{i(),m(void 0)}),s;try{s=Promise.resolve(t(p,`queue`,()=>n(f,p)))}catch(e){s=Promise.reject(e)}let u=()=>{o(),i()};s.then(e=>{u(),m(p.signal.aborted?void 0:e)},e=>{u(),p.signal.aborted?m(void 0):h(e)})};if(l<o){v();return}if(c.length>=s){if(i.overflow!==`drop-old`||s<=0){_();return}c.shift()?.drop()}let y={start:v,drop:_};c.push(y),g=e(p,()=>{let e=c.indexOf(y);e>=0&&c.splice(e,1),_()})})},x=(e,t,n={})=>f(n.key,r=>S(e,t,n,r)),S=(e,n,i,s)=>{let c=i.leading??!0,l=i.trailing??!0,d=s!==u,f=null,p=0,m=null,h=null,g=null,_=!1,v=t=>f===null||t-f>=e||t-f<0||t-p>=e,y=e=>r(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=(e,r,i)=>{b(),p=i,_=!0;try{return t(r,`throttle`,()=>n(e,r))}finally{_=!1}},C=()=>{d&&(h=o(w,e))},w=()=>{if(h=null,!d||m!==null||g)return;let t=Date.now(),n=Math.max(e-(t-(f??-1/0)),e-(t-p));n<=0?s():h=o(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 t=Date.now();v(t)?T(t):m=o(E,Math.min(e-(t-(f??t)),e-(t-p)))},D=()=>{h!==null&&(a(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=(t,n,r,i)=>i&&m!==null?(a(m),m=o(E,e),S(t,n,r)):m===null&&(m=o(E,e),i&&(p=r,c))?S(t,n,r):O(t,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)}};export{h as debounce,p as latest,v as leading,y as queue,x as throttle};
1
+ import{t as e}from"./onAbort-BgMfspK4.js";import{a as t,i as n,o as r,t as i}from"./context-COgyNlrC.js";import{n as a,r as o,t as s}from"./delay-CIHnOh_L.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";import{t as u}from"./with-policy-DBV9dh2F.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=(t,a,o=d)=>{let s=null;return(l,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=i(d,t),m=!1,h=()=>{d.signal.aborted||(m=!0,r(d,t,`start`))};e(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=a(l,d,h);return c(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)),_=(e,n,i,s)=>{let c=s!==d,l=null,u=null;return(d,f)=>{if(f.signal.aborted)return;let p=Date.now(),m=l===null||p-l>=e||p-l<0;return l=p,u!==null&&a(u),u=c?o(s,e):null,i&&m?Promise.resolve(t(f,`debounce`,()=>n(d,f))):(r(f,`debounce`,`drop`),Promise.resolve(void 0))}},v=(n,r,i,a)=>{let o=i.leading??!1,c=i.trailing??!0;if(!c)return _(n,r,o,a);let l=!1,u=0,d=h(`debounce`,([t,i],a,o)=>{let d=u;return e(a,()=>{u===d&&(l=!1)}),s(a,n).then(()=>{if(l=!1,c&&!i)return o(),r(t,a)})},a);return(e,n)=>{if(n.signal.aborted)return;let i=o&&!l;l=!0,u++;let a=d([e,i],n);return i?Promise.resolve(t(n,`debounce`,()=>r(e,n))):a}},y=(n,i={})=>p(i.key,i=>{let a=null;return(o,s)=>{if(s.signal.aborted)return;if(a){r(s,`leading`,`drop`);return}let c={};a=c;let u=()=>{d(),a===c&&(a=null,i())},d=e(s,u);try{return l(t(s,`leading`,()=>n(o,s)),u)}catch(e){throw u(),e}}}),b=(e,t={})=>p(t.key,n=>x(e,t,n)),x=(n,i,a)=>{let o=i.concurrency??1,s=i.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(),r(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=e(p,()=>{i(),m(void 0)}),s;try{s=Promise.resolve(t(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(i.overflow!==`drop-old`||s<=0){_();return}c.shift()?.drop()}let y={start:v,drop:_};c.push(y),g=e(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=(e,n,i,s)=>{let c=i.leading??!0,l=i.trailing??!0,u=s!==d,f=null,p=0,m=null,h=null,g=null,_=!1,v=t=>f===null||t-f>=e||t-f<0||t-p>=e,y=e=>r(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=(e,r,i)=>{b(),p=i,_=!0;try{return t(r,`throttle`,()=>n(e,r))}finally{_=!1}},C=()=>{u&&(h=o(w,e))},w=()=>{if(h=null,!u||m!==null||g)return;let t=Date.now(),n=Math.max(e-(t-(f??-1/0)),e-(t-p));n<=0?s():h=o(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 t=Date.now();v(t)?T(t):m=o(E,Math.min(e-(t-(f??t)),e-(t-p)))},D=()=>{h!==null&&(a(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=(t,n,r,i)=>i&&m!==null?(a(m),m=o(E,e),S(t,n,r)):m===null&&(m=o(E,e),i&&(p=r,c))?S(t,n,r):O(t,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)}};export{g as debounce,m as latest,y as leading,b as queue,S as throttle,u as withPolicy};