use-typed-reducer 3.2.1 → 3.2.2

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 CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="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,19 @@ 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 | Promise<State>;
18
+ [F in keyof Fns]: (...args: Parameters<Fns[F]>) => State | Promise<State> | Partial<State> | Promise<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
  };
23
+ export declare const useMutable: <T extends {}>(state: T) => import("react").MutableRefObject<T>;
34
24
  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: MapReducerReturn<State, ReturnType<Reducers>>];
35
25
  export default useTypedReducer;
package/dist/index.es.js CHANGED
@@ -116,38 +116,6 @@ 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]);
@@ -157,8 +125,9 @@ var useReducer = function (initialState, reducer, props) {
157
125
  var _a = __read(useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
158
126
  var mutableState = useMutable(state);
159
127
  var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
128
+ var mutableReducer = useMutable(reducer);
160
129
  var dispatchers = useMemo(function () {
161
- var reducers = reducer(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; });
130
+ var reducers = mutableReducer.current(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; });
162
131
  return entries(reducers).reduce(function (acc, _a) {
163
132
  var _b;
164
133
  var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
@@ -169,8 +138,8 @@ var useReducer = function (initialState, reducer, props) {
169
138
  }
170
139
  var newState = dispatch.apply(void 0, __spread(params));
171
140
  return newState instanceof Promise
172
- ? newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
173
- : setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
141
+ ? void newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
142
+ : setState(function (prev) { return (__assign(__assign({}, prev), newState)); });
174
143
  }, _b)));
175
144
  }, reducers);
176
145
  }, []);
@@ -178,4 +147,4 @@ var useReducer = function (initialState, reducer, props) {
178
147
  };
179
148
 
180
149
  export default useTypedReducer;
181
- export { useReducer, useReducerWithProps, useTypedReducer };
150
+ export { useMutable, useReducer, useTypedReducer };
package/dist/index.js CHANGED
@@ -120,38 +120,6 @@ 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]);
@@ -161,8 +129,9 @@ var useReducer = function (initialState, reducer, props) {
161
129
  var _a = __read(react.useState(function () { return initialState; }), 2), state = _a[0], setState = _a[1];
162
130
  var mutableState = useMutable(state);
163
131
  var mutableProps = useMutable(props !== null && props !== void 0 ? props : {});
132
+ var mutableReducer = useMutable(reducer);
164
133
  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 : {}; });
134
+ var reducers = mutableReducer.current(function () { return mutableState.current; }, function () { var _a; return (_a = mutableProps.current) !== null && _a !== void 0 ? _a : {}; });
166
135
  return entries(reducers).reduce(function (acc, _a) {
167
136
  var _b;
168
137
  var _c = __read(_a, 2), name = _c[0], dispatch = _c[1];
@@ -173,8 +142,8 @@ var useReducer = function (initialState, reducer, props) {
173
142
  }
174
143
  var newState = dispatch.apply(void 0, __spread(params));
175
144
  return newState instanceof Promise
176
- ? newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
177
- : setState(function (previousState) { return (__assign(__assign({}, previousState), newState)); });
145
+ ? void newState.then(function (state) { return void setState(function (prev) { return (__assign(__assign({}, prev), state)); }); })
146
+ : setState(function (prev) { return (__assign(__assign({}, prev), newState)); });
178
147
  }, _b)));
179
148
  }, reducers);
180
149
  }, []);
@@ -182,6 +151,6 @@ var useReducer = function (initialState, reducer, props) {
182
151
  };
183
152
 
184
153
  exports.default = useTypedReducer;
154
+ exports.useMutable = useMutable;
185
155
  exports.useReducer = useReducer;
186
- exports.useReducerWithProps = useReducerWithProps;
187
156
  exports.useTypedReducer = useTypedReducer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "use-typed-reducer",
3
- "version": "3.2.1",
3
+ "version": "3.2.2",
4
4
  "description": "use-reducer React hook alternative",
5
5
  "author": "g4rcez",
6
6
  "license": "MIT",