Skip to content

Commit

Permalink
Merge pull request #2 from simonihmig/fix
Browse files Browse the repository at this point in the history
Fix updateNode being called with undefined rootNode
  • Loading branch information
pzuraq authored Sep 29, 2020
2 parents ed9ec85 + 941011f commit 8c35231
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/-private/create-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ export default function createStore<S, A extends Action, Ext, StateExt>(
);

const originalGetState = store.getState.bind(store);

let rootNode;

store.getState = (): S => {
const ensureRootNode = (): void => {
if (rootNode === undefined) {
rootNode = createNode({
state: originalGetState(),
});
}
}

let rootNode;

store.getState = (): S => {
ensureRootNode();
return rootNode.proxy.state;
};

store.subscribe(() => {
ensureRootNode();
updateNode(rootNode, {
state: originalGetState(),
});
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ function createCache(fn) {

module('Unit | basic', () => {
module('primitive root', () => {
test('it works', (assert) => {
test('redux works', (assert) => {
let store = createStore((state = 0) => ++state);

store.dispatch({ type: 'INCREMENT' });

assert.equal(store.getState(), 2, 'value is correct');
});

test('caching works', (assert) => {
let store = createStore((state = 0) => ++state);

let cache = createCache(() => store.getState());
Expand Down

0 comments on commit 8c35231

Please sign in to comment.