@zeix/cause-effect 0.9.7 → 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/README.md +7 -7
- package/bun.lockb +0 -0
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/index.ts +2 -2
- package/lib/computed.d.ts +7 -0
- package/lib/computed.ts +9 -0
- package/lib/signal.d.ts +5 -5
- package/lib/signal.ts +7 -7
- package/lib/state.d.ts +32 -6
- package/lib/state.ts +41 -11
- package/lib/util.d.ts +4 -3
- package/lib/util.ts +6 -3
- package/package.json +1 -1
- package/test/cause-effect.test.ts +3 -12
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Cause & Effect
|
|
2
2
|
|
|
3
|
-
Version 0.
|
|
3
|
+
Version 0.10.0
|
|
4
4
|
|
|
5
5
|
**Cause & Effect** - efficient state management with signals that sync instantly and reactively across your application.
|
|
6
6
|
|
|
@@ -24,7 +24,7 @@ bun add @zeix/cause-effect
|
|
|
24
24
|
|
|
25
25
|
### Single State Signal
|
|
26
26
|
|
|
27
|
-
`state()` creates a new state signal. To access the current value of the signal use the `.get()` method. To update the value of the signal use the `.set()` method with a new value or an updater function of the form `(v: T) => T`.
|
|
27
|
+
`state()` creates a new state signal. To access the current value of the signal use the `.get()` method. To update the value of the signal use the `.set()` method with a new value or `.update()` with an updater function of the form `(v: T) => T`.
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
30
|
import { state, effect } from '@zeix/cause-effect'
|
|
@@ -33,7 +33,7 @@ const count = state(42)
|
|
|
33
33
|
effect(() => console.log(count.get())) // logs '42'
|
|
34
34
|
count.set(24) // logs '24'
|
|
35
35
|
document.querySelector('button.increment')
|
|
36
|
-
.addEventListener('click', () => count.
|
|
36
|
+
.addEventListener('click', () => count.update(v => ++v))
|
|
37
37
|
// Click on button logs '25', '26', and so on
|
|
38
38
|
```
|
|
39
39
|
|
|
@@ -49,7 +49,7 @@ const isOdd = computed(() => count.get() % 2)
|
|
|
49
49
|
effect(() => console.log(isOdd.get())) // logs 'false'
|
|
50
50
|
count.set(24) // logs nothing because 24 is also an even number
|
|
51
51
|
document.querySelector('button.increment')
|
|
52
|
-
.addEventListener('click', () => count.
|
|
52
|
+
.addEventListener('click', () => count.update(v => ++v))
|
|
53
53
|
// Click on button logs 'true', 'false', and so on
|
|
54
54
|
```
|
|
55
55
|
|
|
@@ -96,7 +96,7 @@ effect(() => {
|
|
|
96
96
|
})
|
|
97
97
|
// Updates h1 and p of the entry as soon as fetched data for entry becomes available
|
|
98
98
|
document.querySelector('button.next')
|
|
99
|
-
.addEventListener('click', () => entryId.
|
|
99
|
+
.addEventListener('click', () => entryId.update(v => ++v))
|
|
100
100
|
// Click on button updates h1 and p of the entry as soon as fetched data for the next entry is loaded
|
|
101
101
|
```
|
|
102
102
|
|
|
@@ -114,8 +114,8 @@ effect(() => console.log(sum.get())) // logs '7'
|
|
|
114
114
|
document.querySelector('button.double-all')
|
|
115
115
|
.addEventListener('click', () =>
|
|
116
116
|
batch(() => {
|
|
117
|
-
a.
|
|
118
|
-
b.
|
|
117
|
+
a.update(v => v * 2)
|
|
118
|
+
b.update(v => v * 2)
|
|
119
119
|
}
|
|
120
120
|
))
|
|
121
121
|
// Click on button logs '14' only once (instead of first '10' and then '14' without batch)
|
package/bun.lockb
ADDED
|
Binary file
|
package/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name Cause & Effect
|
|
3
|
-
* @version 0.
|
|
3
|
+
* @version 0.10.0
|
|
4
4
|
* @author Esther Brunner
|
|
5
5
|
*/
|
|
6
6
|
export { UNSET, State, state, isState } from './lib/state';
|
|
7
7
|
export { type Computed, computed, isComputed } from './lib/computed';
|
|
8
|
-
export { type Signal, isSignal, toSignal, batch } from './lib/signal';
|
|
8
|
+
export { type Signal, type MaybeSignal, isSignal, toSignal, batch } from './lib/signal';
|
|
9
9
|
export { effect } from './lib/effect';
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var C=(T)=>typeof T==="function",R=(T)=>C(T)&&/^async\s+/.test(T.toString()),Y=(T)=>C(T)&&T.length<2,D=(T)=>(y)=>y instanceof T,K=D(Error),O=D(Promise);var P="Computed",I=(T,y)=>{y=y??R(T);let F=[],H,j=null,J=!0,U=()=>{if(J=!0,y)A(F)},M={[Symbol.toStringTag]:P,get:()=>{if(y)z(F);if(!y||J)B(()=>{let q=(x)=>{H=x,J=!1,j=null},N=(x)=>{j=K(x)?x:new Error(`Computed function failed: ${x}`)};try{let x=T(H);O(x)?x.then(q).catch(N):q(x)}catch(x){N(x)}},U);if(K(j))throw j;return H},map:(q)=>I(()=>q(M.get()))};return M},Q=(T)=>!!T&&typeof T==="object"&&T[Symbol.toStringTag]===P;var L,V=!1,W=[],S=(T)=>Z(T)||Q(T),k=(T,y=!1)=>S(T)?T:Y(T)?I(T,y):X(T),z=(T)=>{if(L&&!T.includes(L))T.push(L)},A=(T)=>T.forEach((y)=>V?W.push(y):y()),B=(T,y)=>{let F=L;L=y,T(),L=F},E=(T)=>{V=!0,T(),V=!1,W.forEach((y)=>y()),W.length=0};var $=Symbol();class G{T;watchers=[];constructor(T){this.value=T}get(){return z(this.watchers),this.value}set(T){if($!==T){if(Object.is(this.value,T))return;this.value=T}if(A(this.watchers),$===T)this.watchers=[]}update(T){this.set(T(this.value))}map(T){return I(()=>T(this.get()))}}var X=(T)=>new G(T),Z=(T)=>T instanceof G;var g=(T)=>{let y=()=>B(()=>{try{T()}catch(F){console.error(F)}},y);y()};export{k as toSignal,X as state,Z as isState,S as isSignal,Q as isComputed,g as effect,I as computed,E as batch,$ as UNSET,G as State};
|
package/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name Cause & Effect
|
|
3
|
-
* @version 0.
|
|
3
|
+
* @version 0.10.0
|
|
4
4
|
* @author Esther Brunner
|
|
5
5
|
*/
|
|
6
6
|
export { UNSET, State, state, isState } from './lib/state'
|
|
7
7
|
export { type Computed, computed, isComputed } from './lib/computed'
|
|
8
|
-
export { type Signal, isSignal, toSignal, batch } from './lib/signal'
|
|
8
|
+
export { type Signal, type MaybeSignal, isSignal, toSignal, batch } from './lib/signal'
|
|
9
9
|
export { effect } from './lib/effect'
|
package/lib/computed.d.ts
CHANGED
|
@@ -11,4 +11,11 @@ export type Computed<T> = {
|
|
|
11
11
|
* @returns {Computed<T>} result of derived state
|
|
12
12
|
*/
|
|
13
13
|
export declare const computed: <T>(fn: (v?: T) => T | Promise<T>, memo?: boolean) => Computed<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Check if a value is a computed state
|
|
16
|
+
*
|
|
17
|
+
* @since 0.9.0
|
|
18
|
+
* @param {unknown} value - value to check
|
|
19
|
+
* @returns {boolean} - true if value is a computed state, false otherwise
|
|
20
|
+
*/
|
|
14
21
|
export declare const isComputed: <T>(value: unknown) => value is Computed<T>;
|
package/lib/computed.ts
CHANGED
|
@@ -70,6 +70,15 @@ export const computed = /*#__PURE__*/ <T>(
|
|
|
70
70
|
return c
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/* === Helper Functions === */
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Check if a value is a computed state
|
|
77
|
+
*
|
|
78
|
+
* @since 0.9.0
|
|
79
|
+
* @param {unknown} value - value to check
|
|
80
|
+
* @returns {boolean} - true if value is a computed state, false otherwise
|
|
81
|
+
*/
|
|
73
82
|
export const isComputed = /*#__PURE__*/ <T>(value: unknown): value is Computed<T> =>
|
|
74
83
|
!!value && typeof value === 'object'
|
|
75
84
|
&& (value as { [key in typeof Symbol.toStringTag]: string })[Symbol.toStringTag] === TYPE_COMPUTED
|
package/lib/signal.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type State } from "./state";
|
|
2
2
|
import { type Computed } from "./computed";
|
|
3
|
-
type Signal<T> = State<T> | Computed<T>;
|
|
4
|
-
type MaybeSignal<T> = State<T> | Computed<T> | T;
|
|
3
|
+
type Signal<T extends {}> = State<T> | Computed<T>;
|
|
4
|
+
type MaybeSignal<T extends {}> = State<T> | Computed<T> | T | ((old?: T) => T);
|
|
5
5
|
type Watcher = () => void;
|
|
6
6
|
/**
|
|
7
7
|
* Check whether a value is a Signal or not
|
|
@@ -10,7 +10,7 @@ type Watcher = () => void;
|
|
|
10
10
|
* @param {any} value - value to check
|
|
11
11
|
* @returns {boolean} - true if value is a Signal, false otherwise
|
|
12
12
|
*/
|
|
13
|
-
declare const isSignal: <T>(value: any) => value is Signal<T>;
|
|
13
|
+
declare const isSignal: <T extends {}>(value: any) => value is Signal<T>;
|
|
14
14
|
/**
|
|
15
15
|
* Convert a value to a Signal if it's not already a Signal
|
|
16
16
|
*
|
|
@@ -19,7 +19,7 @@ declare const isSignal: <T>(value: any) => value is Signal<T>;
|
|
|
19
19
|
* @param memo
|
|
20
20
|
* @returns {Signal<T>} - converted Signal
|
|
21
21
|
*/
|
|
22
|
-
declare const toSignal: <T>(value: MaybeSignal<T>, memo?: boolean) => Signal<T>;
|
|
22
|
+
declare const toSignal: <T extends {}>(value: MaybeSignal<T>, memo?: boolean) => Signal<T>;
|
|
23
23
|
/**
|
|
24
24
|
* Add notify function of active watchers to the set of watchers
|
|
25
25
|
*
|
|
@@ -45,4 +45,4 @@ declare const watch: (run: () => void, mark: Watcher) => void;
|
|
|
45
45
|
* @param {() => void} run - function to run the batch of state changes
|
|
46
46
|
*/
|
|
47
47
|
declare const batch: (run: () => void) => void;
|
|
48
|
-
export { type Signal, type Watcher, isSignal, toSignal, subscribe, notify, watch, batch };
|
|
48
|
+
export { type Signal, type MaybeSignal, type Watcher, isSignal, toSignal, subscribe, notify, watch, batch };
|
package/lib/signal.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { type State, isState, state } from "./state"
|
|
2
2
|
import { computed, type Computed, isComputed } from "./computed"
|
|
3
|
-
import {
|
|
3
|
+
import { isComputeFunction } from "./util"
|
|
4
4
|
|
|
5
5
|
/* === Types === */
|
|
6
6
|
|
|
7
|
-
type Signal<T> = State<T> | Computed<T>
|
|
7
|
+
type Signal<T extends {}> = State<T> | Computed<T>
|
|
8
8
|
|
|
9
|
-
type MaybeSignal<T> = State<T> | Computed<T> | T
|
|
9
|
+
type MaybeSignal<T extends {}> = State<T> | Computed<T> | T | ((old?: T) => T)
|
|
10
10
|
|
|
11
11
|
type Watcher = () => void
|
|
12
12
|
|
|
@@ -30,7 +30,7 @@ const pending: Watcher[] = []
|
|
|
30
30
|
* @param {any} value - value to check
|
|
31
31
|
* @returns {boolean} - true if value is a Signal, false otherwise
|
|
32
32
|
*/
|
|
33
|
-
const isSignal = /*#__PURE__*/ <T>(value: any): value is Signal<T> =>
|
|
33
|
+
const isSignal = /*#__PURE__*/ <T extends {}>(value: any): value is Signal<T> =>
|
|
34
34
|
isState(value) || isComputed(value)
|
|
35
35
|
|
|
36
36
|
/**
|
|
@@ -41,12 +41,12 @@ const isSignal = /*#__PURE__*/ <T>(value: any): value is Signal<T> =>
|
|
|
41
41
|
* @param memo
|
|
42
42
|
* @returns {Signal<T>} - converted Signal
|
|
43
43
|
*/
|
|
44
|
-
const toSignal = /*#__PURE__*/ <T>(
|
|
44
|
+
const toSignal = /*#__PURE__*/ <T extends {}>(
|
|
45
45
|
value: MaybeSignal<T>,
|
|
46
46
|
memo: boolean = false
|
|
47
47
|
): Signal<T> =>
|
|
48
48
|
isSignal<T>(value) ? value
|
|
49
|
-
:
|
|
49
|
+
: isComputeFunction<T>(value) ? computed(value, memo)
|
|
50
50
|
: state(value)
|
|
51
51
|
|
|
52
52
|
/**
|
|
@@ -93,6 +93,6 @@ const batch = (run: () => void): void => {
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
export {
|
|
96
|
-
type Signal, type Watcher,
|
|
96
|
+
type Signal, type MaybeSignal, type Watcher,
|
|
97
97
|
isSignal, toSignal, subscribe, notify, watch, batch
|
|
98
98
|
}
|
package/lib/state.d.ts
CHANGED
|
@@ -6,13 +6,14 @@ export declare const UNSET: any;
|
|
|
6
6
|
* @since 0.9.0
|
|
7
7
|
* @class State
|
|
8
8
|
*/
|
|
9
|
-
export declare class State<T> {
|
|
9
|
+
export declare class State<T extends {}> {
|
|
10
10
|
private value;
|
|
11
11
|
private watchers;
|
|
12
12
|
constructor(value: T);
|
|
13
13
|
/**
|
|
14
14
|
* Get the current value of the state
|
|
15
15
|
*
|
|
16
|
+
* @since 0.9.0
|
|
16
17
|
* @method of State<T>
|
|
17
18
|
* @returns {T} - current value of the state
|
|
18
19
|
*/
|
|
@@ -20,19 +21,44 @@ export declare class State<T> {
|
|
|
20
21
|
/**
|
|
21
22
|
* Set a new value of the state
|
|
22
23
|
*
|
|
24
|
+
* @since 0.9.0
|
|
23
25
|
* @method of State<T>
|
|
24
|
-
* @param {T
|
|
26
|
+
* @param {T} value
|
|
25
27
|
* @returns {void}
|
|
26
28
|
*/
|
|
27
|
-
set(value: T
|
|
29
|
+
set(value: T): void;
|
|
30
|
+
/**
|
|
31
|
+
* Update the state with a new value using a function
|
|
32
|
+
*
|
|
33
|
+
* @since 0.10.0
|
|
34
|
+
* @method of State<T>
|
|
35
|
+
* @param {(value: T) => T} fn
|
|
36
|
+
* @returns {void} - updates the state with the result of the function
|
|
37
|
+
*/
|
|
38
|
+
update(fn: (value: T) => T): void;
|
|
39
|
+
/**
|
|
40
|
+
* Create a derived state from an existing state
|
|
41
|
+
*
|
|
42
|
+
* @since 0.9.0
|
|
43
|
+
* @method of State<T>
|
|
44
|
+
* @param {(value: T) => U} fn
|
|
45
|
+
* @returns {Computed<U>} - derived state
|
|
46
|
+
*/
|
|
28
47
|
map<U>(fn: (value: T) => U): Computed<U>;
|
|
29
48
|
}
|
|
30
49
|
/**
|
|
31
50
|
* Create a new state signal
|
|
32
51
|
*
|
|
33
|
-
* @
|
|
52
|
+
* @since 0.9.0
|
|
34
53
|
* @param {T} value - initial value of the state
|
|
35
54
|
* @returns {State<T>} - new state signal
|
|
36
55
|
*/
|
|
37
|
-
export declare const state: <T>(value: T) => State<T>;
|
|
38
|
-
|
|
56
|
+
export declare const state: <T extends {}>(value: T) => State<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Check if the provided value is a State instance
|
|
59
|
+
*
|
|
60
|
+
* @since 0.9.0
|
|
61
|
+
* @param {unknown} value - value to check
|
|
62
|
+
* @returns {boolean} - true if the value is a State instance, false otherwise
|
|
63
|
+
*/
|
|
64
|
+
export declare const isState: <T extends {}>(value: unknown) => value is State<T>;
|
package/lib/state.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { isFunction, isInstanceOf } from "./util"
|
|
2
1
|
import { type Watcher, subscribe, notify } from "./signal"
|
|
3
2
|
import { type Computed, computed } from "./computed"
|
|
4
3
|
|
|
@@ -14,7 +13,7 @@ export const UNSET: any = Symbol()
|
|
|
14
13
|
* @since 0.9.0
|
|
15
14
|
* @class State
|
|
16
15
|
*/
|
|
17
|
-
export class State<T> {
|
|
16
|
+
export class State<T extends {}> {
|
|
18
17
|
private watchers: Watcher[] = []
|
|
19
18
|
|
|
20
19
|
constructor(private value: T) {}
|
|
@@ -22,6 +21,7 @@ export class State<T> {
|
|
|
22
21
|
/**
|
|
23
22
|
* Get the current value of the state
|
|
24
23
|
*
|
|
24
|
+
* @since 0.9.0
|
|
25
25
|
* @method of State<T>
|
|
26
26
|
* @returns {T} - current value of the state
|
|
27
27
|
*/
|
|
@@ -33,35 +33,65 @@ export class State<T> {
|
|
|
33
33
|
/**
|
|
34
34
|
* Set a new value of the state
|
|
35
35
|
*
|
|
36
|
+
* @since 0.9.0
|
|
36
37
|
* @method of State<T>
|
|
37
|
-
* @param {T
|
|
38
|
+
* @param {T} value
|
|
38
39
|
* @returns {void}
|
|
39
40
|
*/
|
|
40
|
-
set(value: T
|
|
41
|
+
set(value: T): void {
|
|
41
42
|
if (UNSET !== value) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.value = newValue
|
|
43
|
+
if (Object.is(this.value, value)) return
|
|
44
|
+
this.value = value
|
|
45
45
|
}
|
|
46
46
|
notify(this.watchers)
|
|
47
47
|
|
|
48
|
-
// Setting to
|
|
48
|
+
// Setting to UNSET clears the watchers so the signal can be garbage collected
|
|
49
49
|
if (UNSET === value) this.watchers = []
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Update the state with a new value using a function
|
|
54
|
+
*
|
|
55
|
+
* @since 0.10.0
|
|
56
|
+
* @method of State<T>
|
|
57
|
+
* @param {(value: T) => T} fn
|
|
58
|
+
* @returns {void} - updates the state with the result of the function
|
|
59
|
+
*/
|
|
60
|
+
update(fn: (value: T) => T): void {
|
|
61
|
+
this.set(fn(this.value))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Create a derived state from an existing state
|
|
66
|
+
*
|
|
67
|
+
* @since 0.9.0
|
|
68
|
+
* @method of State<T>
|
|
69
|
+
* @param {(value: T) => U} fn
|
|
70
|
+
* @returns {Computed<U>} - derived state
|
|
71
|
+
*/
|
|
52
72
|
map<U>(fn: (value: T) => U): Computed<U> {
|
|
53
73
|
return computed<U>(() => fn(this.get()))
|
|
54
74
|
}
|
|
55
75
|
}
|
|
56
76
|
|
|
77
|
+
/* === Helper Functions === */
|
|
78
|
+
|
|
57
79
|
/**
|
|
58
80
|
* Create a new state signal
|
|
59
81
|
*
|
|
60
|
-
* @
|
|
82
|
+
* @since 0.9.0
|
|
61
83
|
* @param {T} value - initial value of the state
|
|
62
84
|
* @returns {State<T>} - new state signal
|
|
63
85
|
*/
|
|
64
|
-
export const state = /*#__PURE__*/ <T>(value: T): State<T> =>
|
|
86
|
+
export const state = /*#__PURE__*/ <T extends {}>(value: T): State<T> =>
|
|
65
87
|
new State(value)
|
|
66
88
|
|
|
67
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Check if the provided value is a State instance
|
|
91
|
+
*
|
|
92
|
+
* @since 0.9.0
|
|
93
|
+
* @param {unknown} value - value to check
|
|
94
|
+
* @returns {boolean} - true if the value is a State instance, false otherwise
|
|
95
|
+
*/
|
|
96
|
+
export const isState = /*#__PURE__*/ <T extends {}>(value: unknown): value is State<T> =>
|
|
97
|
+
value instanceof State
|
package/lib/util.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
declare const isFunction: (value: unknown) => value is (...args:
|
|
2
|
-
declare const isAsyncFunction: (value: unknown) => value is (...args:
|
|
1
|
+
declare const isFunction: <T>(value: unknown) => value is (...args: unknown[]) => T;
|
|
2
|
+
declare const isAsyncFunction: <T>(value: unknown) => value is (...args: unknown[]) => Promise<T> | PromiseLike<T>;
|
|
3
|
+
declare const isComputeFunction: <T>(value: unknown) => value is ((old?: T) => T);
|
|
3
4
|
declare const isInstanceOf: <T>(type: new (...args: any[]) => T) => (value: unknown) => value is T;
|
|
4
5
|
declare const isError: (value: unknown) => value is Error;
|
|
5
6
|
declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
6
|
-
export { isFunction, isAsyncFunction, isInstanceOf, isError, isPromise };
|
|
7
|
+
export { isFunction, isAsyncFunction, isComputeFunction, isInstanceOf, isError, isPromise };
|
package/lib/util.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/* === Utility Functions === */
|
|
2
2
|
|
|
3
|
-
const isFunction = /*#__PURE__*/ (value: unknown): value is (...args:
|
|
3
|
+
const isFunction = /*#__PURE__*/ <T>(value: unknown): value is (...args: unknown[]) => T =>
|
|
4
4
|
typeof value === 'function'
|
|
5
5
|
|
|
6
|
-
const isAsyncFunction = /*#__PURE__*/ (value: unknown): value is (...args:
|
|
6
|
+
const isAsyncFunction = /*#__PURE__*/ <T>(value: unknown): value is (...args: unknown[]) => Promise<T> | PromiseLike<T> =>
|
|
7
7
|
isFunction(value) && /^async\s+/.test(value.toString())
|
|
8
8
|
|
|
9
|
+
const isComputeFunction = /*#__PURE__*/ <T>(value: unknown): value is ((old?: T) => T) =>
|
|
10
|
+
isFunction(value) && value.length < 2
|
|
11
|
+
|
|
9
12
|
const isInstanceOf = /*#__PURE__*/ <T>(type: new (...args: any[]) => T) =>
|
|
10
13
|
(value: unknown): value is T =>
|
|
11
14
|
value instanceof type
|
|
@@ -13,4 +16,4 @@ const isInstanceOf = /*#__PURE__*/ <T>(type: new (...args: any[]) => T) =>
|
|
|
13
16
|
const isError = /*#__PURE__*/ isInstanceOf(Error)
|
|
14
17
|
const isPromise = /*#__PURE__*/ isInstanceOf(Promise)
|
|
15
18
|
|
|
16
|
-
export { isFunction, isAsyncFunction, isInstanceOf, isError, isPromise }
|
|
19
|
+
export { isFunction, isAsyncFunction, isComputeFunction, isInstanceOf, isError, isPromise }
|
package/package.json
CHANGED
|
@@ -11,15 +11,6 @@ const decrement = (n: number | void) => (n ?? 0) - 1;
|
|
|
11
11
|
|
|
12
12
|
describe('State', function () {
|
|
13
13
|
|
|
14
|
-
describe('Empty cause', function () {
|
|
15
|
-
|
|
16
|
-
test('should be undefined', function () {
|
|
17
|
-
const cause = state(undefined);
|
|
18
|
-
expect(cause.get()).toBeUndefined();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
|
|
23
14
|
describe('Boolean cause', function () {
|
|
24
15
|
|
|
25
16
|
test('should be boolean', function () {
|
|
@@ -45,7 +36,7 @@ describe('State', function () {
|
|
|
45
36
|
|
|
46
37
|
test('should toggle initial value with .set(v => !v)', function () {
|
|
47
38
|
const cause = state(false);
|
|
48
|
-
cause.
|
|
39
|
+
cause.update((v) => !v);
|
|
49
40
|
expect(cause.get()).toBe(true);
|
|
50
41
|
});
|
|
51
42
|
|
|
@@ -71,7 +62,7 @@ describe('State', function () {
|
|
|
71
62
|
|
|
72
63
|
test('should increment value with .set(v => ++v)', function () {
|
|
73
64
|
const cause = state(0);
|
|
74
|
-
cause.
|
|
65
|
+
cause.update(v => ++v);
|
|
75
66
|
expect(cause.get()).toBe(1);
|
|
76
67
|
});
|
|
77
68
|
|
|
@@ -97,7 +88,7 @@ describe('State', function () {
|
|
|
97
88
|
|
|
98
89
|
test('should upper case value with .set(v => v.toUpperCase())', function () {
|
|
99
90
|
const cause = state('foo');
|
|
100
|
-
cause.
|
|
91
|
+
cause.update(v => v ? v.toUpperCase() : '');
|
|
101
92
|
expect(cause.get()).toBe("FOO");
|
|
102
93
|
});
|
|
103
94
|
|