Skip to content

Commit

Permalink
Clear hydrated stores (#785)
Browse files Browse the repository at this point in the history
* Clear hydratedStores on each #react_component call
* Create clearHydratedStores() methods for StoreRegistry.js and ReactOnRails.js
* Add clearHydratedStores() invocation to #initialize_redux_stores helper
* Add JS tests for clearHydratedStores() methods in StoreRegistry.js and ReactOnRails.js
* Remove forgotten console logging
* Update tests and ReactOnRails.js code according to PR review notices
* JS tests now use setStore
* Move clearHydratedStores method to INTERNALLY USED APIs section
  • Loading branch information
udovenko authored and justin808 committed Mar 31, 2017
1 parent 7fee1f6 commit e32cb74
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ def initialize_redux_stores

all_stores = (@registered_stores || []) + (@registered_stores_defer_render || [])

result = all_stores.each_with_object(declarations) do |redux_store_data, memo|
result = <<-JS
ReactOnRails.clearHydratedStores();
JS

result << all_stores.each_with_object(declarations) do |redux_store_data, memo|
store_name = redux_store_data[:store_name]
props = props_string(redux_store_data[:props])
memo << <<-JS
Expand Down
8 changes: 8 additions & 0 deletions node_package/src/ReactOnRails.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ ctx.ReactOnRails = {
return StoreRegistry.setStore(name, store);
},

/**
* Clears hydratedStores to avoid accidental usage of wrong store hydrated in previous/parallel
* request.
*/
clearHydratedStores() {
StoreRegistry.clearHydratedStores();
},

/**
* ReactOnRails.render("HelloWorldApp", {name: "Stranger"}, 'app');
*
Expand Down
7 changes: 7 additions & 0 deletions node_package/src/StoreRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ This can happen if you are server rendering and either:
hydratedStores.set(name, store);
},

/**
* Internally used function to completely clear hydratedStores Map.
*/
clearHydratedStores() {
hydratedStores.clear();
},

/**
* Get a Map containing all registered store generators. Useful for debugging.
* @returns Map where key is the component name and values are the store generators.
Expand Down
21 changes: 21 additions & 0 deletions node_package/tests/ReactOnRails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,24 @@ test('setStore and getStore', (assert) => {

assert.deepEqual(ReactOnRails.stores(), expected);
});

test('clearHydratedStores', (assert) => {
assert.plan(2);
function reducer() {
return {};
}

function storeGenerator(props) {
return createStore(reducer, props);
}

ReactOnRails.setStore('storeGenerator', storeGenerator);
const actual = new Map();
actual.set(storeGenerator);
assert.deepEqual(actual, ReactOnRails.stores());

ReactOnRails.clearHydratedStores();
const expected = new Map();
assert.deepEqual(ReactOnRails.stores(), expected,
'clearHydratedStores should clear hydratedStores map');
});
14 changes: 14 additions & 0 deletions node_package/tests/StoreRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ test('StoreRegistry throws error for retrieving unregistered hydrated store', (a
'Expected an exception for calling StoreRegistry.getStore with an invalid name.',
);
});

test('StoreRegistry clearHydratedStores', (assert) => {
assert.plan(2);
StoreRegistry.stores().clear();

StoreRegistry.setStore('storeGenerator', storeGenerator);
const actual = new Map();
actual.set(storeGenerator);
assert.deepEqual(actual, StoreRegistry.stores());

StoreRegistry.clearHydratedStores();
const expected = new Map();
assert.deepEqual(StoreRegistry.stores(), expected);
});

0 comments on commit e32cb74

Please sign in to comment.