@lincyaw/host 1.0.0-rc.39 → 1.0.0-rc.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/contracts.ts +3 -0
- package/src/step-clock.ts +12 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/host",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.40",
|
|
4
4
|
"description": "Presmium's host interface for plugin components in every form: step state, services, interface text, and the design system (tokens, components, fonts)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"@radix-ui/react-tooltip": "^1.2.8",
|
|
46
46
|
"react": "18.3.1",
|
|
47
47
|
"react-dom": "18.3.1",
|
|
48
|
-
"@lincyaw/
|
|
49
|
-
"@lincyaw/
|
|
48
|
+
"@lincyaw/design": "1.0.0-rc.40",
|
|
49
|
+
"@lincyaw/kernel": "1.0.0-rc.40"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/react": "18.3.28",
|
package/src/contracts.ts
CHANGED
|
@@ -169,6 +169,9 @@ export interface StepClock {
|
|
|
169
169
|
drive(driver: StepClockDriver): Unsubscribe
|
|
170
170
|
wait(work: Promise<unknown>): void
|
|
171
171
|
settled(): Promise<void>
|
|
172
|
+
/** Warm pictures for the given slide indices before playback reaches them. */
|
|
173
|
+
prepare?(slides: readonly number[]): Promise<void>
|
|
174
|
+
registerPreparation?(slide: number, prepare: () => void | Promise<void>): Unsubscribe
|
|
172
175
|
}
|
|
173
176
|
|
|
174
177
|
export const STEP_CLOCK = defineService<StepClock>('presmium.stepClock')
|
package/src/step-clock.ts
CHANGED
|
@@ -17,6 +17,7 @@ const nextFrame = () => new Promise<void>(done => typeof requestAnimationFrame =
|
|
|
17
17
|
function handOff(elapsed: number) { const at = change.getSnapshot(); if (at) change.set({ ...at, at: clockNow() - elapsed, forward: true }); ensureTicking() }
|
|
18
18
|
export function createStepClock(navigation: Navigation): { clock: StepClock, stop: Unsubscribe } {
|
|
19
19
|
const pending = new Set<Promise<unknown>>()
|
|
20
|
+
const preparations = new Map<number, Set<() => void | Promise<void>>>()
|
|
20
21
|
let failure: { error: unknown } | undefined
|
|
21
22
|
let stopped = false
|
|
22
23
|
const rethrow = () => { const failed = failure; failure = undefined; if (failed) throw failed.error }
|
|
@@ -37,9 +38,19 @@ export function createStepClock(navigation: Navigation): { clock: StepClock, sto
|
|
|
37
38
|
}
|
|
38
39
|
},
|
|
39
40
|
wait(work) { if (!stopped) pending.add(work); void work.catch(error => { if (!stopped) failure ??= { error } }).finally(() => pending.delete(work)) },
|
|
41
|
+
registerPreparation(slide, prepare) {
|
|
42
|
+
const callbacks = preparations.get(slide) ?? new Set()
|
|
43
|
+
preparations.set(slide, callbacks); callbacks.add(prepare)
|
|
44
|
+
return () => { callbacks.delete(prepare); if (!callbacks.size) preparations.delete(slide) }
|
|
45
|
+
},
|
|
46
|
+
async prepare(slides) {
|
|
47
|
+
await Promise.all([...new Set(slides)].flatMap(slide => [...preparations.get(slide) ?? []].map(prepare => prepare())))
|
|
48
|
+
await nextFrame()
|
|
49
|
+
await clock.settled()
|
|
50
|
+
},
|
|
40
51
|
async settled() { let waited = false; for (let round = 0; round < 100; round++) { if (stopped) return; if (!pending.size) { if (waited) { waited = false; await nextFrame() } if (!pending.size) return rethrow() } await Promise.allSettled([...pending]); waited = true } throw new Error('the page kept preparing its picture: settled() stopped waiting after 100 rounds') },
|
|
41
52
|
}
|
|
42
|
-
return { clock, stop() { if (stopped) return; stopped = true; stop(); unfollow?.(); waiting = undefined; driver.set(undefined); change.set(undefined); pending.clear(); failure = undefined } }
|
|
53
|
+
return { clock, stop() { if (stopped) return; stopped = true; stop(); unfollow?.(); waiting = undefined; driver.set(undefined); change.set(undefined); pending.clear(); preparations.clear(); failure = undefined } }
|
|
43
54
|
}
|
|
44
55
|
export interface StepTimeOptions { until?: number }
|
|
45
56
|
const settle = (time: number, until: number | undefined) => until !== undefined && time >= until ? Infinity : Math.max(0, time)
|