Skip to content

Instantly share code, notes, and snippets.

@shotamatsuda
Created September 16, 2018 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shotamatsuda/c8c7489b7b720d33183c06318adf6b19 to your computer and use it in GitHub Desktop.
Save shotamatsuda/c8c7489b7b720d33183c06318adf6b19 to your computer and use it in GitHub Desktop.
export function handleAction (action) {
const args = action.payload || []
switch (action.type) {
case DO_SOMETHING:
return doSomething(...args)
default:
break
}
}
function doSomething () {
// ...
}
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)
}
}
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