@lincyaw/host 1.0.0-rc.125 → 1.0.0-rc.127
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/index.ts +1 -0
- package/src/window-control.ts +50 -0
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.127",
|
|
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/design": "1.0.0-rc.
|
|
49
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
48
|
+
"@lincyaw/design": "1.0.0-rc.127",
|
|
49
|
+
"@lincyaw/kernel": "1.0.0-rc.127"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/react": "18.3.28",
|
package/src/index.ts
CHANGED
|
@@ -76,6 +76,7 @@ export { PmToolbar } from './panels.ts'
|
|
|
76
76
|
export { NAVIGATION, OPERATIONS, PAGE_READINESS, PLAYBACK_CONTROL, PRODUCTION_PLAYBACK, STATUS, STEP_CLOCK, SYNC_TRANSPORTS, TIMED_CAPTIONS, TOOLBAR } from './contracts.ts'
|
|
77
77
|
export type { PageReadiness, TimedCaptions } from './contracts.ts'
|
|
78
78
|
export { joinWindows } from './windows.ts'
|
|
79
|
+
export { useAudienceControl, useAudienceRemote } from './window-control.ts'
|
|
79
80
|
export { setSiteBase, siteBase } from './site.ts'
|
|
80
81
|
export type { WindowChannel } from './windows.ts'
|
|
81
82
|
export type {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
2
|
+
import { joinWindows } from './windows.ts'
|
|
3
|
+
import type { WindowChannel } from './windows.ts'
|
|
4
|
+
|
|
5
|
+
type Message = { state: unknown } | { command: string }
|
|
6
|
+
|
|
7
|
+
const isMessage = (value: unknown): value is Message => typeof value === 'object' && value !== null && ('state' in value || typeof (value as { command?: unknown }).command === 'string')
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A control of the audience's window that another window of the same presentation drives (the presenter view): the
|
|
11
|
+
* audience tells the control's state (`undefined` while it offers none) through the windows' channel and runs the
|
|
12
|
+
* commands it receives. A window that joins later is told the current state.
|
|
13
|
+
*/
|
|
14
|
+
export function useAudienceControl<S>(name: string, state: S | undefined, run: (command: string) => void): void {
|
|
15
|
+
const latest = useRef({ state, run })
|
|
16
|
+
latest.current = { state, run }
|
|
17
|
+
const channel = useRef<WindowChannel | undefined>(undefined)
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const windows = joinWindows(name, value => {
|
|
20
|
+
if (isMessage(value) && 'command' in value && latest.current.state !== undefined) latest.current.run(value.command)
|
|
21
|
+
}, () => ({ state: latest.current.state ?? null }))
|
|
22
|
+
channel.current = windows
|
|
23
|
+
return () => {
|
|
24
|
+
windows?.send({ state: null })
|
|
25
|
+
windows?.close()
|
|
26
|
+
channel.current = undefined
|
|
27
|
+
}
|
|
28
|
+
}, [name])
|
|
29
|
+
const shown = JSON.stringify(state ?? null)
|
|
30
|
+
useEffect(() => { channel.current?.send({ state: state ?? null }) }, [name, shown])
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The other side of `useAudienceControl`: the state the audience tells, and the way to send it a command. */
|
|
34
|
+
export function useAudienceRemote<S>(name: string): [S | undefined, (command: string) => void] {
|
|
35
|
+
const [state, setState] = useState<S | undefined>(undefined)
|
|
36
|
+
const channel = useRef<WindowChannel | undefined>(undefined)
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const windows = joinWindows(name, value => {
|
|
39
|
+
if (isMessage(value) && 'state' in value) setState((value.state ?? undefined) as S | undefined)
|
|
40
|
+
}, () => undefined)
|
|
41
|
+
channel.current = windows
|
|
42
|
+
return () => {
|
|
43
|
+
windows?.close()
|
|
44
|
+
channel.current = undefined
|
|
45
|
+
setState(undefined)
|
|
46
|
+
}
|
|
47
|
+
}, [name])
|
|
48
|
+
const send = useCallback((command: string) => { channel.current?.send({ command }) }, [])
|
|
49
|
+
return [state, send]
|
|
50
|
+
}
|