Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
a50bf888
Commit
a50bf888
authored
Jun 01, 2021
by
Robert Knight
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve types and identifier names in `createReducer`
parent
4ca56948
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
12 deletions
+17
-12
util.js
src/sidebar/store/util.js
+17
-12
No files found.
src/sidebar/store/util.js
View file @
a50bf888
...
...
@@ -15,26 +15,31 @@ export function actionTypes(reducers) {
}
/**
* Given objects which map action names to update functions, this returns a
* reducer function that can be passed to the redux `createStore` function.
* Create a standard Redux reducer function from a map of action types to reducers.
*
* @param {Object} actionToUpdateFn - Object mapping action names to update
* functions.
* @template {object} State
* @param {Record<string, (s: State, action: any) => Partial<State>>} reducers -
* Object mapping action types to reducer functions. The result of the
* reducer is merged with the existing state.
*/
export
function
createReducer
(
actionToUpdateFn
)
{
return
(
state
=
{}
,
action
)
=>
{
const
fn
=
actionToUpdateFn
[
action
.
type
];
if
(
!
fn
)
{
export
function
createReducer
(
reducers
)
{
return
(
state
=
/** @type {State} */
({})
,
action
)
=>
{
const
reducer
=
reducers
[
action
.
type
];
if
(
!
reducer
)
{
return
state
;
}
const
stateChanges
=
reducer
(
state
,
action
);
// Some modules return an array rather than an object. They need to be
// handled differently so we don't c
as
t them to an object.
if
(
Array
.
isArray
(
state
))
{
return
[...
fn
(
state
,
action
)]
;
// handled differently so we don't c
onver
t them to an object.
if
(
Array
.
isArray
(
state
Changes
))
{
return
stateChanges
;
}
return
{
// @ts-expect-error - TS isn't certain `state` is spreadable here. Trust us!
...
state
,
...
fn
(
state
,
action
)
,
...
stateChanges
,
};
};
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment