nanoeffects 0.9.0 → 0.10.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.js +1 -1
- package/dist/lifecycle-key-8iX8RUVD.js +1 -0
- package/dist/notrace/actions.js +1 -1
- package/dist/notrace/lifecycle-key-8iX8RUVD.js +1 -0
- package/dist/notrace/policies.js +1 -1
- package/dist/policies.js +1 -1
- package/docs/actions.md +10 -3
- package/docs/migrating-from-effector.md +1 -1
- package/llms-full.txt +11 -4
- package/package.json +2 -2
package/dist/actions.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{n as e,t}from"./onAbort-BgMfspK4.js";import{i as n,n as r,s as i}from"./context-COgyNlrC.js";import{t as a}from"./event-0RIBGYqS.js";import{i as o}from"./bus-CkTHUAvq.js";import{t as
|
|
1
|
+
import{n as e,t}from"./onAbort-BgMfspK4.js";import{i as n,n as r,s as i}from"./context-COgyNlrC.js";import{t as a}from"./event-0RIBGYqS.js";import{i as o}from"./bus-CkTHUAvq.js";import{n as s,t as c}from"./lifecycle-key-8iX8RUVD.js";import{t as l}from"./scope-key-B7l7mUS5.js";const u=(e,t)=>(t&&Object.defineProperty(e,l,{value:t}),e),d=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),f=e=>Reflect.get(e,l);function p(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(u((...t)=>u({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var m=class extends Error{};function h(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new m(`No handler for command "${e}"`);return t(r,i)}return u(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=u(a,n);return s}const g=(e,t)=>[e,t],_=e=>typeof Reflect.get(e??{},`handle`)==`function`,v=(e=[])=>t=>{let n=t.commands;if(_(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},y=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},b=e=>typeof e==`string`?p(e):e,x=(e,t,n,r)=>{let i=f(t);e.emit(Reflect.apply(t,null,[n]),r&&i?d(r,i):r)},S=(t,n,r,a)=>{let o=t;o[i]=!0;try{x(t,n,r,a)}catch(t){e(`cancel listener`,t)}o[i]=!1},C=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof m)throw e;return Promise.reject(e)}},w=(n,r,i={},a)=>{let l=h(n,r),d=e=>b(i[e]??`${n}/${e}`),f=d(`pending`),p=d(`success`),m=d(`fail`),g=d(`cancel`),_=(n,r,i,o)=>{let s=i,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},h=e=>{l===`started`&&(d(),S(s,g,e,a))},_=()=>{l===`idle`&&(l=`started`,u=t(s,h),x(s,f,r,a))},v=e=>{l===`started`&&(d(),s.signal.aborted?S(s,g,s.signal.reason,a):`error`in e?x(s,m,e.error,a):x(s,p,e.value,a))};return Reflect.set(s,c,{start:_,cancel:h,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),C(()=>(o||_(),Reflect.apply(n,null,[r,i]))).then(e=>(v({value:e}),e),t=>{if(l===`idle`)try{_()}catch(t){e(`pending listener`,t)}v({error:t})})},v=e=>{let t=Reflect.get(e,s)===!0;return(n,r)=>_(e,n,r,t)};function y(e,t){return typeof t==`object`?r?_(l,e,t,!1):Reflect.apply(l,null,[e,t]):u(o(T,n,e),a)}let w=Object.assign(y,{type:n,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:g}),T=u(w,a);return T},T=e=>typeof e==`object`&&!!e,E=e=>{if(typeof e==`string`)return e;if(T(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},D=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(E(n),E(t[0]))&&(r===void 0||Object.is(r,t[1]))},O=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`&&D(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 k(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const A=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,j=(e,t,n)=>{n&&e.applyScope?.(t,n)},M=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],N=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},P=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},F=(e=[])=>i=>{let a=i.commands??v()(i),o={handle:(...e)=>{if(!a.handle)throw Error("modulePlugin: the `commands` of the bus is not a registry from `commandsPlugin()`");return Reflect.apply(a.handle,null,e)}},s=e=>{let r=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map;return{mount:c=>{let l=r.get(c);if(l)return l;let u=a.get(c.name);u&&r.get(u)?.();let d=n(e,c.name),f=d.ctx,p=s(f),m=()=>{r.delete(c),a.get(c.name)===c&&a.delete(c.name),f.abort(),d.unlink()};r.set(c,m),a.set(c.name,c);let h=c.scope,g=A(i,h),_=(e,n)=>{let r=Reflect.apply(o.handle,null,M(g,e,n));return t(f,r),r},v=(...e)=>{let t=e.map(e=>typeof e==`function`?_(e,e):p.mount(e));return()=>{for(let e of t)e()}},y=P(f),b=N(f,g);try{j(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=O(i),l=i.emit,u,d=(t=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let n=r({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:n,apps:s(n.ctx)},t)for(let t of e)u.apps.mount(t);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,i,e));i.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:k};return i.app=p,p};export{g as bindHandler,v as commandsPlugin,p as createAction,h as createCommand,w as createJob,k as createModule,y as emitterPlugin,F as modulePlugin};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=Symbol(),t=Symbol();export{t as n,e as t};
|
package/dist/notrace/actions.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{c as e,i as t,l as n,n as r,s as i}from"./context-yYU3_9lq.js";import{a,r as o}from"./bus-BmCTo8BG.js";const
|
|
1
|
+
import{c as e,i as t,l as n,n as r,s as i}from"./context-yYU3_9lq.js";import{a,r as o}from"./bus-BmCTo8BG.js";import{n as s,t as c}from"./lifecycle-key-8iX8RUVD.js";const l=Symbol.for(`nanoeffects.scope`),u=(e,t)=>(t&&Object.defineProperty(e,l,{value:t}),e),d=(e,t)=>({...e,...t,$effects:{...e.$effects,...t.$effects}}),f=e=>Reflect.get(e,l);function p(e,t,n){let r=typeof t==`function`?t:void 0,i=typeof t==`function`?n:t;return Object.assign(u((...t)=>u({type:e,payload:r?r(...t).payload:t[0]},i),i),{type:e,match:t=>t.type===e,toString:()=>e})}var m=class extends Error{};function h(e,t,n){let r=t=>t.type===e;function i(r,i){if(typeof i==`object`){if(!t)throw new m(`No handler for command "${e}"`);return t(r,i)}return u(o(s,e,r),n)}let a=Object.assign(i,{type:e,match:r}),s=u(a,n);return s}const g=(e,t)=>[e,t],_=e=>typeof Reflect.get(e??{},`handle`)==`function`,v=(e=[])=>t=>{let n=t.commands;if(_(n))return n.handle(e),n;let r=/* @__PURE__ */ new Map,i=(e,t)=>{let n={fn:t};return r.set(e,[...r.get(e)??[],n]),()=>{let t=(r.get(e)??[]).filter(e=>e!==n);t.length>0?r.set(e,t):r.delete(e)}};function o(e,t){if(t)return i(a(e),t);let n=e.map(([e,t])=>i(a(e),t));return()=>{for(let e of n)e()}}o(e);let s={run:(e,t,n)=>{let i=(r.get(e.type)??[]).at(-1)?.fn,a=i&&e.wrap?e.wrap(i):i??e;return Reflect.apply(a,null,[t,n])},handle:o};return t.commands=s,s},y=()=>e=>{function t(t){return(...n)=>{let r=t(...n);e.emit(r)}}return Object.assign(e,{emitter:t})},b=e=>typeof e==`string`?p(e):e,x=(e,t,n,r)=>{let i=f(t);e.emit(Reflect.apply(t,null,[n]),r&&i?d(r,i):r)},S=(e,t,r,a)=>{let o=e;o[i]=!0;try{x(e,t,r,a)}catch(e){n(`cancel listener`,e)}o[i]=!1},C=e=>{try{return Promise.resolve(e())}catch(e){if(e instanceof m)throw e;return Promise.reject(e)}},w=(t,r,i={},a)=>{let l=h(t,r),d=e=>b(i[e]??`${t}/${e}`),f=d(`pending`),p=d(`success`),m=d(`fail`),g=d(`cancel`),_=(t,r,i,o)=>{let s=i,l=`idle`,u=()=>{},d=()=>{l=`closed`,u()},h=e=>{l===`started`&&(d(),S(s,g,e,a))},_=()=>{l===`idle`&&(l=`started`,u=e(s,h),x(s,f,r,a))},v=e=>{l===`started`&&(d(),s.signal.aborted?S(s,g,s.signal.reason,a):`error`in e?x(s,m,e.error,a):x(s,p,e.value,a))};return Reflect.set(s,c,{start:_,cancel:h,done:v,drop:()=>{l===`idle`&&(l=`closed`)}}),C(()=>(o||_(),Reflect.apply(t,null,[r,i]))).then(e=>(v({value:e}),e),e=>{if(l===`idle`)try{_()}catch(e){n(`pending listener`,e)}v({error:e})})},v=e=>{let t=Reflect.get(e,s)===!0;return(n,r)=>_(e,n,r,t)};function y(e,n){return typeof n==`object`?r?_(l,e,n,!1):Reflect.apply(l,null,[e,n]):u(o(T,t,e),a)}let w=Object.assign(y,{type:t,match:l.match,wrap:v,pending:f,success:p,fail:m,cancel:g}),T=u(w,a);return T},T=e=>typeof e==`object`&&!!e,E=e=>{if(typeof e==`string`)return e;if(T(e)||typeof e==`function`){let t=Reflect.get(e,`type`);return typeof t==`string`?t:e}return e},D=(e,t)=>{let[n,r]=e;return n===void 0||Object.is(E(n),E(t[0]))&&(r===void 0||Object.is(r,t[1]))},O=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`&&D(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 k(e,t,n){return n?{name:e,setup:t,scope:n}:{name:e,setup:t}}const A=(e,t)=>n=>t?e.fitScope?.(t,n)??n:n,j=(e,t,n)=>{n&&e.applyScope?.(t,n)},M=(e,t,n)=>Array.isArray(t)?[t.map(([t,n])=>[t,e(n)]),n]:[t,e(n)],N=(e,t)=>{let n=e.on;return(...e)=>{let[r,i]=e;return Reflect.apply(n,null,[r,t(i)])}},P=e=>(t,...n)=>{e.run(t,...n).catch(t=>{if(!e.signal.aborted)throw t})},F=(n=[])=>i=>{let a=i.commands??v()(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=A(i,h),_=(t,n)=>{let r=Reflect.apply(o.handle,null,M(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=P(f),b=N(f,g);try{j(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=O(i),l=i.emit,u,d=(e=!0)=>{if(u)return u;c.release(),i.emit===f&&(i.emit=l);let t=r({event:{type:`app`,payload:void 0},bus:i,tracer:i.trace});if(u={root:t,apps:s(t.ctx)},e)for(let e of n)u.apps.mount(e);return c.replay(),u},f=(...e)=>(d(),Reflect.apply(l,i,e));i.emit=f;let p={get commands(){return d(),a},mount:e=>{let{root:t,apps:n}=d();return t.ctx.signal.throwIfAborted(),n.mount(e)},unmount:e=>d().apps.unmount(e),isMounted:e=>d().apps.isMounted(e),dispose:()=>d(!1).root.ctx.abort(),createModule:k};return i.app=p,p};export{g as bindHandler,v as commandsPlugin,p as createAction,h as createCommand,w as createJob,k as createModule,y as emitterPlugin,F as modulePlugin};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=Symbol(),t=Symbol();export{t as n,e as t};
|
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";const d=()=>{},
|
|
1
|
+
import{a as e,c as t,i as n,n as r,o as i,t as a}from"./context-yYU3_9lq.js";import{n as o,r as s,t as c}from"./delay-BosJKFjg.js";import{n as l,t as u}from"./promise-CSLpzwSR.js";import{n as d,t as f}from"./lifecycle-key-8iX8RUVD.js";const p=e=>Reflect.get(e,f),m=e=>{p(e)?.start()},h=(t,n,r)=>(m(t),e(t,n,r)),g=(e,t)=>{i(e,t,`drop`),p(e)?.drop()},_=(e,t)=>{p(e)?.done(t)},v=(e,t,n)=>{let r=p(e);t?r?.cancel(n):r?.drop()},y=e=>(Reflect.set(e,d,!0),e),b=()=>{},x=(e,t)=>{let n=/* @__PURE__ */ new Map;return(r,i)=>{let a=e(r),o=n.get(a);if(!o){let e=t(()=>{n.get(a)===e&&n.delete(a)});o=e,n.set(a,e)}return o(r,i)}},S=(e,t)=>y(e?x(e,t):t(b)),C=(e,t={})=>S(t.key,t=>w(`latest`,(t,n,r)=>(r(),e(t,n)),t)),w=(e,r,o=b)=>{let s=null;return(c,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=a(d,e),h=!1,g=()=>{d.signal.aborted||(h=!0,m(d),i(d,e,`start`))};t(d,e=>{v(d,h,e),p(h?`cancel`:`drop`)});let _=()=>{f(),s===d&&(s=null,o())},y=e=>{if(!d.signal.aborted)return p(h?`done`:`drop`),e};try{let e=r(c,d,g);return l(e)?Promise.resolve(e).then(y,e=>{if(!d.signal.aborted)throw p(`error`,e),e}).finally(_):(_(),y(e))}catch(e){if(_(),d.signal.aborted)return;throw p(`error`,e),e}}},T=(e,t,n={})=>S(n.key,r=>D(e,t,n,r)),E=(e,t,n,r)=>{let i=r!==b,a=null,c=null;return(l,u)=>{if(u.signal.aborted)return;let d=Date.now(),f=a===null||d-a>=e||d-a<0;return a=d,c!==null&&o(c),c=i?s(r,e):null,n&&f?Promise.resolve(h(u,`debounce`,()=>t(l,u))):(g(u,`debounce`),Promise.resolve(void 0))}},D=(e,n,r,i)=>{let a=r.leading??!1,o=r.trailing??!0;if(!o)return E(e,n,a,i);let s=!1,l=0,u=w(`debounce`,([r,i],a,u)=>{let d=l;return t(a,()=>{l===d&&(s=!1)}),c(a,e).then(()=>{if(s=!1,o&&!i)return u(),n(r,a)})},i);return(e,t)=>{if(t.signal.aborted)return;let r=a&&!s;s=!0,l++;let i=u([e,r],t);return r?Promise.resolve(h(t,`debounce`,()=>n(e,t))):i}},O=(e,n={})=>S(n.key,n=>{let r=null;return(i,a)=>{if(a.signal.aborted)return;if(r){g(a,`leading`);return}let o={};r=o;let s=()=>{c(),r===o&&(r=null,n())},c=t(a,s);try{return u(h(a,`leading`,()=>e(i,a)),s)}catch(e){throw s(),e}}}),k=(e,t={})=>S(t.key,n=>A(e,t,n)),A=(e,n,r)=>{let i=n.concurrency??1,a=n.limit??1/0,o=[],s=0,c=()=>{for(;s<i;){let e=o.shift();if(!e)return;e.start()}};return(l,u)=>new Promise((d,f)=>{if(u.signal.aborted){d(void 0);return}let p=b,m=()=>{p(),g(u,`queue`),d(void 0)},v=()=>{if(p(),u.signal.aborted){m();return}s++;let n=!0,i=()=>{n&&(n=!1,s--,c(),s===0&&o.length===0&&r())},a=t(u,()=>{i(),d(void 0)}),g;try{g=Promise.resolve(h(u,`queue`,()=>e(l,u)))}catch(e){g=Promise.reject(e)}let v=()=>{a(),i()};g.then(e=>{_(u,{value:e}),v(),d(u.signal.aborted?void 0:e)},e=>{_(u,{error:e}),v(),u.signal.aborted?d(void 0):f(e)})};if(s<i){v();return}if(o.length>=a){if(n.overflow!==`drop-old`||a<=0){m();return}o.shift()?.drop()}let y={start:v,drop:m};o.push(y),p=t(u,()=>{let e=o.indexOf(y);e>=0&&o.splice(e,1),m()})})},j=(e,t,n={})=>S(n.key,r=>M(e,t,n,r)),M=(e,t,n,r)=>{let i=n.leading??!0,a=n.trailing??!0,c=r!==b,l=null,u=0,d=null,f=null,p=null,m=!1,_=t=>l===null||t-l>=e||t-l<0||t-u>=e,v=e=>g(e,`throttle`),y=()=>{let e=p;p=null,e&&(v(e.ctx),e.resolve())},x=(e,t)=>(y(),new Promise((n,r)=>{p={payload:e,ctx:t,resolve:n,reject:r}})),S=(e,n,r)=>{y(),u=r,m=!0;try{return h(n,`throttle`,()=>t(e,n))}finally{m=!1}},C=()=>{c&&(f=s(w,e))},w=()=>{if(f=null,!c||d!==null||p)return;let t=Date.now(),n=Math.max(e-(t-(l??-1/0)),e-(t-u));n<=0?r():f=s(w,n)},T=e=>{d=null;let t=p;if(a&&t&&!t.ctx.signal.aborted){p=null;try{t.resolve(S(t.payload,t.ctx,e))}catch(e){t.reject(e)}}else y();w()},E=()=>{let t=Date.now();_(t)?T(t):d=s(E,Math.min(e-(t-(l??t)),e-(t-u)))},D=()=>{f!==null&&(o(f),f=null)},O=(e,t)=>{if(!a)v(t);else return x(e,t)},k=(e,t,n,r)=>{if(!r)v(t);else if(u=n,C(),!i)v(t);else return S(e,t,n)},A=(t,n,r,a)=>a&&d!==null?(o(d),d=s(E,e),S(t,n,r)):d===null&&(d=s(E,e),a&&(u=r,i))?S(t,n,r):O(t,n);return(e,t)=>{if(t.signal.aborted)return;let n=Date.now(),r=!m&&_(n);return l=n,D(),a?A(e,t,n,r):k(e,t,n,r)}},N=Reflect.get(Symbol,`dispose`),P=e=>{let t=new AbortController,n=async(...n)=>{let i=n[0],{ctx:a,unlink:o}=r({event:{type:`withPolicy`,payload:i},parent:{signal:t.signal}});try{return await e(i,a)}finally{o()}},i=e=>{t.abort(e),t=new AbortController},a=Object.assign(n,{cancel:i});return N&&Reflect.set(a,N,()=>i()),a};export{T as debounce,C as latest,O as leading,k as queue,j as throttle,P as withPolicy};
|
package/dist/policies.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as e}from"./onAbort-BgMfspK4.js";import{a as t,i as n,o as r,t as i}from"./context-COgyNlrC.js";import{n as a,r as o,t as s}from"./delay-CIHnOh_L.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";import{t as
|
|
1
|
+
import{t as e}from"./onAbort-BgMfspK4.js";import{a as t,i as n,o as r,t as i}from"./context-COgyNlrC.js";import{n as a,r as o,t as s}from"./delay-CIHnOh_L.js";import{n as c,t as l}from"./promise-CSLpzwSR.js";import{n as u,t as d}from"./lifecycle-key-8iX8RUVD.js";import{t as f}from"./with-policy-DBV9dh2F.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)=>{r(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)}},S=(e,t)=>y(e?x(e,t):t(b)),C=(e,t={})=>S(t.key,t=>w(`latest`,(t,n,r)=>(r(),e(t,n)),t)),w=(t,a,o=b)=>{let s=null;return(l,u)=>{if(s?.abort(),u.signal.aborted)return;let{ctx:d,unlink:f}=n(u);s=d;let p=i(d,t),h=!1,g=()=>{d.signal.aborted||(h=!0,m(d),r(d,t,`start`))};e(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}}},T=(e,t,n={})=>S(n.key,r=>D(e,t,n,r)),E=(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))}},D=(t,n,r,i)=>{let a=r.leading??!1,o=r.trailing??!0;if(!o)return E(t,n,a,i);let c=!1,l=0,u=w(`debounce`,([r,i],a,u)=>{let d=l;return e(a,()=>{l===d&&(c=!1)}),s(a,t).then(()=>{if(c=!1,o&&!i)return u(),n(r,a)})},i);return(e,t)=>{if(t.signal.aborted)return;let r=a&&!c;c=!0,l++;let i=u([e,r],t);return r?Promise.resolve(h(t,`debounce`,()=>n(e,t))):i}},O=(t,n={})=>S(n.key,n=>{let r=null;return(i,a)=>{if(a.signal.aborted)return;if(r){g(a,`leading`);return}let o={};r=o;let s=()=>{c(),r===o&&(r=null,n())},c=e(a,s);try{return l(h(a,`leading`,()=>t(i,a)),s)}catch(e){throw s(),e}}}),k=(e,t={})=>S(t.key,n=>A(e,t,n)),A=(t,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=e(u,()=>{i(),d(void 0)}),g;try{g=Promise.resolve(h(u,`queue`,()=>t(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=e(u,()=>{let e=o.indexOf(y);e>=0&&o.splice(e,1),m()})})},j=(e,t,n={})=>S(n.key,r=>M(e,t,n,r)),M=(e,t,n,r)=>{let i=n.leading??!0,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{T as debounce,C as latest,O as leading,k as queue,j as throttle,f as withPolicy};
|
package/docs/actions.md
CHANGED
|
@@ -118,7 +118,7 @@ The registry lives in `nanoeffects/actions`, so a bus without one carries none o
|
|
|
118
118
|
- When the task is aborted before the handler settles, emits `loadUser.cancel(reason)` instead of `success` or `fail`. After `pending`, exactly one of the three follows.
|
|
119
119
|
- `pending` / `success` / `fail` / `cancel` are a string (job makes an action creator) or an existing action creator. Missing ones become `<type>/pending`, `<type>/success`, `<type>/fail` or `<type>/cancel`.
|
|
120
120
|
- `bus.emit(loadUser(id))` returns a promise of the result. Handler errors are always handled: they go to `fail` and the promise resolves with `undefined`. For a result that must reject, use a command.
|
|
121
|
-
- Outcome actions are emitted as the task that ran the job (also for a handler bound with `commands.handle`). `success` and `fail` are not sent after abort; `cancel` is,
|
|
121
|
+
- Outcome actions are emitted as the task that ran the job (also for a handler bound with `commands.handle`). `success` and `fail` are not sent after abort; `cancel` is, at the moment of the abort.
|
|
122
122
|
- A missing handler is not an outcome: `emit` throws `No handler for command "..."` and nothing is sent.
|
|
123
123
|
- Calling the job only builds `{ type, payload }`; the handler runs on `emit`.
|
|
124
124
|
- The outcomes are events like any other: with `onUnhandled` set on the bus, it is called for a `pending` or a `cancel` that nobody listens to.
|
|
@@ -185,9 +185,16 @@ import { loadUser } from "./user";
|
|
|
185
185
|
bus.emit(loadUser("1"));
|
|
186
186
|
```
|
|
187
187
|
|
|
188
|
-
`cancel` is sent
|
|
188
|
+
`cancel` is sent at the moment of the abort, with the abort reason. A handler that ignores the signal and finishes later sends nothing more, so the flag is already `false`.
|
|
189
189
|
|
|
190
|
-
**
|
|
190
|
+
**With a policy.** A policy such as `latest`, `leading`, `debounce`, `throttle` or `queue` around the handler tells the job what it does with each call:
|
|
191
|
+
|
|
192
|
+
- `pending` is sent when the handler really starts: at once for `leading`, after the wait for `debounce`, when its turn comes for `queue`.
|
|
193
|
+
- A call that the policy drops before it starts sends nothing, not even `pending`. A second click that `leading` ignores does not clear the flag of the first.
|
|
194
|
+
- A run that `latest` replaces sends `cancel` before the next call sends `pending`.
|
|
195
|
+
- Every `pending` is followed by exactly one of `success`, `fail` and `cancel`, and nothing is sent without `pending`.
|
|
196
|
+
|
|
197
|
+
A flag is exact when calls do not overlap, or when `latest` replaces them. If several runs can go at once (`queue` with `concurrency` above 1, or a job without a policy called many times), keep a counter: add one on `pending` and take one off on `success`, `fail` and `cancel`. A policy that you write yourself, or that you hide in your own function, does not report to the job; `pending` is then sent at the call, as for a job with no policy.
|
|
191
198
|
|
|
192
199
|
## Mount domains with modules
|
|
193
200
|
|
|
@@ -318,7 +318,7 @@ bus.on(
|
|
|
318
318
|
## What is different
|
|
319
319
|
|
|
320
320
|
- **No state.** There is no `createStore`, `combine`, `restore`, `$store.map`. The bus holds listeners only. Take a store such as Zustand for state ([Stores](stores.md)).
|
|
321
|
-
- **`pending` is an action, not a store.** `createJob` sends `pending` when the handler starts, and `success`, `fail` or `cancel` when it ends; you keep the flag in your store ([Show a loading state](actions.md#show-a-loading-state-with-a-job)).
|
|
321
|
+
- **`pending` is an action, not a store.** `createJob` sends `pending` when the handler starts, and `success`, `fail` or `cancel` when it ends; you keep the flag in your store ([Show a loading state](actions.md#show-a-loading-state-with-a-job)). Policies report to the job, so the flag also follows `latest`, `leading`, `debounce`, `throttle` and `queue`; that section says when to count instead. A running task is also visible through [Tracing](tracing.md).
|
|
322
322
|
- **Cancellation is the default tool.** An Effector effect runs every call to the end. Here `latest` and `ctx.signal` stop what is no longer needed.
|
|
323
323
|
- **Events are matched by name, not by identity.** An event is a string or a `createAction` creator, not a unit that you import and call. Two modules agree on the name.
|
|
324
324
|
- **No graph.** `sample` and `forward` build a static graph. A bus subscribes handlers at runtime and the call order is the order of `on`.
|
package/llms-full.txt
CHANGED
|
@@ -1859,7 +1859,7 @@ The registry lives in `nanoeffects/actions`, so a bus without one carries none o
|
|
|
1859
1859
|
- When the task is aborted before the handler settles, emits `loadUser.cancel(reason)` instead of `success` or `fail`. After `pending`, exactly one of the three follows.
|
|
1860
1860
|
- `pending` / `success` / `fail` / `cancel` are a string (job makes an action creator) or an existing action creator. Missing ones become `<type>/pending`, `<type>/success`, `<type>/fail` or `<type>/cancel`.
|
|
1861
1861
|
- `bus.emit(loadUser(id))` returns a promise of the result. Handler errors are always handled: they go to `fail` and the promise resolves with `undefined`. For a result that must reject, use a command.
|
|
1862
|
-
- Outcome actions are emitted as the task that ran the job (also for a handler bound with `commands.handle`). `success` and `fail` are not sent after abort; `cancel` is,
|
|
1862
|
+
- Outcome actions are emitted as the task that ran the job (also for a handler bound with `commands.handle`). `success` and `fail` are not sent after abort; `cancel` is, at the moment of the abort.
|
|
1863
1863
|
- A missing handler is not an outcome: `emit` throws `No handler for command "..."` and nothing is sent.
|
|
1864
1864
|
- Calling the job only builds `{ type, payload }`; the handler runs on `emit`.
|
|
1865
1865
|
- The outcomes are events like any other: with `onUnhandled` set on the bus, it is called for a `pending` or a `cancel` that nobody listens to.
|
|
@@ -1926,9 +1926,16 @@ import { loadUser } from "./user";
|
|
|
1926
1926
|
bus.emit(loadUser("1"));
|
|
1927
1927
|
```
|
|
1928
1928
|
|
|
1929
|
-
`cancel` is sent
|
|
1929
|
+
`cancel` is sent at the moment of the abort, with the abort reason. A handler that ignores the signal and finishes later sends nothing more, so the flag is already `false`.
|
|
1930
1930
|
|
|
1931
|
-
**
|
|
1931
|
+
**With a policy.** A policy such as `latest`, `leading`, `debounce`, `throttle` or `queue` around the handler tells the job what it does with each call:
|
|
1932
|
+
|
|
1933
|
+
- `pending` is sent when the handler really starts: at once for `leading`, after the wait for `debounce`, when its turn comes for `queue`.
|
|
1934
|
+
- A call that the policy drops before it starts sends nothing, not even `pending`. A second click that `leading` ignores does not clear the flag of the first.
|
|
1935
|
+
- A run that `latest` replaces sends `cancel` before the next call sends `pending`.
|
|
1936
|
+
- Every `pending` is followed by exactly one of `success`, `fail` and `cancel`, and nothing is sent without `pending`.
|
|
1937
|
+
|
|
1938
|
+
A flag is exact when calls do not overlap, or when `latest` replaces them. If several runs can go at once (`queue` with `concurrency` above 1, or a job without a policy called many times), keep a counter: add one on `pending` and take one off on `success`, `fail` and `cancel`. A policy that you write yourself, or that you hide in your own function, does not report to the job; `pending` is then sent at the call, as for a job with no policy.
|
|
1932
1939
|
|
|
1933
1940
|
## Mount domains with modules
|
|
1934
1941
|
|
|
@@ -4244,7 +4251,7 @@ bus.on(
|
|
|
4244
4251
|
## What is different
|
|
4245
4252
|
|
|
4246
4253
|
- **No state.** There is no `createStore`, `combine`, `restore`, `$store.map`. The bus holds listeners only. Take a store such as Zustand for state ([Stores](stores.md)).
|
|
4247
|
-
- **`pending` is an action, not a store.** `createJob` sends `pending` when the handler starts, and `success`, `fail` or `cancel` when it ends; you keep the flag in your store ([Show a loading state](actions.md#show-a-loading-state-with-a-job)).
|
|
4254
|
+
- **`pending` is an action, not a store.** `createJob` sends `pending` when the handler starts, and `success`, `fail` or `cancel` when it ends; you keep the flag in your store ([Show a loading state](actions.md#show-a-loading-state-with-a-job)). Policies report to the job, so the flag also follows `latest`, `leading`, `debounce`, `throttle` and `queue`; that section says when to count instead. A running task is also visible through [Tracing](tracing.md).
|
|
4248
4255
|
- **Cancellation is the default tool.** An Effector effect runs every call to the end. Here `latest` and `ctx.signal` stop what is no longer needed.
|
|
4249
4256
|
- **Events are matched by name, not by identity.** An event is a string or a `createAction` creator, not a unit that you import and call. Two modules agree on the name.
|
|
4250
4257
|
- **No graph.** `sample` and `forward` build a static graph. A bus subscribes handlers at runtime and the call order is the order of `on`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nanoeffects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Cancel stale async work and fix race conditions in event handlers: a typed event bus where every handler is a task with an AbortSignal, plus latest, debounce, throttle, queue, retry, timeout and poll. About 1.4 kB gzip.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"event-bus",
|
|
@@ -201,7 +201,7 @@
|
|
|
201
201
|
{
|
|
202
202
|
"name": "actions (all)",
|
|
203
203
|
"path": "dist/actions.js",
|
|
204
|
-
"limit": "2.
|
|
204
|
+
"limit": "2.86 kB"
|
|
205
205
|
},
|
|
206
206
|
{
|
|
207
207
|
"name": "match (plugin)",
|