Skip to content

Commit

Permalink
Set new store container for each request in subapp-pbundle (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianlent authored and jchip committed Jan 24, 2020
1 parent 55ebfb0 commit 5d05c01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/subapp-pbundle/lib/framework-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class FrameworkLib {
return await this.renderTo(this.createTopComponent(initialProps), this.ref.options);
}

async doReduxBundlerSSR() {
async doReduxBundlerSSR() { // eslint-disable-line complexity
const { subApp, subAppServer, context, options } = this.ref;
const { request } = context.user;
const container = request.container || (request.container = {});

// subApp.reduxReducers || subApp.reduxCreateStore) {
// if sub app has reduxReducers or reduxCreateStore then assume it's using
// redux data model. prepare initial state and store to render it.
Expand All @@ -120,7 +122,7 @@ class FrameworkLib {
// next we take the initial state and create redux store from it
this.store =
reduxData.store ||
(subApp.reduxCreateStore && (await subApp.reduxCreateStore(this.initialState)));
(subApp.reduxCreateStore && (await subApp.reduxCreateStore(this.initialState, container)));
assert(
this.store,
`redux subapp ${subApp.name} didn't provide store, reduxCreateStore, or reducers`
Expand Down
18 changes: 9 additions & 9 deletions packages/subapp-pbundle/src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ function setStoreContainer(container) {
shared = container;
}

function clearSharedStore() {
delete shared.store;
function clearSharedStore(container) {
delete (container || shared).store;
}

function getSharedStore() {
return shared.store;
function getSharedStore(container) {
return (container || shared).store;
}

function setSharedStore(store) {
shared.store = store;
function setSharedStore(store, container) {
(container || shared).store = store;
}

function getReduxCreateStore(info) {
const bundles = info.reduxBundles || [];
return function reduxCreateStore(initialState) {
let store = getSharedStore();
return function reduxCreateStore(initialState, container) {
let store = getSharedStore(container);
if (store) {
store.integrateBundles.apply(this, bundles);
} else {
store = composeBundles.apply(this, bundles)(initialState);
setSharedStore(store);
setSharedStore(store, container);
}
return store;
};
Expand Down

0 comments on commit 5d05c01

Please sign in to comment.