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
10 changes: 10 additions & 0 deletions docs/guides/host-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ calling `close`, or clicking outside the card (inside `container` or outside
`view`, while toolbar clicks and popovers keep it open) dismisses it and
restores focus, leaving the item selected until a new selection reopens it.

**Running jobs.** A host that extracts or compares again while the last drawing
stays shows that a job runs;
`runningIndicator(onChange, {delayMs?, minimumMs?, timers?})` decides when: only
after 300 ms (`runningDelayMs`), so short jobs never flash it, then for at least
400 ms (`runningMinimumMs`). It has no React or DOM, at
`@githubnext/projections-host/running` for code without either (an extension
host). `useRunningIndicator(running)` is the hook every UI's indicator uses (the
default UI's "Updating…", the VS Code UI's summary bar spinner), and
`runningText` its words ("Extracting…", "Comparing…").

## The view model

A host UI decides which views it offers and which controls show. The core makes
Expand Down
4 changes: 4 additions & 0 deletions packages/host/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./running": {
"types": "./dist/running.d.ts",
"import": "./dist/running.js"
},
"./styles.css": "./dist/ui.css",
"./build": {
"types": "./build/collect-renderers.d.mts",
Expand Down
3 changes: 3 additions & 0 deletions packages/host/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export type {ProjectionStageProps} from './stage.js'
export type {PaneArrangement} from './stage/arrangement.js'
export type {PaneEdge, PaneLabels} from './stage/layout.js'
export {useComparisonPair} from './pair.js'
export {runningDelayMs, runningIndicator, runningMinimumMs, runningText} from './running.js'
export type {Running, RunningTimers} from './running.js'
export {useRunningIndicator} from './use-running.js'
export {comparisonSessionDefaults, useComparisonSession} from './comparison-session.js'
export type {ComparisonSessionOptions, ComparisonSessionSettings} from './comparison-session.js'
export type {ComparisonPair, ComparisonPairOptions, ExtractionChoice, PairOutcome} from './pair.js'
Expand Down
101 changes: 101 additions & 0 deletions packages/host/core/src/running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* When a running job's indicator shows: not for a short job, which would only flash, and, once shown, long enough to
* read. Every host UI's indicators follow the same rule: the default UI's "Updating…", the VS Code UI's summary bar
* spinner and the VS Code extension's status bar item. Nothing here imports React or the DOM, so code without either
* (an extension host, a worker) can import it from `@githubnext/projections-host/running`.
*/

/** What a projection tab is doing: extracting a side of its pair, or comparing the two. */
export type Running = 'extracting' | 'comparing'

/** How long a job runs before its indicator shows. */
export const runningDelayMs = 300
/** How long the indicator stays once shown, however soon the job ends. */
export const runningMinimumMs = 400

/** What a job says it is doing, in an indicator's tooltip and accessible text: "Extracting…" or "Comparing…". */
export const runningText = (running: Running) => (running === 'extracting' ? 'Extracting…' : 'Comparing…')

/**
* The clock and timers `runningIndicator` uses, so a test can drive them: `now` in milliseconds, `set` schedules `run`
* after `ms` milliseconds and returns a handle that `clear` cancels. Defaults to `Date.now` and `setTimeout`.
*/
export interface RunningTimers {
now(): number
set(run: () => void, ms: number): unknown
clear(timer: unknown): void
}
const realTimers: RunningTimers = {
now: () => Date.now(),
set: (run, ms) => setTimeout(run, ms),
clear: timer => clearTimeout(timer as ReturnType<typeof setTimeout>),
}

/**
* An indicator of a running job: `set` it to what the job does (or `undefined` once it ends) as often as that is known;
* `current` is what to show. It shows `delayMs` after a job starts, so a shorter job never shows it, and once shown it
* stays at least `minimumMs`, so a job that ends soon after doesn't flash it either. A job that starts again while it
* is still shown keeps it shown. `set` changes `current` at once where it can (a new kind of work while shown, or the
* end after the minimum); the delay and the minimum end in `onChange`, called from a timer, never from `set`.
*/
export function runningIndicator(
onChange: () => void,
{
delayMs = runningDelayMs,
minimumMs = runningMinimumMs,
timers = realTimers,
}: {delayMs?: number; minimumMs?: number; timers?: RunningTimers} = {},
) {
let shown: Running | undefined
let latest: Running | undefined
let since = 0
// While nothing is shown, the pending show; while shown, the pending hide.
let timer: unknown
let pending = false
const cancel = () => {
if (pending) timers.clear(timer)
pending = false
}
const schedule = (ms: number, run: () => void) => {
cancel()
pending = true
timer = timers.set(() => {
pending = false
run()
onChange()
}, ms)
}
return {
get current(): Running | undefined {
return shown
},
set(running: Running | undefined) {
latest = running
if (running) {
if (shown) {
cancel()
shown = running
} else if (!pending)
schedule(delayMs, () => {
shown = latest
since = timers.now()
})
return
}
if (!shown) return cancel()
const left = since + minimumMs - timers.now()
if (left <= 0) {
cancel()
shown = undefined
} else if (!pending) schedule(left, () => (shown = undefined))
},
/** Shows nothing and forgets the job, as for another tab's. */
reset() {
cancel()
shown = latest = undefined
},
dispose() {
cancel()
},
}
}
15 changes: 11 additions & 4 deletions packages/host/core/src/ui/updating-chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import type {CSSProperties} from 'react'
import type {ProjectionController} from '../projection.js'
import {utilityInset, utilitySide} from '../stage/utility.js'
import {useWholePixels} from '../stage/whole-pixels.js'
import {useRunningIndicator} from '../use-running.js'

/** The gap between the chip and an overlay above it at the stage's top right, CSS px. */
const gap = 8

/**
* "Updating…" over the stage's top right while the host extracts or compares again with the last drawing still shown
* (`ProjectionViewProps.running`): the page's only cue then, since the stage keeps the drawing until the new one is
* (`ProjectionViewProps.running`), after the shared delay (`runningIndicator`): the page's only cue then, since the stage keeps the drawing until the new one is
* ready, and a first load says so in the stage itself. It takes the playback bar's and chips' look (`--ph-utility-*`),
* 12px from the stage's edges, below anything already floating at that corner (the playback bar, or the pane chip
* swipe and side by side put there).
Expand All @@ -25,9 +26,15 @@ export function UpdatingChip({
/** Whether the renderer's stage is the view shown (not a built-in table or text view, which keep the last result). */
rendererShown?: boolean
}) {
// A renderer pane still loading says what runs in its own status, where it is shown.
if (!running || (rendererShown && p.rendererStatus === 'loading')) return null
return <Chip reducedMotion={p.reducedMotion} />
// A renderer pane still loading says what runs in its own status, where it is shown. The indicator's latch lives
// only while the chip could show, so time hidden behind that status doesn't count toward the time it stays shown.
if (rendererShown && p.rendererStatus === 'loading') return null
return <LatchedChip running={running} reducedMotion={p.reducedMotion} />
}

/** The chip once the job has run a moment, then for at least a moment, as every host's running indicator. */
function LatchedChip({running, reducedMotion}: {running: 'extracting' | 'comparing' | null | undefined; reducedMotion: boolean}) {
return useRunningIndicator(running) ? <Chip reducedMotion={reducedMotion} /> : null
}

function Chip({reducedMotion}: {reducedMotion: boolean}) {
Expand Down
19 changes: 19 additions & 0 deletions packages/host/core/src/use-running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {useEffect, useReducer, useState} from 'react'
import {runningIndicator} from './running.js'
import type {Running} from './running.js'

/**
* The job an indicator shows (`runningIndicator`): only once it has run a moment, then for at least a moment, so short
* jobs never flash it. `undefined` while nothing shows.
*/
export function useRunningIndicator(running: Running | null | undefined): Running | undefined {
const [, changed] = useReducer((count: number) => count + 1, 0)
const [indicator] = useState(() => runningIndicator(changed))
useEffect(() => {
const before = indicator.current
indicator.set(running ?? undefined)
if (indicator.current !== before) changed()
}, [indicator, running])
useEffect(() => () => indicator.dispose(), [indicator])
return indicator.current
}
3 changes: 2 additions & 1 deletion packages/host/vscode-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ fixture.
- `runningIndicator`, `runningText` (and `runningDelayMs`, `runningMinimumMs`):
when a running job's indicator shows, after 300 ms and then for at least 400
ms, with injectable timers; no React or DOM, so an extension host imports it
from `@githubnext/projections-host-vscode/running`.
from `@githubnext/projections-host-vscode/running`. They are the host core's
(`@githubnext/projections-host/running`), re-exported.

### Building blocks

Expand Down
104 changes: 4 additions & 100 deletions packages/host/vscode-ui/src/running.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,4 @@
/*
* When a running job's indicator shows: not for a short job, which would only flash, and, once shown, long enough to
* read. The summary bar's spinner and the VS Code extension's status bar item follow the same rule. Nothing here
* imports React or the DOM, so the extension host can import it (`@githubnext/projections-host-vscode/running`).
*/

/** What a projection tab is doing: extracting a side of its pair, or comparing the two. */
export type Running = 'extracting' | 'comparing'

/** How long a job runs before its indicator shows. */
export const runningDelayMs = 300
/** How long the indicator stays once shown, however soon the job ends. */
export const runningMinimumMs = 400

/** What a job says it is doing, in an indicator's tooltip and accessible text: "Extracting…" or "Comparing…". */
export const runningText = (running: Running) => (running === 'extracting' ? 'Extracting…' : 'Comparing…')

/**
* The clock and timers `runningIndicator` uses, so a test can drive them: `now` in milliseconds, `set` schedules `run`
* after `ms` milliseconds and returns a handle that `clear` cancels. Defaults to `Date.now` and `setTimeout`.
*/
export interface RunningTimers {
now(): number
set(run: () => void, ms: number): unknown
clear(timer: unknown): void
}
const realTimers: RunningTimers = {
now: () => Date.now(),
set: (run, ms) => setTimeout(run, ms),
clear: timer => clearTimeout(timer as ReturnType<typeof setTimeout>),
}

/**
* An indicator of a running job: `set` it to what the job does (or `undefined` once it ends) as often as that is known;
* `current` is what to show. It shows `delayMs` after a job starts, so a shorter job never shows it, and once shown it
* stays at least `minimumMs`, so a job that ends soon after doesn't flash it either. A job that starts again while it
* is still shown keeps it shown. `set` changes `current` at once where it can (a new kind of work while shown, or the
* end after the minimum); the delay and the minimum end in `onChange`, called from a timer, never from `set`.
*/
export function runningIndicator(
onChange: () => void,
{
delayMs = runningDelayMs,
minimumMs = runningMinimumMs,
timers = realTimers,
}: {delayMs?: number; minimumMs?: number; timers?: RunningTimers} = {},
) {
let shown: Running | undefined
let latest: Running | undefined
let since = 0
// While nothing is shown, the pending show; while shown, the pending hide.
let timer: unknown
let pending = false
const cancel = () => {
if (pending) timers.clear(timer)
pending = false
}
const schedule = (ms: number, run: () => void) => {
cancel()
pending = true
timer = timers.set(() => {
pending = false
run()
onChange()
}, ms)
}
return {
get current(): Running | undefined {
return shown
},
set(running: Running | undefined) {
latest = running
if (running) {
if (shown) {
cancel()
shown = running
} else if (!pending)
schedule(delayMs, () => {
shown = latest
since = timers.now()
})
return
}
if (!shown) return cancel()
const left = since + minimumMs - timers.now()
if (left <= 0) {
cancel()
shown = undefined
} else if (!pending) schedule(left, () => (shown = undefined))
},
/** Shows nothing and forgets the job, as for another tab's. */
reset() {
cancel()
shown = latest = undefined
},
dispose() {
cancel()
},
}
}
// The running indicator is the host core's (`@githubnext/projections-host/running`), React- and DOM-free, so the
// extension host keeps importing it from here (`@githubnext/projections-host-vscode/running`).
export {runningDelayMs, runningIndicator, runningMinimumMs, runningText} from '@githubnext/projections-host/running'
export type {Running, RunningTimers} from '@githubnext/projections-host/running'
18 changes: 3 additions & 15 deletions packages/host/vscode-ui/src/summary-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
summaryFactTitle,
summaryToggle,
summaryToggleTitle,
useRunningIndicator,
useSummaryFit,
scopeHiddenShort,
scopeHiddenText,
Expand All @@ -19,7 +20,7 @@ import type {ProjectionController, SummaryDelta, SummaryFigure} from '@githubnex
import {uiCodicons} from './codicons.js'
import {regeneratedLabel} from './view-controls.js'
import {Overlay} from './overlay/overlay.js'
import {runningIndicator, runningText} from './running.js'
import {runningText} from './running.js'
import type {Running} from './running.js'

/**
Expand Down Expand Up @@ -154,19 +155,6 @@ function Figure({
)
}

/** A job as the bar shows it: only once it has run a moment, then for at least a moment (`runningIndicator`). */
function useShownRunning(running: Running | undefined): Running | undefined {
const [, changed] = useReducer((count: number) => count + 1, 0)
const [indicator] = useState(() => runningIndicator(changed))
useEffect(() => {
const before = indicator.current
indicator.set(running)
if (indicator.current !== before) changed()
}, [indicator, running])
useEffect(() => () => indicator.dispose(), [indicator])
return indicator.current
}

/**
* The summary bar along the bottom of every projection view, drawn like VS Code's status bar: 22px of small
* description-foreground text over the side bar's background, under a hairline. A figure per kind in the renderer's
Expand All @@ -187,7 +175,7 @@ export function SummaryBar({
running?: Running | null | undefined
className?: string
}) {
const shownRunning = useShownRunning(running ?? undefined)
const shownRunning = useRunningIndicator(running ?? undefined)
const bar = useRef<HTMLDivElement>(null)
const more = useRef<HTMLButtonElement>(null)
const [open, setOpen] = useState(false)
Expand Down
Loading