reactish-state 0.3.0 → 0.4.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/cjs/index.js CHANGED
@@ -9,10 +9,13 @@ var state = function state(initialValue, actionCreator) {
9
9
  return value;
10
10
  }
11
11
  function set(newValue) {
12
- value = newValue;
13
- listeners.forEach(function (listener) {
14
- listener();
15
- });
12
+ var nextValue = typeof newValue === 'function' ? newValue(value) : newValue;
13
+ if (!Object.is(value, nextValue)) {
14
+ value = nextValue;
15
+ listeners.forEach(function (listener) {
16
+ listener();
17
+ });
18
+ }
16
19
  }
17
20
  return {
18
21
  get: get,
@@ -5,10 +5,13 @@ var state = function state(initialValue, actionCreator) {
5
5
  return value;
6
6
  }
7
7
  function set(newValue) {
8
- value = newValue;
9
- listeners.forEach(function (listener) {
10
- listener();
11
- });
8
+ var nextValue = typeof newValue === 'function' ? newValue(value) : newValue;
9
+ if (!Object.is(value, nextValue)) {
10
+ value = nextValue;
11
+ listeners.forEach(function (listener) {
12
+ listener();
13
+ });
14
+ }
12
15
  }
13
16
  return {
14
17
  get: get,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reactish-state",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "",
5
5
  "author": "Zheng Song",
6
6
  "license": "MIT",
package/types/react.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { State } from './vanilla';
2
- declare const useSnapshot: <T>(state: State<T, undefined>) => T;
2
+ declare const useSnapshot: <T>(state: State<T, unknown>) => T;
3
3
  export { useSnapshot };
@@ -1,8 +1,9 @@
1
1
  declare type Listener = () => void;
2
- declare type ActionCreator<T, A> = (set: (value: T) => void, get: () => T) => A;
3
- interface State<T, A = undefined> {
2
+ declare type Setter<T> = (newValue: T | ((value: T) => T)) => void;
3
+ declare type ActionCreator<T, A> = (set: Setter<T>, get: () => T) => A;
4
+ interface State<T, A = unknown> {
4
5
  get: () => T;
5
- set: (value: T) => void;
6
+ set: Setter<T>;
6
7
  subscribe: (listener: Listener) => () => void;
7
8
  actions?: A;
8
9
  }