@hrnec06/react_utils 1.3.3 → 1.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hrnec06/react_utils",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "A debugger component for react.",
5
5
  "exports": {
6
6
  ".": "./src/index.ts"
@@ -1,6 +1,6 @@
1
1
  import { ReactUtils } from "@hrnec06/util";
2
2
  import { Signal } from "./useSignal";
3
- import { useState } from "react";
3
+ import { useMemo, useState } from "react";
4
4
 
5
5
  const LazyNoValue = Symbol("LazyNoValue");
6
6
 
@@ -9,10 +9,8 @@ type LazyValue<T> = T | typeof LazyNoValue;
9
9
  export default function useLazySignal<T>(callback: () => T): LazySignal<T>
10
10
  {
11
11
  const [_value, _setValue] = useState<LazyValue<T>>(LazyNoValue);
12
-
13
- const signal = new LazySignal(_value, _setValue, callback);
14
12
 
15
- return signal;
13
+ return useMemo(() => new LazySignal(_value, _setValue, callback), [_value]);
16
14
  }
17
15
 
18
16
  export class LazySignal<T> extends Signal<LazyValue<T>>
@@ -1,22 +1,25 @@
1
1
  import { ReactUtils } from "@hrnec06/util";
2
- import { useState } from "react";
2
+ import { useMemo, useState } from "react";
3
3
 
4
4
  export default function useSignal<T>(value: T): Signal<T>
5
5
  {
6
6
  const [_value, _setValue] = useState(value);
7
7
 
8
- const signal = new Signal(_value, _setValue);
9
-
10
- return signal;
8
+ return useMemo(() => new Signal(_value, _setValue), [_value]);
11
9
  }
12
10
 
13
11
  export class Signal<T>
14
12
  {
13
+ protected _value: T;
14
+ protected setState: ReactUtils.SetState<T>;
15
+
15
16
  constructor(
16
- protected _value: T,
17
- protected setState: ReactUtils.SetState<T>
17
+ _value: T,
18
+ setState: ReactUtils.SetState<T>
18
19
  )
19
20
  {
21
+ this._value = _value;
22
+ this.setState = setState;
20
23
  }
21
24
 
22
25
  public set value(value: T | ((prev: T) => T))