nanoeffects 0.20.0 → 0.22.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/actions.d.ts +2 -2
- package/dist/dev.d.ts +1 -1
- package/dist/effects.d.ts +1 -1
- package/dist/effects.js +1 -1
- package/dist/{module-B0KVDNjY.d.ts → module-1sUEXIcD.d.ts} +1 -1
- package/dist/notrace/actions.d.ts +1 -1
- package/dist/notrace/effects.d.ts +1 -1
- package/dist/notrace/effects.js +1 -1
- package/dist/notrace/{plugin-C6KA4dX-.d.ts → plugin-ByDuI8Vq.d.ts} +20 -3
- package/dist/notrace/policies.d.ts +50 -5
- package/dist/notrace/policies.js +1 -1
- package/dist/{plugin-D9UTzGRG.d.ts → plugin-BiGv84Xu.d.ts} +20 -3
- package/dist/policies.d.ts +50 -5
- package/dist/policies.js +1 -1
- package/dist/react.d.ts +6 -4
- package/dist/scope.d.ts +1 -1
- package/dist/svelte.d.ts +46 -0
- package/dist/svelte.js +1 -0
- package/dist/{types-CbdJKC2z.d.ts → types-DC-aLJit.d.ts} +1 -1
- package/docs/README.md +2 -0
- package/docs/chat-example.md +8 -3
- package/docs/effects.md +8 -1
- package/docs/migrating-from-saga.md +3 -1
- package/docs/package.md +1 -0
- package/docs/policies.md +27 -0
- package/docs/query-libraries.md +2 -0
- package/docs/react.md +5 -3
- package/docs/starter.md +12 -1
- package/docs/stores.md +1 -1
- package/docs/svelte.md +81 -0
- package/llms-full.txt +153 -11
- package/llms.txt +3 -2
- package/package.json +20 -2
package/dist/actions.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { H as PayloadOf, T as EventKey, W as Task, c as WithScope, h as Bus, n as Free, o as Scope, r as FreeActionCreator, t as ActionCreator, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { r as Scoped, t as FieldsOfScope } from "./types-
|
|
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-
|
|
2
|
+
import { r as Scoped, t as FieldsOfScope } from "./types-DC-aLJit.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-1sUEXIcD.js";
|
|
4
4
|
import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
|
|
5
5
|
//#region src/actions/action.d.ts
|
|
6
6
|
type ScopedCreator<P, T extends string, F> = [P] extends [Free] ? FreeActionCreator<T, F> & WithScope<F> : ActionCreator<P, T, PayloadOf<P>, F> & WithScope<F>;
|
package/dist/dev.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { J as UnsubscribeCallback, h as Bus, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { i as WithApp, n as ModuleApi } from "./module-
|
|
2
|
+
import { i as WithApp, n as ModuleApi } from "./module-1sUEXIcD.js";
|
|
3
3
|
//#region src/dev/types.d.ts
|
|
4
4
|
/** The parts of what `modulePlugin` returned that the diagnostics report. */
|
|
5
5
|
type ReportedApp<E extends EventMap> = Pick<ModuleApi<E>, "mount" | "unmount" | "isMounted">;
|
package/dist/effects.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { P as WithSignal, W as Task, u as Effect, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-
|
|
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-BiGv84Xu.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{d as e,i as t,o as n,s as r,t as i,u as a}from"./context-CJGW6OWj.js";import{n as o,r as s,t as c}from"./delay-BnBdGPh7.js";const l=(e,t)=>t?.signal?new Promise((n,r)=>{let i=a(t,r);e.then(n,r).finally(i)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=t(n??{}),d=i(c,e);r(c,e,`start`),a(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,r)=>{let{ctx:i,unlink:a}=t(e);try{return await n(i,`run`,()=>r(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,r)=>{let i=Object.entries(r).map(([r,i])=>{let a=t(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:r,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(t,n)=>t.signal.aborted?Promise.reject(t.signal.reason):new Promise((r,i)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=a(t,e=>{s=`cancelled`;for(let e of o.reverse())c(e);i(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{n({resolve:u(r),reject:u(i),onCancel:d})}catch(e){u(i)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)t in e||Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,t,n,r)=>{if(e.signal.aborted)return Promise.resolve();let i=r.leading??!1,c=r.waitForFn??!1;return u(`poll`,e,e=>new Promise((r,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof n==`function`?n(e):n)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let n=u++;d(e,e=>t(e,n)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};a(e,p),i?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,t,n={}){return u(`take`,e,r=>new Promise((i,o)=>{let{predicate:s}=n,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)};c.push(e.on(
|
|
1
|
+
import{d as e,i as t,o as n,s as r,t as i,u as a}from"./context-CJGW6OWj.js";import{n as o,r as s,t as c}from"./delay-BnBdGPh7.js";const l=(e,t)=>t?.signal?new Promise((n,r)=>{let i=a(t,r);e.then(n,r).finally(i)}):e,u=async(e,n,o,s)=>{n?.signal?.throwIfAborted();let{ctx:c,unlink:u}=t(n??{}),d=i(c,e);r(c,e,`start`),a(c,()=>d(`cancel`));try{let e=await l(o(c),n);return d(`done`),s?.(!1),e}catch(e){throw d(`error`,e),s?.(!0),e}finally{u()}},d=async(e,r)=>{let{ctx:i,unlink:a}=t(e);try{return await n(i,`run`,()=>r(i))}finally{a()}},f=e=>e,p=(e,t)=>n=>e(t,f(n)),m=e=>Array.isArray(e)?Array(e.length):{},h=(e,r)=>{let i=Object.entries(r).map(([r,i])=>{let a=t(e),o={signal:a.ctx.signal,abort:a.ctx.abort},s=()=>i(o);return{key:r,task:new Promise(e=>e(n(a.ctx,`run`,s))).finally(a.unlink),cancel:a.ctx.abort}});return{tasks:i.map(({key:e,task:t})=>[e,t]),cancelAll:()=>{for(let e of i)e.cancel()}}},g=(e,t,n,r,i)=>{let a=()=>{};return u(e,t,async e=>{let t=h(e,n);return a=t.cancelAll,r(t.tasks)},e=>{(e||i===`end`)&&a()})};function _(e,t){return g(`all`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>e)),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const v=e=>e.then(e=>({status:`fulfilled`,value:e}),e=>({status:`rejected`,reason:e}));function y(e,t){return g(`allSettled`,e,t,async e=>{let n=await Promise.all(e.map(([,e])=>v(e))),r=m(t);for(let[t,[i]]of e.entries())r[i]=n[t];return r},`failure`)}const b=e=>e>0?e:0,x=(e={})=>{let t=b(e.base??100),n=e.factor??2,r=b(e.max??1/0),i=e.jitter??!1;return e=>{let a=b(Math.min(r,t*n**(e-1)));return i?Math.random()*a||0:a}},S=(t,n)=>t.signal.aborted?Promise.reject(t.signal.reason):new Promise((r,i)=>{let o=[],s=`pending`,c=t=>{try{t()}catch(t){e(`cancel cleanup failed`,t)}},l=a(t,e=>{s=`cancelled`;for(let e of o.reverse())c(e);i(e)}),u=e=>(...t)=>{s===`pending`&&(s=`settled`,l(),o.length=0,e(...t))},d=e=>{s===`pending`?o.push(e):s===`cancelled`&&c(e)};try{n({resolve:u(r),reject:u(i),onCancel:d})}catch(e){u(i)(e)}}),C=e=>t=>{let n=t.addContext,r=Object.entries(e);return t.addContext=e=>{n?.(e);for(let[t,n]of r)t in e||Object.assign(e,{[t]:(...t)=>n(e,...t)})},t},w=(e,t,n,r)=>{if(e.signal.aborted)return Promise.resolve();let i=r.leading??!1,c=r.waitForFn??!1;return u(`poll`,e,e=>new Promise((r,l)=>{let u=0,f=null,p=()=>{f!==null&&(o(f),f=null)},m=e=>{try{f=s(h,typeof n==`function`?n(e):n)}catch(e){p(),l(e)}},h=()=>{f=null,c||m(u+1),g()},g=()=>{let n=u++;d(e,e=>t(e,n)).then(()=>{c&&!e.signal.aborted&&m(u)},e=>{p(),l(e)})};a(e,p),i?(g(),c||m(u)):m(0)})).catch(t=>{if(!e.signal.aborted)throw t})};function T(e,t,n,r={}){if(typeof e!=`object`){let r=t,i=n??{};return(t,n)=>w(n,p(r,t),e,i)}let i=t;return w(e,(e,t)=>i(t),n,r)}function E(e,t){let n=m(t);return Object.keys(t).length===0?Promise.resolve(n):g(`race`,e,t,async e=>{let[t,r]=await Promise.race(e.map(([e,t])=>t.then(t=>[e,t])));return n[t]=r,n},`end`)}const D=(e,t,n)=>typeof e==`function`?e(t,n):e,O=e=>({times:e.times??3,delay:e.delay??0,shouldRetry:e.shouldRetry??(()=>!0)}),k=(e,t,n)=>u(`retry`,e,async e=>{let{times:r,delay:i,shouldRetry:a}=O(n);for(let n=1;;n++)try{return await d(e,e=>t(e,n))}catch(t){if(e.signal.throwIfAborted(),!(n<r)||!a(t,n))throw t;let o=D(i,n,t);o>0&&await c(e,o)}});function A(e,t,n={}){if(typeof e==`function`){let n=e;return(e,r)=>k(r,p(n,e),t??{})}let r=t;return k(e,(e,t)=>r(t),n)}function j(e,t,n={}){return u(`take`,e,r=>new Promise((i,o)=>{let{predicate:s}=n,c=[],l=()=>{for(let e of c)e()},u=e=>{l(),o(e)},d=t;c.push(e.on(d,e=>{(!s||s(e))&&(l(),i(e))})),c.push(a(r,u)),c.push(a({signal:n.signal},u))}))}const M=(e,r,i)=>u(`timeout`,e,async e=>{let a=t(e),c=s(()=>a.ctx.abort(new DOMException(`Timed out after ${r}ms`,`TimeoutError`)),r);try{return await l(Promise.resolve(n(a.ctx,`run`,()=>i(a.ctx))),a.ctx)}finally{o(c),a.unlink()}});function N(e,t,n){if(typeof e==`number`){let n=t;return(t,r)=>M(r,e,p(n,t))}let r=t,i=n;return M(e,r,e=>i({signal:e.signal,abort:e.abort}))}export{_ as all,y as allSettled,x as backoff,S as cancellable,c as delay,C as effectsPlugin,a as onAbort,T as poll,E as race,A as retry,j as take,N as timeout};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { H as PayloadOf, J as UnsubscribeCallback, W as Task, _ as Command, b as Context, c as WithScope, h as Bus, o as Scope, v as CommandCall, y as Commands, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-
|
|
2
|
+
import { n as ScopeSlots, r as Scoped, t as FieldsOfScope } from "./types-DC-aLJit.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> & {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { C as EventKey, F as EventMap, L as PayloadOf, R as Task, _ as Commands, a as Scope, g as CommandCall, h as Command, n as Free, o as WithFields, p as Bus, r as FreeActionCreator, s as WithScope, t as ActionCreator, u as Effects, v as Context, z as UnsubscribeCallback } from "./action-creator-D2CotugT.js";
|
|
2
|
-
import { t as BoundOf } from "./plugin-
|
|
2
|
+
import { t as BoundOf } from "./plugin-ByDuI8Vq.js";
|
|
3
3
|
import { t as OriginOptions } from "./origin-key-CxK0SkeG.js";
|
|
4
4
|
//#region src/scope/types.d.ts
|
|
5
5
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { F as EventMap, M as WithSignal, R as Task, l as Effect } from "./action-creator-D2CotugT.js";
|
|
2
|
-
import { a as RaceStarter, c as all, d as delay, i as take, l as EffectContext, n as effectsPlugin, o as race, r as TakeOptions, s as allSettled, u as Starter } from "./plugin-
|
|
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-ByDuI8Vq.js";
|
|
3
3
|
//#region src/utils/onAbort.d.ts
|
|
4
4
|
export declare const onAbort: (ctx: {
|
|
5
5
|
signal?: AbortSignal | undefined;
|
package/dist/notrace/effects.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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(
|
|
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)},d=n;c.push(e.on(d,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,4 +1,4 @@
|
|
|
1
|
-
import { F as EventMap, I as Keys, c as BoundForm, j as WithContext, p as Bus, u as Effects, v as Context, y as ContextOf } from "./action-creator-D2CotugT.js";
|
|
1
|
+
import { F as EventMap, I as Keys, c as BoundForm, j as WithContext, p as Bus, r as FreeActionCreator, t as ActionCreator, u as Effects, v as Context, y as ContextOf } from "./action-creator-D2CotugT.js";
|
|
2
2
|
//#region src/core/context-types.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* `unknown` when none of the `fields` is already a context field of the bus,
|
|
@@ -124,7 +124,8 @@ type TakeOptions<P> = {
|
|
|
124
124
|
signal?: AbortSignal | undefined;
|
|
125
125
|
};
|
|
126
126
|
/**
|
|
127
|
-
* Waits for the next `event` on the bus and resolves with its payload.
|
|
127
|
+
* Waits for the next `event` on the bus and resolves with its payload. `event` is the name
|
|
128
|
+
* of an event or an action creator, as for `bus.on`. It sees
|
|
128
129
|
* only events emitted after the call, takes only one, and unsubscribes when it
|
|
129
130
|
* is done. If `ctx` (or `options.signal`) aborts first, it unsubscribes and
|
|
130
131
|
* rejects with the abort reason.
|
|
@@ -141,6 +142,9 @@ type TakeOptions<P> = {
|
|
|
141
142
|
* take(ctx, "saved", { signal, predicate: (id) => id === draftId }),
|
|
142
143
|
* );
|
|
143
144
|
*
|
|
145
|
+
* // an action creator works as the name does, and gives its payload type
|
|
146
|
+
* const stopped = await take(ctx, stopRequested);
|
|
147
|
+
*
|
|
144
148
|
* // a type guard narrows the result: `failed` is `{ ok: false }`
|
|
145
149
|
* const failed = await take(ctx, "result", {
|
|
146
150
|
* predicate: (result): result is { ok: false } => !result.ok,
|
|
@@ -151,6 +155,14 @@ declare function take<E extends EventMap, K extends Keys<E>, T extends E[K]>(ctx
|
|
|
151
155
|
predicate: (payload: E[K]) => payload is T;
|
|
152
156
|
}): Promise<T>;
|
|
153
157
|
declare function take<E extends EventMap, K extends Keys<E>>(ctx: Context<E>, event: K, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
158
|
+
declare function take<E extends EventMap, K extends Keys<E>, T extends E[K]>(ctx: Context<E>, creator: FreeActionCreator<K>, options: TakeOptions<E[K]> & {
|
|
159
|
+
predicate: (payload: E[K]) => payload is T;
|
|
160
|
+
}): Promise<T>;
|
|
161
|
+
declare function take<E extends EventMap, K extends Keys<E>>(ctx: Context<E>, creator: FreeActionCreator<K>, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
162
|
+
declare function take<E extends EventMap, P, T extends P>(ctx: Context<E>, creator: ActionCreator<P>, options: TakeOptions<P> & {
|
|
163
|
+
predicate: (payload: P) => payload is T;
|
|
164
|
+
}): Promise<T>;
|
|
165
|
+
declare function take<E extends EventMap, P>(ctx: Context<E>, creator: ActionCreator<P>, options?: TakeOptions<P>): Promise<P>;
|
|
154
166
|
//#endregion
|
|
155
167
|
//#region src/effects/plugin.d.ts
|
|
156
168
|
/** The effects that `effectsPlugin` can put on `ctx`. */
|
|
@@ -177,7 +189,12 @@ type Bound<E extends EventMap> = {
|
|
|
177
189
|
<T extends Results>(effects: T): Promise<{ [K in keyof T]: PromiseSettledResult<Awaited<ReturnType<T[K]>>>; }>;
|
|
178
190
|
<const T extends List>(effects: T): Promise<{ -readonly [K in keyof T]: PromiseSettledResult<Awaited<ReturnType<T[K]>>>; }>;
|
|
179
191
|
};
|
|
180
|
-
take:
|
|
192
|
+
take: {
|
|
193
|
+
<K extends Keys<E>>(event: K, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
194
|
+
/** A creator without a payload type: the payload comes from the event map. */
|
|
195
|
+
<K extends Keys<E>>(creator: FreeActionCreator<K>, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
196
|
+
<P>(creator: ActionCreator<P>, options?: TakeOptions<P>): Promise<P>;
|
|
197
|
+
};
|
|
181
198
|
delay: (ms: number | (() => number)) => Promise<void>;
|
|
182
199
|
};
|
|
183
200
|
/** Any function whose first parameter takes `{ signal }` can go on the context, not only the built-in ones. */
|
|
@@ -9,6 +9,14 @@ type KeyOption<P> = {
|
|
|
9
9
|
*/
|
|
10
10
|
key?: (payload: P) => unknown;
|
|
11
11
|
};
|
|
12
|
+
/** Options of a policy called without a task: `key` is left out. */
|
|
13
|
+
type Unkeyed<O> = Omit<O, "key"> & {
|
|
14
|
+
key?: undefined;
|
|
15
|
+
};
|
|
16
|
+
/** Options of a policy called without a task, with a `key` that tells the payload type. */
|
|
17
|
+
type Keyed<O, P> = O & {
|
|
18
|
+
key: (payload: P) => unknown;
|
|
19
|
+
};
|
|
12
20
|
//#endregion
|
|
13
21
|
//#region src/policies/debounce.d.ts
|
|
14
22
|
type DebounceResult<R> = Promise<R | undefined> | Promise<Awaited<R> | undefined> | undefined;
|
|
@@ -18,6 +26,10 @@ type DebounceOptions<P = never> = KeyOption<P> & {
|
|
|
18
26
|
/** Run the last call of a burst after `ms` of quiet. Default `true`. */
|
|
19
27
|
trailing?: boolean;
|
|
20
28
|
};
|
|
29
|
+
/** What `debounce(ms)` returns: it turns a task into a `debounce` task. */
|
|
30
|
+
type DebounceWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
|
|
31
|
+
/** What `debounce(ms, { key })` returns: the payload type comes from `key`. */
|
|
32
|
+
type KeyedDebounceWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
|
|
21
33
|
/**
|
|
22
34
|
* Waits for `ms` of quiet, then runs; a new call restarts the wait and drops
|
|
23
35
|
* the previous. With `leading` the first call of a burst runs at once, and the
|
|
@@ -38,14 +50,24 @@ type DebounceOptions<P = never> = KeyOption<P> & {
|
|
|
38
50
|
* bus.emit("input", "re"); // only the last call runs, 300 ms after it
|
|
39
51
|
* ```
|
|
40
52
|
*/
|
|
41
|
-
export declare
|
|
53
|
+
export declare function debounce<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: DebounceOptions<P>): Task<P, E, DebounceResult<R>>;
|
|
54
|
+
export declare function debounce(ms: number, options?: Unkeyed<DebounceOptions>): DebounceWrapper;
|
|
55
|
+
export declare function debounce<P>(ms: number, options: Keyed<DebounceOptions<P>, P>): KeyedDebounceWrapper<P>;
|
|
42
56
|
//#endregion
|
|
43
57
|
//#region src/policies/latest.d.ts
|
|
44
58
|
type LatestOptions<P = never> = KeyOption<P>;
|
|
59
|
+
type LatestResult<R> = R | Promise<Awaited<R> | undefined> | undefined;
|
|
60
|
+
/** What `latest()` returns: it turns a task into a `latest` task. */
|
|
61
|
+
type LatestWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
|
|
62
|
+
/** What `latest({ key })` returns: the payload type comes from `key`. */
|
|
63
|
+
type KeyedLatestWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
|
|
45
64
|
/**
|
|
46
65
|
* Aborts the previous run when a new call arrives; stale results are dropped.
|
|
47
66
|
* With `options.key` each key has its own latest run.
|
|
48
67
|
*
|
|
68
|
+
* Called without the task, `latest()` returns a function that takes the task,
|
|
69
|
+
* so `usePolicy(callback, latest())` has no `(task) => ...` to get wrong.
|
|
70
|
+
*
|
|
49
71
|
* @example
|
|
50
72
|
* ```ts
|
|
51
73
|
* bus.on(
|
|
@@ -61,12 +83,21 @@ type LatestOptions<P = never> = KeyOption<P>;
|
|
|
61
83
|
*
|
|
62
84
|
* // one latest run per user instead of one for all
|
|
63
85
|
* bus.on("load", latest(loadUser, { key: (user) => user.id }));
|
|
86
|
+
*
|
|
87
|
+
* // without the task: for a hook that builds the task itself
|
|
88
|
+
* const search = usePolicy(runSearch, latest());
|
|
64
89
|
* ```
|
|
65
90
|
*/
|
|
66
|
-
export declare
|
|
91
|
+
export declare function latest<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LatestOptions<P>): Task<P, E, LatestResult<R>>;
|
|
92
|
+
export declare function latest(options?: Unkeyed<LatestOptions>): LatestWrapper;
|
|
93
|
+
export declare function latest<P>(options: Keyed<LatestOptions<P>, P>): KeyedLatestWrapper<P>;
|
|
67
94
|
//#endregion
|
|
68
95
|
//#region src/policies/leading.d.ts
|
|
69
96
|
type LeadingOptions<P = never> = KeyOption<P>;
|
|
97
|
+
/** What `leading()` returns: it turns a task into a `leading` task. */
|
|
98
|
+
type LeadingWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
99
|
+
/** What `leading({ key })` returns: the payload type comes from `key`. */
|
|
100
|
+
type KeyedLeadingWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
70
101
|
/**
|
|
71
102
|
* Drops new calls while the task is still running. The lock is released when
|
|
72
103
|
* the run settles or when its task is aborted, whichever comes first, so a
|
|
@@ -86,7 +117,9 @@ type LeadingOptions<P = never> = KeyOption<P>;
|
|
|
86
117
|
* bus.emit("save", draft); // dropped while the first save is in flight
|
|
87
118
|
* ```
|
|
88
119
|
*/
|
|
89
|
-
export declare
|
|
120
|
+
export declare function leading<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LeadingOptions<P>): Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
121
|
+
export declare function leading(options?: Unkeyed<LeadingOptions>): LeadingWrapper;
|
|
122
|
+
export declare function leading<P>(options: Keyed<LeadingOptions<P>, P>): KeyedLeadingWrapper<P>;
|
|
90
123
|
//#endregion
|
|
91
124
|
//#region src/policies/queue.d.ts
|
|
92
125
|
type QueueOptions<P = never> = KeyOption<P> & {
|
|
@@ -101,6 +134,10 @@ type QueueOptions<P = never> = KeyOption<P> & {
|
|
|
101
134
|
*/
|
|
102
135
|
overflow?: "drop-new" | "drop-old";
|
|
103
136
|
};
|
|
137
|
+
/** What `queue()` returns: it turns a task into a `queue` task. */
|
|
138
|
+
type QueueWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
139
|
+
/** What `queue({ key })` returns: the payload type comes from `key`. */
|
|
140
|
+
type KeyedQueueWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
104
141
|
/**
|
|
105
142
|
* Runs calls one after another, in the order they arrived: a call starts when
|
|
106
143
|
* fewer than `concurrency` calls are running. Every call returns a promise with
|
|
@@ -131,7 +168,9 @@ type QueueOptions<P = never> = KeyOption<P> & {
|
|
|
131
168
|
* bus.emit("save", b); // waits for a, then runs
|
|
132
169
|
* ```
|
|
133
170
|
*/
|
|
134
|
-
export declare
|
|
171
|
+
export declare function queue<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: QueueOptions<P>): Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
172
|
+
export declare function queue(options?: Unkeyed<QueueOptions>): QueueWrapper;
|
|
173
|
+
export declare function queue<P>(options: Keyed<QueueOptions<P>, P>): KeyedQueueWrapper<P>;
|
|
135
174
|
//#endregion
|
|
136
175
|
//#region src/policies/throttle.d.ts
|
|
137
176
|
type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
@@ -140,6 +179,10 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
|
140
179
|
/** Run the latest call made while waiting, at the end of the window. Default `true`. */
|
|
141
180
|
trailing?: boolean;
|
|
142
181
|
};
|
|
182
|
+
/** What `throttle(ms)` returns: it turns a task into a `throttle` task. */
|
|
183
|
+
type ThrottleWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
184
|
+
/** What `throttle(ms, { key })` returns: the payload type comes from `key`. */
|
|
185
|
+
type KeyedThrottleWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
143
186
|
/**
|
|
144
187
|
* At most one run per `ms`, with the semantics of lodash `throttle` (and
|
|
145
188
|
* patronum): a call made when the throttle is idle, that is, at least `ms`
|
|
@@ -166,7 +209,9 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
|
166
209
|
* bus.emit("scroll", 30); // runs once the 200 ms window closes
|
|
167
210
|
* ```
|
|
168
211
|
*/
|
|
169
|
-
export declare
|
|
212
|
+
export declare function throttle<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: ThrottleOptions<P>): Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
213
|
+
export declare function throttle(ms: number, options?: Unkeyed<ThrottleOptions>): ThrottleWrapper;
|
|
214
|
+
export declare function throttle<P>(ms: number, options: Keyed<ThrottleOptions<P>, P>): KeyedThrottleWrapper<P>;
|
|
170
215
|
//#endregion
|
|
171
216
|
//#region src/policies/with-policy.d.ts
|
|
172
217
|
/** What `withPolicy` returns: the task as a plain function that can be cancelled. */
|
package/dist/notrace/policies.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as e,c as t,i as n,n as r,o as i,t as a}from"./context-yYU3_9lq.js";import{n as o,r as s,t as c}from"./delay-BosJKFjg.js";import{n as l,t as u}from"./promise-CSLpzwSR.js";import{n as d,t as f}from"./lifecycle-key-8iX8RUVD.js";const p=e=>Reflect.get(e,f),m=e=>{p(e)?.start()},h=(t,n,r)=>(m(t),e(t,n,r)),g=(e,t)=>{i(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,d,!0),e),b=()=>{},x=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},
|
|
1
|
+
import{a as e,c as t,i as n,n as r,o as i,t as a}from"./context-yYU3_9lq.js";import{n as o,r as s,t as c}from"./delay-BosJKFjg.js";import{n as l,t as u}from"./promise-CSLpzwSR.js";import{n as d,t as f}from"./lifecycle-key-8iX8RUVD.js";const p=e=>Reflect.get(e,f),m=e=>{p(e)?.start()},h=(t,n,r)=>(m(t),e(t,n,r)),g=(e,t)=>{i(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,d,!0),e),b=()=>{},x=(e,t,n)=>typeof e==`function`?n(e,t??{}):t=>n(t,e??{}),S=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},C=(e,t)=>y(e?S(e,t):t(b));function w(e,t){return x(e,t,T)}const T=(e,t)=>C(t.key,t=>E(`latest`,(t,n,r)=>(r(),e(t,n)),t)),E=(e,r,o=b)=>{let s=null;return(c,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=a(d,e),h=!1,g=()=>{d.signal.aborted||(h=!0,m(d),i(d,e,`start`))};t(d,e=>{v(d,h,e),p(h?`cancel`:`drop`)});let _=()=>{f(),s===d&&(s=null,o())},y=e=>{if(!d.signal.aborted)return p(h?`done`:`drop`),e};try{let e=r(c,d,g);return l(e)?Promise.resolve(e).then(y,e=>{if(!d.signal.aborted)throw p(`error`,e),e}).finally(_):(_(),y(e))}catch(e){if(_(),d.signal.aborted)return;throw p(`error`,e),e}}};function D(e,t,n){return x(t,n,(t,n)=>O(e,t,n))}const O=(e,t,n)=>C(n.key,r=>A(e,t,n,r)),k=(e,t,n,r)=>{let i=r!==b,a=null,c=null;return(l,u)=>{if(u.signal.aborted)return;let d=Date.now(),f=a===null||d-a>=e||d-a<0;return a=d,c!==null&&o(c),c=i?s(r,e):null,n&&f?Promise.resolve(h(u,`debounce`,()=>t(l,u))):(g(u,`debounce`),Promise.resolve(void 0))}},A=(e,n,r,i)=>{let a=r.leading??!1,o=r.trailing??!0;if(!o)return k(e,n,a,i);let s=!1,l=0,u=E(`debounce`,([r,i],a,u)=>{let d=l;return t(a,()=>{l===d&&(s=!1)}),c(a,e).then(()=>{if(s=!1,o&&!i)return u(),n(r,a)})},i);return(e,t)=>{if(t.signal.aborted)return;let r=a&&!s;s=!0,l++;let i=u([e,r],t);return r?Promise.resolve(h(t,`debounce`,()=>n(e,t))):i}};function j(e,t){return x(e,t,M)}const M=(e,n)=>C(n.key,n=>{let r=null;return(i,a)=>{if(a.signal.aborted)return;if(r){g(a,`leading`);return}let o={};r=o;let s=()=>{c(),r===o&&(r=null,n())},c=t(a,s);try{return u(h(a,`leading`,()=>e(i,a)),s)}catch(e){throw s(),e}}});function N(e,t){return x(e,t,P)}const P=(e,t)=>C(t.key,n=>F(e,t,n)),F=(e,n,r)=>{let i=n.concurrency??1,a=n.limit??1/0,o=[],s=0,c=()=>{for(;s<i;){let e=o.shift();if(!e)return;e.start()}};return(l,u)=>new Promise((d,f)=>{if(u.signal.aborted){d(void 0);return}let p=b,m=()=>{p(),g(u,`queue`),d(void 0)},v=()=>{if(p(),u.signal.aborted){m();return}s++;let n=!0,i=()=>{n&&(n=!1,s--,c(),s===0&&o.length===0&&r())},a=t(u,()=>{i(),d(void 0)}),g;try{g=Promise.resolve(h(u,`queue`,()=>e(l,u)))}catch(e){g=Promise.reject(e)}let v=()=>{a(),i()};g.then(e=>{_(u,{value:e}),v(),d(u.signal.aborted?void 0:e)},e=>{_(u,{error:e}),v(),u.signal.aborted?d(void 0):f(e)})};if(s<i){v();return}if(o.length>=a){if(n.overflow!==`drop-old`||a<=0){m();return}o.shift()?.drop()}let y={start:v,drop:m};o.push(y),p=t(u,()=>{let e=o.indexOf(y);e>=0&&o.splice(e,1),m()})})};function I(e,t,n){return x(t,n,(t,n)=>L(e,t,n))}const L=(e,t,n)=>C(n.key,r=>R(e,t,n,r)),R=(e,t,n,r)=>{let i=n.leading??!0,a=n.trailing??!0,c=r!==b,l=null,u=0,d=null,f=null,p=null,m=!1,_=t=>l===null||t-l>=e||t-l<0||t-u>=e,v=e=>g(e,`throttle`),y=()=>{let e=p;p=null,e&&(v(e.ctx),e.resolve())},x=(e,t)=>(y(),new Promise((n,r)=>{p={payload:e,ctx:t,resolve:n,reject:r}})),S=(e,n,r)=>{y(),u=r,m=!0;try{return h(n,`throttle`,()=>t(e,n))}finally{m=!1}},C=()=>{c&&(f=s(w,e))},w=()=>{if(f=null,!c||d!==null||p)return;let t=Date.now(),n=Math.max(e-(t-(l??-1/0)),e-(t-u));n<=0?r():f=s(w,n)},T=e=>{d=null;let t=p;if(a&&t&&!t.ctx.signal.aborted){p=null;try{t.resolve(S(t.payload,t.ctx,e))}catch(e){t.reject(e)}}else y();w()},E=()=>{let t=Date.now();_(t)?T(t):d=s(E,Math.min(e-(t-(l??t)),e-(t-u)))},D=()=>{f!==null&&(o(f),f=null)},O=(e,t)=>{if(!a)v(t);else return x(e,t)},k=(e,t,n,r)=>{if(!r)v(t);else if(u=n,C(),!i)v(t);else return S(e,t,n)},A=(t,n,r,a)=>a&&d!==null?(o(d),d=s(E,e),S(t,n,r)):d===null&&(d=s(E,e),a&&(u=r,i))?S(t,n,r):O(t,n);return(e,t)=>{if(t.signal.aborted)return;let n=Date.now(),r=!m&&_(n);return l=n,D(),a?A(e,t,n,r):k(e,t,n,r)}},z=Reflect.get(Symbol,`dispose`),B=e=>{let t=new AbortController,n=async(...n)=>{let i=n[0],{ctx:a,unlink:o}=r({event:{type:`withPolicy`,payload:i},parent:{signal:t.signal}});try{return await e(i,a)}finally{o()}},i=e=>{t.abort(e),t=new AbortController},a=Object.assign(n,{cancel:i});return z&&Reflect.set(a,z,()=>i()),a};export{D as debounce,w as latest,j as leading,N as queue,I as throttle,B as withPolicy};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { B as Keys, N as WithContext, b as Context, d as Effects, h as Bus, l as BoundForm, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
1
|
+
import { B as Keys, N as WithContext, b as Context, d as Effects, h as Bus, l as BoundForm, r as FreeActionCreator, t as ActionCreator, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
2
|
import { n as NoClash } from "./context-types-BKIH5a8s.js";
|
|
3
3
|
//#region src/utils/delay.d.ts
|
|
4
4
|
/**
|
|
@@ -118,7 +118,8 @@ type TakeOptions<P> = {
|
|
|
118
118
|
signal?: AbortSignal | undefined;
|
|
119
119
|
};
|
|
120
120
|
/**
|
|
121
|
-
* Waits for the next `event` on the bus and resolves with its payload.
|
|
121
|
+
* Waits for the next `event` on the bus and resolves with its payload. `event` is the name
|
|
122
|
+
* of an event or an action creator, as for `bus.on`. It sees
|
|
122
123
|
* only events emitted after the call, takes only one, and unsubscribes when it
|
|
123
124
|
* is done. If `ctx` (or `options.signal`) aborts first, it unsubscribes and
|
|
124
125
|
* rejects with the abort reason.
|
|
@@ -135,6 +136,9 @@ type TakeOptions<P> = {
|
|
|
135
136
|
* take(ctx, "saved", { signal, predicate: (id) => id === draftId }),
|
|
136
137
|
* );
|
|
137
138
|
*
|
|
139
|
+
* // an action creator works as the name does, and gives its payload type
|
|
140
|
+
* const stopped = await take(ctx, stopRequested);
|
|
141
|
+
*
|
|
138
142
|
* // a type guard narrows the result: `failed` is `{ ok: false }`
|
|
139
143
|
* const failed = await take(ctx, "result", {
|
|
140
144
|
* predicate: (result): result is { ok: false } => !result.ok,
|
|
@@ -145,6 +149,14 @@ declare function take<E extends EventMap, K extends Keys<E>, T extends E[K]>(ctx
|
|
|
145
149
|
predicate: (payload: E[K]) => payload is T;
|
|
146
150
|
}): Promise<T>;
|
|
147
151
|
declare function take<E extends EventMap, K extends Keys<E>>(ctx: Context<E>, event: K, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
152
|
+
declare function take<E extends EventMap, K extends Keys<E>, T extends E[K]>(ctx: Context<E>, creator: FreeActionCreator<K>, options: TakeOptions<E[K]> & {
|
|
153
|
+
predicate: (payload: E[K]) => payload is T;
|
|
154
|
+
}): Promise<T>;
|
|
155
|
+
declare function take<E extends EventMap, K extends Keys<E>>(ctx: Context<E>, creator: FreeActionCreator<K>, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
156
|
+
declare function take<E extends EventMap, P, T extends P>(ctx: Context<E>, creator: ActionCreator<P>, options: TakeOptions<P> & {
|
|
157
|
+
predicate: (payload: P) => payload is T;
|
|
158
|
+
}): Promise<T>;
|
|
159
|
+
declare function take<E extends EventMap, P>(ctx: Context<E>, creator: ActionCreator<P>, options?: TakeOptions<P>): Promise<P>;
|
|
148
160
|
//#endregion
|
|
149
161
|
//#region src/effects/plugin.d.ts
|
|
150
162
|
/** The effects that `effectsPlugin` can put on `ctx`. */
|
|
@@ -171,7 +183,12 @@ type Bound<E extends EventMap> = {
|
|
|
171
183
|
<T extends Results>(effects: T): Promise<{ [K in keyof T]: PromiseSettledResult<Awaited<ReturnType<T[K]>>>; }>;
|
|
172
184
|
<const T extends List>(effects: T): Promise<{ -readonly [K in keyof T]: PromiseSettledResult<Awaited<ReturnType<T[K]>>>; }>;
|
|
173
185
|
};
|
|
174
|
-
take:
|
|
186
|
+
take: {
|
|
187
|
+
<K extends Keys<E>>(event: K, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
188
|
+
/** A creator without a payload type: the payload comes from the event map. */
|
|
189
|
+
<K extends Keys<E>>(creator: FreeActionCreator<K>, options?: TakeOptions<E[K]>): Promise<E[K]>;
|
|
190
|
+
<P>(creator: ActionCreator<P>, options?: TakeOptions<P>): Promise<P>;
|
|
191
|
+
};
|
|
175
192
|
delay: (ms: number | (() => number)) => Promise<void>;
|
|
176
193
|
};
|
|
177
194
|
/** Any function whose first parameter takes `{ signal }` can go on the context, not only the built-in ones. */
|
package/dist/policies.d.ts
CHANGED
|
@@ -10,6 +10,14 @@ type KeyOption<P> = {
|
|
|
10
10
|
*/
|
|
11
11
|
key?: (payload: P) => unknown;
|
|
12
12
|
};
|
|
13
|
+
/** Options of a policy called without a task: `key` is left out. */
|
|
14
|
+
type Unkeyed<O> = Omit<O, "key"> & {
|
|
15
|
+
key?: undefined;
|
|
16
|
+
};
|
|
17
|
+
/** Options of a policy called without a task, with a `key` that tells the payload type. */
|
|
18
|
+
type Keyed<O, P> = O & {
|
|
19
|
+
key: (payload: P) => unknown;
|
|
20
|
+
};
|
|
13
21
|
//#endregion
|
|
14
22
|
//#region src/policies/debounce.d.ts
|
|
15
23
|
type DebounceResult<R> = Promise<R | undefined> | Promise<Awaited<R> | undefined> | undefined;
|
|
@@ -19,6 +27,10 @@ type DebounceOptions<P = never> = KeyOption<P> & {
|
|
|
19
27
|
/** Run the last call of a burst after `ms` of quiet. Default `true`. */
|
|
20
28
|
trailing?: boolean;
|
|
21
29
|
};
|
|
30
|
+
/** What `debounce(ms)` returns: it turns a task into a `debounce` task. */
|
|
31
|
+
type DebounceWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
|
|
32
|
+
/** What `debounce(ms, { key })` returns: the payload type comes from `key`. */
|
|
33
|
+
type KeyedDebounceWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, DebounceResult<R>>;
|
|
22
34
|
/**
|
|
23
35
|
* Waits for `ms` of quiet, then runs; a new call restarts the wait and drops
|
|
24
36
|
* the previous. With `leading` the first call of a burst runs at once, and the
|
|
@@ -39,14 +51,24 @@ type DebounceOptions<P = never> = KeyOption<P> & {
|
|
|
39
51
|
* bus.emit("input", "re"); // only the last call runs, 300 ms after it
|
|
40
52
|
* ```
|
|
41
53
|
*/
|
|
42
|
-
export declare
|
|
54
|
+
export declare function debounce<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: DebounceOptions<P>): Task<P, E, DebounceResult<R>>;
|
|
55
|
+
export declare function debounce(ms: number, options?: Unkeyed<DebounceOptions>): DebounceWrapper;
|
|
56
|
+
export declare function debounce<P>(ms: number, options: Keyed<DebounceOptions<P>, P>): KeyedDebounceWrapper<P>;
|
|
43
57
|
//#endregion
|
|
44
58
|
//#region src/policies/latest.d.ts
|
|
45
59
|
type LatestOptions<P = never> = KeyOption<P>;
|
|
60
|
+
type LatestResult<R> = R | Promise<Awaited<R> | undefined> | undefined;
|
|
61
|
+
/** What `latest()` returns: it turns a task into a `latest` task. */
|
|
62
|
+
type LatestWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
|
|
63
|
+
/** What `latest({ key })` returns: the payload type comes from `key`. */
|
|
64
|
+
type KeyedLatestWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, LatestResult<R>>;
|
|
46
65
|
/**
|
|
47
66
|
* Aborts the previous run when a new call arrives; stale results are dropped.
|
|
48
67
|
* With `options.key` each key has its own latest run.
|
|
49
68
|
*
|
|
69
|
+
* Called without the task, `latest()` returns a function that takes the task,
|
|
70
|
+
* so `usePolicy(callback, latest())` has no `(task) => ...` to get wrong.
|
|
71
|
+
*
|
|
50
72
|
* @example
|
|
51
73
|
* ```ts
|
|
52
74
|
* bus.on(
|
|
@@ -62,12 +84,21 @@ type LatestOptions<P = never> = KeyOption<P>;
|
|
|
62
84
|
*
|
|
63
85
|
* // one latest run per user instead of one for all
|
|
64
86
|
* bus.on("load", latest(loadUser, { key: (user) => user.id }));
|
|
87
|
+
*
|
|
88
|
+
* // without the task: for a hook that builds the task itself
|
|
89
|
+
* const search = usePolicy(runSearch, latest());
|
|
65
90
|
* ```
|
|
66
91
|
*/
|
|
67
|
-
export declare
|
|
92
|
+
export declare function latest<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LatestOptions<P>): Task<P, E, LatestResult<R>>;
|
|
93
|
+
export declare function latest(options?: Unkeyed<LatestOptions>): LatestWrapper;
|
|
94
|
+
export declare function latest<P>(options: Keyed<LatestOptions<P>, P>): KeyedLatestWrapper<P>;
|
|
68
95
|
//#endregion
|
|
69
96
|
//#region src/policies/leading.d.ts
|
|
70
97
|
type LeadingOptions<P = never> = KeyOption<P>;
|
|
98
|
+
/** What `leading()` returns: it turns a task into a `leading` task. */
|
|
99
|
+
type LeadingWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
100
|
+
/** What `leading({ key })` returns: the payload type comes from `key`. */
|
|
101
|
+
type KeyedLeadingWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
71
102
|
/**
|
|
72
103
|
* Drops new calls while the task is still running. The lock is released when
|
|
73
104
|
* the run settles or when its task is aborted, whichever comes first, so a
|
|
@@ -87,7 +118,9 @@ type LeadingOptions<P = never> = KeyOption<P>;
|
|
|
87
118
|
* bus.emit("save", draft); // dropped while the first save is in flight
|
|
88
119
|
* ```
|
|
89
120
|
*/
|
|
90
|
-
export declare
|
|
121
|
+
export declare function leading<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: LeadingOptions<P>): Task<P, E, R | Promise<Awaited<R>> | undefined>;
|
|
122
|
+
export declare function leading(options?: Unkeyed<LeadingOptions>): LeadingWrapper;
|
|
123
|
+
export declare function leading<P>(options: Keyed<LeadingOptions<P>, P>): KeyedLeadingWrapper<P>;
|
|
91
124
|
//#endregion
|
|
92
125
|
//#region src/policies/queue.d.ts
|
|
93
126
|
type QueueOptions<P = never> = KeyOption<P> & {
|
|
@@ -102,6 +135,10 @@ type QueueOptions<P = never> = KeyOption<P> & {
|
|
|
102
135
|
*/
|
|
103
136
|
overflow?: "drop-new" | "drop-old";
|
|
104
137
|
};
|
|
138
|
+
/** What `queue()` returns: it turns a task into a `queue` task. */
|
|
139
|
+
type QueueWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
140
|
+
/** What `queue({ key })` returns: the payload type comes from `key`. */
|
|
141
|
+
type KeyedQueueWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
105
142
|
/**
|
|
106
143
|
* Runs calls one after another, in the order they arrived: a call starts when
|
|
107
144
|
* fewer than `concurrency` calls are running. Every call returns a promise with
|
|
@@ -132,7 +169,9 @@ type QueueOptions<P = never> = KeyOption<P> & {
|
|
|
132
169
|
* bus.emit("save", b); // waits for a, then runs
|
|
133
170
|
* ```
|
|
134
171
|
*/
|
|
135
|
-
export declare
|
|
172
|
+
export declare function queue<P, E extends EventMap, R>(fn: Task<P, E, R>, options?: QueueOptions<P>): Task<P, E, Promise<Awaited<R> | undefined>>;
|
|
173
|
+
export declare function queue(options?: Unkeyed<QueueOptions>): QueueWrapper;
|
|
174
|
+
export declare function queue<P>(options: Keyed<QueueOptions<P>, P>): KeyedQueueWrapper<P>;
|
|
136
175
|
//#endregion
|
|
137
176
|
//#region src/policies/throttle.d.ts
|
|
138
177
|
type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
@@ -141,6 +180,10 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
|
141
180
|
/** Run the latest call made while waiting, at the end of the window. Default `true`. */
|
|
142
181
|
trailing?: boolean;
|
|
143
182
|
};
|
|
183
|
+
/** What `throttle(ms)` returns: it turns a task into a `throttle` task. */
|
|
184
|
+
type ThrottleWrapper = <P, E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
185
|
+
/** What `throttle(ms, { key })` returns: the payload type comes from `key`. */
|
|
186
|
+
type KeyedThrottleWrapper<P> = <E extends EventMap, R>(fn: Task<P, E, R>) => Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
144
187
|
/**
|
|
145
188
|
* At most one run per `ms`, with the semantics of lodash `throttle` (and
|
|
146
189
|
* patronum): a call made when the throttle is idle, that is, at least `ms`
|
|
@@ -167,6 +210,8 @@ type ThrottleOptions<P = never> = KeyOption<P> & {
|
|
|
167
210
|
* bus.emit("scroll", 30); // runs once the 200 ms window closes
|
|
168
211
|
* ```
|
|
169
212
|
*/
|
|
170
|
-
export declare
|
|
213
|
+
export declare function throttle<P, E extends EventMap, R>(ms: number, fn: Task<P, E, R>, options?: ThrottleOptions<P>): Task<P, E, R | Promise<R | undefined> | undefined>;
|
|
214
|
+
export declare function throttle(ms: number, options?: Unkeyed<ThrottleOptions>): ThrottleWrapper;
|
|
215
|
+
export declare function throttle<P>(ms: number, options: Keyed<ThrottleOptions<P>, P>): KeyedThrottleWrapper<P>;
|
|
171
216
|
//#endregion
|
|
172
217
|
export { type DebounceOptions, type LatestOptions, type LeadingOptions, type PolicyFunction, type QueueOptions, type ThrottleOptions, withPolicy };
|
package/dist/policies.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{i as e,o as t,s as n,t as r,u as i}from"./context-CJGW6OWj.js";import{n as a,r as o,t as s}from"./delay-BnBdGPh7.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";import{n as u,t as d}from"./lifecycle-key-8iX8RUVD.js";import{t as f}from"./with-policy-CW50Edgk.js";const p=e=>Reflect.get(e,d),m=e=>{p(e)?.start()},h=(e,n,r)=>(m(e),t(e,n,r)),g=(e,t)=>{n(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,u,!0),e),b=()=>{},x=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},
|
|
1
|
+
import{i as e,o as t,s as n,t as r,u as i}from"./context-CJGW6OWj.js";import{n as a,r as o,t as s}from"./delay-BnBdGPh7.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";import{n as u,t as d}from"./lifecycle-key-8iX8RUVD.js";import{t as f}from"./with-policy-CW50Edgk.js";const p=e=>Reflect.get(e,d),m=e=>{p(e)?.start()},h=(e,n,r)=>(m(e),t(e,n,r)),g=(e,t)=>{n(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,u,!0),e),b=()=>{},x=(e,t,n)=>typeof e==`function`?n(e,t??{}):t=>n(t,e??{}),S=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},C=(e,t)=>y(e?S(e,t):t(b));function w(e,t){return x(e,t,T)}const T=(e,t)=>C(t.key,t=>E(`latest`,(t,n,r)=>(r(),e(t,n)),t)),E=(t,a,o=b)=>{let s=null;return(l,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=e(u);s=d;let p=r(d,t),h=!1,g=()=>{d.signal.aborted||(h=!0,m(d),n(d,t,`start`))};i(d,e=>{v(d,h,e),p(h?`cancel`:`drop`)});let _=()=>{f(),s===d&&(s=null,o())},y=e=>{if(!d.signal.aborted)return p(h?`done`:`drop`),e};try{let e=a(l,d,g);return c(e)?Promise.resolve(e).then(y,e=>{if(!d.signal.aborted)throw p(`error`,e),e}).finally(_):(_(),y(e))}catch(e){if(_(),d.signal.aborted)return;throw p(`error`,e),e}}};function D(e,t,n){return x(t,n,(t,n)=>O(e,t,n))}const O=(e,t,n)=>C(n.key,r=>A(e,t,n,r)),k=(e,t,n,r)=>{let i=r!==b,s=null,c=null;return(l,u)=>{if(u.signal.aborted)return;let d=Date.now(),f=s===null||d-s>=e||d-s<0;return s=d,c!==null&&a(c),c=i?o(r,e):null,n&&f?Promise.resolve(h(u,`debounce`,()=>t(l,u))):(g(u,`debounce`),Promise.resolve(void 0))}},A=(e,t,n,r)=>{let a=n.leading??!1,o=n.trailing??!0;if(!o)return k(e,t,a,r);let c=!1,l=0,u=E(`debounce`,([n,r],a,u)=>{let d=l;return i(a,()=>{l===d&&(c=!1)}),s(a,e).then(()=>{if(c=!1,o&&!r)return u(),t(n,a)})},r);return(e,n)=>{if(n.signal.aborted)return;let r=a&&!c;c=!0,l++;let i=u([e,r],n);return r?Promise.resolve(h(n,`debounce`,()=>t(e,n))):i}};function j(e,t){return x(e,t,M)}const M=(e,t)=>C(t.key,t=>{let n=null;return(r,a)=>{if(a.signal.aborted)return;if(n){g(a,`leading`);return}let o={};n=o;let s=()=>{c(),n===o&&(n=null,t())},c=i(a,s);try{return l(h(a,`leading`,()=>e(r,a)),s)}catch(e){throw s(),e}}});function N(e,t){return x(e,t,P)}const P=(e,t)=>C(t.key,n=>F(e,t,n)),F=(e,t,n)=>{let r=t.concurrency??1,a=t.limit??1/0,o=[],s=0,c=()=>{for(;s<r;){let e=o.shift();if(!e)return;e.start()}};return(l,u)=>new Promise((d,f)=>{if(u.signal.aborted){d(void 0);return}let p=b,m=()=>{p(),g(u,`queue`),d(void 0)},v=()=>{if(p(),u.signal.aborted){m();return}s++;let t=!0,r=()=>{t&&(t=!1,s--,c(),s===0&&o.length===0&&n())},a=i(u,()=>{r(),d(void 0)}),g;try{g=Promise.resolve(h(u,`queue`,()=>e(l,u)))}catch(e){g=Promise.reject(e)}let v=()=>{a(),r()};g.then(e=>{_(u,{value:e}),v(),d(u.signal.aborted?void 0:e)},e=>{_(u,{error:e}),v(),u.signal.aborted?d(void 0):f(e)})};if(s<r){v();return}if(o.length>=a){if(t.overflow!==`drop-old`||a<=0){m();return}o.shift()?.drop()}let y={start:v,drop:m};o.push(y),p=i(u,()=>{let e=o.indexOf(y);e>=0&&o.splice(e,1),m()})})};function I(e,t,n){return x(t,n,(t,n)=>L(e,t,n))}const L=(e,t,n)=>C(n.key,r=>R(e,t,n,r)),R=(e,t,n,r)=>{let i=n.leading??!0,s=n.trailing??!0,c=r!==b,l=null,u=0,d=null,f=null,p=null,m=!1,_=t=>l===null||t-l>=e||t-l<0||t-u>=e,v=e=>g(e,`throttle`),y=()=>{let e=p;p=null,e&&(v(e.ctx),e.resolve())},x=(e,t)=>(y(),new Promise((n,r)=>{p={payload:e,ctx:t,resolve:n,reject:r}})),S=(e,n,r)=>{y(),u=r,m=!0;try{return h(n,`throttle`,()=>t(e,n))}finally{m=!1}},C=()=>{c&&(f=o(w,e))},w=()=>{if(f=null,!c||d!==null||p)return;let t=Date.now(),n=Math.max(e-(t-(l??-1/0)),e-(t-u));n<=0?r():f=o(w,n)},T=e=>{d=null;let t=p;if(s&&t&&!t.ctx.signal.aborted){p=null;try{t.resolve(S(t.payload,t.ctx,e))}catch(e){t.reject(e)}}else y();w()},E=()=>{let t=Date.now();_(t)?T(t):d=o(E,Math.min(e-(t-(l??t)),e-(t-u)))},D=()=>{f!==null&&(a(f),f=null)},O=(e,t)=>{if(!s)v(t);else return x(e,t)},k=(e,t,n,r)=>{if(!r)v(t);else if(u=n,C(),!i)v(t);else return S(e,t,n)},A=(t,n,r,s)=>s&&d!==null?(a(d),d=o(E,e),S(t,n,r)):d===null&&(d=o(E,e),s&&(u=r,i))?S(t,n,r):O(t,n);return(e,t)=>{if(t.signal.aborted)return;let n=Date.now(),r=!m&&_(n);return l=n,D(),s?A(e,t,n,r):k(e,t,n,r)}};export{D as debounce,w as latest,j as leading,N as queue,I as throttle,f as withPolicy};
|
package/dist/react.d.ts
CHANGED
|
@@ -85,9 +85,11 @@ export declare const reactPlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus
|
|
|
85
85
|
* render. This hook keeps the policy across renders and calls the `callback` of
|
|
86
86
|
* the latest render, so an inline callback sees fresh props and state.
|
|
87
87
|
*
|
|
88
|
-
* `make`
|
|
89
|
-
*
|
|
90
|
-
*
|
|
88
|
+
* `make` builds the policy around the callback. Pass a policy called without
|
|
89
|
+
* its task (`debounce(300)`, `latest()`), or a function `(task) => policy` for
|
|
90
|
+
* a combined policy such as `(task) => throttle(100, latest(task))`; `task`
|
|
91
|
+
* runs the callback. `make` runs once, and again when an item of `deps`
|
|
92
|
+
* changes: list there what it reads, such as the wait time. The pending calls of the old function are
|
|
91
93
|
* cancelled then, and when the component unmounts. Needs `react` 18 or newer.
|
|
92
94
|
*
|
|
93
95
|
* @example
|
|
@@ -99,7 +101,7 @@ export declare const reactPlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus
|
|
|
99
101
|
* const [results, setResults] = useState([]);
|
|
100
102
|
* const onChange = usePolicy(
|
|
101
103
|
* async (query: string) => setResults(await search(query)), // latest render's callback
|
|
102
|
-
*
|
|
104
|
+
* debounce(wait), // the policy, made once per wait
|
|
103
105
|
* [wait],
|
|
104
106
|
* );
|
|
105
107
|
* return <input onChange={(event) => onChange(event.target.value)} />;
|
package/dist/scope.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { P as WithSignal, a as EmitScope, d as Effects, f as Signaled, h as Bus, o as Scope, u as Effect, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { n as ScopeSlots, r as Scoped } from "./types-
|
|
2
|
+
import { n as ScopeSlots, r as Scoped } from "./types-DC-aLJit.js";
|
|
3
3
|
//#region src/scope/effect.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Marks a function whose first parameter is `{ signal }` as an effect, so that
|
package/dist/svelte.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { T as EventKey, h as Bus, r as FreeActionCreator, t as ActionCreator, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
+
import { t as StateOf } from "./state-of-C19H6BoV.js";
|
|
3
|
+
import { Readable } from "svelte/store";
|
|
4
|
+
//#region src/svelte/plugin.d.ts
|
|
5
|
+
/** What `sveltePlugin` adds to the bus. */
|
|
6
|
+
type SvelteApi<E extends EventMap> = {
|
|
7
|
+
/**
|
|
8
|
+
* A readable Svelte store: `initial` at first, then the payload of every emit of `event`.
|
|
9
|
+
* The bus listens only while something subscribes (the first subscriber starts it, the last
|
|
10
|
+
* one stops it), and the bus keeps no values, so an event emitted while nothing reads the
|
|
11
|
+
* store is lost. It is state of the components that read it, not a store of the app. A
|
|
12
|
+
* function payload is stored, not called. The method does not read `this`, so
|
|
13
|
+
* `export const { eventStore } = bus` gives a free function.
|
|
14
|
+
*/
|
|
15
|
+
eventStore: {
|
|
16
|
+
<K extends EventKey<E>, S>(event: K, initial: StateOf<E[K], S>): Readable<StateOf<E[K], S>>;
|
|
17
|
+
/** A creator without a payload type: the payload comes from the event map. */
|
|
18
|
+
<K extends EventKey<E>, S>(creator: FreeActionCreator<K>, initial: StateOf<E[K], S>): Readable<StateOf<E[K], S>>;
|
|
19
|
+
<P, T extends string>(creator: ActionCreator<P, T>, initial: P): Readable<P>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Adds `bus.eventStore(event, initial)`: an event of the bus as a Svelte store, so `$found` in
|
|
24
|
+
* a component shows the last payload and Svelte subscribes and unsubscribes for you. The store
|
|
25
|
+
* is not a store of the app: it keeps the last payload only while something reads it, and an
|
|
26
|
+
* event that came before is lost. Keep a value that must outlive the component in a store of
|
|
27
|
+
* your own. Needs `svelte` 5 or newer. Returns the same bus.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { createBus } from "nanoeffects";
|
|
32
|
+
* import { createAction } from "nanoeffects/actions";
|
|
33
|
+
* import { sveltePlugin } from "nanoeffects/svelte";
|
|
34
|
+
*
|
|
35
|
+
* export const bus = createBus().use(sveltePlugin());
|
|
36
|
+
* export const { eventStore } = bus; // a free function
|
|
37
|
+
*
|
|
38
|
+
* const searchLoaded = createAction<string[]>("search/loaded");
|
|
39
|
+
*
|
|
40
|
+
* // in a component: `$found` is string[], set by every searchLoaded
|
|
41
|
+
* const found = eventStore(searchLoaded, []);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare const sveltePlugin: () => <E extends EventMap>(bus: Bus<E>) => Bus<E> & SvelteApi<E>;
|
|
45
|
+
//#endregion
|
|
46
|
+
export type { SvelteApi };
|
package/dist/svelte.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readable as e}from"svelte/store";const t=()=>t=>{function n(n,r){let i=n;return e(r,e=>t.on(i,e))}return Object.assign(t,{eventStore:n})};export{t as sveltePlugin};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { d as Effects, o as Scope, s as WithFields, z as EventMap } from "./action-creator-Bpa8BUS9.js";
|
|
2
|
-
import { t as BoundOf } from "./plugin-
|
|
2
|
+
import { t as BoundOf } from "./plugin-BiGv84Xu.js";
|
|
3
3
|
//#region src/scope/types.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* The slots `scopePlugin` fills for `modulePlugin`. They are not on `Bus`: a bus
|