Repository navigation
Expand file tree
/
Copy pathport.ts
More file actions
155 lines (132 loc) · 3.72 KB
/
Copy pathport.ts
File metadata and controls
155 lines (132 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { browser } from '#imports';
import type { CaptureStateValue } from '@/core/capture/machine';
import { logger } from '@/lib/logger';
import type { VoiceErrorReason } from '@/lib/voice-messages';
const PORT_NAME = 'mimik-panel';
const OBSERVER_PORT_NAME = 'mimik-panel-observer';
export interface PanelStateUpdate {
type: 'STATE_UPDATE';
state: CaptureStateValue;
stepCount: number;
currentGuideId: string | null;
}
export type VoicePhase = 'idle' | 'recording' | 'transcribing' | 'error';
export interface PanelVoiceUpdate {
type: 'VOICE_UPDATE';
phase: VoicePhase;
reason?: VoiceErrorReason;
error?: string;
narrated?: number;
}
type PortMessage = PanelStateUpdate | PanelVoiceUpdate;
type Port = ReturnType<typeof browser.runtime.connect>;
function openPort(
name: string,
onPortMessage: (msg: PortMessage) => void,
onConnect?: () => void,
onDisconnect?: () => void,
): () => void {
let port: Port | null = null;
let destroyed = false;
function connect() {
if (destroyed) return;
try {
port = browser.runtime.connect({ name });
logger.debug('Port connected to background', name);
onConnect?.();
port.onMessage.addListener(onPortMessage);
port.onDisconnect.addListener(() => {
port = null;
if (!destroyed) {
logger.debug('Port disconnected, reconnecting in 1s...');
onDisconnect?.();
setTimeout(connect, 1000);
}
});
} catch {
if (!destroyed) {
logger.debug('Port connect failed, retrying in 1s...');
setTimeout(connect, 1000);
}
}
}
connect();
return () => {
destroyed = true;
port?.disconnect();
port = null;
};
}
export function connectToBackground(callbacks: {
onStateUpdate: (update: PanelStateUpdate) => void;
onConnect: () => void;
onDisconnect: () => void;
onVoiceUpdate?: (update: PanelVoiceUpdate) => void;
}): () => void {
return openPort(
PORT_NAME,
(msg) => {
if (msg.type === 'STATE_UPDATE') {
callbacks.onStateUpdate(msg);
} else if (msg.type === 'VOICE_UPDATE') {
callbacks.onVoiceUpdate?.(msg);
}
},
callbacks.onConnect,
callbacks.onDisconnect,
);
}
export function observeVoiceFromBackground(
onVoiceUpdate: (update: PanelVoiceUpdate) => void,
onConnect?: () => void,
): () => void {
return openPort(
OBSERVER_PORT_NAME,
(msg) => {
if (msg.type === 'VOICE_UPDATE') onVoiceUpdate(msg);
},
onConnect,
);
}
const panelPorts = new Set<Port>();
const observerPorts = new Set<Port>();
let lastVoiceUpdate: PanelVoiceUpdate | null = null;
export function setupPortListener(onPanelConnect?: (port: Port) => void) {
browser.runtime.onConnect.addListener((port) => {
if (port.name === OBSERVER_PORT_NAME) {
observerPorts.add(port);
if (lastVoiceUpdate) postTo(port, lastVoiceUpdate);
port.onDisconnect.addListener(() => {
observerPorts.delete(port);
});
return;
}
if (port.name !== PORT_NAME) return;
panelPorts.add(port);
onPanelConnect?.(port);
port.onDisconnect.addListener(() => {
panelPorts.delete(port);
});
});
}
function postTo(port: Port, message: PortMessage): boolean {
try {
port.postMessage(message);
return true;
} catch {
return false;
}
}
function broadcastTo(ports: Set<Port>, message: PortMessage): void {
for (const port of ports) {
if (!postTo(port, message)) ports.delete(port);
}
}
export function broadcastStateToPanel(update: PanelStateUpdate): void {
broadcastTo(panelPorts, update);
}
export function broadcastVoiceToPanel(update: PanelVoiceUpdate): void {
lastVoiceUpdate = update;
broadcastTo(panelPorts, update);
broadcastTo(observerPorts, update);
}