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
99d764ab
Unverified
Commit
99d764ab
authored
Mar 01, 2018
by
Robert Knight
Committed by
GitHub
Mar 01, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #682 from hypothesis/allow-multiple-handlers-for-action
Allow multiple state modules to handle the same action
parents
7838cfde
f500b5d5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
10 deletions
+42
-10
index.js
src/sidebar/reducers/index.js
+3
-3
util-test.js
src/sidebar/reducers/test/util-test.js
+20
-0
util.js
src/sidebar/reducers/util.js
+19
-7
No files found.
src/sidebar/reducers/index.js
View file @
99d764ab
...
...
@@ -37,14 +37,14 @@ function init(settings) {
);
}
var
update
=
util
.
createReducer
(
Object
.
assign
(
var
update
=
util
.
createReducer
(
...[
annotations
.
update
,
frames
.
update
,
links
.
update
,
selection
.
update
,
session
.
update
,
viewer
.
update
)
);
viewer
.
update
,
]
);
module
.
exports
=
{
init
:
init
,
...
...
src/sidebar/reducers/test/util-test.js
View file @
99d764ab
...
...
@@ -68,6 +68,26 @@ describe('reducer utils', function () {
annotations
:
[{
id
:
1
}],
});
});
it
(
'applies update functions from each input object'
,
()
=>
{
var
firstCounterActions
=
{
INCREMENT_COUNTER
(
state
)
{
return
{
firstCounter
:
state
.
firstCounter
+
1
};
},
};
var
secondCounterActions
=
{
INCREMENT_COUNTER
(
state
)
{
return
{
secondCounter
:
state
.
secondCounter
+
1
};
},
};
var
reducer
=
util
.
createReducer
(
firstCounterActions
,
secondCounterActions
);
var
state
=
{
firstCounter
:
5
,
secondCounter
:
10
};
var
action
=
{
type
:
'INCREMENT_COUNTER'
};
var
newState
=
reducer
(
state
,
action
);
assert
.
deepEqual
(
newState
,
{
firstCounter
:
6
,
secondCounter
:
11
});
});
});
describe
(
'#bindSelectors'
,
function
()
{
...
...
src/sidebar/reducers/util.js
View file @
99d764ab
...
...
@@ -11,16 +11,28 @@ function actionTypes(updateFns) {
}
/**
* Given an object which maps action names to update functions, this returns
* a reducer function that can be passed to the redux `createStore` function.
* Given objects which map action names to update functions, this returns a
* reducer function that can be passed to the redux `createStore` function.
*
* @param {Object[]} actionToUpdateFn - Objects mapping action names to update
* functions.
*/
function
createReducer
(
updateFns
)
{
return
function
(
state
,
action
)
{
var
fn
=
updateFns
[
action
.
type
];
if
(
!
fn
)
{
function
createReducer
(...
actionToUpdateFn
)
{
// Combine the (action name => update function) maps together into a single
// (action name => update functions) map.
var
actionToUpdateFns
=
{};
actionToUpdateFn
.
forEach
(
map
=>
{
Object
.
keys
(
map
).
forEach
(
k
=>
{
actionToUpdateFns
[
k
]
=
(
actionToUpdateFns
[
k
]
||
[]).
concat
(
map
[
k
]);
});
});
return
(
state
,
action
)
=>
{
var
fns
=
actionToUpdateFns
[
action
.
type
];
if
(
!
fns
)
{
return
state
;
}
return
Object
.
assign
({},
state
,
fn
(
state
,
action
));
return
Object
.
assign
({},
state
,
...
fns
.
map
(
f
=>
f
(
state
,
action
)
));
};
}
...
...
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