reactish-state 0.8.0 → 0.8.1
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/middleware/cjs/index.js +15 -9
- package/dist/middleware/es/immer.js +4 -1
- package/dist/middleware/es/persist.js +4 -4
- package/dist/middleware/es/reduxDevtools.js +7 -4
- package/package.json +1 -1
- package/types/common.d.ts +8 -2
- package/types/index.d.ts +0 -1
- package/types/vanilla/state.d.ts +4 -6
- package/types/middleware/logger.d.ts +0 -3
|
@@ -15,7 +15,10 @@ var applyMiddleware = function applyMiddleware() {
|
|
|
15
15
|
|
|
16
16
|
var immer = function immer(set) {
|
|
17
17
|
return function (value) {
|
|
18
|
-
|
|
18
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
19
|
+
rest[_key - 1] = arguments[_key];
|
|
20
|
+
}
|
|
21
|
+
return set.apply(void 0, [typeof value === 'function' ? immer$1.produce(value) : value].concat(rest));
|
|
19
22
|
};
|
|
20
23
|
};
|
|
21
24
|
|
|
@@ -28,12 +31,12 @@ var persist = function persist(_temp) {
|
|
|
28
31
|
} : _ref$getStorage;
|
|
29
32
|
var states = [];
|
|
30
33
|
var middleware = function middleware(set, get, config) {
|
|
31
|
-
var key = config ? config.key
|
|
34
|
+
var key = (config == null ? void 0 : config.key) || '';
|
|
32
35
|
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.');
|
|
33
36
|
if (prefix) key = prefix + key;
|
|
34
37
|
states.push([key, set]);
|
|
35
|
-
return function (
|
|
36
|
-
set(
|
|
38
|
+
return function () {
|
|
39
|
+
set.apply(void 0, arguments);
|
|
37
40
|
getStorage().setItem(key, JSON.stringify(get()));
|
|
38
41
|
};
|
|
39
42
|
};
|
|
@@ -42,7 +45,7 @@ var persist = function persist(_temp) {
|
|
|
42
45
|
var key = _ref2[0],
|
|
43
46
|
set = _ref2[1];
|
|
44
47
|
var value = getStorage().getItem(key);
|
|
45
|
-
value && set(JSON.parse(value));
|
|
48
|
+
value && set(JSON.parse(value), 'HYDRATE');
|
|
46
49
|
});
|
|
47
50
|
states.length = 0;
|
|
48
51
|
};
|
|
@@ -55,10 +58,13 @@ var reduxDevtools = function reduxDevtools(set, get, config) {
|
|
|
55
58
|
name: config == null ? void 0 : config.key
|
|
56
59
|
});
|
|
57
60
|
devtools.init(get());
|
|
58
|
-
return function (
|
|
59
|
-
set(
|
|
60
|
-
devtools.send({
|
|
61
|
-
type:
|
|
61
|
+
return function (value, action) {
|
|
62
|
+
set.apply(null, arguments);
|
|
63
|
+
devtools.send(typeof action === 'string' ? {
|
|
64
|
+
type: action
|
|
65
|
+
} : action || {
|
|
66
|
+
type: 'SET',
|
|
67
|
+
value: value
|
|
62
68
|
}, get());
|
|
63
69
|
};
|
|
64
70
|
};
|
|
@@ -2,7 +2,10 @@ import { produce } from 'immer';
|
|
|
2
2
|
|
|
3
3
|
var immer = function immer(set) {
|
|
4
4
|
return function (value) {
|
|
5
|
-
|
|
5
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
6
|
+
rest[_key - 1] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
return set.apply(void 0, [typeof value === 'function' ? produce(value) : value].concat(rest));
|
|
6
9
|
};
|
|
7
10
|
};
|
|
8
11
|
|
|
@@ -7,12 +7,12 @@ var persist = function persist(_temp) {
|
|
|
7
7
|
} : _ref$getStorage;
|
|
8
8
|
var states = [];
|
|
9
9
|
var middleware = function middleware(set, get, config) {
|
|
10
|
-
var key = config ? config.key
|
|
10
|
+
var key = (config == null ? void 0 : 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]);
|
|
14
|
-
return function (
|
|
15
|
-
set(
|
|
14
|
+
return function () {
|
|
15
|
+
set.apply(void 0, arguments);
|
|
16
16
|
getStorage().setItem(key, JSON.stringify(get()));
|
|
17
17
|
};
|
|
18
18
|
};
|
|
@@ -21,7 +21,7 @@ var persist = function persist(_temp) {
|
|
|
21
21
|
var key = _ref2[0],
|
|
22
22
|
set = _ref2[1];
|
|
23
23
|
var value = getStorage().getItem(key);
|
|
24
|
-
value && set(JSON.parse(value));
|
|
24
|
+
value && set(JSON.parse(value), 'HYDRATE');
|
|
25
25
|
});
|
|
26
26
|
states.length = 0;
|
|
27
27
|
};
|
|
@@ -4,10 +4,13 @@ var reduxDevtools = function reduxDevtools(set, get, config) {
|
|
|
4
4
|
name: config == null ? void 0 : config.key
|
|
5
5
|
});
|
|
6
6
|
devtools.init(get());
|
|
7
|
-
return function (
|
|
8
|
-
set(
|
|
9
|
-
devtools.send({
|
|
10
|
-
type:
|
|
7
|
+
return function (value, action) {
|
|
8
|
+
set.apply(null, arguments);
|
|
9
|
+
devtools.send(typeof action === 'string' ? {
|
|
10
|
+
type: action
|
|
11
|
+
} : action || {
|
|
12
|
+
type: 'SET',
|
|
13
|
+
value: value
|
|
11
14
|
}, get());
|
|
12
15
|
};
|
|
13
16
|
};
|
package/package.json
CHANGED
package/types/common.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
export declare type Getter<T> = () => T;
|
|
2
|
-
export declare type Setter<T> = (newValue: T | ((value: T) => T)
|
|
2
|
+
export declare type Setter<T> = (newValue: T | ((value: T) => T), action?: string | {
|
|
3
|
+
type: string;
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
}) => void;
|
|
3
6
|
export declare type Listener = () => void;
|
|
4
7
|
export interface Reactish<T> {
|
|
5
8
|
get: Getter<T>;
|
|
6
9
|
subscribe: (listener: Listener) => () => void;
|
|
7
10
|
}
|
|
11
|
+
export interface Config {
|
|
12
|
+
key?: string;
|
|
13
|
+
}
|
|
8
14
|
export interface Middleware {
|
|
9
|
-
<T
|
|
15
|
+
<T>(set: Setter<T>, get: Getter<T>, config?: Config): Setter<T>;
|
|
10
16
|
}
|
package/types/index.d.ts
CHANGED
package/types/vanilla/state.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import type { Setter, Reactish, Middleware } from '../common';
|
|
1
|
+
import type { Setter, Reactish, Config, Middleware } from '../common';
|
|
2
2
|
declare type ActionCreator<T, A> = ((set: Setter<T>, get: () => T) => A) | null | undefined;
|
|
3
3
|
interface State<T, A = unknown, C extends ActionCreator<T, A> = undefined> extends Reactish<T> {
|
|
4
4
|
set: Setter<T>;
|
|
5
5
|
actions: C extends undefined ? never : A;
|
|
6
6
|
}
|
|
7
|
-
declare
|
|
8
|
-
declare const createState: <T, X>({ middleware }?: {
|
|
7
|
+
declare const createState: ({ middleware }?: {
|
|
9
8
|
middleware?: Middleware | undefined;
|
|
10
|
-
}) => <A>(initialValue: T, actionCreator?: ActionCreator<T, A>, config?:
|
|
11
|
-
declare const state:
|
|
12
|
-
export type { StateCreator };
|
|
9
|
+
}) => <T, A>(initialValue: T, actionCreator?: ActionCreator<T, A>, config?: Config) => State<T, A, ActionCreator<T, A>>;
|
|
10
|
+
declare const state: <T, A>(initialValue: T, actionCreator?: ActionCreator<T, A>, config?: Config) => State<T, A, ActionCreator<T, A>>;
|
|
13
11
|
export { state, createState };
|