nanoeffects 0.6.0 → 0.8.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.
- package/dist/{action-creator-c292nK7v.d.ts → action-creator-BrZF6hXc.d.ts} +4 -8
- package/dist/actions.d.ts +6 -6
- package/dist/actions.js +1 -1
- package/dist/{context-plugin-1pYmSQEG.d.ts → context-plugin--HkNN1ha.d.ts} +1 -1
- package/dist/dev.d.ts +9 -8
- package/dist/dev.js +1 -1
- package/dist/effects.d.ts +2 -2
- package/dist/effects.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/match.d.ts +50 -0
- package/dist/match.js +1 -0
- package/dist/{module-BlF2KSgy.d.ts → module-DqKegXHt.d.ts} +44 -19
- package/dist/notrace/{action-creator-DKG_sIia.d.ts → action-creator-DHAEgwuh.d.ts} +4 -8
- package/dist/notrace/actions.d.ts +59 -24
- package/dist/notrace/actions.js +1 -1
- package/dist/notrace/{context-plugin-Dt1IgVo8.d.ts → context-plugin-BapQkGwQ.d.ts} +1 -1
- package/dist/notrace/effects.d.ts +2 -2
- package/dist/notrace/effects.js +1 -1
- package/dist/notrace/index.d.ts +2 -2
- package/dist/notrace/{plugin-B0AXKusc.d.ts → plugin-Cxadz6uU.d.ts} +2 -2
- package/dist/notrace/policies.d.ts +1 -1
- package/dist/notrace/redux.d.ts +1 -1
- package/dist/observable.d.ts +1 -1
- package/dist/oop.d.ts +131 -0
- package/dist/oop.js +1 -0
- package/dist/{plugin-CCYlC0XS.d.ts → plugin-DkXCvsmB.d.ts} +2 -2
- package/dist/policies.d.ts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/redux.d.ts +1 -1
- package/dist/scope.d.ts +8 -9
- package/dist/scope.js +1 -1
- package/dist/trace.d.ts +1 -1
- package/dist/types-KavbvHix.d.ts +25 -0
- package/docs/README.md +2 -0
- package/docs/actions.md +80 -36
- package/docs/guide.md +142 -18
- package/docs/hmr.md +6 -5
- package/docs/migrating-from-effector.md +49 -16
- package/docs/migrating-from-rxjs.md +17 -6
- package/docs/oop.md +286 -0
- package/docs/package.md +4 -0
- package/docs/plugins.md +284 -0
- package/docs/react.md +17 -5
- package/docs/scope.md +76 -36
- package/docs/stores.md +263 -109
- package/llms-full.txt +1232 -231
- package/llms.txt +2 -0
- package/package.json +20 -2
- package/dist/types-mLaAx-EZ.d.ts +0 -15
|
@@ -224,10 +224,6 @@ interface Bus<E extends EventMap> {
|
|
|
224
224
|
readonly trace?: Tracer | undefined;
|
|
225
225
|
/** Called with every new task context to add fields to it, for what needs the task, like `effectsPlugin`. Chain to the previous one. */
|
|
226
226
|
addContext?: ((ctx: object) => void) | undefined;
|
|
227
|
-
/** Puts the fields of a module's scope on the context of one of its tasks; `scopePlugin` sets it. Without it a module's scope is ignored. */
|
|
228
|
-
applyScope?: ((ctx: object, scope: Scope) => void) | undefined;
|
|
229
|
-
/** Wraps a handler so it runs with the fields of a module's scope; `scopePlugin` sets it. */
|
|
230
|
-
fitScope?: ((scope: Scope, fn: (payload: never, ctx: never) => unknown) => unknown) | undefined;
|
|
231
227
|
}
|
|
232
228
|
type BusOptions<E extends EventMap> = {
|
|
233
229
|
/** Tracing of every task phase; make it with `createTracer` from `nanoeffects/trace`. */
|
|
@@ -305,17 +301,17 @@ type WithScope<F> = {
|
|
|
305
301
|
type Carrying<X, F> = X & ([unknown] extends [F] ? unknown : WithScope<F>);
|
|
306
302
|
/**
|
|
307
303
|
* What a task gets on top of the context of its bus, for one place: every key
|
|
308
|
-
* is a context value and goes on `ctx` as it is, except
|
|
304
|
+
* is a context value and goes on `ctx` as it is, except `$effects`, whose
|
|
309
305
|
* functions (each takes `{ signal }` first) are bound to the task. Made work by
|
|
310
306
|
* `scopePlugin` from `nanoeffects/scope`; without it a scope is ignored.
|
|
311
307
|
*
|
|
312
308
|
* @example
|
|
313
309
|
* ```ts
|
|
314
|
-
* const scope = { cache, effects: { race } }; // ctx.cache, and ctx.race bound to the task
|
|
310
|
+
* const scope = { cache, $effects: { race } }; // ctx.cache, and ctx.race bound to the task
|
|
315
311
|
* ```
|
|
316
312
|
*/
|
|
317
313
|
type Scope = {
|
|
318
|
-
readonly effects?: Effects;
|
|
314
|
+
readonly $effects?: Effects;
|
|
319
315
|
readonly [field: string]: unknown;
|
|
320
316
|
};
|
|
321
317
|
/**
|
|
@@ -325,7 +321,7 @@ type Scope = {
|
|
|
325
321
|
* an error.
|
|
326
322
|
*/
|
|
327
323
|
type EmitScope<E extends EventMap, F = unknown> = ([unknown] extends [ContextOf<E> & F] ? unknown : Partial<ContextOf<E> & F>) & {
|
|
328
|
-
effects?: Partial<Effects>;
|
|
324
|
+
$effects?: Partial<Effects>;
|
|
329
325
|
};
|
|
330
326
|
/** `A` with the fields of `B` replacing those of the same name, not intersected with them. */
|
|
331
327
|
type Merge<A, B> = Omit<A, keyof B> & B;
|
package/dist/actions.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { B as EventMap, H as PayloadOf, T as EventKey, W as Task, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator } from "./action-creator-
|
|
2
|
-
import {
|
|
3
|
-
import { a as
|
|
1
|
+
import { B as EventMap, H as PayloadOf, T as EventKey, W as Task, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
import { r as Scoped, t as FieldsOfScope } from "./types-KavbvHix.js";
|
|
3
|
+
import { a as createModule, c as CarryingCalls, d as commandsPlugin, f as createCommand, l as MadeCommand, n as ModuleApi, o as modulePlugin, r as ModuleContext, s as Binding, t as Module, u as bindHandler } from "./module-DqKegXHt.js";
|
|
4
4
|
//#region src/actions/action.d.ts
|
|
5
5
|
type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCreator<T, F> & WithScope<F> : ActionCreator<P, T, PayloadOf<P>, F> & WithScope<F>;
|
|
6
6
|
/**
|
|
@@ -15,7 +15,7 @@ type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCrea
|
|
|
15
15
|
* generic: `createAction<User, "user/loaded">("user/loaded")`.
|
|
16
16
|
* - With `prepare` the arguments of the creator can differ from the payload,
|
|
17
17
|
* and both types are inferred.
|
|
18
|
-
* - A scope as the last argument, `{ cache, effects: { race } }`, gives the
|
|
18
|
+
* - A scope as the last argument, `{ cache, $effects: { race } }`, gives the
|
|
19
19
|
* listeners of its calls those context fields; it works on a bus that uses `scopePlugin`
|
|
20
20
|
* from `nanoeffects/scope` and is ignored on any other.
|
|
21
21
|
*
|
|
@@ -102,7 +102,7 @@ type Job<P, R, T extends string, S extends Outcome, F extends Outcome> = MadeCom
|
|
|
102
102
|
* effect. It returns the command, with the two actions on it: `job.success` and
|
|
103
103
|
* `job.fail`.
|
|
104
104
|
*
|
|
105
|
-
* A scope as the last argument, `{ cache, effects: { race } }`, gives the handler
|
|
105
|
+
* A scope as the last argument, `{ cache, $effects: { race } }`, gives the handler
|
|
106
106
|
* and the listeners of the outcome those context fields; see `scopePlugin`.
|
|
107
107
|
*
|
|
108
108
|
* `success` and `fail` are a type string, from which the job makes an action
|
|
@@ -135,7 +135,7 @@ export declare const createJob: <P = void, R = void, T extends string = string,
|
|
|
135
135
|
/** Gets the error when the handler fails: a type to make an action creator of, or an action creator. */
|
|
136
136
|
fail?: F;
|
|
137
137
|
},
|
|
138
|
-
/** Context values and
|
|
138
|
+
/** Context values and `$effects` for the handler and for the listeners of the outcome; see `scopePlugin`. */
|
|
139
139
|
scope?: X) => CarryingCalls<Job<P, R, T, S, F>, P, Promise<Awaited<R> | undefined>, FieldsOfScope<E, X>>;
|
|
140
140
|
//#endregion
|
|
141
141
|
export { type ActionCreator, type Binding, type EmitterApi, type FreeActionCreator, type Job, type Module, type ModuleApi, type ModuleContext, bindHandler, commandsPlugin, createCommand, createModule, modulePlugin };
|
package/dist/actions.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as e}from"./onAbort-BgMfspK4.js";import{i as t,n}from"./context-CK0ZomWt.js";import{t as r}from"./event-0RIBGYqS.js";import{r as i}from"./bus-BAs9x7IQ.js";import{t as a}from"./scope-key-B7l7mUS5.js";const o=(e,t)=>(t&&Object.defineProperty(e,a,{value:t}),e);function
|
|
1
|
+
import{t as e}from"./onAbort-BgMfspK4.js";import{i as t,n}from"./context-CK0ZomWt.js";import{t as r}from"./event-0RIBGYqS.js";import{r as i}from"./bus-BAs9x7IQ.js";import{t as a}from"./scope-key-B7l7mUS5.js";const 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=>typeof Reflect.get(e??{},`handle`)==`function`,m=(e=[])=>t=>{let n=t.commands;if(p(n))return n.handle(e),n;let i=/* @__PURE__ */ new Map,a=(e,t)=>{let n={fn:t};return i.set(e,[...i.get(e)??[],n]),()=>{let t=(i.get(e)??[]).filter(e=>e!==n);t.length>0?i.set(e,t):i.delete(e)}};function o(e,t){if(t)return a(r(e),t);let n=e.map(([e,t])=>a(r(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let r=(i.get(e.type)??[]).at(-1)?.fn,a=r&&e.wrap?e.wrap(r):r??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},h=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},g=e=>typeof e==`string`?l(e):e,_=(e,t,n,r)=>{let i=c(t);e.emit(Reflect.apply(t,null,[n]),r&&i?s(r,i):r)},v=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof u)throw e;return Promise.reject(e)}},y=(e,t,n={},r)=>{let a=d(e,t),s=g(n.success??`${e}/success`),c=g(n.fail??`${e}/fail`),l=(e,t,n)=>{let i=n;return v(()=>Reflect.apply(e,null,[t,n])).then(e=>(_(i,s,e,r),e),e=>{_(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},b=e=>typeof e==`object`&&!!e,x=e=>{if(typeof e==`string`)return e;if(b(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},S=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(x(n),x(t[0]))&&(r===void 0||Object.is(r,t[1]))},C=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`&&S(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 w(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const T=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,E=(e,t,n)=>{n&&e.applyScope?.(t,n)},D=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],O=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},k=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},A=(r=[])=>i=>{let a=i.commands??m()(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=T(i,h),_=(t,n)=>{let r=Reflect.apply(o.handle,null,D(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=k(f),b=O(f,g);try{E(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=C(i),l=i.emit,u,d=(e=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let t=n({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:t,apps:s(t.ctx)},e)for(let e of r)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:w};return i.app=p,p};export{f as bindHandler,m as commandsPlugin,l as createAction,d as createCommand,y as createJob,w as createModule,h as emitterPlugin,A as modulePlugin};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as EventMap, N as WithContext, h as Bus, x as ContextOf } from "./action-creator-
|
|
1
|
+
import { B as EventMap, N as WithContext, h as Bus, x as ContextOf } from "./action-creator-BrZF6hXc.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 = {
|
package/dist/dev.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as EventMap, J as UnsubscribeCallback, h as Bus } from "./action-creator-
|
|
2
|
-
import { n as ModuleApi } from "./module-
|
|
1
|
+
import { B as EventMap, J as UnsubscribeCallback, h as Bus } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
import { i as WithApp, n as ModuleApi } from "./module-DqKegXHt.js";
|
|
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">;
|
|
@@ -12,11 +12,11 @@ type DevOptions<E extends EventMap = EventMap> = {
|
|
|
12
12
|
modules?: boolean;
|
|
13
13
|
/** Receives every message. Default: `console.warn` for leaks and `console.info` for modules. */
|
|
14
14
|
log?: (message: string) => void;
|
|
15
|
-
/** What `modulePlugin` returned, to report its modules. */
|
|
15
|
+
/** What `modulePlugin` returned, to report its modules. Without it the app that `modulePlugin` put on the bus is used, in either order of the two plugins. */
|
|
16
16
|
app?: ReportedApp<E>;
|
|
17
17
|
};
|
|
18
18
|
/** A plugin that connects the diagnostics to a bus; the function it returns disconnects them. */
|
|
19
|
-
type DevPlugin = <E extends EventMap>(options?: DevOptions<E>) => (bus: Bus<E>) => UnsubscribeCallback;
|
|
19
|
+
type DevPlugin = <E extends EventMap>(options?: DevOptions<E>) => (bus: Bus<E> & WithApp) => UnsubscribeCallback;
|
|
20
20
|
//#endregion
|
|
21
21
|
//#region src/dev/plugin.d.ts
|
|
22
22
|
/**
|
|
@@ -28,8 +28,9 @@ type DevPlugin = <E extends EventMap>(options?: DevOptions<E>) => (bus: Bus<E>)
|
|
|
28
28
|
* the count: a file that was reloaded and subscribes again, or an
|
|
29
29
|
* unsubscribe that is never called. The count follows the function that `on`
|
|
30
30
|
* returns; `bus.off(event, fn)` is not seen.
|
|
31
|
-
* - **Modules.**
|
|
32
|
-
*
|
|
31
|
+
* - **Modules.** It reports a module that is mounted, replaced by name and
|
|
32
|
+
* unmounted, for the app that `modulePlugin` returned: the `app` option, or
|
|
33
|
+
* else the one `modulePlugin` put on the bus, whichever plugin was used first.
|
|
33
34
|
*
|
|
34
35
|
* It wraps `bus.on`, `app.mount` and `app.unmount`; the function that `bus.use` returns
|
|
35
36
|
* puts the originals back. In a production build `nanoeffects/dev` is a no-op, so
|
|
@@ -37,8 +38,8 @@ type DevPlugin = <E extends EventMap>(options?: DevOptions<E>) => (bus: Bus<E>)
|
|
|
37
38
|
*
|
|
38
39
|
* @example
|
|
39
40
|
* ```ts
|
|
40
|
-
*
|
|
41
|
-
* const disconnect = bus.use(devPlugin(
|
|
41
|
+
* bus.use(modulePlugin([userModule]));
|
|
42
|
+
* const disconnect = bus.use(devPlugin()); // warnings and messages in the console while developing
|
|
42
43
|
* ```
|
|
43
44
|
*/
|
|
44
45
|
export declare const devPlugin: DevPlugin;
|
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:
|
|
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};
|
package/dist/effects.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as EventMap, W as Task, u as Effect } from "./action-creator-
|
|
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-
|
|
1
|
+
import { B as EventMap, W as Task, u as Effect } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-DkXCvsmB.js";
|
|
3
3
|
//#region src/utils/onAbort.d.ts
|
|
4
4
|
export declare const onAbort: (ctx: {
|
|
5
5
|
signal?: AbortSignal | undefined;
|
package/dist/effects.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{n as e,t}from"./onAbort-BgMfspK4.js";import{a as n,i as r,o as i,t as a}from"./context-CK0ZomWt.js";import{n as o,r as s,t as c}from"./delay-CIHnOh_L.js";const l=(e,n)=>n?.signal?new Promise((r,i)=>{let a=t(n,i);e.then(r,i).finally(a)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=r(n??{}),d=a(c,e);i(c,e,`start`),t(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,t)=>{let{ctx:i,unlink:a}=r(e);try{return await n(i,`run`,()=>t(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,t)=>{let i=Object.entries(t).map(([t,i])=>{let a=r(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:t,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(n,r)=>n.signal.aborted?Promise.reject(n.signal.reason):new Promise((i,a)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=t(n,e=>{s=`cancelled`;for(let e of o.reverse())c(e);a(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{r({resolve:u(i),reject:u(a),onCancel:d})}catch(e){u(a)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,n,r,i)=>{if(e.signal.aborted)return Promise.resolve();let a=i.leading??!1,c=i.waitForFn??!1;return u(`poll`,e,e=>new Promise((i,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof r==`function`?r(e):r)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let t=u++;d(e,e=>n(e,t)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};t(e,p),a?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,n,r={}){return u(`take`,e,i=>new Promise((a,o)=>{let{predicate:s}=r,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)};c.push(e.on(n,e=>{(!s||s(e))&&(l(),a(e))})),c.push(t(i,u)),c.push(t({signal:r.signal},u))}))}const M=(e,t,i)=>u(`timeout`,e,async e=>{let a=r(e),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${t}ms`,`TimeoutError`)),t);try{return await l(Promise.resolve(n(a.ctx,`run`,()=>i(a.ctx))),a.ctx)}finally{o(c),a.unlink()}});function N(e,t,n){if(typeof e==`number`){let n=t;return(t,r)=>M(r,e,p(n,t))}let r=t,i=n;return M(e,r,e=>i({signal:e.signal,abort:e.abort}))}export{_ as all,y as allSettled,x as backoff,S as cancellable,c as delay,C as effectsPlugin,t as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
|
|
1
|
+
import{n as e,t}from"./onAbort-BgMfspK4.js";import{a as n,i as r,o as i,t as a}from"./context-CK0ZomWt.js";import{n as o,r as s,t as c}from"./delay-CIHnOh_L.js";const l=(e,n)=>n?.signal?new Promise((r,i)=>{let a=t(n,i);e.then(r,i).finally(a)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=r(n??{}),d=a(c,e);i(c,e,`start`),t(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,t)=>{let{ctx:i,unlink:a}=r(e);try{return await n(i,`run`,()=>t(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,t)=>{let i=Object.entries(t).map(([t,i])=>{let a=r(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:t,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(n,r)=>n.signal.aborted?Promise.reject(n.signal.reason):new Promise((i,a)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=t(n,e=>{s=`cancelled`;for(let e of o.reverse())c(e);a(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{r({resolve:u(i),reject:u(a),onCancel:d})}catch(e){u(a)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)t in e||Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,n,r,i)=>{if(e.signal.aborted)return Promise.resolve();let a=i.leading??!1,c=i.waitForFn??!1;return u(`poll`,e,e=>new Promise((i,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof r==`function`?r(e):r)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let t=u++;d(e,e=>n(e,t)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};t(e,p),a?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,n,r={}){return u(`take`,e,i=>new Promise((a,o)=>{let{predicate:s}=r,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)};c.push(e.on(n,e=>{(!s||s(e))&&(l(),a(e))})),c.push(t(i,u)),c.push(t({signal:r.signal},u))}))}const M=(e,t,i)=>u(`timeout`,e,async e=>{let a=r(e),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${t}ms`,`TimeoutError`)),t);try{return await l(Promise.resolve(n(a.ctx,`run`,()=>i(a.ctx))),a.ctx)}finally{o(c),a.unlink()}});function N(e,t,n){if(typeof e==`number`){let n=t;return(t,r)=>M(r,e,p(n,t))}let r=t,i=n;return M(e,r,e=>i({signal:e.signal,abort:e.abort}))}export{_ as all,y as allSettled,x as backoff,S as cancellable,c as delay,C as effectsPlugin,t as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { A as HandlerRef, B as EventMap, C as Emit, D as EventPayload, E as EventLike, F as commandCall, G as Trace, H as PayloadOf, I as createBus, J as UnsubscribeCallback, K as TraceEvent, L as Tracer, M as On, N as WithContext, O as EventsOf, P as WithType, R as anyEventType, S as ContextOption, T as EventKey, U as Phase, V as Keys, W as Task, _ as Command, b as Context, g as BusOptions, h as Bus, i as WithPayload, j as Off, k as Handler, m as AnyEvent, p as Action, q as TraceLogger, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as EffectName } from "./action-creator-
|
|
2
|
-
import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin
|
|
1
|
+
import { A as HandlerRef, B as EventMap, C as Emit, D as EventPayload, E as EventLike, F as commandCall, G as Trace, H as PayloadOf, I as createBus, J as UnsubscribeCallback, K as TraceEvent, L as Tracer, M as On, N as WithContext, O as EventsOf, P as WithType, R as anyEventType, S as ContextOption, T as EventKey, U as Phase, V as Keys, W as Task, _ as Command, b as Context, g as BusOptions, h as Bus, i as WithPayload, j as Off, k as Handler, m as AnyEvent, p as Action, q as TraceLogger, v as CommandCall, w as Emittable, x as ContextOf, y as Commands, z as EffectName } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
import { n as NoClash, r as contextPlugin, t as ContextTask } from "./context-plugin--HkNN1ha.js";
|
|
3
3
|
export { type Action, type AnyEvent, type Bus, type BusOptions, type Command, type CommandCall, type Commands, type Context, type ContextOf, type ContextOption, type ContextTask, type EffectName, type Emit, type Emittable, type EventKey, type EventLike, type EventMap, type EventPayload, type EventsOf, type Handler, type HandlerRef, type Keys, type NoClash, type Off, type On, type PayloadOf, type Phase, type Task, type Trace, type TraceEvent, type TraceLogger, type Tracer, type UnsubscribeCallback, type WithContext, type WithPayload, type WithType, anyEventType, commandCall, contextPlugin, createBus };
|
package/dist/match.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { B as EventMap, D as EventPayload, J as UnsubscribeCallback, W as Task, h as Bus, p as Action } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
//#region src/match/plugin.d.ts
|
|
3
|
+
/** A function over the action of an event: `{ type, payload }`. */
|
|
4
|
+
type Predicate<E extends EventMap> = (action: Action<E>) => boolean;
|
|
5
|
+
/** A predicate that is a type guard narrows the payload that the handler gets. */
|
|
6
|
+
type Guard<E extends EventMap, A extends Action<E>> = (action: Action<E>) => action is A;
|
|
7
|
+
type MatchOn<E extends EventMap> = {
|
|
8
|
+
<A extends Action<E>>(guard: Guard<E, A>, fn: Task<A["payload"], E>): UnsubscribeCallback;
|
|
9
|
+
(predicate: Predicate<E>, fn: Task<EventPayload<E>, E>): UnsubscribeCallback;
|
|
10
|
+
};
|
|
11
|
+
type MatchOff<E extends EventMap> = {
|
|
12
|
+
<A extends Action<E>>(guard: Guard<E, A>, fn?: Task<A["payload"], E>): void;
|
|
13
|
+
(predicate: Predicate<E>, fn?: Task<EventPayload<E>, E>): void;
|
|
14
|
+
};
|
|
15
|
+
/** What `matchPlugin` adds to `on`, `once` and `off` of the bus: the same methods that also take a predicate. */
|
|
16
|
+
type MatchApi<E extends EventMap> = {
|
|
17
|
+
on: MatchOn<E>;
|
|
18
|
+
once: MatchOn<E>;
|
|
19
|
+
off: MatchOff<E>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Lets `on`, `once` and `off` of the bus take a predicate as the first argument,
|
|
23
|
+
* to subscribe to the events that it picks instead of to one named event. The
|
|
24
|
+
* predicate gets `{ type, payload }` of every event; when it returns true the
|
|
25
|
+
* handler runs with the payload and the task context of that event. A type guard
|
|
26
|
+
* narrows the payload of the handler. An action creator or a command, which have
|
|
27
|
+
* a string `type`, keep their meaning. Returns the same bus.
|
|
28
|
+
*
|
|
29
|
+
* The subscription listens to `"*"`, so it runs after the listeners of the event
|
|
30
|
+
* itself, makes a task for every event whether or not the predicate accepts it,
|
|
31
|
+
* and counts as a listener for `onUnhandled`. For a few known events,
|
|
32
|
+
* `on(creator, handler)` is cheaper.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { createBus } from "nanoeffects";
|
|
37
|
+
* import { matchPlugin } from "nanoeffects/match";
|
|
38
|
+
*
|
|
39
|
+
* const bus = createBus().use(matchPlugin());
|
|
40
|
+
*
|
|
41
|
+
* // every event of the user domain, with the context of that event
|
|
42
|
+
* bus.on(
|
|
43
|
+
* (action) => action.type.startsWith("user/"),
|
|
44
|
+
* (payload, ctx) => console.log(ctx.type, payload),
|
|
45
|
+
* );
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const matchPlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus<E> & MatchApi<E>;
|
|
49
|
+
//#endregion
|
|
50
|
+
export type { MatchApi };
|
package/dist/match.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>typeof e==`function`&&typeof Reflect.get(e,`type`)!=`string`,t=e=>e,n=()=>n=>{let{on:r,once:i,off:a}=n,o=/* @__PURE__ */ new Map,s=(e,t,i)=>{let a=Reflect.apply(r,n,[`*`,(n,r)=>{if(e({type:Reflect.get(r,`type`),payload:n}))return i&&c(),t(n,r)}]),s={handler:t,remove:c};o.set(e,[...o.get(e)??[],s]);function c(){a();let t=(o.get(e)??[]).filter(e=>e!==s);t.length>0?o.set(e,t):o.delete(e)}return c},c=(t,a,o)=>e(t)?s(t,a,o):Reflect.apply(o?i:r,n,[t,a]);return Object.assign(n,t({on:(e,t)=>c(e,t,!1),once:(e,t)=>c(e,t,!0),off:(t,r)=>{if(r===void 0&&(t===void 0||t===`*`)&&o.clear(),!e(t))return Reflect.apply(a,n,[t,r]);let i=o.get(t)??[],s=i.filter(e=>e.handler===r),c=r===void 0?i:s.slice(0,1);for(let e of c)e.remove()}}))};export{n as matchPlugin};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { B as EventMap, H as PayloadOf, J as UnsubscribeCallback, W as Task, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, v as CommandCall } from "./action-creator-
|
|
2
|
-
import { n as Scoped, t as FieldsOfScope } from "./types-
|
|
1
|
+
import { B as EventMap, H as PayloadOf, J as UnsubscribeCallback, W as Task, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, v as CommandCall, y as Commands } from "./action-creator-BrZF6hXc.js";
|
|
2
|
+
import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-KavbvHix.js";
|
|
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> & {
|
|
@@ -33,7 +33,7 @@ type ScopedCommand<P, R, T extends string, F> = ((payload: P) => CommandCall<P,
|
|
|
33
33
|
* `command.match(action)` tells whether an action has the type of the command,
|
|
34
34
|
* as it does for an action creator.
|
|
35
35
|
*
|
|
36
|
-
* A scope as the third argument, `{ cache, effects: { race } }`, puts those fields on
|
|
36
|
+
* A scope as the third argument, `{ cache, $effects: { race } }`, puts those fields on
|
|
37
37
|
* the context of the handler and of the listeners of the command; the handler
|
|
38
38
|
* sees them typed. It works on a bus that uses `scopePlugin` from
|
|
39
39
|
* `nanoeffects/scope` and is ignored on any other.
|
|
@@ -66,13 +66,21 @@ type Binding<E extends EventMap = EventMap> = readonly [Command<never, unknown>
|
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
68
|
declare const bindHandler: <P, R, E extends EventMap>(command: Command<P, R>, fn: Task<P, E, R>) => Binding<E>;
|
|
69
|
+
/** The registry of handlers that `commandsPlugin` returns: `run` for the bus, `handle` to bind more. */
|
|
70
|
+
interface Registry$1<E extends EventMap> extends Commands {
|
|
71
|
+
handle<P, R>(command: Command<P, R>, fn: Task<P, E, R>): UnsubscribeCallback;
|
|
72
|
+
handle(type: string, fn: Task<never, E, unknown>): UnsubscribeCallback;
|
|
73
|
+
handle(bindings: readonly Binding<E>[]): UnsubscribeCallback;
|
|
74
|
+
}
|
|
69
75
|
/**
|
|
70
76
|
* The plugin that gives a bus the handlers bound to commands:
|
|
71
77
|
* `bus.use(commandsPlugin(bindings))` makes the registry, sets it as the
|
|
72
78
|
* commands of the bus and returns it. Each bus has its own, so each test can
|
|
73
79
|
* have its own implementation. `handle` binds more later and returns the
|
|
74
|
-
* function that unbinds. A second `commandsPlugin` on the same bus
|
|
75
|
-
* the registry of
|
|
80
|
+
* function that unbinds. A second `commandsPlugin` on the same bus binds its
|
|
81
|
+
* list in the registry that is there and returns it, so the order of the
|
|
82
|
+
* plugins does not matter. A `commands` that is not such a registry (a custom
|
|
83
|
+
* runner) is replaced.
|
|
76
84
|
*
|
|
77
85
|
* What the bus runs for a command: the handler bound last, else the one the
|
|
78
86
|
* command was created with, else it throws `No handler for command "..."`.
|
|
@@ -86,14 +94,7 @@ declare const bindHandler: <P, R, E extends EventMap>(command: Command<P, R>, fn
|
|
|
86
94
|
* unbind(); // the command's own handler is back
|
|
87
95
|
* ```
|
|
88
96
|
*/
|
|
89
|
-
declare const commandsPlugin: <E extends EventMap>(bindings?: readonly Binding<E>[]) => (bus: Bus<E>) =>
|
|
90
|
-
run: (command: Command, payload: unknown, ctx: unknown) => unknown;
|
|
91
|
-
handle: {
|
|
92
|
-
<P, R>(command: Command<P, R>, fn: Task<P, E, R>): UnsubscribeCallback;
|
|
93
|
-
(type: string, fn: Task<never, E, unknown>): UnsubscribeCallback;
|
|
94
|
-
(bindings: readonly Binding<E>[]): UnsubscribeCallback;
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
+
declare const commandsPlugin: <E extends EventMap>(bindings?: readonly Binding<E>[]) => (bus: Bus<E>) => Registry$1<E>;
|
|
97
98
|
//#endregion
|
|
98
99
|
//#region src/actions/module.d.ts
|
|
99
100
|
/**
|
|
@@ -153,6 +154,25 @@ type Module<E extends EventMap = EventMap> = {
|
|
|
153
154
|
declare function createModule<E extends EventMap = EventMap>(name: string, setup: (m: ModuleContext<E>) => void): Module<E>;
|
|
154
155
|
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>;
|
|
155
156
|
type Registry<E extends EventMap> = ReturnType<ReturnType<typeof commandsPlugin<E>>>;
|
|
157
|
+
/**
|
|
158
|
+
* The modules of an app as the plugins that report on them see them: the part
|
|
159
|
+
* of what `modulePlugin` returns that `devPlugin` wraps.
|
|
160
|
+
*/
|
|
161
|
+
interface ModuleHost {
|
|
162
|
+
mount(module: {
|
|
163
|
+
readonly name: string;
|
|
164
|
+
}): UnsubscribeCallback;
|
|
165
|
+
unmount(module: {
|
|
166
|
+
readonly name: string;
|
|
167
|
+
}): void;
|
|
168
|
+
isMounted(module: {
|
|
169
|
+
readonly name: string;
|
|
170
|
+
}): boolean;
|
|
171
|
+
}
|
|
172
|
+
/** The slot `modulePlugin` fills; a bus has it in its type once the plugin returned it. */
|
|
173
|
+
interface WithApp {
|
|
174
|
+
app?: ModuleHost | undefined;
|
|
175
|
+
}
|
|
156
176
|
/** What `modulePlugin` returns. */
|
|
157
177
|
type ModuleApi<E extends EventMap = EventMap> = {
|
|
158
178
|
/** The registry of command handlers of the bus: the one it was made with, or the app's own. */
|
|
@@ -173,10 +193,15 @@ type ModuleApi<E extends EventMap = EventMap> = {
|
|
|
173
193
|
};
|
|
174
194
|
/**
|
|
175
195
|
* The plugin that assembles an app from modules: `bus.use(modulePlugin(modules))`
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
196
|
+
* returns `mount`, `unmount`, `isMounted`, `dispose` and `commands` for the
|
|
197
|
+
* modules that come and go, and mounts `modules`, in the order given, on the
|
|
198
|
+
* first use of the app or of the bus: the first `bus.emit`, or the first call
|
|
199
|
+
* of one of its methods. By then every plugin is on the bus, so the plugins can
|
|
200
|
+
* be used in any order. The `on`, `once` and `off` calls made in between are
|
|
201
|
+
* held and made right after the modules, so the listeners of a module stay
|
|
202
|
+
* ahead of them. The bus runs the commands that the modules bind: the registry
|
|
203
|
+
* that the bus has is used (a later `commandsPlugin` binds in it), otherwise the
|
|
204
|
+
* app attaches its own, as `app.commands`. The app is also `bus.app`.
|
|
180
205
|
*
|
|
181
206
|
* A module is known by its `name`: mounting a module whose name is taken
|
|
182
207
|
* replaces the mounted one, which is unmounted first. That is what hot reload
|
|
@@ -205,6 +230,6 @@ type ModuleApi<E extends EventMap = EventMap> = {
|
|
|
205
230
|
* app.dispose();
|
|
206
231
|
* ```
|
|
207
232
|
*/
|
|
208
|
-
declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E>) => ModuleApi<E>;
|
|
233
|
+
declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E> & ScopeSlots & WithApp) => ModuleApi<E>;
|
|
209
234
|
//#endregion
|
|
210
|
-
export {
|
|
235
|
+
export { createModule as a, CarryingCalls as c, commandsPlugin as d, createCommand as f, WithApp as i, MadeCommand as l, ModuleApi as n, modulePlugin as o, ModuleContext as r, Binding as s, Module as t, bindHandler as u };
|
|
@@ -224,10 +224,6 @@ interface Bus<E extends EventMap> {
|
|
|
224
224
|
readonly trace?: Tracer | undefined;
|
|
225
225
|
/** Called with every new task context to add fields to it, for what needs the task, like `effectsPlugin`. Chain to the previous one. */
|
|
226
226
|
addContext?: ((ctx: object) => void) | undefined;
|
|
227
|
-
/** Puts the fields of a module's scope on the context of one of its tasks; `scopePlugin` sets it. Without it a module's scope is ignored. */
|
|
228
|
-
applyScope?: ((ctx: object, scope: Scope) => void) | undefined;
|
|
229
|
-
/** Wraps a handler so it runs with the fields of a module's scope; `scopePlugin` sets it. */
|
|
230
|
-
fitScope?: ((scope: Scope, fn: (payload: never, ctx: never) => unknown) => unknown) | undefined;
|
|
231
227
|
}
|
|
232
228
|
type BusOptions<E extends EventMap> = {
|
|
233
229
|
/** Tracing of every task phase; make it with `createTracer` from `nanoeffects/trace`. */
|
|
@@ -305,17 +301,17 @@ type WithScope<F> = {
|
|
|
305
301
|
type Carrying<X, F> = X & ([unknown] extends [F] ? unknown : WithScope<F>);
|
|
306
302
|
/**
|
|
307
303
|
* What a task gets on top of the context of its bus, for one place: every key
|
|
308
|
-
* is a context value and goes on `ctx` as it is, except
|
|
304
|
+
* is a context value and goes on `ctx` as it is, except `$effects`, whose
|
|
309
305
|
* functions (each takes `{ signal }` first) are bound to the task. Made work by
|
|
310
306
|
* `scopePlugin` from `nanoeffects/scope`; without it a scope is ignored.
|
|
311
307
|
*
|
|
312
308
|
* @example
|
|
313
309
|
* ```ts
|
|
314
|
-
* const scope = { cache, effects: { race } }; // ctx.cache, and ctx.race bound to the task
|
|
310
|
+
* const scope = { cache, $effects: { race } }; // ctx.cache, and ctx.race bound to the task
|
|
315
311
|
* ```
|
|
316
312
|
*/
|
|
317
313
|
type Scope = {
|
|
318
|
-
readonly effects?: Effects;
|
|
314
|
+
readonly $effects?: Effects;
|
|
319
315
|
readonly [field: string]: unknown;
|
|
320
316
|
};
|
|
321
317
|
/**
|
|
@@ -325,7 +321,7 @@ type Scope = {
|
|
|
325
321
|
* an error.
|
|
326
322
|
*/
|
|
327
323
|
type EmitScope<E extends EventMap, F = unknown> = ([unknown] extends [ContextOf<E> & F] ? unknown : Partial<ContextOf<E> & F>) & {
|
|
328
|
-
effects?: Partial<Effects>;
|
|
324
|
+
$effects?: Partial<Effects>;
|
|
329
325
|
};
|
|
330
326
|
/** `A` with the fields of `B` replacing those of the same name, not intersected with them. */
|
|
331
327
|
type Merge<A, B> = Omit<A, keyof B> & B;
|
|
@@ -1,11 +1,21 @@
|
|
|
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-
|
|
2
|
-
import { t as BoundOf } from "./plugin-
|
|
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
|
-
effects: infer H extends Effects;
|
|
15
|
+
$effects: infer H extends Effects;
|
|
6
16
|
} ? BoundOf<H, E> : unknown;
|
|
7
|
-
/** The fields a scope gives a task: its values as they are and its
|
|
8
|
-
type FieldsOfScope<E extends EventMap, S extends Scope> = [keyof S] extends [never] ? unknown : Omit<S, "effects"> & BoundEffects<E, S>;
|
|
17
|
+
/** The fields a scope gives a task: its values as they are and its `$effects` bound; `unknown` for an empty scope. */
|
|
18
|
+
type FieldsOfScope<E extends EventMap, S extends Scope> = [keyof S] extends [never] ? unknown : Omit<S, "$effects"> & BoundEffects<E, S>;
|
|
9
19
|
/**
|
|
10
20
|
* The event map `E` with the fields of a scope on top of its context fields.
|
|
11
21
|
* A field the scope sets replaces the one of the bus in the type too.
|
|
@@ -26,7 +36,7 @@ type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCrea
|
|
|
26
36
|
* generic: `createAction<User, "user/loaded">("user/loaded")`.
|
|
27
37
|
* - With `prepare` the arguments of the creator can differ from the payload,
|
|
28
38
|
* and both types are inferred.
|
|
29
|
-
* - A scope as the last argument, `{ cache, effects: { race } }`, gives the
|
|
39
|
+
* - A scope as the last argument, `{ cache, $effects: { race } }`, gives the
|
|
30
40
|
* listeners of its calls those context fields; it works on a bus that uses `scopePlugin`
|
|
31
41
|
* from `nanoeffects/scope` and is ignored on any other.
|
|
32
42
|
*
|
|
@@ -87,7 +97,7 @@ type ScopedCommand<P, R, T extends string, F> = ((payload: P) => CommandCall<P,
|
|
|
87
97
|
* `command.match(action)` tells whether an action has the type of the command,
|
|
88
98
|
* as it does for an action creator.
|
|
89
99
|
*
|
|
90
|
-
* A scope as the third argument, `{ cache, effects: { race } }`, puts those fields on
|
|
100
|
+
* A scope as the third argument, `{ cache, $effects: { race } }`, puts those fields on
|
|
91
101
|
* the context of the handler and of the listeners of the command; the handler
|
|
92
102
|
* sees them typed. It works on a bus that uses `scopePlugin` from
|
|
93
103
|
* `nanoeffects/scope` and is ignored on any other.
|
|
@@ -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
|
|
129
|
-
* the registry of
|
|
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. */
|
|
@@ -208,7 +219,7 @@ type Job<P, R, T extends string, S extends Outcome, F extends Outcome> = MadeCom
|
|
|
208
219
|
* effect. It returns the command, with the two actions on it: `job.success` and
|
|
209
220
|
* `job.fail`.
|
|
210
221
|
*
|
|
211
|
-
* A scope as the last argument, `{ cache, effects: { race } }`, gives the handler
|
|
222
|
+
* A scope as the last argument, `{ cache, $effects: { race } }`, gives the handler
|
|
212
223
|
* and the listeners of the outcome those context fields; see `scopePlugin`.
|
|
213
224
|
*
|
|
214
225
|
* `success` and `fail` are a type string, from which the job makes an action
|
|
@@ -241,7 +252,7 @@ export declare const createJob: <P = void, R = void, T extends string = string,
|
|
|
241
252
|
/** Gets the error when the handler fails: a type to make an action creator of, or an action creator. */
|
|
242
253
|
fail?: F;
|
|
243
254
|
},
|
|
244
|
-
/** Context values and
|
|
255
|
+
/** Context values and `$effects` for the handler and for the listeners of the outcome; see `scopePlugin`. */
|
|
245
256
|
scope?: X) => CarryingCalls<Job<P, R, T, S, F>, P, Promise<Awaited<R> | undefined>, FieldsOfScope<E, X>>;
|
|
246
257
|
//#endregion
|
|
247
258
|
//#region src/actions/module.d.ts
|
|
@@ -302,6 +313,25 @@ type Module<E extends EventMap = EventMap> = {
|
|
|
302
313
|
export declare function createModule<E extends EventMap = EventMap>(name: string, setup: (m: ModuleContext<E>) => void): Module<E>;
|
|
303
314
|
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
315
|
type Registry<E extends EventMap> = ReturnType<ReturnType<typeof commandsPlugin<E>>>;
|
|
316
|
+
/**
|
|
317
|
+
* The modules of an app as the plugins that report on them see them: the part
|
|
318
|
+
* of what `modulePlugin` returns that `devPlugin` wraps.
|
|
319
|
+
*/
|
|
320
|
+
interface ModuleHost {
|
|
321
|
+
mount(module: {
|
|
322
|
+
readonly name: string;
|
|
323
|
+
}): UnsubscribeCallback;
|
|
324
|
+
unmount(module: {
|
|
325
|
+
readonly name: string;
|
|
326
|
+
}): void;
|
|
327
|
+
isMounted(module: {
|
|
328
|
+
readonly name: string;
|
|
329
|
+
}): boolean;
|
|
330
|
+
}
|
|
331
|
+
/** The slot `modulePlugin` fills; a bus has it in its type once the plugin returned it. */
|
|
332
|
+
interface WithApp {
|
|
333
|
+
app?: ModuleHost | undefined;
|
|
334
|
+
}
|
|
305
335
|
/** What `modulePlugin` returns. */
|
|
306
336
|
type ModuleApi<E extends EventMap = EventMap> = {
|
|
307
337
|
/** The registry of command handlers of the bus: the one it was made with, or the app's own. */
|
|
@@ -322,10 +352,15 @@ type ModuleApi<E extends EventMap = EventMap> = {
|
|
|
322
352
|
};
|
|
323
353
|
/**
|
|
324
354
|
* The plugin that assembles an app from modules: `bus.use(modulePlugin(modules))`
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
355
|
+
* returns `mount`, `unmount`, `isMounted`, `dispose` and `commands` for the
|
|
356
|
+
* modules that come and go, and mounts `modules`, in the order given, on the
|
|
357
|
+
* first use of the app or of the bus: the first `bus.emit`, or the first call
|
|
358
|
+
* of one of its methods. By then every plugin is on the bus, so the plugins can
|
|
359
|
+
* be used in any order. The `on`, `once` and `off` calls made in between are
|
|
360
|
+
* held and made right after the modules, so the listeners of a module stay
|
|
361
|
+
* ahead of them. The bus runs the commands that the modules bind: the registry
|
|
362
|
+
* that the bus has is used (a later `commandsPlugin` binds in it), otherwise the
|
|
363
|
+
* app attaches its own, as `app.commands`. The app is also `bus.app`.
|
|
329
364
|
*
|
|
330
365
|
* A module is known by its `name`: mounting a module whose name is taken
|
|
331
366
|
* replaces the mounted one, which is unmounted first. That is what hot reload
|
|
@@ -354,6 +389,6 @@ type ModuleApi<E extends EventMap = EventMap> = {
|
|
|
354
389
|
* app.dispose();
|
|
355
390
|
* ```
|
|
356
391
|
*/
|
|
357
|
-
export declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E>) => ModuleApi<E>;
|
|
392
|
+
export declare const modulePlugin: <E extends EventMap>(modules?: readonly Module<E>[]) => (bus: Bus<E> & ScopeSlots & WithApp) => ModuleApi<E>;
|
|
358
393
|
//#endregion
|
|
359
394
|
export type { ActionCreator, Binding, EmitterApi, FreeActionCreator, Job, Module, ModuleApi, ModuleContext };
|
package/dist/notrace/actions.js
CHANGED
|
@@ -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);function
|
|
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=>typeof Reflect.get(e??{},`handle`)==`function`,m=(e=[])=>t=>{let n=t.commands;if(p(n))return n.handle(e),n;let i=/* @__PURE__ */ new Map,a=(e,t)=>{let n={fn:t};return i.set(e,[...i.get(e)??[],n]),()=>{let t=(i.get(e)??[]).filter(e=>e!==n);t.length>0?i.set(e,t):i.delete(e)}};function o(e,t){if(t)return a(r(e),t);let n=e.map(([e,t])=>a(r(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let r=(i.get(e.type)??[]).at(-1)?.fn,a=r&&e.wrap?e.wrap(r):r??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},h=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},g=e=>typeof e==`string`?l(e):e,_=(e,t,n,r)=>{let i=c(t);e.emit(Reflect.apply(t,null,[n]),r&&i?s(r,i):r)},v=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof u)throw e;return Promise.reject(e)}},y=(e,t,n={},r)=>{let a=d(e,t),s=g(n.success??`${e}/success`),c=g(n.fail??`${e}/fail`),l=(e,t,n)=>{let i=n;return v(()=>Reflect.apply(e,null,[t,n])).then(e=>(_(i,s,e,r),e),e=>{_(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},b=e=>typeof e==`object`&&!!e,x=e=>{if(typeof e==`string`)return e;if(b(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},S=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(x(n),x(t[0]))&&(r===void 0||Object.is(r,t[1]))},C=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`&&S(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 w(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const T=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,E=(e,t,n)=>{n&&e.applyScope?.(t,n)},D=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],O=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},k=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},A=(r=[])=>i=>{let a=i.commands??m()(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=>{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=e(t,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=T(i,h),_=(e,t)=>{let r=Reflect.apply(o.handle,null,D(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=k(f),b=O(f,g);try{E(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=C(i),l=i.emit,u,d=(e=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let n=t({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:n,apps:s(n.ctx)},e)for(let e of r)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:w};return i.app=p,p};export{f as bindHandler,m as commandsPlugin,l as createAction,d as createCommand,y as createJob,w as createModule,h as emitterPlugin,A as modulePlugin};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { R as EventMap, j as WithContext, p as Bus, y as ContextOf } from "./action-creator-
|
|
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 = {
|