Skip to content

Host core: playback's lifecycle as a state machine, with one stop path - #115

Merged
agorischek merged 5 commits into
mainfrom
host-playback-machine
Nov 21, 2026
Merged

agorischek merged 5 commits into
mainfrom
host-playback-machine

Conversation

@agorischek

Copy link
Copy Markdown
Collaborator

Stacked on #110, which adds @githubnext/projections-machine. Review the last commit; this retargets to main once #110 merges.

Why

Playback's lifecycle was implicit, spread across usePlaybackActions, usePlaybackLoop and usePlaybackPosition: four effects, nine refs, and transitions made in callbacks, effects and during render. The code review survey found:

  • Two separate stop-at-end paths: the frame that reaches the end, and an effect that compared the shown position with the endpoint.
  • An optimistic playingRef that drifts from the view. If a controlled embedder declined a play, the next click on the single play button paused instead of asking again.
  • A float comparison to detect "the embedder started playback", which is really an event.
  • Direction kept in three places, and travelled as a ref read during render, so the play button's direction could render stale.

What changes

playbackLifecycle in projection/playback-lifecycle.ts is the single owner:

  • States. paused, holding a play that was asked for but isn't shown yet as armed; or playing, which advances frame by frame from its run. The frames are an activity of playing.
  • Events. User actions (play, pause, toggle, togglePlayback, seek) and frame ticks are events. So is what the view shows, sent when it changes: playing, the position, a controlled direction, reduced motion, the mode and the subject. Playing and the position remain requests, applied by the core or by the embedder that controls them.
  • Exactly one stop path. Only the frame that reaches the end stops playback.
  • Declined plays are noticed. If the render after a play request still shows paused, the arm is cleared, so the next click asks to play again.
  • Embedder-started playback is an explicit transition. It plays away from the end it starts at, or under reduced motion jumps to the endpoint.

usePlaybackControl in projection/playback-control.ts runs the machine with useMachine and replaces the old actions and loop hooks. usePlaybackPosition now keeps only the timeline and position. The ProjectionController API is unchanged.

There is one deliberate behaviour change. When the embedder keeps controlled playing on across a subject change, travelled stays true, because playback continues to travel. Previously it was reset while playback continued.

Tests

test/playback-lifecycle.test.mjs:

  • request and echo rules, including that asking twice while armed doesn't re-request;
  • a declined play;
  • a single stop at the end, after which the button plays backward;
  • an embedder starting at an end, with and without reduced motion;
  • a new subject;
  • an explore over three plans (pause holds, looping, timeline), checking that the slider stays in range, that playing implies travelled, that a stop request leaves playback paused and disarmed, that a play request arms, and that a non-looping frame reaching its end stops. It runs with complete: true.

The exploration found a case that broke "playing implies travelled" (a subject change while the embedder keeps playing). That case drove the travelled change above.

Verification

  • tools check --fast passes. The lifecycle baseline drops by 3.
  • tsc passes for host core and its browser configuration.
  • Host core's node suites pass, except the targets test, which needs the engine's Wasm and fails the same way without this change.
  • The browser specs (host.spec.ts controlled playback, holds.spec.ts) are left to CI.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved synchronization bugs between controlled props and the playback actor can freeze playback or cause pause, seek, and direction actions to conflict with the displayed state.

4 open issues
What changed in this PR

Moves host-core playback onto the state-machine package introduced in #110, centralizing lifecycle handling and endpoint stopping while preserving the ProjectionController API.

Changes:

  • Replaces the actions and frame-loop hooks with a playback lifecycle machine and React adapter.
  • Separates timeline calculations from playback state and request handling.
  • Adds lifecycle tests and state exploration, and removes obsolete lifecycle-check baseline entries.
File Description
tools/​scripts/​check-machines/​baseline.json Removes baseline entries for migrated playback hooks.
packages/​host/​core/​test/​playback-lifecycle.test.mjs Tests requests, transitions, endpoint stopping, and lifecycle invariants.
packages/​host/​core/​src/​projection/​use-projection.ts Integrates the new playback controller.
packages/​host/​core/​src/​projection/​playback-position.ts Retains timeline and position calculations without lifecycle state.
packages/​host/​core/​src/​projection/​playback-loop.ts Removes the effect-based frame loop.
packages/​host/​core/​src/​projection/​playback-lifecycle.ts Defines playback states, events, requests, and frame activity.
packages/​host/​core/​src/​projection/​playback-control.ts Connects the machine to React, controlled values, and animation frames.
packages/​host/​core/​src/​projection/​playback-actions.ts Removes the ref-based action handlers.
packages/​host/​core/​package.json Adds the state-machine dependency.
package-lock.json Records the host-core dependency change.

Review effort: Balanced

agorischek added a commit that referenced this pull request Nov 21, 2026
… be shown

From the code review on #115. The lifecycle heard what the view shows only when it changed, so a request an embedder
declined or hadn't applied yet left the two apart:

- The position and a controlled direction are reconciled whenever they differ from the lifecycle's. A declined
  seek no longer leaves the next play starting from the rejected position, and togglePlayback plays the way
  `nextDirection` says.
- Whether playback is shown playing is reconciled after every render. Pause then play in one event, while
  playing throughout, plays on.
- `paused` is `stopping` from a stop it asked for until the view stops showing playback. An embedder that applies
  the stop later doesn't see playback start again from the end. If it declines the stop, Pause asks again rather
  than asking to play.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Base automatically changed from machine-core to main November 21, 2026 01:29
agorischek and others added 2 commits November 20, 2026 17:40
Playback was spread over two hooks, four effects and nine refs: an optimistic `playingRef`, a direction kept in three
places, a `travelled` ref read during render, a float comparison to tell a play the embedder started from one the view
asked for, and two separate ways playback stopped at its end.

`playbackLifecycle` (playback-lifecycle.ts, on @githubnext/projections-machine) is now the one place:

- `paused`, with a play asked for but not yet shown `armed`, or `playing`, frame by frame from its run.
- Actions (play, pause, toggle, togglePlayback, seek) and frames are events. What the view shows (playing, the
  position, a controlled direction, reduced motion, the mode, the subject) arrives as events when it changes. Playing
  and the position stay requests the core or the embedder applies.
- A play the view shows from `armed` plays from it. One the embedder started plays away from an end it is at.
- Only a frame stops playback at its end; the view showing the end doesn't stop it a second time.
- A play the embedder declines (the next render still shows paused) is asked for again by the next click, instead
  of being taken for playback that the click pauses.
- `travelled` and the direction are state, so the play button's direction renders when they change.

usePlaybackControl (playback-control.ts) runs it with useMachine. The frames are an activity of `playing`. It replaces
usePlaybackActions and usePlaybackLoop. usePlaybackPosition keeps only the timeline and the position. The
controller's API is unchanged.

The tests cover the request and echo rules, the single stop, embedder start and decline, reduced motion, and a new
subject. An exploration checks that every request leaves playback where it says and that the slider stays in range.
The lifecycle baseline loses the three patterns these files had.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… be shown

From the code review on #115. The lifecycle heard what the view shows only when it changed, so a request an embedder
declined or hadn't applied yet left the two apart:

- The position and a controlled direction are reconciled whenever they differ from the lifecycle's. A declined
  seek no longer leaves the next play starting from the rejected position, and togglePlayback plays the way
  `nextDirection` says.
- Whether playback is shown playing is reconciled after every render. Pause then play in one event, while
  playing throughout, plays on.
- `paused` is `stopping` from a stop it asked for until the view stops showing playback. An embedder that applies
  the stop later doesn't see playback start again from the end. If it declines the stop, Pause asks again rather
  than asking to play.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek force-pushed the host-playback-machine branch from 08b201a to 78a9788 Compare November 21, 2026 01:40
@agorischek
agorischek merged commit 06754ca into main Nov 21, 2026
52 checks passed
@agorischek
agorischek deleted the host-playback-machine branch November 21, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants