Skip to content

Commit

Permalink
Issue 179: onChange not called for immediate states (#180)
Browse files Browse the repository at this point in the history
* Fix #179: Make sure to call onChange with 'immediate' states

* Adapt test expectations

* Add changeset as requested
  • Loading branch information
ehuelsmann committed Nov 28, 2023
1 parent 987fc5a commit 52742ab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-foxes-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"robot3": major
---

Call onChange callbacks for immediate states too
7 changes: 3 additions & 4 deletions packages/core/machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ function transitionTo(service, machine, fromEvent, candidates) {

if (d._onEnter) d._onEnter(machine, to, service.context, context, fromEvent);
let state = newMachine.state.value;
service.machine = newMachine;
service.onChange(service);
return state.enter(newMachine, service, fromEvent);
}
}
Expand All @@ -181,10 +183,7 @@ function send(service, event) {

let service = {
send(event) {
this.machine = send(this, event);

// TODO detect change
this.onChange(this);
send(this, event);
}
};

Expand Down
7 changes: 4 additions & 3 deletions packages/core/test/test-invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ QUnit.module('Invoke', hooks => {
});

QUnit.test('Invoking a machine that immediately finishes', async assert => {
assert.expect(1);
assert.expect(3);
const expectations = [ 'two', 'nestedTwo', 'three' ];

const child = createMachine({
nestedOne: state(
Expand All @@ -336,9 +337,9 @@ QUnit.module('Invoke', hooks => {
let service = interpret(parent, s => {
// TODO not totally sure if this is correct, but I think it should
// hit this only once and be equal to three
assert.equal(s.machine.current, 'three');
assert.equal(s.machine.current, expectations.shift());
});

service.send('next');
});
});
});

0 comments on commit 52742ab

Please sign in to comment.