use-typed-reducer 3.2.1 → 3.2.3
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/index.d.ts +7 -14
- package/dist/index.es.js +26 -41
- package/dist/index.js +28 -41
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MutableRefObject } from "react";
|
|
1
2
|
declare type VoidableFn<Fn extends (...any: any[]) => any> = ReturnType<Fn> extends Promise<any> ? (...a: Parameters<Fn>) => Promise<void> : (...a: Parameters<Fn>) => void;
|
|
2
3
|
declare type Action<State> = (...args: any) => Promise<(state: State) => State> | ((state: State) => State);
|
|
3
4
|
declare type Actions<Actions, State> = {
|
|
@@ -6,30 +7,22 @@ declare type Actions<Actions, State> = {
|
|
|
6
7
|
export declare type Dispatch<ST, Fns extends Actions<Fns, ST>> = {
|
|
7
8
|
[R in keyof Fns]: (...args: any[]) => Promise<(state: ST) => ST> | ((state: ST) => ST);
|
|
8
9
|
};
|
|
9
|
-
declare type ActionPropState<S, P> = (...args: any) => Promise<(state: S, props: P) => S> | ((state: S, props: P) => S);
|
|
10
|
-
declare type ActionPropsState<A, S, P> = {
|
|
11
|
-
[K in keyof A]: ActionPropState<S, P>;
|
|
12
|
-
};
|
|
13
|
-
declare type DispatchProps<ST extends {}, P, Reducers extends ActionPropsState<Reducers, ST, P>> = {
|
|
14
|
-
[K in keyof Reducers]: (...args: any[]) => Promise<(state: ST, props: P) => ST> | ((state: ST, props: P) => ST);
|
|
15
|
-
};
|
|
16
|
-
declare type DefaultReducer<S extends {}> = (state: S) => S;
|
|
17
|
-
declare type WithProps<S extends {}, P> = (state: S, props: P) => S;
|
|
18
|
-
export declare type Reducer<S extends {}, A extends (...args: any) => DefaultReducer<S>> = (...params: Parameters<A>) => DefaultReducer<S>;
|
|
19
|
-
export declare type ReducerWithProps<S extends {}, P extends {}, A extends (...args: any) => WithProps<S, P>> = (...params: Parameters<A>) => WithProps<S, P>;
|
|
20
10
|
declare type RemoveReduceReturns<State extends {}, Reducers extends Dispatch<State, Reducers>> = {
|
|
21
11
|
[R in keyof Reducers]: VoidableFn<Reducers[R]>;
|
|
22
12
|
};
|
|
23
13
|
export declare const useTypedReducer: <State extends {}, Reducers extends Dispatch<State, Reducers>>(initialState: State, reducers: Reducers) => [state: State, dispatch: RemoveReduceReturns<State, Reducers>];
|
|
24
|
-
export declare const useReducerWithProps: <State extends {}, Props extends {}, Reducers extends DispatchProps<State, Props, Reducers>>(initialState: State, props: Props, reducers: Reducers) => [state: State, dispatch: Reducers];
|
|
25
14
|
declare type FnMap<State> = {
|
|
26
15
|
[k: string]: (...args: any[]) => Partial<State> | Promise<Partial<State>>;
|
|
27
16
|
};
|
|
28
17
|
declare type MappedReducers<State extends {}, Fns extends FnMap<State>> = {
|
|
29
|
-
[F in keyof Fns]: (...args: Parameters<Fns[F]>) => State |
|
|
18
|
+
[F in keyof Fns]: (...args: Parameters<Fns[F]>) => Promise<Partial<State>> | Partial<State>;
|
|
30
19
|
};
|
|
31
20
|
declare type MapReducerReturn<State extends {}, Fns extends FnMap<State>> = {
|
|
32
21
|
[F in keyof Fns]: VoidableFn<Fns[F]>;
|
|
33
22
|
};
|
|
34
|
-
export declare const
|
|
23
|
+
export declare const useMutable: <T extends {}>(state: T) => MutableRefObject<T>;
|
|
24
|
+
export declare const dispatchCallback: <Prev extends unknown, T extends Prev | ((prev: Prev) => Prev)>(prev: Prev, setter: T) => any;
|
|
25
|
+
export declare type DispatchCallback<T extends any> = T | ((prev: T) => T);
|
|
26
|
+
export declare const useReducer: <State extends {}, Reducers extends (getState: () => State, getProps: () => Props, initialState: State) => MappedReducers<State, FnMap<State>>, Props extends {}, Middlewares extends ((state: State, key: keyof ReturnType<Reducers>) => State)[]>(initialState: State, reducer: Reducers, props?: Props | undefined, middlewares?: Middlewares | undefined) => readonly [state: State, dispatchers: MapReducerReturn<State, ReturnType<Reducers>>];
|
|
27
|
+
export declare const createReducer: <State extends {} = {}, Props extends {} = {}>() => <Reducer extends (getState: () => State, getProps: () => Props, initialState: State) => MappedReducers<State, FnMap<State>>>(reducer: Reducer) => Reducer;
|
|
35
28
|
export default useTypedReducer;
|
package/dist/index.es.js
CHANGED
|
@@ -116,49 +116,23 @@ var useTypedReducer = function (initialState, reducers) {
|
|
|
116
116
|
}, [reducers]);
|
|
117
117
|
return [state, dispatches];
|
|
118
118
|
};
|
|
119
|
-
var useReducerWithProps = function (initialState, props, reducers) {
|
|
120
|
-
var _a = __read(useState(initialState), 2), state = _a[0], setState = _a[1];
|
|
121
|
-
var refProps = useRef(props);
|
|
122
|
-
useEffect(function () {
|
|
123
|
-
refProps.current = props;
|
|
124
|
-
}, [props]);
|
|
125
|
-
var dispatches = useMemo(function () {
|
|
126
|
-
return entries(reducers).reduce(function (acc, _a) {
|
|
127
|
-
var _b;
|
|
128
|
-
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
129
|
-
return (__assign(__assign({}, acc), (_b = {}, _b[name] = function () {
|
|
130
|
-
var params = [];
|
|
131
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
132
|
-
params[_i] = arguments[_i];
|
|
133
|
-
}
|
|
134
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
135
|
-
var dispatcher;
|
|
136
|
-
return __generator(this, function (_a) {
|
|
137
|
-
switch (_a.label) {
|
|
138
|
-
case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
|
|
139
|
-
case 1:
|
|
140
|
-
dispatcher = _a.sent();
|
|
141
|
-
setState(function (previousState) { return dispatcher(previousState, refProps.current); });
|
|
142
|
-
return [2 /*return*/];
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
}, _b)));
|
|
147
|
-
}, reducers);
|
|
148
|
-
}, [reducers]);
|
|
149
|
-
return [state, dispatches];
|
|
150
|
-
};
|
|
151
119
|
var useMutable = function (state) {
|
|
152
120
|
var mutable = useRef(state !== null && state !== void 0 ? state : {});
|
|
153
121
|
useEffect(function () { return void (mutable.current = state); }, [state]);
|
|
154
122
|
return mutable;
|
|
155
123
|
};
|
|
156
|
-
var
|
|
124
|
+
var dispatchCallback = function (prev, setter) {
|
|
125
|
+
return typeof setter === "function" ? setter(prev) : setter;
|
|
126
|
+
};
|
|
127
|
+
var useReducer = function (initialState, reducer, props, middlewares) {
|
|
157
128
|
var _a = __read(useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
|
|
158
129
|
var mutableState = useMutable(state);
|
|
159
130
|
var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
|
|
131
|
+
var mutableReducer = useMutable(reducer);
|
|
132
|
+
var middleware = useMutable(middlewares !== null && middlewares !== void 0 ? middlewares : []);
|
|
133
|
+
var savedInitialState = useRef(initialState);
|
|
160
134
|
var dispatchers = useMemo(function () {
|
|
161
|
-
var reducers =
|
|
135
|
+
var reducers = mutableReducer.current(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; }, savedInitialState.current);
|
|
162
136
|
return entries(reducers).reduce(function (acc, _a) {
|
|
163
137
|
var _b;
|
|
164
138
|
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
@@ -167,15 +141,26 @@ var useReducer = function (initialState, reducer, props) {
|
|
|
167
141
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
168
142
|
params[_i] = arguments[_i];
|
|
169
143
|
}
|
|
170
|
-
var
|
|
171
|
-
return
|
|
172
|
-
?
|
|
173
|
-
|
|
144
|
+
var st = dispatch.apply(void 0, __spread(params));
|
|
145
|
+
return st instanceof Promise
|
|
146
|
+
? void st.then(function (state) {
|
|
147
|
+
return setState(function (prev) {
|
|
148
|
+
return middleware.current.reduce(function (acc, fn) { return fn(acc, name); }, __assign(__assign({}, prev), { state: state }));
|
|
149
|
+
});
|
|
150
|
+
})
|
|
151
|
+
: setState(function (prev) {
|
|
152
|
+
return middleware.current.reduce(function (acc, fn) { return fn(acc, name); }, __assign(__assign({}, prev), st));
|
|
153
|
+
});
|
|
174
154
|
}, _b)));
|
|
175
|
-
},
|
|
176
|
-
}, []);
|
|
155
|
+
}, {});
|
|
156
|
+
}, [mutableProps, mutableReducer, mutableState]);
|
|
177
157
|
return [state, dispatchers];
|
|
158
|
+
};
|
|
159
|
+
var createReducer = function () {
|
|
160
|
+
return function (reducer) {
|
|
161
|
+
return reducer;
|
|
162
|
+
};
|
|
178
163
|
};
|
|
179
164
|
|
|
180
165
|
export default useTypedReducer;
|
|
181
|
-
export {
|
|
166
|
+
export { createReducer, dispatchCallback, useMutable, useReducer, useTypedReducer };
|
package/dist/index.js
CHANGED
|
@@ -120,49 +120,23 @@ var useTypedReducer = function (initialState, reducers) {
|
|
|
120
120
|
}, [reducers]);
|
|
121
121
|
return [state, dispatches];
|
|
122
122
|
};
|
|
123
|
-
var useReducerWithProps = function (initialState, props, reducers) {
|
|
124
|
-
var _a = __read(react.useState(initialState), 2), state = _a[0], setState = _a[1];
|
|
125
|
-
var refProps = react.useRef(props);
|
|
126
|
-
react.useEffect(function () {
|
|
127
|
-
refProps.current = props;
|
|
128
|
-
}, [props]);
|
|
129
|
-
var dispatches = react.useMemo(function () {
|
|
130
|
-
return entries(reducers).reduce(function (acc, _a) {
|
|
131
|
-
var _b;
|
|
132
|
-
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
133
|
-
return (__assign(__assign({}, acc), (_b = {}, _b[name] = function () {
|
|
134
|
-
var params = [];
|
|
135
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
136
|
-
params[_i] = arguments[_i];
|
|
137
|
-
}
|
|
138
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
139
|
-
var dispatcher;
|
|
140
|
-
return __generator(this, function (_a) {
|
|
141
|
-
switch (_a.label) {
|
|
142
|
-
case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
|
|
143
|
-
case 1:
|
|
144
|
-
dispatcher = _a.sent();
|
|
145
|
-
setState(function (previousState) { return dispatcher(previousState, refProps.current); });
|
|
146
|
-
return [2 /*return*/];
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
}, _b)));
|
|
151
|
-
}, reducers);
|
|
152
|
-
}, [reducers]);
|
|
153
|
-
return [state, dispatches];
|
|
154
|
-
};
|
|
155
123
|
var useMutable = function (state) {
|
|
156
124
|
var mutable = react.useRef(state !== null && state !== void 0 ? state : {});
|
|
157
125
|
react.useEffect(function () { return void (mutable.current = state); }, [state]);
|
|
158
126
|
return mutable;
|
|
159
127
|
};
|
|
160
|
-
var
|
|
128
|
+
var dispatchCallback = function (prev, setter) {
|
|
129
|
+
return typeof setter === "function" ? setter(prev) : setter;
|
|
130
|
+
};
|
|
131
|
+
var useReducer = function (initialState, reducer, props, middlewares) {
|
|
161
132
|
var _a = __read(react.useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
|
|
162
133
|
var mutableState = useMutable(state);
|
|
163
134
|
var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
|
|
135
|
+
var mutableReducer = useMutable(reducer);
|
|
136
|
+
var middleware = useMutable(middlewares !== null && middlewares !== void 0 ? middlewares : []);
|
|
137
|
+
var savedInitialState = react.useRef(initialState);
|
|
164
138
|
var dispatchers = react.useMemo(function () {
|
|
165
|
-
var reducers =
|
|
139
|
+
var reducers = mutableReducer.current(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; }, savedInitialState.current);
|
|
166
140
|
return entries(reducers).reduce(function (acc, _a) {
|
|
167
141
|
var _b;
|
|
168
142
|
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
@@ -171,17 +145,30 @@ var useReducer = function (initialState, reducer, props) {
|
|
|
171
145
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
172
146
|
params[_i] = arguments[_i];
|
|
173
147
|
}
|
|
174
|
-
var
|
|
175
|
-
return
|
|
176
|
-
?
|
|
177
|
-
|
|
148
|
+
var st = dispatch.apply(void 0, __spread(params));
|
|
149
|
+
return st instanceof Promise
|
|
150
|
+
? void st.then(function (state) {
|
|
151
|
+
return setState(function (prev) {
|
|
152
|
+
return middleware.current.reduce(function (acc, fn) { return fn(acc, name); }, __assign(__assign({}, prev), { state: state }));
|
|
153
|
+
});
|
|
154
|
+
})
|
|
155
|
+
: setState(function (prev) {
|
|
156
|
+
return middleware.current.reduce(function (acc, fn) { return fn(acc, name); }, __assign(__assign({}, prev), st));
|
|
157
|
+
});
|
|
178
158
|
}, _b)));
|
|
179
|
-
},
|
|
180
|
-
}, []);
|
|
159
|
+
}, {});
|
|
160
|
+
}, [mutableProps, mutableReducer, mutableState]);
|
|
181
161
|
return [state, dispatchers];
|
|
162
|
+
};
|
|
163
|
+
var createReducer = function () {
|
|
164
|
+
return function (reducer) {
|
|
165
|
+
return reducer;
|
|
166
|
+
};
|
|
182
167
|
};
|
|
183
168
|
|
|
169
|
+
exports.createReducer = createReducer;
|
|
184
170
|
exports.default = useTypedReducer;
|
|
171
|
+
exports.dispatchCallback = dispatchCallback;
|
|
172
|
+
exports.useMutable = useMutable;
|
|
185
173
|
exports.useReducer = useReducer;
|
|
186
|
-
exports.useReducerWithProps = useReducerWithProps;
|
|
187
174
|
exports.useTypedReducer = useTypedReducer;
|