Created
September 16, 2018 11:43
-
-
Save shotamatsuda/c8c7489b7b720d33183c06318adf6b19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function handleAction (action) { | |
const args = action.payload || [] | |
switch (action.type) { | |
case DO_SOMETHING: | |
return doSomething(...args) | |
default: | |
break | |
} | |
} | |
function doSomething () { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function workerActions (store) { | |
const worker = new window.Worker('/worker.js') | |
worker.addEventListener('message', ({ data: action }) => { | |
store.dispatch(action) | |
}) | |
return next => action => { | |
worker.postMessage(action) | |
return next(action) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore, applyMiddleware } from 'redux' | |
import { handleAction } from './action' | |
import reducer from './reducer' | |
const middleware = store => next => action => { | |
self.postMessage(action) | |
return next(action) | |
} | |
const store = createStore(reducer, applyMiddleware(middleware)) | |
let promise = Promise.resolve() | |
self.addEventListener('message', event => { | |
promise = promise.then(() => { | |
return store.dispatch(handleAction(event.data)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment