Created
October 9, 2018 09:06
-
-
Save tirthbodawala/fb689d0df60388563e93441f20d09dc2 to your computer and use it in GitHub Desktop.
Redux Saga with PawJS
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 ReduxClient from '@pawjs/redux/client'; | |
import createSagaMiddleware from 'redux-saga'; | |
import mySaga from './app/saga'; | |
import reducers from '../app/reducers'; | |
const appInitialState = {}; | |
// ... other imports | |
export default class Client { | |
constructor({addPlugin}) { | |
const reduxClient = new ReduxClient({addPlugin}); | |
reduxClient.setReducers(reducers); | |
this.sagaMiddleware = createSagaMiddleware(); | |
reduxClient.addMiddleware(this.sagaMiddleware); | |
addPlugin(reduxClient); | |
} | |
apply(clientHandler) { | |
clientHandler | |
.hooks | |
.reduxInitialState | |
.tapPromise("ReduxInitialState", async ({getInitialState, setInitialState}) => { | |
const initialState = Object.assign({}, getInitialState(), appInitialState); | |
setInitialState(initialState); | |
}); | |
clientHandler | |
.hooks | |
.before | |
.tapPromise("RunSagaMiddleware", async () => { | |
return this.sagaMiddleware.run(mySaga); | |
}); | |
} | |
} |
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 ReduxServer from '@pawjs/redux/server'; | |
import createSagaMiddleware from 'redux-saga'; | |
import reducers from '../app/reducers'; | |
import mySaga from './app/saga'; | |
const appInitialState = {}; | |
export default class Server { | |
constructor({addPlugin}) { | |
this.sagaMiddleware = createSagaMiddleware(); | |
const reduxServer = new ReduxServer({addPlugin}); | |
reduxServer.setReducers(reducers); | |
reduxServer.addMiddleware(this.sagaMiddleware); | |
addPlugin(reduxServer); | |
} | |
apply(serverHandler) { | |
serverHandler | |
.hooks | |
.reduxInitialState | |
.tapPromise("AppInitialState", async ({getInitialState, setInitialState}) => { | |
const initialState = Object.assign({}, getInitialState(), appInitialState); | |
setInitialState(initialState); | |
}); | |
serverHandler | |
.hooks | |
.beforeAppRender | |
.tapPromise("RunSagaMiddleware", async () => { | |
return this.sagaMiddleware.run(mySaga); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment