Skip to content

Commit

Permalink
fix(core): listener relative state
Browse files Browse the repository at this point in the history
  • Loading branch information
artalar committed Oct 1, 2022
1 parent 6998bd5 commit 95526b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@reatom/core",
"private": false,
"version": "2.0.21",
"version": "2.0.22",
"description": "State manager for both simple and complex applications",
"source": "src/index.ts",
"main": "build/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function createStore({
let lastState = impossibleValue
const listener: typeof cb = (_state, causes) => {
const { state } = cache.get(atom)!
Object.is(lastState, state) || cb(state, causes)
Object.is(lastState, state) || cb(lastState = _state, causes)
}

listeners.add(listener)
Expand Down
27 changes: 8 additions & 19 deletions packages/core/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ test('State updates order', async () => {
const a = createAtom(
{ setB: () => null, _setC: () => null },
({ onAction, schedule, create }, state = 'a') => {

onAction('setB', () => {
state = 'b'
schedule((dispatch) => {
Expand All @@ -554,30 +553,20 @@ test('State updates order', async () => {
state = 'c'
})

return state;
return state
},
);
)
const store = createStore()
const listener = mockFn()
store.subscribe(a, listener)
store.dispatch(a.setB());
store.dispatch(a.setB())

await sleep()

/* Debug log */
// listener.calls.forEach(c => {
// console.log(
// parseCauses(c.i.at(1))
// )
// })

assert.equal(parseCauses(listener.lastInput(1)), [
'DISPATCH: setB_atom14',
'setB (setB_atom14) handler',
'DISPATCH: _setC_atom14',
'_setC_atom14 action'
])
assert.is(listener.calls.at(1)?.i.at(0), 'b');
assert.equal(
listener.calls.map((c) => c.i[0]),
['a', 'c'],
)
})


test.run()

0 comments on commit 95526b2

Please sign in to comment.