-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsagas.js
47 lines (39 loc) · 1.75 KB
/
sagas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import 'babel-polyfill' // required for using generators
import { takeEvery } from 'redux-saga'
import { put } from 'redux-saga/effects'
import { INIT as reduxPouchInit} from 'redux-pouchdb'
import canvas from './canvas'
import * as actions from './actions'
// Hook our local action handlers to actions defined in modules
// (because just like reducers can handle actions, other actions should too)
const hookedActions = {
[canvas.signalItemTapped.getType()]: actions.handleTapItem,
[canvas.signalItemDraggedOut.getType()]: actions.handleDraggedOut,
[canvas.signalDropOnCanvas.getType()]: actions.handleDropOnCanvas,
[canvas.signalCanvasTapped.getType()]: actions.handleTapCanvas,
[canvas.signalEdgeTapped.getType()]: actions.handleTapEdge,
[canvas.signalReceivedDrop.getType()]: actions.handleReceivedDrop,
[canvas.signalResetCanvas.getType()]: actions.handleResetCanvas,
[canvas.signalEscape.getType()]: actions.handleEscape,
// Initialise the canvas when stored state has been loaded from PouchDB
[reduxPouchInit]: actions.initCanvas,
}
let sagas = []
// Create a saga for each pair of action+handler
for (let sourceAction in hookedActions) {
let actionHandler = hookedActions[sourceAction]
sagas.push(bindHandler(sourceAction, actionHandler))
}
// A saga that simply listens to one type of action and triggers its assigned handler.
function bindHandler(sourceAction, actionHandler) {
return function* actionListener() {
yield* takeEvery(sourceAction, function* (actionObject) {
try {
yield put(actionHandler(actionObject.payload))
} catch (err) {
console.error(`Error in saga handling ${sourceAction}: ${err}`)
}
})
}
}
export default sagas