@lincyaw/host 1.0.0-rc.69 → 1.0.0-rc.70
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 +6 -3
- package/src/server.ts +7 -1
- package/src/step-clock.ts +19 -9
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.70",
|
|
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.70",
|
|
49
|
+
"@lincyaw/kernel": "1.0.0-rc.70"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/react": "18.3.28",
|
package/src/contracts.ts
CHANGED
|
@@ -81,6 +81,8 @@ export interface ServerClient {
|
|
|
81
81
|
readonly available: boolean
|
|
82
82
|
readonly member: Store<Member | undefined>
|
|
83
83
|
fetch(plugin: string, path: string, init?: RequestInit): Promise<Response>
|
|
84
|
+
/** A document-scoped GET resource for native media requests, using the page's existing authentication. */
|
|
85
|
+
resourceURL?(plugin: string, path: string): string
|
|
84
86
|
events(plugin: string, path: string): EventSource
|
|
85
87
|
}
|
|
86
88
|
|
|
@@ -167,10 +169,11 @@ export interface StepClockDriver {
|
|
|
167
169
|
|
|
168
170
|
export interface StepClock {
|
|
169
171
|
drive(driver: StepClockDriver): Unsubscribe
|
|
170
|
-
wait(work: Promise<unknown
|
|
171
|
-
|
|
172
|
+
wait(work: Promise<unknown>, slide?: number): void
|
|
173
|
+
/** Wait for the named slides, or every pending picture when omitted (capture/export). */
|
|
174
|
+
settled(slides?: readonly number[]): Promise<void>
|
|
172
175
|
/** Warm pictures for the given slide indices before playback reaches them. */
|
|
173
|
-
prepare?(slides: readonly number[]): Promise<void>
|
|
176
|
+
prepare?(slides: readonly number[], options?: { wait?: boolean }): Promise<void>
|
|
174
177
|
registerPreparation?(slide: number, prepare: () => void | Promise<void>): Unsubscribe
|
|
175
178
|
}
|
|
176
179
|
|
package/src/server.ts
CHANGED
|
@@ -23,10 +23,16 @@ export function createServerClient(options: { document?: string, root?: string,
|
|
|
23
23
|
const root = (options.root ?? '/').replace(/\/?$/, '/')
|
|
24
24
|
const member = createStore<Member | undefined>(undefined)
|
|
25
25
|
const url = (plugin: string, path: string) => `${root}__presmium/${plugin}/${path.replace(/^\//, '')}`
|
|
26
|
+
const resourceURL = (plugin: string, path: string) => {
|
|
27
|
+
const target = url(plugin, path)
|
|
28
|
+
// After query parsing, the server decodes this selector just like the document header.
|
|
29
|
+
return `${target}${target.includes('?') ? '&' : '?'}document=${encodeURIComponent(documentHeader(document))}`
|
|
30
|
+
}
|
|
26
31
|
const client: ServerClient = {
|
|
27
32
|
available: true, member,
|
|
33
|
+
resourceURL,
|
|
28
34
|
fetch(plugin, path, init = {}) { const headers = new Headers(init.headers); headers.set('Presmium-Document', documentHeader(document)); if (!headers.has('Accept-Language')) headers.set('Accept-Language', currentLocale()); return fetch(url(plugin, path), { ...init, headers }) },
|
|
29
|
-
events(plugin, path) {
|
|
35
|
+
events(plugin, path) { return new EventSource(resourceURL(plugin, path)) },
|
|
30
36
|
}
|
|
31
37
|
void fetch(`${root}${SERVER_PATH.slice(1)}/hello`, { headers: { 'Presmium-Document': documentHeader(document) } }).then(response => response.ok ? response.json() : undefined).then((hello: { member?: Member } | undefined) => member.set(hello?.member)).catch(() => undefined)
|
|
32
38
|
documents.set(client, document)
|
package/src/step-clock.ts
CHANGED
|
@@ -16,11 +16,12 @@ function isForward(to: NavigationPosition, from: StepChange | undefined) { retur
|
|
|
16
16
|
const nextFrame = () => new Promise<void>(done => typeof requestAnimationFrame === 'undefined' ? setTimeout(done, 0) : requestAnimationFrame(() => done()))
|
|
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
|
-
const pending = new
|
|
19
|
+
const pending = new Map<Promise<unknown>, number | undefined>()
|
|
20
20
|
const preparations = new Map<number, Set<() => void | Promise<void>>>()
|
|
21
|
-
|
|
21
|
+
const failures = new Map<number | undefined, unknown>()
|
|
22
22
|
let stopped = false
|
|
23
|
-
const
|
|
23
|
+
const matches = (slide: number | undefined, slides?: readonly number[]) => slide === undefined || slides === undefined || slides.includes(slide)
|
|
24
|
+
const rethrow = (slides?: readonly number[]) => { for (const [slide, error] of failures) if (matches(slide, slides)) { failures.delete(slide); throw error } }
|
|
24
25
|
const stop = observe(navigation.position, position => { const before = change.getSnapshot(); if (before && before.slide === position.slide && before.step === position.step) return; change.set({ slide: position.slide, step: position.step, at: clockNow(), forward: isForward(position, before) }); ensureTicking() }, true)
|
|
25
26
|
let unfollow: Unsubscribe | undefined
|
|
26
27
|
// A driver that comes while an exclusive one holds the clock waits for it, the latest one only.
|
|
@@ -37,20 +38,29 @@ export function createStepClock(navigation: Navigation): { clock: StepClock, sto
|
|
|
37
38
|
if (waiting) { const held = waiting; waiting = undefined; activate(held) } else if (last !== undefined) handOff(last)
|
|
38
39
|
}
|
|
39
40
|
},
|
|
40
|
-
wait(work) { if (!stopped) pending.
|
|
41
|
+
wait(work, slide) { if (!stopped) pending.set(work, slide); void work.catch(error => { if (!stopped && !failures.has(slide)) failures.set(slide, error) }).finally(() => pending.delete(work)) },
|
|
41
42
|
registerPreparation(slide, prepare) {
|
|
42
43
|
const callbacks = preparations.get(slide) ?? new Set()
|
|
43
44
|
preparations.set(slide, callbacks); callbacks.add(prepare)
|
|
44
45
|
return () => { callbacks.delete(prepare); if (!callbacks.size) preparations.delete(slide) }
|
|
45
46
|
},
|
|
46
|
-
async prepare(slides) {
|
|
47
|
-
|
|
47
|
+
async prepare(slides, options) {
|
|
48
|
+
for (const slide of new Set(slides)) for (const prepare of preparations.get(slide) ?? []) clock.wait(Promise.resolve().then(prepare), slide)
|
|
48
49
|
await nextFrame()
|
|
49
|
-
await clock.settled()
|
|
50
|
+
if (options?.wait !== false) await clock.settled(slides)
|
|
51
|
+
},
|
|
52
|
+
async settled(slides) {
|
|
53
|
+
const own = () => [...pending].filter(([, slide]) => matches(slide, slides)).map(([work]) => work)
|
|
54
|
+
let waited = false
|
|
55
|
+
for (let round = 0; round < 100; round++) {
|
|
56
|
+
if (stopped) return
|
|
57
|
+
if (!own().length) { if (waited) { waited = false; await nextFrame() } if (!own().length) return rethrow(slides) }
|
|
58
|
+
await Promise.allSettled(own()); waited = true
|
|
59
|
+
}
|
|
60
|
+
throw new Error('the page kept preparing its picture: settled() stopped waiting after 100 rounds')
|
|
50
61
|
},
|
|
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') },
|
|
52
62
|
}
|
|
53
|
-
return { clock, stop() { if (stopped) return; stopped = true; stop(); unfollow?.(); waiting = undefined; driver.set(undefined); change.set(undefined); pending.clear(); preparations.clear();
|
|
63
|
+
return { clock, stop() { if (stopped) return; stopped = true; stop(); unfollow?.(); waiting = undefined; driver.set(undefined); change.set(undefined); pending.clear(); preparations.clear(); failures.clear() } }
|
|
54
64
|
}
|
|
55
65
|
export interface StepTimeOptions { until?: number }
|
|
56
66
|
const settle = (time: number, until: number | undefined) => until !== undefined && time >= until ? Infinity : Math.max(0, time)
|