Skip to content

Instantly share code, notes, and snippets.

@tirthbodawala
Created October 9, 2018 09:06
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 tirthbodawala/fb689d0df60388563e93441f20d09dc2 to your computer and use it in GitHub Desktop.
Save tirthbodawala/fb689d0df60388563e93441f20d09dc2 to your computer and use it in GitHub Desktop.
Redux Saga with PawJS
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);
});
}
}
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