@zeix/cause-effect 0.9.7 → 0.10.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Cause & Effect
2
2
 
3
- Version 0.9.7
3
+ Version 0.10.1
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.set(v => ++v))
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.set(v => ++v))
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.set(v => ++v))
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.set(v => v * 2)
118
- b.set(v => v * 2)
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/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @name Cause & Effect
3
- * @version 0.9.7
3
+ * @version 0.10.1
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 A=(y)=>typeof y==="function",D=(y)=>A(y)&&/^async\s+/.test(y.toString()),G=(y)=>(x)=>x instanceof y,X=G(Error),O=G(Promise);var R="Computed",q=(y,x)=>{x=x??D(y);let j=[],Q,B=null,W=!0,S=()=>{if(W=!0,x)I(j)},Y={[Symbol.toStringTag]:R,get:()=>{if(x)H(j);if(!x||W)J(()=>{let F=(L)=>{Q=L,W=!1,B=null},C=(L)=>{B=X(L)?L:new Error(`Computed function failed: ${L}`)};try{let L=y(Q);O(L)?L.then(F).catch(C):F(L)}catch(L){C(L)}},S);if(X(B))throw B;return Q},map:(F)=>q(()=>F(Y.get()))};return Y},Z=(y)=>!!y&&typeof y==="object"&&y[Symbol.toStringTag]===R;var z,$=!1,N=[],P=(y)=>M(y)||Z(y),U=(y,x=!1)=>P(y)?y:A(y)?q(y,x):k(y),H=(y)=>{if(z&&!y.includes(z))y.push(z)},I=(y)=>y.forEach((x)=>$?N.push(x):x()),J=(y,x)=>{let j=z;z=x,y(),z=j},T=(y)=>{$=!0,y(),$=!1,N.forEach((x)=>x()),N.length=0};var V=Symbol();class K{y;watchers=[];constructor(y){this.value=y}get(){return H(this.watchers),this.value}set(y){if(V!==y){let x=A(y)?y(this.value):y;if(Object.is(this.value,x))return;this.value=x}if(I(this.watchers),V===y)this.watchers=[]}map(y){return q(()=>y(this.get()))}}var k=(y)=>new K(y),M=G(K);var p=(y)=>{let x=()=>J(()=>{try{y()}catch(j){console.error(j)}},x);x()};export{U as toSignal,k as state,M as isState,P as isSignal,Z as isComputed,p as effect,q as computed,T as batch,V as UNSET,K as State};
1
+ var M=(T)=>typeof T==="function",C=(T)=>M(T)&&/^async\s+/.test(T.toString()),R=(T)=>M(T)&&T.length<2,Y=(T)=>(y)=>y instanceof T,K=Y(Error),D=Y(Promise);var O="Computed",I=(T,y)=>{y=y??C(T);let F=[],H,j=null,J=!0,U=()=>{if(J=!0,y)A(F)},Z={[Symbol.toStringTag]:O,get:()=>{if(y)z(F);if(!y||J)B(()=>{let q=(x)=>{H=x,J=!1,j=null},$=(x)=>{j=K(x)?x:new Error(`Computed function failed: ${x}`)};try{let x=T(H);D(x)?x.then(q).catch($):q(x)}catch(x){$(x)}},U);if(K(j))throw j;return H},map:(q)=>I(()=>q(Z.get()))};return Z},N=(T)=>!!T&&typeof T==="object"&&T[Symbol.toStringTag]===O;var L,Q=!1,V=[],P=(T)=>X(T)||N(T),k=(T,y=!1)=>P(T)?T:R(T)?I(T,y):W(T),z=(T)=>{if(L&&!T.includes(L))T.push(L)},A=(T)=>T.forEach((y)=>Q?V.push(y):y()),B=(T,y)=>{let F=L;L=y,T(),L=F},E=(T)=>{Q=!0,T(),Q=!1,V.forEach((y)=>y()),V.length=0};var S=Symbol();class G{T;watchers=[];constructor(T){this.value=T}get(){return z(this.watchers),this.value}set(T){if(Object.is(this.value,T))return;if(this.value=T,A(this.watchers),S===T)this.watchers=[]}update(T){this.set(T(this.value))}map(T){return I(()=>T(this.get()))}}var W=(T)=>new G(T),X=(T)=>T instanceof G;var g=(T)=>{let y=()=>B(()=>{try{T()}catch(F){console.error(F)}},y);y()};export{k as toSignal,W as state,X as isState,P as isSignal,N as isComputed,g as effect,I as computed,E as batch,S as UNSET,G as State};
package/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @name Cause & Effect
3
- * @version 0.9.7
3
+ * @version 0.10.1
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
@@ -1,7 +1,7 @@
1
1
  export type Computed<T> = {
2
2
  [Symbol.toStringTag]: "Computed";
3
3
  get: () => T;
4
- map: <U>(fn: (value: T) => U) => Computed<U>;
4
+ map: <U extends {}>(fn: (value: T) => U) => Computed<U>;
5
5
  };
6
6
  /**
7
7
  * Create a derived state from existing states
@@ -10,5 +10,12 @@ export type Computed<T> = {
10
10
  * @param {() => T} fn - compute function to derive state
11
11
  * @returns {Computed<T>} result of derived state
12
12
  */
13
- export declare const computed: <T>(fn: (v?: T) => T | Promise<T>, memo?: boolean) => Computed<T>;
13
+ export declare const computed: <T extends {}>(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
@@ -6,7 +6,7 @@ import { isAsyncFunction, isError, isPromise } from "./util"
6
6
  export type Computed<T> = {
7
7
  [Symbol.toStringTag]: "Computed"
8
8
  get: () => T
9
- map: <U>(fn: (value: T) => U) => Computed<U>
9
+ map: <U extends {}>(fn: (value: T) => U) => Computed<U>
10
10
  }
11
11
 
12
12
  /* === Constants === */
@@ -22,7 +22,7 @@ const TYPE_COMPUTED = 'Computed'
22
22
  * @param {() => T} fn - compute function to derive state
23
23
  * @returns {Computed<T>} result of derived state
24
24
  */
25
- export const computed = /*#__PURE__*/ <T>(
25
+ export const computed = /*#__PURE__*/ <T extends {}>(
26
26
  fn: (v?: T) => T | Promise<T>,
27
27
  memo?: boolean
28
28
  ): Computed<T> => {
@@ -64,12 +64,21 @@ export const computed = /*#__PURE__*/ <T>(
64
64
  if (isError(error)) throw error
65
65
  return value
66
66
  },
67
- map: <U>(fn: (value: T) => U): Computed<U> =>
67
+ map: <U extends {}>(fn: (value: T) => U): Computed<U> =>
68
68
  computed(() => fn(c.get())),
69
69
  }
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 { isFunction } from "./util"
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
- : isFunction(value) ? computed(value, memo)
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 | ((v: T) => T)} value
26
+ * @param {T} value
25
27
  * @returns {void}
26
28
  */
27
- set(value: T | ((v: T) => T)): void;
28
- map<U>(fn: (value: T) => U): Computed<U>;
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
+ */
47
+ map<U extends {}>(fn: (value: T) => U): Computed<U>;
29
48
  }
30
49
  /**
31
50
  * Create a new state signal
32
51
  *
33
- * @static method of State<T>
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
- export declare const isState: (value: unknown) => value is State<any>;
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,63 @@ 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 | ((v: T) => T)} value
38
+ * @param {T} value
38
39
  * @returns {void}
39
40
  */
40
- set(value: T | ((v: T) => T)): void {
41
- if (UNSET !== value) {
42
- const newValue = isFunction(value) ? value(this.value) : value
43
- if (Object.is(this.value, newValue)) return
44
- this.value = newValue
45
- }
41
+ set(value: T): void {
42
+ if (Object.is(this.value, value)) return
43
+ this.value = value
46
44
  notify(this.watchers)
47
45
 
48
- // Setting to null clears the watchers so the signal can be garbage collected
46
+ // Setting to UNSET clears the watchers so the signal can be garbage collected
49
47
  if (UNSET === value) this.watchers = []
50
48
  }
51
49
 
52
- map<U>(fn: (value: T) => U): Computed<U> {
50
+ /**
51
+ * Update the state with a new value using a function
52
+ *
53
+ * @since 0.10.0
54
+ * @method of State<T>
55
+ * @param {(value: T) => T} fn
56
+ * @returns {void} - updates the state with the result of the function
57
+ */
58
+ update(fn: (value: T) => T): void {
59
+ this.set(fn(this.value))
60
+ }
61
+
62
+ /**
63
+ * Create a derived state from an existing state
64
+ *
65
+ * @since 0.9.0
66
+ * @method of State<T>
67
+ * @param {(value: T) => U} fn
68
+ * @returns {Computed<U>} - derived state
69
+ */
70
+ map<U extends {}>(fn: (value: T) => U): Computed<U> {
53
71
  return computed<U>(() => fn(this.get()))
54
72
  }
55
73
  }
56
74
 
75
+ /* === Helper Functions === */
76
+
57
77
  /**
58
78
  * Create a new state signal
59
79
  *
60
- * @static method of State<T>
80
+ * @since 0.9.0
61
81
  * @param {T} value - initial value of the state
62
82
  * @returns {State<T>} - new state signal
63
83
  */
64
- export const state = /*#__PURE__*/ <T>(value: T): State<T> =>
84
+ export const state = /*#__PURE__*/ <T extends {}>(value: T): State<T> =>
65
85
  new State(value)
66
86
 
67
- export const isState = /*#__PURE__*/ isInstanceOf(State)
87
+ /**
88
+ * Check if the provided value is a State instance
89
+ *
90
+ * @since 0.9.0
91
+ * @param {unknown} value - value to check
92
+ * @returns {boolean} - true if the value is a State instance, false otherwise
93
+ */
94
+ export const isState = /*#__PURE__*/ <T extends {}>(value: unknown): value is State<T> =>
95
+ value instanceof State
package/lib/util.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- declare const isFunction: (value: unknown) => value is (...args: any[]) => any;
2
- declare const isAsyncFunction: (value: unknown) => value is (...args: any[]) => Promise<any> | PromiseLike<any>;
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: any[]) => any =>
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: any[]) => Promise<any> | PromiseLike<any> =>
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeix/cause-effect",
3
- "version": "0.9.7",
3
+ "version": "0.10.1",
4
4
  "author": "Esther Brunner",
5
5
  "main": "index.js",
6
6
  "module": "index.ts",
@@ -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.set((v) => !v);
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.set(v => ++v);
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.set(v => v ? v.toUpperCase() : '');
91
+ cause.update(v => v ? v.toUpperCase() : '');
101
92
  expect(cause.get()).toBe("FOO");
102
93
  });
103
94