Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/guides/host-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ three groups of fields:
`setChangesOnly`, `setVariedOnly`, `setRegeneratedOnly`, `setSearch`,
`select`, `hover`, `setRenderer`, `setControl`, `runAction`, and `openSource`.

**Status events.** A host that waits on the renderer listens to the core instead
of observing the view's DOM:

- `onRendererStatusChange(status)` is called with `rendererStatus` whenever it
changes, and with the first (`none`, `loading`, `ready` or `failed`): say, to
swap a poster image for the live view once it is `ready`, or keep the poster
when it `failed`.
- `paint` (`PaintState`, the pure `paintState(controller)`) says whether the
frames show what is asked: `painting` until every shown pane has acknowledged
painting it (in two-pane modes the before pane its start and the after pane
its end, in Animated the current progress), then `painted`.
- `onPainted({mode, progress})` is called once each time the frames settle on
what is shown while paused: after a load, a mode change, a seek or the end of
playback. For a host that captures the projection, or reveals it, only once it
is drawn.

`ProjectionStage({projection, labels?, paneLabels?, hidden?, stackBelow?, renderStatus?, legend?, fill?})`
mounts and coordinates the sandboxed frames for the active comparison mode:

Expand Down
2 changes: 2 additions & 0 deletions packages/host/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export type {
ScopeHidden,
StageBinding,
} from './projection.js'
export {paintState} from './projection/paint.js'
export type {PaintState, Painted} from './projection/paint.js'
export {ProjectionStage} from './stage.js'
export {nothingHiddenTitle, scopeHiddenShort, scopeHiddenText} from './scope.js'
export {arrangementHysteresis, paneArrangement, paneGeometry} from './stage/arrangement.js'
Expand Down
9 changes: 6 additions & 3 deletions packages/host/core/src/pane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface PaneState {
capabilities?: RendererCapabilities
/** The progress the renderer last acknowledged painting. */
rendered?: number
/** The `init` that acknowledgement answers: until the pane catches up with a new one, `rendered` is the old one's. */
renderedInit?: InitPayload
steps: number[]
/** How many step slots the renderer's presentation spans, when it reports more than its steps. */
stepSlots?: number
Expand Down Expand Up @@ -124,6 +126,8 @@ export function RendererPane(props: RendererPaneProps) {
onState(state)
}, [state, onState])

// The init the frame was last given, so an acknowledgement records the one it answers.
const initialized = useRef(init)
const url = renderer.url
const title = props.label ? `${renderer.title} renderer, ${props.label}` : `${renderer.title} renderer`
const titleRef = useRef(title)
Expand All @@ -148,7 +152,7 @@ export function RendererPane(props: RendererPaneProps) {
setState(previous => ({...previous, status: 'failed', message: event.message}))
break
case 'rendered':
setState(previous => ({...previous, rendered: event.progress}))
setState(previous => ({...previous, rendered: event.progress, renderedInit: initialized.current}))
break
case 'steps':
setState(({stepSlots: _slots, ...previous}) => ({
Expand Down Expand Up @@ -244,11 +248,10 @@ export function RendererPane(props: RendererPaneProps) {
useEffect(() => {
frame.current?.setFill(fill)
}, [fill])
const initialized = useRef(init)
useEffect(() => {
if (initialized.current === init) return
initialized.current = init
setState(({rendered: _rendered, stepSlots: _slots, ...previous}) => ({...previous, steps: []}))
setState(({rendered: _rendered, renderedInit: _init, stepSlots: _slots, ...previous}) => ({...previous, steps: []}))
frame.current?.setInit(init, latest.current.progress)
}, [init])
useEffect(() => {
Expand Down
75 changes: 75 additions & 0 deletions packages/host/core/src/projection/paint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {useEffect} from 'react'
import type {InitPayload} from '@githubnext/projections-renderer-sdk/protocol'
import {twoPaneMode} from '../modes.js'
import type {ComparisonMode} from '../modes.js'
import type {PaneKey, ProjectionController, ProjectionOptions, RendererStatus} from './types.js'

/**
* Whether the renderer frames show what the controller asks: `none` without a renderer; `loading` or `failed` while a
* shown pane is; `painting` while a pane hasn't yet acknowledged painting the progress it was sent; `painted` once every
* shown pane has (in two-pane modes the before pane its start and the after pane its end, in Animated the current
* progress).
*/
export type PaintState = 'none' | 'loading' | 'failed' | 'painting' | 'painted'

/**
* The frames' paint state for a controller's renderer, mode, progress and panes (see `PaintState`). With the stage's
* `init`, an acknowledgement of an earlier one (the frames haven't caught up with a new mode or subject yet) doesn't
* count.
*/
export function paintState(
projection: Pick<ProjectionController, 'renderer' | 'comparing' | 'mode' | 'progress' | 'panes'> & {stage?: {init: InitPayload}},
): PaintState {
const {renderer, comparing, mode, progress, panes} = projection
const init = projection.stage?.init
if (!renderer) return 'none'
const keys: PaneKey[] = twoPaneMode(mode) ? ['before', 'after'] : ['main']
const shown = keys.map(key => panes[key])
if (shown.some(pane => pane.status === 'failed')) return 'failed'
if (shown.some(pane => pane.status === 'loading')) return 'loading'
const expected = (key: PaneKey) => (key === 'before' ? 0 : key === 'after' ? 1 : comparing && mode === 'animated' ? progress : undefined)
const painted = keys.every((key, index) => {
const pane = shown[index]!
const rendered = init && pane.renderedInit && pane.renderedInit !== init ? undefined : pane.rendered
const target = expected(key)
return rendered !== undefined && (target === undefined || Math.abs(rendered - target) < 1e-9)
})
return painted ? 'painted' : 'painting'
}

/** What `onPainted` reports: the mode and progress the frames settled on. */
export interface Painted {
mode: ComparisonMode
progress: number
}

/**
* Tells the embedder when the renderer status changes (`onRendererStatusChange`, with the first status too) and when
* the frames settle on what is shown while paused (`onPainted`, once each time they get there), so a host waits on the
* core instead of observing the view's DOM.
*/
export function useStatusEvents({
latest,
status,
paint,
playing,
mode,
progress,
}: {
latest: {current: Pick<ProjectionOptions, 'onRendererStatusChange' | 'onPainted'>}
status: RendererStatus
paint: PaintState
playing: boolean
mode: ComparisonMode
progress: number
}) {
useEffect(() => {
latest.current.onRendererStatusChange?.(status)
}, [latest, status])
const settled = paint === 'painted' && !playing ? `${mode}|${progress}` : undefined
useEffect(() => {
if (settled !== undefined) latest.current.onPainted?.({mode, progress})
// Only a new settled state reports; mode and progress are part of it.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [latest, settled])
}
14 changes: 14 additions & 0 deletions packages/host/core/src/projection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {Holds} from '../playback.js'
import type {ProjectionSelection, ProjectionTarget, TargetAnnotation, TargetResolution} from '../targets/target.js'
import type {SummaryFigure, SummaryFilter} from '../summary.js'
import type {ScopeCounts, ScopeHidden} from './filtering.js'
import type {PaintState, Painted} from './paint.js'

/**
* Change highlighting: `animated` fades emphasis in and out with progress
Expand Down Expand Up @@ -81,6 +82,17 @@ export type ProjectionOptions = {
onAction?: (id: string) => void
/** Called whenever the selection changes, with the selected row and the targets it presents (`null` when nothing is selected). */
onSelectionChange?: (selection: ProjectionSelection | null) => void
/**
* Called with the renderer status (`rendererStatus`) whenever it changes, and with the first: for a host that waits on
* the renderer, say to swap a poster image for the live view once it is `ready`, or to keep the poster if it `failed`.
*/
onRendererStatusChange?: (status: RendererStatus) => void
/**
* Called when the frames settle on what is shown while paused (`paint` becomes `painted`): once each time, after a
* load, a mode change, a seek or the end of playback. For a host that captures or reveals the projection only once it
* is drawn.
*/
onPainted?: (painted: Painted) => void
/**
* Markers on targets, for example how many comments each has. Renderers
* with the `annotations` capability and the host's views draw one marker
Expand Down Expand Up @@ -233,6 +245,8 @@ export interface ProjectionController {
panes: Readonly<Record<PaneKey, PaneState>>
/** The progress the animated frame last acknowledged painting. */
renderedProgress: number | undefined
/** Whether the frames show what is asked (`PaintState`): `painted` once every shown pane acknowledged painting it. */
paint: PaintState

/** The transition slider's position (see `ProjectionOptions.position`). */
position: number
Expand Down
4 changes: 4 additions & 0 deletions packages/host/core/src/projection/use-projection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {rendererState, useComparisonModes, useRendererControls} from './renderer
import {useSelection} from './selection.js'
import type {Anchors} from './selection.js'
import {useStageBinding} from './stage-binding.js'
import {paintState, useStatusEvents} from './paint.js'

/**
* The headless Projections host. It owns the state that must be correct and
Expand Down Expand Up @@ -93,8 +94,10 @@ export function useProjection(options: ProjectionOptions): ProjectionController
const summary = useMemo(() => summaryFigures(subject, JSON.parse(summaryKey) ?? undefined), [subject, summaryKey])
const {visibleRows, filter, counts: scopeCounts, hidden: scopeHidden} = useRowFilter(rows, subject, filters, summary)
const payloads = useRendererPayloads({options, renderer, mode, progress, direction, highlighting, theme, reducedMotion})
const paint = paintState({renderer, comparing, mode, progress, panes, stage: payloads})
const controls = useRendererControls({latest, internal, setInternal, options, renderer, panes, paneKeys})
const status = rendererState(renderer, panes, paneKeys)
useStatusEvents({latest, status: status.status, paint, playing, mode, progress})

const selection = useSelection({latest, request, subject, rows, selectedId, placed, anchors, frames})

Expand Down Expand Up @@ -147,6 +150,7 @@ export function useProjection(options: ProjectionOptions): ProjectionController
rendererStatus: status.status,
panes,
renderedProgress: panes.main.rendered,
paint,
position,
progress,
holds,
Expand Down
37 changes: 37 additions & 0 deletions packages/host/core/test/paint.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {test} from 'node:test'
import assert from 'node:assert/strict'
import {paintState} from '../dist/index.js'

const renderer = {name: 'graph', title: 'Graph', source: 'builtin', url: 'graph.html'}
const pane = (status, rendered) => ({status, rendered, steps: [], rendererErrors: [], rejected: 0, controls: []})
const panes = (main, before = pane('loading'), after = pane('loading')) => ({main, before, after})
const state = overrides => ({renderer, comparing: true, mode: 'animated', progress: 0.5, panes: panes(pane('ready', 0.5)), ...overrides})

test('animated frames are painted once they acknowledge the progress shown', () => {
assert.equal(paintState(state()), 'painted')
assert.equal(paintState(state({panes: panes(pane('ready', 0.25))})), 'painting')
assert.equal(paintState(state({panes: panes(pane('ready'))})), 'painting')
assert.equal(paintState(state({panes: panes(pane('loading'))})), 'loading')
assert.equal(paintState(state({panes: panes(pane('failed'))})), 'failed')
assert.equal(paintState(state({renderer: undefined})), 'none')
})

test('two-pane modes are painted once the before pane shows its start and the after pane its end', () => {
const twoPanes = (before, after) => state({mode: 'side-by-side', panes: panes(pane('loading'), before, after)})
assert.equal(paintState(twoPanes(pane('ready', 0), pane('ready', 1))), 'painted')
assert.equal(paintState(twoPanes(pane('ready', 0), pane('ready'))), 'painting')
assert.equal(paintState(twoPanes(pane('ready', 0), pane('failed'))), 'failed')
})

test('one extraction, or a diff, is painted once its frame has painted at all', () => {
assert.equal(paintState(state({comparing: false, panes: panes(pane('ready', 1))})), 'painted')
assert.equal(paintState(state({mode: 'diff', panes: panes(pane('ready', 1))})), 'painted')
})

test('an acknowledgement of an earlier init does not count until the frame catches up', () => {
const animated = {comparisonMode: 'animated'}
const diff = {comparisonMode: 'diff'}
const acknowledged = (init, rendered) => ({...pane('ready', rendered), renderedInit: init})
assert.equal(paintState(state({mode: 'diff', panes: panes(acknowledged(animated, 0.5)), stage: {init: diff}})), 'painting')
assert.equal(paintState(state({mode: 'diff', panes: panes(acknowledged(diff, 1)), stage: {init: diff}})), 'painted')
})