use-typed-reducer 3.1.1 → 3.2.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/index.d.ts +14 -7
- package/dist/index.es.js +37 -1
- package/dist/index.js +37 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -9,13 +9,20 @@ declare type ActionPropState<S, P> = (...args: any) => Promise<(state: S, props:
|
|
|
9
9
|
declare type ActionPropsState<A, S, P> = {
|
|
10
10
|
[K in keyof A]: ActionPropState<S, P>;
|
|
11
11
|
};
|
|
12
|
-
declare type DispatchProps<ST extends
|
|
12
|
+
declare type DispatchProps<ST extends {}, P, Reducers extends ActionPropsState<Reducers, ST, P>> = {
|
|
13
13
|
[K in keyof Reducers]: (...args: any[]) => Promise<(state: ST, props: P) => ST> | ((state: ST, props: P) => ST);
|
|
14
14
|
};
|
|
15
|
-
declare type DefaultReducer<S extends
|
|
16
|
-
declare type WithProps<S extends
|
|
17
|
-
export declare type Reducer<S extends
|
|
18
|
-
export declare type ReducerWithProps<S extends
|
|
19
|
-
export declare const useTypedReducer: <State extends
|
|
20
|
-
export declare const useReducerWithProps: <State extends
|
|
15
|
+
declare type DefaultReducer<S extends {}> = (state: S) => S;
|
|
16
|
+
declare type WithProps<S extends {}, P> = (state: S, props: P) => S;
|
|
17
|
+
export declare type Reducer<S extends {}, A extends (...args: any) => DefaultReducer<S>> = (...params: Parameters<A>) => DefaultReducer<S>;
|
|
18
|
+
export declare type ReducerWithProps<S extends {}, P extends {}, A extends (...args: any) => WithProps<S, P>> = (...params: Parameters<A>) => WithProps<S, P>;
|
|
19
|
+
export declare const useTypedReducer: <State extends {}, Reducers extends Dispatch<State, Reducers>>(initialState: State, reducers: Reducers) => [state: State, dispatch: Reducers];
|
|
20
|
+
export declare const useReducerWithProps: <State extends {}, Props extends {}, Reducers extends DispatchProps<State, Props, Reducers>>(initialState: State, props: Props, reducers: Reducers) => [state: State, dispatch: Reducers];
|
|
21
|
+
declare type FnMap<State> = {
|
|
22
|
+
[k: string]: (...args: any[]) => Partial<State> | Promise<Partial<State>>;
|
|
23
|
+
};
|
|
24
|
+
declare type MappedReducers<State extends {}, Fns extends FnMap<State>> = {
|
|
25
|
+
[F in keyof Fns]: (...args: Parameters<Fns[F]>) => State | Promise<State>;
|
|
26
|
+
};
|
|
27
|
+
export declare const useReducer: <State extends {}, Reducers extends (getState: () => State, getProps: () => Props) => MappedReducers<State, FnMap<State>>, Props extends {}>(initialState: State, reducer: Reducers, props?: Props | undefined) => [state: State, dispatchers: MappedReducers<State, ReturnType<Reducers>>];
|
|
21
28
|
export default useTypedReducer;
|
package/dist/index.es.js
CHANGED
|
@@ -147,7 +147,43 @@ var useReducerWithProps = function (initialState, props, reducers) {
|
|
|
147
147
|
}, reducers);
|
|
148
148
|
}, [reducers]);
|
|
149
149
|
return [state, dispatches];
|
|
150
|
+
};
|
|
151
|
+
var useMutable = function (state) {
|
|
152
|
+
var mutable = useRef(state !== null && state !== void 0 ? state : {});
|
|
153
|
+
useEffect(function () { return void (mutable.current = state); }, [state]);
|
|
154
|
+
return mutable;
|
|
155
|
+
};
|
|
156
|
+
var useReducer = function (initialState, reducer, props) {
|
|
157
|
+
var _a = __read(useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
|
|
158
|
+
var mutableState = useMutable(state);
|
|
159
|
+
var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
|
|
160
|
+
var dispatchers = useMemo(function () {
|
|
161
|
+
var reducers = reducer(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; });
|
|
162
|
+
return entries(reducers).reduce(function (acc, _a) {
|
|
163
|
+
var _b;
|
|
164
|
+
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
165
|
+
return (__assign(__assign({}, acc), (_b = {}, _b[name] = function () {
|
|
166
|
+
var params = [];
|
|
167
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
168
|
+
params[_i] = arguments[_i];
|
|
169
|
+
}
|
|
170
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
171
|
+
var newState;
|
|
172
|
+
return __generator(this, function (_a) {
|
|
173
|
+
switch (_a.label) {
|
|
174
|
+
case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
|
|
175
|
+
case 1:
|
|
176
|
+
newState = _a.sent();
|
|
177
|
+
setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
|
|
178
|
+
return [2 /*return*/];
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
}, _b)));
|
|
183
|
+
}, reducers);
|
|
184
|
+
}, []);
|
|
185
|
+
return [state, dispatchers];
|
|
150
186
|
};
|
|
151
187
|
|
|
152
188
|
export default useTypedReducer;
|
|
153
|
-
export { useReducerWithProps, useTypedReducer };
|
|
189
|
+
export { useReducer, useReducerWithProps, useTypedReducer };
|
package/dist/index.js
CHANGED
|
@@ -151,8 +151,45 @@ var useReducerWithProps = function (initialState, props, reducers) {
|
|
|
151
151
|
}, reducers);
|
|
152
152
|
}, [reducers]);
|
|
153
153
|
return [state, dispatches];
|
|
154
|
+
};
|
|
155
|
+
var useMutable = function (state) {
|
|
156
|
+
var mutable = react.useRef(state !== null && state !== void 0 ? state : {});
|
|
157
|
+
react.useEffect(function () { return void (mutable.current = state); }, [state]);
|
|
158
|
+
return mutable;
|
|
159
|
+
};
|
|
160
|
+
var useReducer = function (initialState, reducer, props) {
|
|
161
|
+
var _a = __read(react.useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
|
|
162
|
+
var mutableState = useMutable(state);
|
|
163
|
+
var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
|
|
164
|
+
var dispatchers = react.useMemo(function () {
|
|
165
|
+
var reducers = reducer(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; });
|
|
166
|
+
return entries(reducers).reduce(function (acc, _a) {
|
|
167
|
+
var _b;
|
|
168
|
+
var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
|
|
169
|
+
return (__assign(__assign({}, acc), (_b = {}, _b[name] = function () {
|
|
170
|
+
var params = [];
|
|
171
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
172
|
+
params[_i] = arguments[_i];
|
|
173
|
+
}
|
|
174
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
175
|
+
var newState;
|
|
176
|
+
return __generator(this, function (_a) {
|
|
177
|
+
switch (_a.label) {
|
|
178
|
+
case 0: return [4 /*yield*/, dispatch.apply(void 0, __spread(params))];
|
|
179
|
+
case 1:
|
|
180
|
+
newState = _a.sent();
|
|
181
|
+
setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
|
|
182
|
+
return [2 /*return*/];
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
}, _b)));
|
|
187
|
+
}, reducers);
|
|
188
|
+
}, []);
|
|
189
|
+
return [state, dispatchers];
|
|
154
190
|
};
|
|
155
191
|
|
|
156
192
|
exports.default = useTypedReducer;
|
|
193
|
+
exports.useReducer = useReducer;
|
|
157
194
|
exports.useReducerWithProps = useReducerWithProps;
|
|
158
195
|
exports.useTypedReducer = useTypedReducer;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-typed-reducer",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "use-reducer React hook alternative",
|
|
5
5
|
"author": "g4rcez",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,8 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "rollup -c",
|
|
20
|
-
"start": "rollup -c -w"
|
|
21
|
-
"prepare": "yarn run build"
|
|
20
|
+
"start": "rollup -c -w"
|
|
22
21
|
},
|
|
23
22
|
"peerDependencies": {
|
|
24
23
|
"react": ">= 16.8.3"
|
|
@@ -32,6 +31,7 @@
|
|
|
32
31
|
"babel-core": "6.26.3",
|
|
33
32
|
"babel-runtime": "6.26.0",
|
|
34
33
|
"cross-env": "7.0.0",
|
|
34
|
+
"prettier": "2.8.7",
|
|
35
35
|
"react-scripts-ts": "3.1.0",
|
|
36
36
|
"rollup": "2.26.11",
|
|
37
37
|
"rollup-plugin-alias": "2.2.0",
|