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.
- package/dist/cjs/index.cjs +17 -0
- package/dist/cjs/react/shim.cjs +10 -0
- package/dist/cjs/react/useSelector.cjs +35 -0
- package/dist/cjs/react/useSnapshot.cjs +15 -0
- package/dist/cjs/utils.cjs +17 -0
- package/dist/cjs/vanilla/selector.cjs +35 -0
- package/dist/cjs/vanilla/state.cjs +35 -0
- package/dist/esm/index.mjs +5 -0
- package/dist/esm/react/shim.mjs +8 -0
- package/dist/esm/react/{useSelector.js → useSelector.mjs} +2 -2
- package/dist/esm/react/useSnapshot.mjs +13 -0
- package/dist/esm/vanilla/{selector.js → selector.mjs} +2 -2
- package/dist/esm/vanilla/{state.js → state.mjs} +1 -1
- package/dist/middleware/cjs/applyMiddleware.cjs +10 -0
- package/dist/middleware/cjs/index.cjs +11 -0
- package/dist/middleware/cjs/persist.cjs +39 -0
- package/dist/middleware/cjs/reduxDevtools.cjs +34 -0
- package/dist/middleware/esm/index.mjs +3 -0
- package/dist/middleware/esm/{persist.js → persist.mjs} +1 -1
- package/dist/middleware/esm/{reduxDevtools.js → reduxDevtools.mjs} +1 -1
- package/dist/plugin/cjs/applyPlugin.cjs +5 -0
- package/dist/plugin/cjs/index.cjs +9 -0
- package/dist/plugin/cjs/{index.js → reduxDevtools.cjs} +1 -4
- package/dist/plugin/esm/{applyPlugin.js → applyPlugin.mjs} +1 -1
- package/dist/plugin/esm/index.mjs +2 -0
- package/dist/plugin/esm/{reduxDevtools.js → reduxDevtools.mjs} +1 -1
- package/dist/shim/cjs/index.cjs +7 -0
- package/dist/shim/cjs/reactShim.cjs +7 -0
- package/dist/shim/esm/index.mjs +1 -0
- package/dist/shim/esm/reactShim.mjs +5 -0
- package/package.json +57 -42
- package/types/index.d.ts +1 -0
- package/types/middleware/persist.d.ts +1 -1
- package/types/react/shim.d.ts +4 -0
- package/types/react/useSelector.d.ts +2 -2
- package/types/shim/index.d.ts +1 -0
- package/types/shim/reactShim.d.ts +3 -0
- package/types/vanilla/selector.d.ts +1 -1
- package/types/vanilla/state.d.ts +1 -1
- package/dist/cjs/index.js +0 -116
- package/dist/esm/index.js +0 -4
- package/dist/esm/react/useSnapshot.js +0 -8
- package/dist/middleware/cjs/index.js +0 -79
- package/dist/middleware/esm/index.js +0 -3
- package/dist/plugin/esm/index.js +0 -2
- /package/dist/esm/{utils.js → utils.mjs} +0 -0
- /package/dist/middleware/cjs/{immer.js → immer.cjs} +0 -0
- /package/dist/middleware/esm/{applyMiddleware.js → applyMiddleware.mjs} +0 -0
- /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,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';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
|
-
import { createSubscriber, getReactishValues, isEqual } from '../utils.
|
|
3
|
-
import { useSnapshot } from './useSnapshot.
|
|
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 {
|
|
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
|
|
27
|
+
plugin?.(selector, config);
|
|
28
28
|
return selector;
|
|
29
29
|
};
|
|
30
30
|
const selector = /*#__PURE__*/createSelector();
|
|
@@ -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;
|
|
@@ -7,7 +7,7 @@ const persist = ({
|
|
|
7
7
|
set,
|
|
8
8
|
get
|
|
9
9
|
}, config) => {
|
|
10
|
-
let 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
|
|
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);
|
|
@@ -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
|
|
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;
|
|
@@ -11,7 +11,7 @@ const reduxDevtools = ({
|
|
|
11
11
|
get,
|
|
12
12
|
subscribe
|
|
13
13
|
}, config) => {
|
|
14
|
-
const key = config
|
|
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 @@
|
|
|
1
|
+
export { reactShim } from './reactShim.mjs';
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reactish-state",
|
|
3
|
-
"version": "0.
|
|
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":
|
|
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.
|
|
10
|
-
"module": "./dist/esm/index.
|
|
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
|
-
"
|
|
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
|
|
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.
|
|
45
|
-
"default": "./dist/esm/index.
|
|
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.
|
|
50
|
-
"default": "./dist/middleware/esm/index.
|
|
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.
|
|
55
|
-
"default": "./dist/middleware/esm/immer.
|
|
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.
|
|
60
|
-
"default": "./dist/plugin/esm/index.
|
|
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
|
|
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.
|
|
82
|
+
"use-sync-external-store": "^1.4.0"
|
|
74
83
|
},
|
|
75
84
|
"devDependencies": {
|
|
76
|
-
"@babel/core": "^7.
|
|
77
|
-
"@babel/preset-env": "^7.
|
|
78
|
-
"@babel/preset-react": "^7.
|
|
79
|
-
"@babel/preset-typescript": "^7.
|
|
80
|
-
"@redux-devtools/extension": "^3.
|
|
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": "^
|
|
83
|
-
"@
|
|
84
|
-
"@testing-library/
|
|
85
|
-
"@
|
|
86
|
-
"@types/
|
|
87
|
-
"@types/
|
|
88
|
-
"@
|
|
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": "^
|
|
92
|
-
"eslint-config-prettier": "^
|
|
93
|
-
"eslint-plugin-jest": "^
|
|
94
|
-
"eslint-plugin-react": "^7.
|
|
95
|
-
"eslint-plugin-react-hooks": "^
|
|
96
|
-
"eslint-plugin-react-hooks-addons": "^0.
|
|
97
|
-
"
|
|
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.
|
|
102
|
-
"react": "^
|
|
103
|
-
"react-dom": "^
|
|
104
|
-
"rollup": "^4.
|
|
105
|
-
"typescript": "^5.
|
|
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
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { ReactishArray,
|
|
2
|
-
declare const useSelector: <RA extends ReactishArray, T>(selectorParamFactory: () =>
|
|
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';
|
package/types/vanilla/state.d.ts
CHANGED
|
@@ -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
|
|
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,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;
|
package/dist/plugin/esm/index.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|