reactish-state 0.11.2 → 1.0.0-alpha.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.
Files changed (49) hide show
  1. package/dist/cjs/index.cjs +17 -0
  2. package/dist/cjs/react/shim.cjs +10 -0
  3. package/dist/cjs/react/useSelector.cjs +35 -0
  4. package/dist/cjs/react/useSnapshot.cjs +15 -0
  5. package/dist/cjs/utils.cjs +17 -0
  6. package/dist/cjs/vanilla/selector.cjs +35 -0
  7. package/dist/cjs/vanilla/state.cjs +35 -0
  8. package/dist/esm/index.mjs +5 -0
  9. package/dist/esm/react/shim.mjs +8 -0
  10. package/dist/esm/react/{useSelector.js → useSelector.mjs} +2 -2
  11. package/dist/esm/react/useSnapshot.mjs +13 -0
  12. package/dist/esm/vanilla/{selector.js → selector.mjs} +2 -2
  13. package/dist/esm/vanilla/{state.js → state.mjs} +1 -1
  14. package/dist/middleware/cjs/applyMiddleware.cjs +10 -0
  15. package/dist/middleware/cjs/index.cjs +11 -0
  16. package/dist/middleware/cjs/persist.cjs +39 -0
  17. package/dist/middleware/cjs/reduxDevtools.cjs +34 -0
  18. package/dist/middleware/esm/index.mjs +3 -0
  19. package/dist/middleware/esm/{persist.js → persist.mjs} +1 -1
  20. package/dist/middleware/esm/{reduxDevtools.js → reduxDevtools.mjs} +1 -1
  21. package/dist/plugin/cjs/applyPlugin.cjs +5 -0
  22. package/dist/plugin/cjs/index.cjs +9 -0
  23. package/dist/plugin/cjs/{index.js → reduxDevtools.cjs} +1 -4
  24. package/dist/plugin/esm/{applyPlugin.js → applyPlugin.mjs} +1 -1
  25. package/dist/plugin/esm/index.mjs +2 -0
  26. package/dist/plugin/esm/{reduxDevtools.js → reduxDevtools.mjs} +1 -1
  27. package/dist/shim/cjs/index.cjs +7 -0
  28. package/dist/shim/cjs/reactShim.cjs +7 -0
  29. package/dist/shim/esm/index.mjs +1 -0
  30. package/dist/shim/esm/reactShim.mjs +5 -0
  31. package/package.json +57 -42
  32. package/types/index.d.ts +1 -0
  33. package/types/middleware/persist.d.ts +1 -1
  34. package/types/react/shim.d.ts +4 -0
  35. package/types/react/useSelector.d.ts +2 -2
  36. package/types/shim/index.d.ts +1 -0
  37. package/types/shim/reactShim.d.ts +3 -0
  38. package/types/vanilla/selector.d.ts +1 -1
  39. package/types/vanilla/state.d.ts +1 -1
  40. package/dist/cjs/index.js +0 -116
  41. package/dist/esm/index.js +0 -4
  42. package/dist/esm/react/useSnapshot.js +0 -8
  43. package/dist/middleware/cjs/index.js +0 -79
  44. package/dist/middleware/esm/index.js +0 -3
  45. package/dist/plugin/esm/index.js +0 -2
  46. /package/dist/esm/{utils.js → utils.mjs} +0 -0
  47. /package/dist/middleware/cjs/{immer.js → immer.cjs} +0 -0
  48. /package/dist/middleware/esm/{applyMiddleware.js → applyMiddleware.mjs} +0 -0
  49. /package/dist/middleware/esm/{immer.js → immer.mjs} +0 -0
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ var state = require('./vanilla/state.cjs');
4
+ var selector = require('./vanilla/selector.cjs');
5
+ var useSnapshot = require('./react/useSnapshot.cjs');
6
+ var useSelector = require('./react/useSelector.cjs');
7
+ var shim = require('./react/shim.cjs');
8
+
9
+
10
+
11
+ exports.createState = state.createState;
12
+ exports.state = state.state;
13
+ exports.createSelector = selector.createSelector;
14
+ exports.selector = selector.selector;
15
+ exports.useSnapshot = useSnapshot.useSnapshot;
16
+ exports.useSelector = useSelector.useSelector;
17
+ exports.setReactShim = shim.setReactShim;
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ exports.useSyncExternalStore = React.useSyncExternalStore;
6
+ const setReactShim = ([shim]) => {
7
+ exports.useSyncExternalStore = shim;
8
+ };
9
+
10
+ exports.setReactShim = setReactShim;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+ var utils = require('../utils.cjs');
5
+ var useSnapshot = require('./useSnapshot.cjs');
6
+
7
+ const useSelector = (selectorParamFactory, deps) => {
8
+ const items = selectorParamFactory();
9
+ const cutoff = items.length - 1;
10
+ const selectorFunc = items[cutoff];
11
+ items.length = cutoff;
12
+ const [context] = React.useState(() => ({
13
+ sub: utils.createSubscriber(items)
14
+ }));
15
+ const get = () => {
16
+ const {
17
+ cache
18
+ } = context;
19
+ const reactishValues = utils.getReactishValues(items);
20
+ const args = reactishValues.concat(deps || selectorFunc);
21
+ if (cache && utils.isEqual(args, cache.args)) return cache.val;
22
+ const val = selectorFunc(...reactishValues);
23
+ context.cache = {
24
+ args,
25
+ val
26
+ };
27
+ return val;
28
+ };
29
+ return useSnapshot.useSnapshot({
30
+ get,
31
+ subscribe: context.sub
32
+ });
33
+ };
34
+
35
+ exports.useSelector = useSelector;
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ var shim = require('./shim.cjs');
4
+
5
+ const useSnapshot = ({
6
+ subscribe,
7
+ get
8
+ }) => {
9
+ if (process.env.NODE_ENV !== 'production' && !shim.useSyncExternalStore) {
10
+ throw new Error('[reactish-state] Shim setup is required for React 16/17.');
11
+ }
12
+ return shim.useSyncExternalStore(subscribe, get, get);
13
+ };
14
+
15
+ exports.useSnapshot = useSnapshot;
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const isEqual = (args1, args2) => {
4
+ for (let i = 0; i < args1.length; i++) {
5
+ if (!Object.is(args1[i], args2[i])) return false;
6
+ }
7
+ return true;
8
+ };
9
+ const createSubscriber = items => listener => {
10
+ const unsubscribers = items.map(item => item.subscribe(listener));
11
+ return () => unsubscribers.forEach(unsubscribe => unsubscribe());
12
+ };
13
+ const getReactishValues = items => items.map(item => item.get());
14
+
15
+ exports.createSubscriber = createSubscriber;
16
+ exports.getReactishValues = getReactishValues;
17
+ exports.isEqual = isEqual;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var utils = require('../utils.cjs');
4
+
5
+ const createSelector = ({
6
+ plugin
7
+ } = {}) => (...items) => {
8
+ const {
9
+ length
10
+ } = items;
11
+ const cutoff = typeof items[length - 1] === 'function' ? length - 1 : length - 2;
12
+ const selectorFunc = items[cutoff];
13
+ const config = items[cutoff + 1];
14
+ items.length = cutoff;
15
+ let cache;
16
+ const selector = {
17
+ get: () => {
18
+ const args = utils.getReactishValues(items);
19
+ if (cache && utils.isEqual(args, cache.args)) return cache.val;
20
+ const val = selectorFunc(...args);
21
+ cache = {
22
+ args,
23
+ val
24
+ };
25
+ return val;
26
+ },
27
+ subscribe: utils.createSubscriber(items)
28
+ };
29
+ plugin?.(selector, config);
30
+ return selector;
31
+ };
32
+ const selector = /*#__PURE__*/createSelector();
33
+
34
+ exports.createSelector = createSelector;
35
+ exports.selector = selector;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const createState = ({
4
+ middleware
5
+ } = {}) => (initialValue, actionCreator, config) => {
6
+ let value = initialValue;
7
+ const listeners = new Set();
8
+ const get = () => value;
9
+ let set = newValue => {
10
+ const nextValue = typeof newValue === 'function' ? newValue(value) : newValue;
11
+ if (!Object.is(value, nextValue)) {
12
+ value = nextValue;
13
+ listeners.forEach(listener => listener());
14
+ }
15
+ };
16
+ const subscribe = listener => {
17
+ listeners.add(listener);
18
+ return () => listeners.delete(listener);
19
+ };
20
+ if (middleware) set = middleware({
21
+ set,
22
+ get,
23
+ subscribe
24
+ }, config);
25
+ return {
26
+ get,
27
+ set,
28
+ subscribe,
29
+ actions: actionCreator?.(set, get)
30
+ };
31
+ };
32
+ const state = /*#__PURE__*/createState();
33
+
34
+ exports.createState = createState;
35
+ exports.state = state;
@@ -0,0 +1,5 @@
1
+ export { createState, state } from './vanilla/state.mjs';
2
+ export { createSelector, selector } from './vanilla/selector.mjs';
3
+ export { useSnapshot } from './react/useSnapshot.mjs';
4
+ export { useSelector } from './react/useSelector.mjs';
5
+ export { setReactShim } from './react/shim.mjs';
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+
3
+ let useSyncExternalStore = React.useSyncExternalStore;
4
+ const setReactShim = ([shim]) => {
5
+ useSyncExternalStore = shim;
6
+ };
7
+
8
+ export { setReactShim, useSyncExternalStore };
@@ -1,6 +1,6 @@
1
1
  import { useState } from 'react';
2
- import { createSubscriber, getReactishValues, isEqual } from '../utils.js';
3
- import { useSnapshot } from './useSnapshot.js';
2
+ import { createSubscriber, getReactishValues, isEqual } from '../utils.mjs';
3
+ import { useSnapshot } from './useSnapshot.mjs';
4
4
 
5
5
  const useSelector = (selectorParamFactory, deps) => {
6
6
  const items = selectorParamFactory();
@@ -0,0 +1,13 @@
1
+ import { useSyncExternalStore } from './shim.mjs';
2
+
3
+ const useSnapshot = ({
4
+ subscribe,
5
+ get
6
+ }) => {
7
+ if (process.env.NODE_ENV !== 'production' && !useSyncExternalStore) {
8
+ throw new Error('[reactish-state] Shim setup is required for React 16/17.');
9
+ }
10
+ return useSyncExternalStore(subscribe, get, get);
11
+ };
12
+
13
+ export { useSnapshot };
@@ -1,4 +1,4 @@
1
- import { getReactishValues, createSubscriber, isEqual } from '../utils.js';
1
+ import { createSubscriber, getReactishValues, isEqual } from '../utils.mjs';
2
2
 
3
3
  const createSelector = ({
4
4
  plugin
@@ -24,7 +24,7 @@ const createSelector = ({
24
24
  },
25
25
  subscribe: createSubscriber(items)
26
26
  };
27
- plugin == null || plugin(selector, config);
27
+ plugin?.(selector, config);
28
28
  return selector;
29
29
  };
30
30
  const selector = /*#__PURE__*/createSelector();
@@ -24,7 +24,7 @@ const createState = ({
24
24
  get,
25
25
  set,
26
26
  subscribe,
27
- actions: actionCreator && actionCreator(set, get)
27
+ actions: actionCreator?.(set, get)
28
28
  };
29
29
  };
30
30
  const state = /*#__PURE__*/createState();
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ const applyMiddleware = (middlewares, {
4
+ fromRight
5
+ } = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware({
6
+ ...api,
7
+ set
8
+ }, config) : set, api.set);
9
+
10
+ exports.applyMiddleware = applyMiddleware;
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var applyMiddleware = require('./applyMiddleware.cjs');
4
+ var persist = require('./persist.cjs');
5
+ var reduxDevtools = require('./reduxDevtools.cjs');
6
+
7
+
8
+
9
+ exports.applyMiddleware = applyMiddleware.applyMiddleware;
10
+ exports.persist = persist.persist;
11
+ exports.reduxDevtools = reduxDevtools.reduxDevtools;
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const persist = ({
4
+ prefix,
5
+ getStorage = () => localStorage
6
+ } = {}) => {
7
+ const states = [];
8
+ const middleware = ({
9
+ set,
10
+ get
11
+ }, config) => {
12
+ let key = config?.key || '';
13
+ if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `persist` middleware is used.');
14
+ if (prefix) key = prefix + key;
15
+ states.push([key, set]);
16
+ return (...args) => {
17
+ set(...args);
18
+ try {
19
+ getStorage().setItem(key, JSON.stringify(get()));
20
+ } catch (_unused) {
21
+ /* continue regardless of error */
22
+ }
23
+ };
24
+ };
25
+ middleware.hydrate = () => {
26
+ states.forEach(([key, set]) => {
27
+ try {
28
+ const value = getStorage().getItem(key);
29
+ value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
30
+ } catch (_unused2) {
31
+ /* continue regardless of error */
32
+ }
33
+ });
34
+ states.length = 0;
35
+ };
36
+ return middleware;
37
+ };
38
+
39
+ exports.persist = persist;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const reduxDevtools = ({
4
+ name
5
+ } = {}) => {
6
+ let devtoolsExt;
7
+ if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return;
8
+ const devtools = devtoolsExt.connect({
9
+ name
10
+ });
11
+ const mergedState = {};
12
+ return ({
13
+ set,
14
+ get
15
+ }, config) => {
16
+ const key = config?.key;
17
+ if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` middleware is used.');
18
+ mergedState[key] = get();
19
+ devtools.init(mergedState);
20
+ return (...args) => {
21
+ const [value, action] = args;
22
+ set(...args);
23
+ mergedState[key] = get();
24
+ devtools.send(typeof action === 'string' ? {
25
+ type: action
26
+ } : action || {
27
+ type: `SET_${key}`,
28
+ value
29
+ }, mergedState);
30
+ };
31
+ };
32
+ };
33
+
34
+ exports.reduxDevtools = reduxDevtools;
@@ -0,0 +1,3 @@
1
+ export { applyMiddleware } from './applyMiddleware.mjs';
2
+ export { persist } from './persist.mjs';
3
+ export { reduxDevtools } from './reduxDevtools.mjs';
@@ -7,7 +7,7 @@ const persist = ({
7
7
  set,
8
8
  get
9
9
  }, config) => {
10
- let key = (config == null ? void 0 : config.key) || '';
10
+ let key = config?.key || '';
11
11
  if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `persist` middleware is used.');
12
12
  if (prefix) key = prefix + key;
13
13
  states.push([key, set]);
@@ -11,7 +11,7 @@ const reduxDevtools = ({
11
11
  set,
12
12
  get
13
13
  }, config) => {
14
- const key = config == null ? void 0 : config.key;
14
+ const key = config?.key;
15
15
  if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` middleware is used.');
16
16
  mergedState[key] = get();
17
17
  devtools.init(mergedState);
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const applyPlugin = plugins => (reactish, config) => plugins.forEach(plugin => plugin?.(reactish, config));
4
+
5
+ exports.applyPlugin = applyPlugin;
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ var applyPlugin = require('./applyPlugin.cjs');
4
+ var reduxDevtools = require('./reduxDevtools.cjs');
5
+
6
+
7
+
8
+ exports.applyPlugin = applyPlugin.applyPlugin;
9
+ exports.reduxDevtools = reduxDevtools.reduxDevtools;
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const applyPlugin = plugins => (reactish, config) => plugins.forEach(plugin => plugin == null ? void 0 : plugin(reactish, config));
4
-
5
3
  const reduxDevtools = ({
6
4
  name
7
5
  } = {}) => {
@@ -15,7 +13,7 @@ const reduxDevtools = ({
15
13
  get,
16
14
  subscribe
17
15
  }, config) => {
18
- const key = config == null ? void 0 : config.key;
16
+ const key = config?.key;
19
17
  if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` plugin is used.');
20
18
  const updateState = () => {
21
19
  mergedState[key] = get();
@@ -26,5 +24,4 @@ const reduxDevtools = ({
26
24
  };
27
25
  };
28
26
 
29
- exports.applyPlugin = applyPlugin;
30
27
  exports.reduxDevtools = reduxDevtools;
@@ -1,3 +1,3 @@
1
- const applyPlugin = plugins => (reactish, config) => plugins.forEach(plugin => plugin == null ? void 0 : plugin(reactish, config));
1
+ const applyPlugin = plugins => (reactish, config) => plugins.forEach(plugin => plugin?.(reactish, config));
2
2
 
3
3
  export { applyPlugin };
@@ -0,0 +1,2 @@
1
+ export { applyPlugin } from './applyPlugin.mjs';
2
+ export { reduxDevtools } from './reduxDevtools.mjs';
@@ -11,7 +11,7 @@ const reduxDevtools = ({
11
11
  get,
12
12
  subscribe
13
13
  }, config) => {
14
- const key = config == null ? void 0 : config.key;
14
+ const key = config?.key;
15
15
  if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` plugin is used.');
16
16
  const updateState = () => {
17
17
  mergedState[key] = get();
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var reactShim = require('./reactShim.cjs');
4
+
5
+
6
+
7
+ exports.reactShim = reactShim.reactShim;
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var shim = require('use-sync-external-store/shim/index.js');
4
+
5
+ const reactShim = [shim.useSyncExternalStore];
6
+
7
+ exports.reactShim = reactShim;
@@ -0,0 +1 @@
1
+ export { reactShim } from './reactShim.mjs';
@@ -0,0 +1,5 @@
1
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
2
+
3
+ const reactShim = [useSyncExternalStore];
4
+
5
+ export { reactShim };
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "reactish-state",
3
- "version": "0.11.2",
3
+ "version": "1.0.0-alpha.0",
4
4
  "description": "Simple, decentralized state management for React.",
5
5
  "author": "Zheng Song",
6
6
  "license": "MIT",
7
- "repository": "szhsin/reactish-state",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/szhsin/reactish-state.git"
10
+ },
8
11
  "homepage": "https://github.com/szhsin/reactish-state#readme",
9
- "main": "./dist/cjs/index.js",
10
- "module": "./dist/esm/index.js",
12
+ "main": "./dist/cjs/index.cjs",
13
+ "module": "./dist/esm/index.mjs",
11
14
  "types": "./types/index.d.ts",
12
15
  "sideEffects": false,
13
16
  "files": [
@@ -28,12 +31,12 @@
28
31
  "watch": "rollup -c -w",
29
32
  "clean": "rm -Rf dist types",
30
33
  "types": "tsc",
31
- "posttypes": "rm -Rf types/__tests__",
34
+ "prepare": "rm -Rf types/__tests__",
32
35
  "lint": "eslint .",
33
36
  "lint:fix": "eslint --fix .",
34
37
  "pret": "prettier -c .",
35
38
  "pret:fix": "prettier -w .",
36
- "build": "run-s pret clean lint types bundle",
39
+ "build": "run-s pret lint clean types bundle",
37
40
  "test": "jest",
38
41
  "test:watch": "jest --watch",
39
42
  "eg": "npm run dev --prefix examples"
@@ -41,28 +44,34 @@
41
44
  "exports": {
42
45
  ".": {
43
46
  "types": "./types/index.d.ts",
44
- "require": "./dist/cjs/index.js",
45
- "default": "./dist/esm/index.js"
47
+ "require": "./dist/cjs/index.cjs",
48
+ "default": "./dist/esm/index.mjs"
46
49
  },
47
50
  "./middleware": {
48
51
  "types": "./types/middleware/index.d.ts",
49
- "require": "./dist/middleware/cjs/index.js",
50
- "default": "./dist/middleware/esm/index.js"
52
+ "require": "./dist/middleware/cjs/index.cjs",
53
+ "default": "./dist/middleware/esm/index.mjs"
51
54
  },
52
55
  "./middleware/immer": {
53
56
  "types": "./types/middleware/immer.d.ts",
54
- "require": "./dist/middleware/cjs/immer.js",
55
- "default": "./dist/middleware/esm/immer.js"
57
+ "require": "./dist/middleware/cjs/immer.cjs",
58
+ "default": "./dist/middleware/esm/immer.mjs"
56
59
  },
57
60
  "./plugin": {
58
61
  "types": "./types/plugin/index.d.ts",
59
- "require": "./dist/plugin/cjs/index.js",
60
- "default": "./dist/plugin/esm/index.js"
61
- }
62
+ "require": "./dist/plugin/cjs/index.cjs",
63
+ "default": "./dist/plugin/esm/index.mjs"
64
+ },
65
+ "./shim": {
66
+ "types": "./types/shim/index.d.ts",
67
+ "require": "./dist/shim/cjs/index.cjs",
68
+ "default": "./dist/shim/esm/index.mjs"
69
+ },
70
+ "./package.json": "./package.json"
62
71
  },
63
72
  "peerDependencies": {
64
73
  "immer": ">=6.0",
65
- "react": "^16.8 || ^17.0 || ^18.0"
74
+ "react": "^16.8 || ^17 || ^18 || ^19"
66
75
  },
67
76
  "peerDependenciesMeta": {
68
77
  "immer": {
@@ -70,38 +79,44 @@
70
79
  }
71
80
  },
72
81
  "dependencies": {
73
- "use-sync-external-store": "^1.2.0"
82
+ "use-sync-external-store": "^1.4.0"
74
83
  },
75
84
  "devDependencies": {
76
- "@babel/core": "^7.23.2",
77
- "@babel/preset-env": "^7.23.2",
78
- "@babel/preset-react": "^7.22.15",
79
- "@babel/preset-typescript": "^7.23.2",
80
- "@redux-devtools/extension": "^3.2.5",
85
+ "@babel/core": "^7.26.9",
86
+ "@babel/preset-env": "^7.26.9",
87
+ "@babel/preset-react": "^7.26.3",
88
+ "@babel/preset-typescript": "^7.26.0",
89
+ "@redux-devtools/extension": "^3.3.0",
81
90
  "@rollup/plugin-babel": "^6.0.4",
82
- "@rollup/plugin-node-resolve": "^15.2.3",
83
- "@testing-library/jest-dom": "^6.1.4",
84
- "@testing-library/react": "^14.0.0",
85
- "@types/jest": "^29.5.7",
86
- "@types/react": "^18.2.34",
87
- "@types/use-sync-external-store": "^0.0.5",
88
- "@typescript-eslint/eslint-plugin": "^6.9.1",
89
- "@typescript-eslint/parser": "^6.9.1",
91
+ "@rollup/plugin-node-resolve": "^16.0.0",
92
+ "@rollup/plugin-replace": "^6.0.2",
93
+ "@testing-library/jest-dom": "^6.6.3",
94
+ "@testing-library/react": "^16.2.0",
95
+ "@types/jest": "^29.5.14",
96
+ "@types/react": "^19.0.8",
97
+ "@types/use-sync-external-store": "^0.0.6",
90
98
  "babel-plugin-pure-annotations": "^0.1.2",
91
- "eslint": "^8.53.0",
92
- "eslint-config-prettier": "^9.0.0",
93
- "eslint-plugin-jest": "^27.6.0",
94
- "eslint-plugin-react": "^7.33.2",
95
- "eslint-plugin-react-hooks": "^4.6.0",
96
- "eslint-plugin-react-hooks-addons": "^0.3.1",
97
- "immer": "^10.0.3",
99
+ "eslint": "^9.20.1",
100
+ "eslint-config-prettier": "^10.0.1",
101
+ "eslint-plugin-jest": "^28.11.0",
102
+ "eslint-plugin-react": "^7.37.4",
103
+ "eslint-plugin-react-hooks": "^5.1.0",
104
+ "eslint-plugin-react-hooks-addons": "^0.4.1",
105
+ "globals": "^15.15.0",
106
+ "immer": "^10.1.1",
98
107
  "jest": "^29.7.0",
99
108
  "jest-environment-jsdom": "^29.7.0",
100
109
  "npm-run-all": "^4.1.5",
101
- "prettier": "^3.0.3",
102
- "react": "^18.2.0",
103
- "react-dom": "^18.2.0",
104
- "rollup": "^4.3.0",
105
- "typescript": "^5.2.2"
110
+ "prettier": "^3.4.2",
111
+ "react": "^19.0.0",
112
+ "react-dom": "^19.0.0",
113
+ "rollup": "^4.34.2",
114
+ "typescript": "^5.7.3",
115
+ "typescript-eslint": "^8.24.0"
116
+ },
117
+ "overrides": {
118
+ "whatwg-url@11.0.0": {
119
+ "tr46": "^4"
120
+ }
106
121
  }
107
122
  }
package/types/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export * from './vanilla/state';
3
3
  export * from './vanilla/selector';
4
4
  export * from './react/useSnapshot';
5
5
  export * from './react/useSelector';
6
+ export { setReactShim } from './react/shim';
@@ -1,6 +1,6 @@
1
1
  import type { Middleware } from '../common';
2
2
  interface PersistMiddleware extends Middleware {
3
- hydrate(): void;
3
+ hydrate(this: void): void;
4
4
  }
5
5
  type Persist = (options?: {
6
6
  prefix?: string;
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ declare let useSyncExternalStore: typeof React.useSyncExternalStore;
3
+ declare const setReactShim: ([shim]: readonly [typeof React.useSyncExternalStore]) => void;
4
+ export { useSyncExternalStore, setReactShim };
@@ -1,3 +1,3 @@
1
- import type { ReactishArray, SelectorFunc, SelectorParams } from '../common';
2
- declare const useSelector: <RA extends ReactishArray, T>(selectorParamFactory: () => [...RA, SelectorFunc<RA, T>], deps?: unknown[]) => T;
1
+ import type { ReactishArray, SelectorParams } from '../common';
2
+ declare const useSelector: <RA extends ReactishArray, T>(selectorParamFactory: () => SelectorParams<RA, T>, deps?: unknown[]) => T;
3
3
  export { useSelector };
@@ -0,0 +1 @@
1
+ export * from './reactShim';
@@ -0,0 +1,3 @@
1
+ import { useSyncExternalStore } from 'use-sync-external-store/shim';
2
+ declare const reactShim: readonly [typeof useSyncExternalStore];
3
+ export { reactShim };
@@ -1,6 +1,6 @@
1
1
  import type { Plugin, Selector } from '../common';
2
2
  declare const createSelector: ({ plugin }?: {
3
- plugin?: Plugin | undefined;
3
+ plugin?: Plugin;
4
4
  }) => Selector;
5
5
  declare const selector: Selector;
6
6
  export type { Selector };
@@ -5,7 +5,7 @@ interface State<T, A = unknown, C extends ActionCreator<T, A> = undefined> exten
5
5
  actions: C extends undefined ? never : A;
6
6
  }
7
7
  declare const createState: ({ middleware }?: {
8
- middleware?: Middleware | undefined;
8
+ middleware?: Middleware;
9
9
  }) => <T, A>(initialValue: T, actionCreator?: ActionCreator<T, A>, config?: Config) => State<T, A, ActionCreator<T, A>>;
10
10
  declare const state: <T, A>(initialValue: T, actionCreator?: ActionCreator<T, A>, config?: Config) => State<T, A, ActionCreator<T, A>>;
11
11
  export type { State, ActionCreator };
package/dist/cjs/index.js DELETED
@@ -1,116 +0,0 @@
1
- 'use strict';
2
-
3
- var shim = require('use-sync-external-store/shim');
4
- var react = require('react');
5
-
6
- const createState = ({
7
- middleware
8
- } = {}) => (initialValue, actionCreator, config) => {
9
- let value = initialValue;
10
- const listeners = new Set();
11
- const get = () => value;
12
- let set = newValue => {
13
- const nextValue = typeof newValue === 'function' ? newValue(value) : newValue;
14
- if (!Object.is(value, nextValue)) {
15
- value = nextValue;
16
- listeners.forEach(listener => listener());
17
- }
18
- };
19
- const subscribe = listener => {
20
- listeners.add(listener);
21
- return () => listeners.delete(listener);
22
- };
23
- if (middleware) set = middleware({
24
- set,
25
- get,
26
- subscribe
27
- }, config);
28
- return {
29
- get,
30
- set,
31
- subscribe,
32
- actions: actionCreator && actionCreator(set, get)
33
- };
34
- };
35
- const state = /*#__PURE__*/createState();
36
-
37
- const isEqual = (args1, args2) => {
38
- for (let i = 0; i < args1.length; i++) {
39
- if (!Object.is(args1[i], args2[i])) return false;
40
- }
41
- return true;
42
- };
43
- const createSubscriber = items => listener => {
44
- const unsubscribers = items.map(item => item.subscribe(listener));
45
- return () => unsubscribers.forEach(unsubscribe => unsubscribe());
46
- };
47
- const getReactishValues = items => items.map(item => item.get());
48
-
49
- const createSelector = ({
50
- plugin
51
- } = {}) => (...items) => {
52
- const {
53
- length
54
- } = items;
55
- const cutoff = typeof items[length - 1] === 'function' ? length - 1 : length - 2;
56
- const selectorFunc = items[cutoff];
57
- const config = items[cutoff + 1];
58
- items.length = cutoff;
59
- let cache;
60
- const selector = {
61
- get: () => {
62
- const args = getReactishValues(items);
63
- if (cache && isEqual(args, cache.args)) return cache.val;
64
- const val = selectorFunc(...args);
65
- cache = {
66
- args,
67
- val
68
- };
69
- return val;
70
- },
71
- subscribe: createSubscriber(items)
72
- };
73
- plugin == null || plugin(selector, config);
74
- return selector;
75
- };
76
- const selector = /*#__PURE__*/createSelector();
77
-
78
- const useSnapshot = ({
79
- subscribe,
80
- get
81
- }) => shim.useSyncExternalStore(subscribe, get, get);
82
-
83
- const useSelector = (selectorParamFactory, deps) => {
84
- const items = selectorParamFactory();
85
- const cutoff = items.length - 1;
86
- const selectorFunc = items[cutoff];
87
- items.length = cutoff;
88
- const [context] = react.useState(() => ({
89
- sub: createSubscriber(items)
90
- }));
91
- const get = () => {
92
- const {
93
- cache
94
- } = context;
95
- const reactishValues = getReactishValues(items);
96
- const args = reactishValues.concat(deps || selectorFunc);
97
- if (cache && isEqual(args, cache.args)) return cache.val;
98
- const val = selectorFunc(...reactishValues);
99
- context.cache = {
100
- args,
101
- val
102
- };
103
- return val;
104
- };
105
- return useSnapshot({
106
- get,
107
- subscribe: context.sub
108
- });
109
- };
110
-
111
- exports.createSelector = createSelector;
112
- exports.createState = createState;
113
- exports.selector = selector;
114
- exports.state = state;
115
- exports.useSelector = useSelector;
116
- exports.useSnapshot = useSnapshot;
package/dist/esm/index.js DELETED
@@ -1,4 +0,0 @@
1
- export { createState, state } from './vanilla/state.js';
2
- export { createSelector, selector } from './vanilla/selector.js';
3
- export { useSnapshot } from './react/useSnapshot.js';
4
- export { useSelector } from './react/useSelector.js';
@@ -1,8 +0,0 @@
1
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
2
-
3
- const useSnapshot = ({
4
- subscribe,
5
- get
6
- }) => useSyncExternalStore(subscribe, get, get);
7
-
8
- export { useSnapshot };
@@ -1,79 +0,0 @@
1
- 'use strict';
2
-
3
- const applyMiddleware = (middlewares, {
4
- fromRight
5
- } = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware({
6
- ...api,
7
- set
8
- }, config) : set, api.set);
9
-
10
- const persist = ({
11
- prefix,
12
- getStorage = () => localStorage
13
- } = {}) => {
14
- const states = [];
15
- const middleware = ({
16
- set,
17
- get
18
- }, config) => {
19
- let key = (config == null ? void 0 : config.key) || '';
20
- if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `persist` middleware is used.');
21
- if (prefix) key = prefix + key;
22
- states.push([key, set]);
23
- return (...args) => {
24
- set(...args);
25
- try {
26
- getStorage().setItem(key, JSON.stringify(get()));
27
- } catch (_unused) {
28
- /* continue regardless of error */
29
- }
30
- };
31
- };
32
- middleware.hydrate = () => {
33
- states.forEach(([key, set]) => {
34
- try {
35
- const value = getStorage().getItem(key);
36
- value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
37
- } catch (_unused2) {
38
- /* continue regardless of error */
39
- }
40
- });
41
- states.length = 0;
42
- };
43
- return middleware;
44
- };
45
-
46
- const reduxDevtools = ({
47
- name
48
- } = {}) => {
49
- let devtoolsExt;
50
- if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return;
51
- const devtools = devtoolsExt.connect({
52
- name
53
- });
54
- const mergedState = {};
55
- return ({
56
- set,
57
- get
58
- }, config) => {
59
- const key = config == null ? void 0 : config.key;
60
- if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` middleware is used.');
61
- mergedState[key] = get();
62
- devtools.init(mergedState);
63
- return (...args) => {
64
- const [value, action] = args;
65
- set(...args);
66
- mergedState[key] = get();
67
- devtools.send(typeof action === 'string' ? {
68
- type: action
69
- } : action || {
70
- type: `SET_${key}`,
71
- value
72
- }, mergedState);
73
- };
74
- };
75
- };
76
-
77
- exports.applyMiddleware = applyMiddleware;
78
- exports.persist = persist;
79
- exports.reduxDevtools = reduxDevtools;
@@ -1,3 +0,0 @@
1
- export { applyMiddleware } from './applyMiddleware.js';
2
- export { persist } from './persist.js';
3
- export { reduxDevtools } from './reduxDevtools.js';
@@ -1,2 +0,0 @@
1
- export { applyPlugin } from './applyPlugin.js';
2
- export { reduxDevtools } from './reduxDevtools.js';
File without changes
File without changes
File without changes