Repository navigation
Machine core: run a transition's effects before its new activities start - #139
Merged
Merged
Conversation
When a transition changed the state, the actor started the new state's activities before running the transition's effects, so an activity could act on a world its own transition hadn't finished setting up. The private tour hit this: its page check read a diff's loading clock before the resume effect shifted it by the paused time. Now the activities the state no longer lists end, the effects run, and then the activities it newly lists start. If an effect stops the actor, the next start starts them. The guide documents the order, and that a send from a callback can end its own runner before returning: a frame loop requests its next frame before sending its tick, or checks signal.aborted after. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A control asked for on a ready page is posted before its expiry timer starts, so its timeout is measured from the post; playback that stops at its end has ended its frame loop before it asks the view to stop. The webview test fails on the previous order. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The guide incorrectly implies effects are canceled when their keys disappear from a new state, so it must distinguish activity removal from keyed-effect supersession and actor-stop cleanup.
1 open issue
What changed in this PR
This PR updates the shared machine actor so transition effects run after obsolete activities end but before new activities start. This lets new activities observe setup completed by the transition’s effects.
Changes:
- Split activity reconciliation into stop and start phases around effects.
- Add regression tests for actor ordering, webview timeout startup, and playback cleanup.
- Document transition ordering and cancellation during callback-driven sends.
| File | Description |
|---|---|
packages/machine/test/actor.test.mjs |
Tests ordering, stop/restart behavior, and callback-triggered cancellation. |
packages/machine/src/machine.ts |
Updates the emit API documentation with the new ordering. |
packages/machine/src/actor.ts |
Runs transition effects before starting newly listed activities. |
packages/integrations/vscode/test/page-lifecycle.test.mjs |
Verifies ready-page controls are posted before their timeouts start. |
packages/host/core/test/playback-lifecycle.test.mjs |
Verifies frame cleanup precedes playback stop requests. |
docs/guides/state-machines.md |
Explains transition ordering and runner cancellation precautions. |
Review effort: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

When a transition changed the state, the actor started the new state's activities before it ran the transition's effects. An activity could therefore act on a world its own transition hadn't finished setting up. The private tour hit this (githubnext/projections.private#56): its page check read a diff's loading clock before the resume effect had shifted it by the paused time, so a long pause failed the tour.
The new order
When a transition changes the state:
If an effect stops the actor, the new activities start on the next
start. A transition that keeps the state runs its effects as before.Guide
docs/guides/state-machines.mddocuments the order. It also documents a related hazard the private carousel hit (githubnext/projections.private#60): asendfrom a callback (a frame, a timer, a response) is handled before it returns, and can end the very runner that sent it. So a runner that does more work after sending should request it first, such as a frame loop's next frame, or checksignal.aborted.Existing machines
All three machines on main keep their behaviour:
show,resume,build) don't depend on the next poll having started.playingask for no effects, and a stop's frame loop ended before its effects under the old order too.Tests
packages/machine/test/actor.test.mjs:start;sendfrom a callback that leaves the state ends its own activity before it returns.page-lifecycle.test.mjs: a control is posted before its timeout starts. This test fails on the previous order.playback-lifecycle.test.mjs: playback stopping at its end has no frames running when it asks the view to stop.Locally: machine tests 20/20, page lifecycle 7/7, playback lifecycle 9/9, and
tools check --fastpasses.