Skip to content

Commit

Permalink
fix(core): always notify effects subscribers when a hydration has met…
Browse files Browse the repository at this point in the history
…adata (#98)
  • Loading branch information
bowheart authored May 14, 2024
1 parent e84fa84 commit ee07e87
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/api/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ export class Store<State = any> {
(this.constructor as typeof Store).hierarchyConfig
)[0]

if (newState === this._state) {
// Nothing to do. TODO: Should this inform effects subscribers?
// short-circuit if there's no change and no metadata that needs to reach
// this (or a parent/child) store's effects subscribers
if (newState === this._state && !meta) {
return this._state
}

Expand Down
45 changes: 45 additions & 0 deletions packages/core/test/units/Store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,51 @@ describe('Store.setState()', () => {
type: zeduxTypes.hydrate,
})
})

test('always informs effects subscribers if metadata is passed', () => {
const effectsSubscriber = jest.fn()
const nextSubscriber = jest.fn()
const parentEffectsSubscriber = jest.fn()
const parentNextSubscriber = jest.fn()
const store = createStore(null, 1)
const parentStore = createStore({ child: store })

store.subscribe({ effects: effectsSubscriber, next: nextSubscriber })
parentStore.subscribe({
effects: parentEffectsSubscriber,
next: parentNextSubscriber,
})

store.setState(1)

expect(effectsSubscriber).not.toHaveBeenCalled()
expect(nextSubscriber).not.toHaveBeenCalled()
expect(parentEffectsSubscriber).not.toHaveBeenCalled()
expect(parentNextSubscriber).not.toHaveBeenCalled()

store.setState(1, 'test metadata')

expect(effectsSubscriber).toHaveBeenCalledTimes(1)
expect(effectsSubscriber).toHaveBeenCalledWith(
expect.objectContaining({
action: expect.objectContaining({
meta: 'test metadata',
}),
})
)
expect(nextSubscriber).not.toHaveBeenCalled()
expect(parentEffectsSubscriber).toHaveBeenCalledTimes(1)
expect(parentEffectsSubscriber).toHaveBeenCalledWith(
expect.objectContaining({
action: expect.objectContaining({
metaType: zeduxTypes.delegate,
payload: expect.objectContaining({
meta: 'test metadata',
}),
}),
})
)
})
})

describe('Store.setStateDeep()', () => {
Expand Down

0 comments on commit ee07e87

Please sign in to comment.