@mentra/miniapp 3.2.1-dev.314 → 3.3.0-beta.942
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/README.md +16 -0
- package/dist/background/index.d.ts +6 -3
- package/dist/background/index.d.ts.map +1 -1
- package/dist/background/index.js +1 -0
- package/dist/background/index.js.map +1 -1
- package/dist/background/register.d.ts +6 -0
- package/dist/background/register.d.ts.map +1 -1
- package/dist/background/register.js +19 -4
- package/dist/background/register.js.map +1 -1
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/modules/display.d.ts +26 -0
- package/dist/modules/display.d.ts.map +1 -1
- package/dist/modules/display.js +1 -0
- package/dist/modules/display.js.map +1 -1
- package/dist/modules/events.d.ts +8 -4
- package/dist/modules/events.d.ts.map +1 -1
- package/dist/modules/events.js +46 -6
- package/dist/modules/events.js.map +1 -1
- package/dist/modules/meeting.d.ts +129 -0
- package/dist/modules/meeting.d.ts.map +1 -1
- package/dist/modules/meeting.js +200 -14
- package/dist/modules/meeting.js.map +1 -1
- package/dist/modules/mic.d.ts +5 -4
- package/dist/modules/mic.d.ts.map +1 -1
- package/dist/modules/mic.js +5 -4
- package/dist/modules/mic.js.map +1 -1
- package/dist/modules/miniapps.d.ts +101 -4
- package/dist/modules/miniapps.d.ts.map +1 -1
- package/dist/modules/miniapps.js +59 -4
- package/dist/modules/miniapps.js.map +1 -1
- package/dist/modules/speaker.d.ts +2 -0
- package/dist/modules/speaker.d.ts.map +1 -1
- package/dist/modules/speaker.js +5 -0
- package/dist/modules/speaker.js.map +1 -1
- package/dist/modules/stream.d.ts +46 -0
- package/dist/modules/stream.d.ts.map +1 -1
- package/dist/modules/stream.js +107 -0
- package/dist/modules/stream.js.map +1 -1
- package/dist/protocol.d.ts +86 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +82 -0
- package/dist/protocol.js.map +1 -1
- package/dist/react/StreamPreview.d.ts +41 -0
- package/dist/react/StreamPreview.d.ts.map +1 -0
- package/dist/react/StreamPreview.js +144 -0
- package/dist/react/StreamPreview.js.map +1 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/stream-preview/PreviewConnection.d.ts +146 -0
- package/dist/react/stream-preview/PreviewConnection.d.ts.map +1 -0
- package/dist/react/stream-preview/PreviewConnection.js +493 -0
- package/dist/react/stream-preview/PreviewConnection.js.map +1 -0
- package/dist/react/stream-preview/controlChannel.d.ts +75 -0
- package/dist/react/stream-preview/controlChannel.d.ts.map +1 -0
- package/dist/react/stream-preview/controlChannel.js +55 -0
- package/dist/react/stream-preview/controlChannel.js.map +1 -0
- package/dist/react/stream-preview/creditLoop.d.ts +81 -0
- package/dist/react/stream-preview/creditLoop.d.ts.map +1 -0
- package/dist/react/stream-preview/creditLoop.js +146 -0
- package/dist/react/stream-preview/creditLoop.js.map +1 -0
- package/dist/react/stream-preview/protocol.d.ts +113 -0
- package/dist/react/stream-preview/protocol.d.ts.map +1 -0
- package/dist/react/stream-preview/protocol.js +148 -0
- package/dist/react/stream-preview/protocol.js.map +1 -0
- package/dist/react/stream-preview/resizeCoalescer.d.ts +38 -0
- package/dist/react/stream-preview/resizeCoalescer.d.ts.map +1 -0
- package/dist/react/stream-preview/resizeCoalescer.js +71 -0
- package/dist/react/stream-preview/resizeCoalescer.js.map +1 -0
- package/dist/react/stream-preview/testFrames.d.ts +38 -0
- package/dist/react/stream-preview/testFrames.d.ts.map +1 -0
- package/dist/react/stream-preview/testFrames.js +50 -0
- package/dist/react/stream-preview/testFrames.js.map +1 -0
- package/dist/react/stream-preview/tiers.d.ts +21 -0
- package/dist/react/stream-preview/tiers.d.ts.map +1 -0
- package/dist/react/stream-preview/tiers.js +33 -0
- package/dist/react/stream-preview/tiers.js.map +1 -0
- package/dist/react/stream-preview/trace.d.ts +23 -0
- package/dist/react/stream-preview/trace.d.ts.map +1 -0
- package/dist/react/stream-preview/trace.js +71 -0
- package/dist/react/stream-preview/trace.js.map +1 -0
- package/dist/react/stream-preview/transport.d.ts +79 -0
- package/dist/react/stream-preview/transport.d.ts.map +1 -0
- package/dist/react/stream-preview/transport.js +209 -0
- package/dist/react/stream-preview/transport.js.map +1 -0
- package/dist/react/stream-preview/yuvRenderer.d.ts +31 -0
- package/dist/react/stream-preview/yuvRenderer.d.ts.map +1 -0
- package/dist/react/stream-preview/yuvRenderer.js +398 -0
- package/dist/react/stream-preview/yuvRenderer.js.map +1 -0
- package/dist/react/useHistoryState.d.ts +35 -0
- package/dist/react/useHistoryState.d.ts.map +1 -0
- package/dist/react/useHistoryState.js +117 -0
- package/dist/react/useHistoryState.js.map +1 -0
- package/dist/session.d.ts +25 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +61 -1
- package/dist/session.js.map +1 -1
- package/dist/ui/index.d.ts +3 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +2 -0
- package/dist/ui/index.js.map +1 -1
- package/package.json +8 -3
- package/src/background/index.ts +28 -1
- package/src/background/register.ts +28 -6
- package/src/index.ts +29 -1
- package/src/modules/display.ts +29 -0
- package/src/modules/events.ts +51 -6
- package/src/modules/meeting.ts +292 -15
- package/src/modules/mic.ts +5 -4
- package/src/modules/miniapps.ts +145 -4
- package/src/modules/speaker.ts +10 -4
- package/src/modules/stream.ts +153 -1
- package/src/protocol.ts +125 -0
- package/src/react/StreamPreview.tsx +187 -0
- package/src/react/index.ts +3 -0
- package/src/react/stream-preview/PreviewConnection.ts +587 -0
- package/src/react/stream-preview/controlChannel.ts +119 -0
- package/src/react/stream-preview/creditLoop.ts +189 -0
- package/src/react/stream-preview/protocol.ts +227 -0
- package/src/react/stream-preview/resizeCoalescer.ts +88 -0
- package/src/react/stream-preview/testFrames.ts +86 -0
- package/src/react/stream-preview/tiers.ts +37 -0
- package/src/react/stream-preview/trace.ts +81 -0
- package/src/react/stream-preview/transport.ts +242 -0
- package/src/react/stream-preview/yuvRenderer.ts +453 -0
- package/src/react/useHistoryState.ts +135 -0
- package/src/session.ts +76 -0
- package/src/ui/index.ts +3 -0
package/src/background/index.ts
CHANGED
|
@@ -55,6 +55,19 @@ export type {LanguageHint, TranscriptionLanguage} from "../modules/languages"
|
|
|
55
55
|
// (the registering side lives in the background JSContext). `InvokeOptions` is
|
|
56
56
|
// for system miniapps that call other apps' actions via `session.actions.invoke`.
|
|
57
57
|
export type {ActionContext, ActionHandler, InvokeOptions} from "../modules/actions"
|
|
58
|
+
export type {
|
|
59
|
+
InstallMiniappCompatibility,
|
|
60
|
+
InstallMiniappCompatibilityRequest,
|
|
61
|
+
InstallMiniappRequest,
|
|
62
|
+
InstallMiniappResult,
|
|
63
|
+
InstallMiniappPhase,
|
|
64
|
+
InstallMiniappProgress,
|
|
65
|
+
ListMiniappsOptions,
|
|
66
|
+
MiniappActionInfo,
|
|
67
|
+
MiniappCompatibility,
|
|
68
|
+
MiniappHardwareRequirement,
|
|
69
|
+
MiniappInfo,
|
|
70
|
+
} from "../modules/miniapps"
|
|
58
71
|
|
|
59
72
|
// Session module types — useful for typing controller classes or
|
|
60
73
|
// utility helpers that take a session-like dependency.
|
|
@@ -104,6 +117,10 @@ export type {
|
|
|
104
117
|
MeetingParticipant,
|
|
105
118
|
MeetingParticipantState,
|
|
106
119
|
MeetingMediaSource,
|
|
120
|
+
MeetingStillError,
|
|
121
|
+
MeetingStillFailure,
|
|
122
|
+
MeetingStillPhase,
|
|
123
|
+
MeetingStillResult,
|
|
107
124
|
} from "../modules/meeting"
|
|
108
125
|
export {
|
|
109
126
|
MEETING_HOST_UPDATE_MESSAGE,
|
|
@@ -111,7 +128,15 @@ export {
|
|
|
111
128
|
parseMeetingParticipants,
|
|
112
129
|
validateMeetingVideoSource,
|
|
113
130
|
} from "../modules/meeting"
|
|
114
|
-
export type {
|
|
131
|
+
export type {
|
|
132
|
+
StreamModule,
|
|
133
|
+
StreamPreviewOptions,
|
|
134
|
+
PreviewHandle,
|
|
135
|
+
PreviewHandleState,
|
|
136
|
+
PreviewHandleStatus,
|
|
137
|
+
} from "../modules/stream"
|
|
138
|
+
export {PreviewError} from "../modules/stream"
|
|
139
|
+
export type {PreviewErrorCode, PreviewSource, PreviewStatus} from "../protocol"
|
|
115
140
|
export type {SystemModule} from "../modules/system"
|
|
116
141
|
export type {TranscriptionModule} from "../modules/transcription"
|
|
117
142
|
export type {TranslationModule} from "../modules/translation"
|
|
@@ -135,6 +160,8 @@ export type {
|
|
|
135
160
|
RenderRectStyle,
|
|
136
161
|
RenderResult,
|
|
137
162
|
RenderTextStyle,
|
|
163
|
+
RenderTextLayout,
|
|
164
|
+
RenderTextLine,
|
|
138
165
|
} from "../modules/display"
|
|
139
166
|
export type {DashboardMode} from "../modules/dashboard"
|
|
140
167
|
export type {PlayAudioOptions, SpeakOptions, SpeakResult, SpeakerState, SpeakerStateEvent} from "../modules/speaker"
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* are registered before any host events fan out.
|
|
15
15
|
* 3. Call `session.connect()` so the host receives CONNECT and the
|
|
16
16
|
* first CONNECT_ACK populates `userId`, `capabilities`, etc.
|
|
17
|
+
* 4. Send READY once the handler's returned promise settles. Hosts that
|
|
18
|
+
* advertise `hostFeatures.initReady` keep the UI closed until then.
|
|
17
19
|
*
|
|
18
20
|
* The handler runs once per spawn. If the host kills + respawns the
|
|
19
21
|
* JSContext (crash recovery, dev reload), the polyfill + bundle are
|
|
@@ -44,6 +46,10 @@ interface InitGlobals {
|
|
|
44
46
|
* vast majority of setup should live inside the handler so it can run
|
|
45
47
|
* with a connected session.
|
|
46
48
|
*
|
|
49
|
+
* Return the handler's startup promise (don't `void` it): the Mentra App
|
|
50
|
+
* shows its splash and holds UI requests until it settles, so register
|
|
51
|
+
* `session.ui.handle` handlers before any slow await.
|
|
52
|
+
*
|
|
47
53
|
* @example
|
|
48
54
|
* registerMiniapp((session) => {
|
|
49
55
|
* session.transcription.on((tx) => {
|
|
@@ -56,23 +62,39 @@ export function registerMiniapp<TChannels extends object = Record<string, unknow
|
|
|
56
62
|
options: MiniappSessionOptions = {},
|
|
57
63
|
): void {
|
|
58
64
|
const g = globalThis as unknown as InitGlobals
|
|
59
|
-
g.__mentraInitCallback = (
|
|
65
|
+
g.__mentraInitCallback = (sessionId: string) => {
|
|
60
66
|
const session = new MiniappSession<TChannels>(options)
|
|
67
|
+
// The handler's returned promise defines readiness: the host keeps the UI
|
|
68
|
+
// closed until it settles, so handlers registered after an await still
|
|
69
|
+
// exist before the first UI request arrives.
|
|
70
|
+
session.announceInitReady(sessionId)
|
|
71
|
+
let initSettled: Promise<void> = Promise.resolve()
|
|
61
72
|
// Fire the user handler first so any session.* subscriptions get
|
|
62
73
|
// registered before the CONNECT_ACK fan-out lands.
|
|
63
74
|
try {
|
|
64
75
|
const result = handler(session)
|
|
65
76
|
if (result && typeof (result as Promise<void>).then === "function") {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
initSettled = (result as Promise<void>).then(
|
|
78
|
+
() => undefined,
|
|
79
|
+
(err: unknown) => {
|
|
80
|
+
// eslint-disable-next-line no-console
|
|
81
|
+
console.error("[mentra-miniapp] registerMiniapp handler rejected:", err)
|
|
82
|
+
},
|
|
83
|
+
)
|
|
70
84
|
}
|
|
71
85
|
} catch (err) {
|
|
72
86
|
// eslint-disable-next-line no-console
|
|
73
87
|
console.error("[mentra-miniapp] registerMiniapp handler threw:", err)
|
|
74
88
|
}
|
|
75
|
-
session.connect()
|
|
89
|
+
const connected = session.connect()
|
|
90
|
+
// A failed handler still reports READY: its UI must not stay closed.
|
|
91
|
+
void Promise.all([connected, initSettled]).then(
|
|
92
|
+
() => session.reportInitReady(),
|
|
93
|
+
() => {
|
|
94
|
+
/* connect failure is reported below */
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
connected.catch((err: unknown) => {
|
|
76
98
|
// eslint-disable-next-line no-console
|
|
77
99
|
console.error("[mentra-miniapp] session.connect() rejected:", err)
|
|
78
100
|
// Surface to the host's crash controller as a structured uncaught
|
package/src/index.ts
CHANGED
|
@@ -74,6 +74,8 @@ export type {
|
|
|
74
74
|
RenderRectStyle,
|
|
75
75
|
RenderResult,
|
|
76
76
|
RenderTextStyle,
|
|
77
|
+
RenderTextLayout,
|
|
78
|
+
RenderTextLine,
|
|
77
79
|
ViewType,
|
|
78
80
|
} from "./modules/display"
|
|
79
81
|
export type {
|
|
@@ -129,7 +131,14 @@ export type {
|
|
|
129
131
|
StreamResult,
|
|
130
132
|
StreamStatus,
|
|
131
133
|
StreamVideoConfig,
|
|
134
|
+
StreamPreviewOptions,
|
|
135
|
+
PreviewHandle,
|
|
136
|
+
PreviewHandleState,
|
|
137
|
+
PreviewHandleStatus,
|
|
132
138
|
} from "./modules/stream"
|
|
139
|
+
export {PreviewError} from "./modules/stream"
|
|
140
|
+
export {PREVIEW_ERROR_CODES, PREVIEW_UI_CHANNEL, isPreviewErrorCode} from "./protocol"
|
|
141
|
+
export type {PreviewErrorCode, PreviewSource, PreviewStatus} from "./protocol"
|
|
133
142
|
export type {
|
|
134
143
|
ShareOptions,
|
|
135
144
|
ShareResult,
|
|
@@ -138,7 +147,19 @@ export type {
|
|
|
138
147
|
ScanQrOptions,
|
|
139
148
|
ScanQrResult,
|
|
140
149
|
} from "./modules/system"
|
|
141
|
-
export type {
|
|
150
|
+
export type {
|
|
151
|
+
MiniappInfo,
|
|
152
|
+
MiniappActionInfo,
|
|
153
|
+
MiniappCompatibility,
|
|
154
|
+
ListMiniappsOptions,
|
|
155
|
+
InstallMiniappCompatibility,
|
|
156
|
+
InstallMiniappCompatibilityRequest,
|
|
157
|
+
InstallMiniappRequest,
|
|
158
|
+
InstallMiniappResult,
|
|
159
|
+
InstallMiniappPhase,
|
|
160
|
+
InstallMiniappProgress,
|
|
161
|
+
MiniappHardwareRequirement,
|
|
162
|
+
} from "./modules/miniapps"
|
|
142
163
|
export type {ActionContext, ActionHandler, InvokeOptions} from "./modules/actions"
|
|
143
164
|
|
|
144
165
|
// Domain module types — exported so consumers can type module references
|
|
@@ -186,7 +207,10 @@ export type {SpeakerModule} from "./modules/speaker"
|
|
|
186
207
|
export type {
|
|
187
208
|
MeetingModule,
|
|
188
209
|
MeetingJoinOptions,
|
|
210
|
+
MeetingCreateOptions,
|
|
211
|
+
CreatedMeeting,
|
|
189
212
|
MeetingConfiguration,
|
|
213
|
+
MeetingIdentity,
|
|
190
214
|
MeetingIdentityMode,
|
|
191
215
|
MeetingGuestReason,
|
|
192
216
|
MeetingState,
|
|
@@ -206,6 +230,10 @@ export type {
|
|
|
206
230
|
MeetingSoftApStep,
|
|
207
231
|
MeetingSoftApStepState,
|
|
208
232
|
MeetingSoftApStepStatus,
|
|
233
|
+
MeetingStillError,
|
|
234
|
+
MeetingStillFailure,
|
|
235
|
+
MeetingStillPhase,
|
|
236
|
+
MeetingStillResult,
|
|
209
237
|
} from "./modules/meeting"
|
|
210
238
|
export {
|
|
211
239
|
MEETING_HOST_UPDATE_MESSAGE,
|
package/src/modules/display.ts
CHANGED
|
@@ -35,6 +35,12 @@ export interface RenderTextStyle {
|
|
|
35
35
|
radius?: number
|
|
36
36
|
/** What happens to text that doesn't fit the box after wrapping. Default "clip". */
|
|
37
37
|
overflow?: "clip" | "ellipsis"
|
|
38
|
+
/** Optional line limit in addition to the box's physical capacity. */
|
|
39
|
+
maxLines?: number
|
|
40
|
+
/** Which end of overflowing text stays visible. Default "start". */
|
|
41
|
+
textWindow?: "start" | "end"
|
|
42
|
+
/** Align the line region inside the box. With maxLines, reserves that many rows. Default "top". */
|
|
43
|
+
verticalAlign?: "top" | "bottom"
|
|
38
44
|
/** Line-break policy for wrapping (host wraps; the box carries pre-wrapped text). */
|
|
39
45
|
breakMode?: DisplayBreakMode
|
|
40
46
|
}
|
|
@@ -54,8 +60,29 @@ export type RenderElement =
|
|
|
54
60
|
| {type: "image"; id?: string; box: RenderBox; data: string}
|
|
55
61
|
| {type: "rect"; id?: string; box: RenderBox; style?: RenderRectStyle}
|
|
56
62
|
|
|
63
|
+
/** One visible line, with UTF-16 offsets into the element's original text (end exclusive).
|
|
64
|
+
* Inserted hyphens/ellipsis are in `text` but do not consume source characters.
|
|
65
|
+
*/
|
|
66
|
+
export interface RenderTextLine {
|
|
67
|
+
text: string
|
|
68
|
+
start: number
|
|
69
|
+
end: number
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Host-computed text output; this is not a hardware display acknowledgment. */
|
|
73
|
+
export interface RenderTextLayout {
|
|
74
|
+
lines: RenderTextLine[]
|
|
75
|
+
/** Source starts of all wrapped lines, for replacing text at line boundaries. */
|
|
76
|
+
lineStarts: number[]
|
|
77
|
+
/** Number of lines this element can show, after box and maxLines limits. */
|
|
78
|
+
capacity: number
|
|
79
|
+
truncated: boolean
|
|
80
|
+
}
|
|
81
|
+
|
|
57
82
|
export interface RenderOptions {
|
|
58
83
|
view?: ViewType
|
|
84
|
+
/** Include text output and source offsets keyed by element id (or text[index]). */
|
|
85
|
+
includeTextLayout?: boolean
|
|
59
86
|
/** Auto-clear after this many ms (same semantics as legacy display options). */
|
|
60
87
|
durationMs?: number
|
|
61
88
|
/** Update only while this app's accepted main-view frame is still current.
|
|
@@ -80,6 +107,7 @@ export interface RenderResult {
|
|
|
80
107
|
/** Ids of elements the host dropped (budget/bounds/device limits) — dropped is never silent. */
|
|
81
108
|
dropped?: string[]
|
|
82
109
|
reason?: string
|
|
110
|
+
textLayout?: Record<string, RenderTextLayout>
|
|
83
111
|
}
|
|
84
112
|
|
|
85
113
|
export class DisplayManager {
|
|
@@ -117,6 +145,7 @@ export class DisplayManager {
|
|
|
117
145
|
view: options.view ?? "main",
|
|
118
146
|
elements,
|
|
119
147
|
durationMs: options.durationMs,
|
|
148
|
+
includeTextLayout: options.includeTextLayout,
|
|
120
149
|
ifDisplayToken: options.ifDisplayToken,
|
|
121
150
|
})
|
|
122
151
|
.catch((err) => ({
|
package/src/modules/events.ts
CHANGED
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
* modules are the canonical surface.
|
|
18
18
|
*
|
|
19
19
|
* Internally, EventManager owns:
|
|
20
|
-
* 1. The ref-count map
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* 1. The ref-count map deduplicates upstream stream demand. Transcription
|
|
21
|
+
* updates also carry private listener identities, so the runtime can
|
|
22
|
+
* preserve a separate content boundary for each handler.
|
|
23
23
|
* 2. Inbound event fan-out via `_forwardEvent(streamType, data)`, called
|
|
24
24
|
* by MiniappSession.handleIncoming when an EVENT envelope arrives.
|
|
25
25
|
*
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
|
|
31
31
|
import {EventEmitter} from "eventemitter3"
|
|
32
32
|
|
|
33
|
+
import {makeRequestId} from "../envelope"
|
|
33
34
|
import {MiniappRequestType, MiniappStreamType} from "../protocol"
|
|
34
35
|
import {MiniappSession} from "../session"
|
|
35
36
|
|
|
@@ -168,6 +169,13 @@ export interface AudioChunkData {
|
|
|
168
169
|
// ---------------------------------------------------------------------------
|
|
169
170
|
|
|
170
171
|
export class EventManager {
|
|
172
|
+
private readonly listenerScope = makeRequestId()
|
|
173
|
+
private listenerSeq = 0
|
|
174
|
+
private transcriptionListeners = new Map<
|
|
175
|
+
string,
|
|
176
|
+
{stream: string; forceLocal: boolean; invoke: (data: unknown, route?: TranscriptionEventRoute) => void}
|
|
177
|
+
>()
|
|
178
|
+
private retireListeners = new Set<() => void>()
|
|
171
179
|
private readonly emitter = new EventEmitter()
|
|
172
180
|
/** Stream -> ref count. Outbound SUBSCRIBE tracks active streams and routing changes. */
|
|
173
181
|
private readonly refCounts = new Map<string, number>()
|
|
@@ -191,7 +199,11 @@ export class EventManager {
|
|
|
191
199
|
subscribe(stream: string, handler: (data: unknown) => void, options: {forceLocal?: boolean} = {}): UnsubscribeFn {
|
|
192
200
|
const isInternal = stream.startsWith("_")
|
|
193
201
|
const forceLocal = options.forceLocal === true && stream.startsWith(`${MiniappStreamType.TRANSCRIPTION}:`)
|
|
202
|
+
const isTranscription = stream.startsWith(`${MiniappStreamType.TRANSCRIPTION}:`)
|
|
203
|
+
const listenerId = isTranscription ? `${this.listenerScope}:${++this.listenerSeq}` : undefined
|
|
204
|
+
let active = true
|
|
194
205
|
const listener = (data: unknown, route?: TranscriptionEventRoute) => {
|
|
206
|
+
if (!active) return
|
|
195
207
|
if (stream.startsWith(`${MiniappStreamType.TRANSCRIPTION}:`)) {
|
|
196
208
|
// Hosts that predate routed transcription events omit `route`. Treat
|
|
197
209
|
// those events as the default/cloud path so a forceLocal listener can
|
|
@@ -201,17 +213,31 @@ export class EventManager {
|
|
|
201
213
|
handler(data)
|
|
202
214
|
}
|
|
203
215
|
this.emitter.on(stream, listener)
|
|
216
|
+
const retire = () => {
|
|
217
|
+
active = false
|
|
218
|
+
}
|
|
219
|
+
this.retireListeners.add(retire)
|
|
220
|
+
if (listenerId) this.transcriptionListeners.set(listenerId, {stream, forceLocal, invoke: listener})
|
|
204
221
|
if (!isInternal) {
|
|
205
222
|
const before = this.refCounts.get(stream) ?? 0
|
|
206
223
|
this.refCounts.set(stream, before + 1)
|
|
207
224
|
const forceLocalBefore = this.forceLocalRefCounts.get(stream) ?? 0
|
|
208
225
|
const defaultBefore = before - forceLocalBefore
|
|
209
226
|
if (forceLocal) this.forceLocalRefCounts.set(stream, forceLocalBefore + 1)
|
|
210
|
-
if (
|
|
227
|
+
if (
|
|
228
|
+
isTranscription ||
|
|
229
|
+
before === 0 ||
|
|
230
|
+
(forceLocal && forceLocalBefore === 0) ||
|
|
231
|
+
(!forceLocal && defaultBefore === 0)
|
|
232
|
+
) {
|
|
211
233
|
this.sendSubscriptionUpdate()
|
|
212
234
|
}
|
|
213
235
|
}
|
|
214
236
|
return () => {
|
|
237
|
+
if (!active) return
|
|
238
|
+
retire()
|
|
239
|
+
this.retireListeners.delete(retire)
|
|
240
|
+
if (listenerId) this.transcriptionListeners.delete(listenerId)
|
|
215
241
|
this.emitter.off(stream, listener)
|
|
216
242
|
if (isInternal) return
|
|
217
243
|
const current = this.refCounts.get(stream) ?? 0
|
|
@@ -232,12 +258,15 @@ export class EventManager {
|
|
|
232
258
|
} else if (current - forceLocalCurrent <= 1) {
|
|
233
259
|
subscriptionChanged = true
|
|
234
260
|
}
|
|
235
|
-
if (subscriptionChanged) this.sendSubscriptionUpdate()
|
|
261
|
+
if (isTranscription || subscriptionChanged) this.sendSubscriptionUpdate()
|
|
236
262
|
}
|
|
237
263
|
}
|
|
238
264
|
|
|
239
265
|
/** Unsubscribe every handler on every stream this EventManager owns. */
|
|
240
266
|
unsubscribeAll(): void {
|
|
267
|
+
for (const retire of this.retireListeners) retire()
|
|
268
|
+
this.retireListeners.clear()
|
|
269
|
+
this.transcriptionListeners.clear()
|
|
241
270
|
this.emitter.removeAllListeners()
|
|
242
271
|
this.refCounts.clear()
|
|
243
272
|
this.forceLocalRefCounts.clear()
|
|
@@ -249,7 +278,18 @@ export class EventManager {
|
|
|
249
278
|
// -------------------------------------------------------------------------
|
|
250
279
|
|
|
251
280
|
/** @internal */
|
|
252
|
-
_forwardEvent(
|
|
281
|
+
_forwardEvent(
|
|
282
|
+
stream: string,
|
|
283
|
+
data: unknown,
|
|
284
|
+
transcriptionRoute?: TranscriptionEventRoute,
|
|
285
|
+
listenerId?: string,
|
|
286
|
+
): void {
|
|
287
|
+
if (listenerId !== undefined) {
|
|
288
|
+
const listener = this.transcriptionListeners.get(listenerId)
|
|
289
|
+
if (listener && (listener.stream === stream || listener.stream === "transcription:auto"))
|
|
290
|
+
listener.invoke(data, transcriptionRoute)
|
|
291
|
+
return
|
|
292
|
+
}
|
|
253
293
|
this.emitter.emit(stream, data, transcriptionRoute)
|
|
254
294
|
|
|
255
295
|
// Wildcard fan-out: handlers register under wildcard patterns
|
|
@@ -294,6 +334,11 @@ export class EventManager {
|
|
|
294
334
|
this.session.sendOneShot({
|
|
295
335
|
type: MiniappRequestType.SUBSCRIBE,
|
|
296
336
|
subscriptions,
|
|
337
|
+
transcriptionListeners: [...this.transcriptionListeners].map(([id, {stream, forceLocal}]) => ({
|
|
338
|
+
id,
|
|
339
|
+
stream,
|
|
340
|
+
forceLocal,
|
|
341
|
+
})),
|
|
297
342
|
})
|
|
298
343
|
}
|
|
299
344
|
}
|