@zeix/cause-effect 0.13.0 → 0.13.1
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/README.md +1 -1
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/index.ts +2 -2
- package/lib/computed.d.ts +3 -3
- package/lib/computed.ts +4 -4
- package/lib/signal.d.ts +4 -3
- package/lib/signal.ts +4 -3
- package/package.json +2 -2
package/README.md
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name Cause & Effect
|
|
3
|
-
* @version 0.13.
|
|
3
|
+
* @version 0.13.1
|
|
4
4
|
* @author Esther Brunner
|
|
5
5
|
*/
|
|
6
6
|
export { CircularDependencyError } from './lib/util';
|
|
7
|
-
export { type Signal, type MaybeSignal, UNSET, isSignal, isComputedCallback, toSignal } from './lib/signal';
|
|
7
|
+
export { type Signal, type MaybeSignal, type ComputedCallback, UNSET, isSignal, isComputedCallback, toSignal } from './lib/signal';
|
|
8
8
|
export { type State, state, isState } from './lib/state';
|
|
9
9
|
export { type Computed, type ComputedMatcher, computed, isComputed } from './lib/computed';
|
|
10
10
|
export { type EffectMatcher, type TapMatcher, effect } from './lib/effect';
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var X=(x)=>typeof x==="function",d=(x)=>X(x)&&x.constructor.name==="AsyncFunction",P=(x,y)=>Object.prototype.toString.call(x)===`[object ${y}]`,c=(x)=>x instanceof Error,I=(x)=>x instanceof DOMException&&x.name==="AbortError",m=(x)=>x instanceof Promise,j=(x)=>c(x)?x:Error(String(x));class
|
|
1
|
+
var X=(x)=>typeof x==="function",d=(x)=>X(x)&&x.constructor.name==="AsyncFunction",P=(x,y)=>Object.prototype.toString.call(x)===`[object ${y}]`,c=(x)=>x instanceof Error,I=(x)=>x instanceof DOMException&&x.name==="AbortError",m=(x)=>x instanceof Promise,j=(x)=>c(x)?x:Error(String(x));class C extends Error{constructor(x){super(`Circular dependency in ${x} detected`);return this}}var A,R=new Set,U=0,_=new Map,D,v=()=>{D=void 0;let x=Array.from(_.values());_.clear();for(let y of x)y()},t=()=>{if(D)cancelAnimationFrame(D);D=requestAnimationFrame(v)};queueMicrotask(v);var N=(x)=>{if(A&&!x.has(A)){let y=A;x.add(y),A.cleanups.add(()=>{x.delete(y)})}},V=(x)=>{for(let y of x)if(U)R.add(y);else y()},k=()=>{while(R.size){let x=Array.from(R);R.clear();for(let y of x)y()}},a=(x)=>{U++;try{x()}finally{k(),U--}},M=(x,y)=>{let z=A;A=y;try{x()}finally{A=z}},r=(x,y)=>new Promise((z,J)=>{let B=()=>{try{z(x())}catch(K){J(K)}};if(y)_.set(y,B);t()});function Y(x){let{signals:y,ok:z,err:J=console.error,nil:B=()=>{}}=X(x)?{signals:[],ok:x}:x,K=!1,L=()=>M(()=>{if(K)throw new C("effect");K=!0;let G=void 0;try{G=S({signals:y,ok:z,err:J,nil:B})}catch(Q){J(j(Q))}if(X(G))L.cleanups.add(G);K=!1},L);return L.cleanups=new Set,L(),()=>{L.cleanups.forEach((G)=>G()),L.cleanups.clear()}}var o="Computed",e=(x,y)=>{if(!y)return!1;return x.name===y.name&&x.message===y.message},F=(x)=>{let y=new Set,z=X(x)?void 0:{nil:()=>Z,err:(...$)=>{if($.length>1)throw new AggregateError($);else throw $[0]},...x},J=z?z.ok:x,B=Z,K,L=!0,G=!1,Q=!1,H,W=($)=>{if(!Object.is($,B))B=$,L=!1,K=void 0,G=!0},g=()=>{G=Z!==B,B=Z,K=void 0},p=($)=>{let O=j($);G=!e(O,K),B=Z,K=O},n=($)=>{if(Q=!1,H=void 0,W($),G)V(y)},u=($)=>{if(Q=!1,H=void 0,p($),G)V(y)},l=()=>{Q=!1,H=void 0,f()},q=()=>{if(L=!0,H?.abort("Aborted because source signal changed"),y.size){if(G)V(y)}else q.cleanups.forEach(($)=>$()),q.cleanups.clear()};q.cleanups=new Set;let f=()=>M(()=>{if(Q)throw new C("computed");if(G=!1,d(J)){if(H)return B;if(H=new AbortController,z)z.abort=z.abort instanceof AbortSignal?AbortSignal.any([z.abort,H.signal]):H.signal;H.signal.addEventListener("abort",l,{once:!0})}let $;Q=!0;try{$=z&&z.signals.length?S(z):J(H?.signal)}catch(O){I(O)?g():p(O),Q=!1;return}if(m($))$.then(n,u);else if($==null||Z===$)g();else W($);Q=!1},q),T={[Symbol.toStringTag]:o,get:()=>{if(N(y),k(),L)f();if(K)throw K;return B},map:($)=>F({signals:[T],ok:$}),tap:($)=>Y({signals:[T],...X($)?{ok:$}:$})};return T},b=(x)=>P(x,o);var i="State",E=(x)=>{let y=new Set,z=x,J={[Symbol.toStringTag]:i,get:()=>{return N(y),z},set:(B)=>{if(Object.is(z,B))return;if(z=B,V(y),Z===z)y.clear()},update:(B)=>{J.set(B(z))},map:(B)=>F({signals:[J],ok:B}),tap:(B)=>Y({signals:[J],...X(B)?{ok:B}:B})};return J},w=(x)=>P(x,i);var Z=Symbol(),h=(x)=>w(x)||b(x),s=(x)=>X(x)&&x.length<2,xx=(x)=>h(x)?x:s(x)?F(x):E(x),S=(x)=>{let{signals:y,abort:z,ok:J,err:B,nil:K}=x,L=[],G=!1,Q=y.map((H)=>{try{let W=H.get();if(W===Z)G=!0;return W}catch(W){if(I(W))throw W;L.push(j(W))}});try{return G?K(z):L.length?B(...L):J(...Q)}catch(H){if(I(H))throw H;let W=j(H);return B(W)}};export{M as watch,xx as toSignal,E as state,w as isState,h as isSignal,s as isComputedCallback,b as isComputed,r as enqueue,Y as effect,F as computed,a as batch,Z as UNSET,C as CircularDependencyError};
|
package/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name Cause & Effect
|
|
3
|
-
* @version 0.13.
|
|
3
|
+
* @version 0.13.1
|
|
4
4
|
* @author Esther Brunner
|
|
5
5
|
*/
|
|
6
6
|
export { CircularDependencyError } from './lib/util'
|
|
7
7
|
export {
|
|
8
|
-
type Signal, type MaybeSignal,
|
|
8
|
+
type Signal, type MaybeSignal, type ComputedCallback,
|
|
9
9
|
UNSET, isSignal, isComputedCallback, toSignal
|
|
10
10
|
} from './lib/signal'
|
|
11
11
|
|
package/lib/computed.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Signal } from './signal';
|
|
1
|
+
import { type Signal, type ComputedCallback } from './signal';
|
|
2
2
|
import { type TapMatcher } from './effect';
|
|
3
3
|
export type ComputedMatcher<S extends Signal<{}>[], R extends {}> = {
|
|
4
4
|
signals: S;
|
|
@@ -19,10 +19,10 @@ export type Computed<T extends {}> = {
|
|
|
19
19
|
* Create a derived signal from existing signals
|
|
20
20
|
*
|
|
21
21
|
* @since 0.9.0
|
|
22
|
-
* @param {ComputedMatcher<S, T> |
|
|
22
|
+
* @param {ComputedMatcher<S, T> | ComputedCallback<T>} matcher - computed matcher or callback
|
|
23
23
|
* @returns {Computed<T>} - Computed signal
|
|
24
24
|
*/
|
|
25
|
-
export declare const computed: <T extends {}, S extends Signal<{}>[] = []>(matcher: ComputedMatcher<S, T> |
|
|
25
|
+
export declare const computed: <T extends {}, S extends Signal<{}>[] = []>(matcher: ComputedMatcher<S, T> | ComputedCallback<T>) => Computed<T>;
|
|
26
26
|
/**
|
|
27
27
|
* Check if a value is a computed state
|
|
28
28
|
*
|
package/lib/computed.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Signal, match, UNSET } from './signal'
|
|
1
|
+
import { type Signal, type ComputedCallback, match, UNSET } from './signal'
|
|
2
2
|
import { CircularDependencyError, isAbortError, isAsyncFunction, isFunction, isObjectOfType, isPromise, toError } from './util'
|
|
3
3
|
import { type Watcher, flush, notify, subscribe, watch } from './scheduler'
|
|
4
4
|
import { type TapMatcher, type EffectMatcher, effect } from './effect'
|
|
@@ -42,11 +42,11 @@ const isEquivalentError = /*#__PURE__*/ (
|
|
|
42
42
|
* Create a derived signal from existing signals
|
|
43
43
|
*
|
|
44
44
|
* @since 0.9.0
|
|
45
|
-
* @param {ComputedMatcher<S, T> |
|
|
45
|
+
* @param {ComputedMatcher<S, T> | ComputedCallback<T>} matcher - computed matcher or callback
|
|
46
46
|
* @returns {Computed<T>} - Computed signal
|
|
47
47
|
*/
|
|
48
48
|
export const computed = <T extends {}, S extends Signal<{}>[] = []>(
|
|
49
|
-
matcher: ComputedMatcher<S, T> |
|
|
49
|
+
matcher: ComputedMatcher<S, T> | ComputedCallback<T>,
|
|
50
50
|
): Computed<T> => {
|
|
51
51
|
const watchers: Set<Watcher> = new Set()
|
|
52
52
|
const m = isFunction(matcher) ? undefined : {
|
|
@@ -57,7 +57,7 @@ export const computed = <T extends {}, S extends Signal<{}>[] = []>(
|
|
|
57
57
|
},
|
|
58
58
|
...matcher,
|
|
59
59
|
} as Required<ComputedMatcher<S, T>>
|
|
60
|
-
const fn = (m ? m.ok : matcher) as
|
|
60
|
+
const fn = (m ? m.ok : matcher) as ComputedCallback<T>
|
|
61
61
|
|
|
62
62
|
// Internal state
|
|
63
63
|
let value: T = UNSET
|
package/lib/signal.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type State } from "./state";
|
|
2
2
|
import { type Computed } from "./computed";
|
|
3
3
|
type Signal<T extends {}> = State<T> | Computed<T>;
|
|
4
|
-
type
|
|
4
|
+
type ComputedCallback<T extends {}> = (abort?: AbortSignal) => T | Promise<T>;
|
|
5
|
+
type MaybeSignal<T extends {}> = Signal<T> | T | ComputedCallback<T>;
|
|
5
6
|
declare const UNSET: any;
|
|
6
7
|
/**
|
|
7
8
|
* Check whether a value is a Signal or not
|
|
@@ -18,7 +19,7 @@ declare const isSignal: <T extends {}>(value: any) => value is Signal<T>;
|
|
|
18
19
|
* @param {unknown} value - value to check
|
|
19
20
|
* @returns {boolean} - true if value is a callback or callbacks object, false otherwise
|
|
20
21
|
*/
|
|
21
|
-
declare const isComputedCallback: <T extends {}>(value: unknown) => value is
|
|
22
|
+
declare const isComputedCallback: <T extends {}>(value: unknown) => value is ComputedCallback<T>;
|
|
22
23
|
/**
|
|
23
24
|
* Convert a value to a Signal if it's not already a Signal
|
|
24
25
|
*
|
|
@@ -41,4 +42,4 @@ declare const match: <S extends Signal<{}>[], R>(matcher: {
|
|
|
41
42
|
err: ((...errors: Error[]) => R | Promise<R>);
|
|
42
43
|
nil: (abort?: AbortSignal) => R | Promise<R>;
|
|
43
44
|
}) => R | Promise<R>;
|
|
44
|
-
export { type Signal, type MaybeSignal, UNSET, isSignal, isComputedCallback, toSignal, match, };
|
|
45
|
+
export { type Signal, type MaybeSignal, type ComputedCallback, UNSET, isSignal, isComputedCallback, toSignal, match, };
|
package/lib/signal.ts
CHANGED
|
@@ -5,7 +5,8 @@ import { isAbortError, isFunction, toError } from "./util"
|
|
|
5
5
|
/* === Types === */
|
|
6
6
|
|
|
7
7
|
type Signal<T extends {}> = State<T> | Computed<T>
|
|
8
|
-
type
|
|
8
|
+
type ComputedCallback<T extends {}> = (abort?: AbortSignal) => T | Promise<T>
|
|
9
|
+
type MaybeSignal<T extends {}> = Signal<T> | T | ComputedCallback<T>
|
|
9
10
|
|
|
10
11
|
/* === Constants === */
|
|
11
12
|
|
|
@@ -32,7 +33,7 @@ const isSignal = /*#__PURE__*/ <T extends {}>(value: any): value is Signal<T> =>
|
|
|
32
33
|
*/
|
|
33
34
|
const isComputedCallback = /*#__PURE__*/ <T extends {}>(
|
|
34
35
|
value: unknown
|
|
35
|
-
): value is
|
|
36
|
+
): value is ComputedCallback<T> =>
|
|
36
37
|
isFunction(value) && value.length < 2
|
|
37
38
|
|
|
38
39
|
/**
|
|
@@ -96,6 +97,6 @@ const match = <S extends Signal<{}>[], R>(
|
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
export {
|
|
99
|
-
type Signal, type MaybeSignal,
|
|
100
|
+
type Signal, type MaybeSignal, type ComputedCallback,
|
|
100
101
|
UNSET, isSignal, isComputedCallback, toSignal, match,
|
|
101
102
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeix/cause-effect",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"author": "Esther Brunner",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.ts",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"@types/bun": "latest",
|
|
9
|
-
|
|
9
|
+
"random": "^5.3.0"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
12
|
"typescript": "^5.6.3"
|