reactish-state 0.3.0 → 0.5.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,
@@ -26,11 +29,37 @@ var state = function state(initialValue, actionCreator) {
26
29
  actions: actionCreator && actionCreator(set, get)
27
30
  };
28
31
  };
32
+ var selector = function selector() {
33
+ for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
34
+ items[_key] = arguments[_key];
35
+ }
36
+ var lastIndex = items.length - 1;
37
+ var selectorFunc = items[lastIndex];
38
+ items.length = lastIndex;
39
+ return {
40
+ get: function get() {
41
+ return selectorFunc.apply(void 0, items.map(function (item) {
42
+ return item.get();
43
+ }));
44
+ },
45
+ subscribe: function subscribe(listener) {
46
+ var unsubscribers = items.map(function (item) {
47
+ return item.subscribe(listener);
48
+ });
49
+ return function () {
50
+ return unsubscribers.forEach(function (unsubscribe) {
51
+ return unsubscribe();
52
+ });
53
+ };
54
+ }
55
+ };
56
+ };
29
57
 
30
58
  var useSnapshot = function useSnapshot(state) {
31
59
  var value = shim.useSyncExternalStore(state.subscribe, state.get, state.get);
32
60
  return value;
33
61
  };
34
62
 
63
+ exports.selector = selector;
35
64
  exports.state = state;
36
65
  exports.useSnapshot = useSnapshot;
package/dist/es/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { state } from './vanilla.js';
1
+ export { selector, state } from './vanilla.js';
2
2
  export { useSnapshot } from './react.js';
@@ -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,
@@ -22,5 +25,30 @@ var state = function state(initialValue, actionCreator) {
22
25
  actions: actionCreator && actionCreator(set, get)
23
26
  };
24
27
  };
28
+ var selector = function selector() {
29
+ for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
30
+ items[_key] = arguments[_key];
31
+ }
32
+ var lastIndex = items.length - 1;
33
+ var selectorFunc = items[lastIndex];
34
+ items.length = lastIndex;
35
+ return {
36
+ get: function get() {
37
+ return selectorFunc.apply(void 0, items.map(function (item) {
38
+ return item.get();
39
+ }));
40
+ },
41
+ subscribe: function subscribe(listener) {
42
+ var unsubscribers = items.map(function (item) {
43
+ return item.subscribe(listener);
44
+ });
45
+ return function () {
46
+ return unsubscribers.forEach(function (unsubscribe) {
47
+ return unsubscribe();
48
+ });
49
+ };
50
+ }
51
+ };
52
+ };
25
53
 
26
- export { state };
54
+ export { selector, state };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reactish-state",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "author": "Zheng Song",
6
6
  "license": "MIT",
package/types/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { state } from './vanilla';
1
+ export { state, selector } from './vanilla';
2
2
  export { useSnapshot } from './react';
package/types/react.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { State } from './vanilla';
2
- declare const useSnapshot: <T>(state: State<T, undefined>) => T;
1
+ import { Reactish } from './vanilla';
2
+ declare const useSnapshot: <T>(state: Reactish<T>) => T;
3
3
  export { useSnapshot };
@@ -1,10 +1,22 @@
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 Reactish<T> {
4
5
  get: () => T;
5
- set: (value: T) => void;
6
6
  subscribe: (listener: Listener) => () => void;
7
+ }
8
+ interface State<T, A = unknown> extends Reactish<T> {
9
+ set: Setter<T>;
7
10
  actions?: A;
8
11
  }
9
12
  declare const state: <T, A>(initialValue: T, actionCreator?: ActionCreator<T, A>) => State<T, A>;
10
- export { state, State };
13
+ declare type ReactishArray = Reactish<unknown>[];
14
+ declare type ReactishValueArray<R extends ReactishArray> = {
15
+ [index in keyof R]: ReturnType<R[index]['get']>;
16
+ };
17
+ declare type SelectorFunc<R extends ReactishArray, V> = (...args: ReactishValueArray<R>) => V;
18
+ declare const selector: <R extends ReactishArray, V>(...items: [...R, SelectorFunc<R, V>]) => {
19
+ get: () => V;
20
+ subscribe: (listener: Listener) => () => void;
21
+ };
22
+ export { Reactish, state, selector };