refactor/channel & ipc (#349)
Co-authored-by: paisley <8197966+su8su@users.noreply.github.com> Co-authored-by: zuolingxuan <zuolingxuan@bytedance.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
8b45960662
commit
e28eba01e1
@@ -2,6 +2,21 @@ import { createHostEventSource } from './host-api';
|
||||
|
||||
let eventSource: EventSource | null = null;
|
||||
|
||||
const HOST_EVENT_TO_IPC_CHANNEL: Record<string, string> = {
|
||||
'gateway:status': 'gateway:status-changed',
|
||||
'gateway:error': 'gateway:error',
|
||||
'gateway:notification': 'gateway:notification',
|
||||
'gateway:chat-message': 'gateway:chat-message',
|
||||
'gateway:channel-status': 'gateway:channel-status',
|
||||
'gateway:exit': 'gateway:exit',
|
||||
'oauth:code': 'oauth:code',
|
||||
'oauth:success': 'oauth:success',
|
||||
'oauth:error': 'oauth:error',
|
||||
'channel:whatsapp-qr': 'channel:whatsapp-qr',
|
||||
'channel:whatsapp-success': 'channel:whatsapp-success',
|
||||
'channel:whatsapp-error': 'channel:whatsapp-error',
|
||||
};
|
||||
|
||||
function getEventSource(): EventSource {
|
||||
if (!eventSource) {
|
||||
eventSource = createHostEventSource();
|
||||
@@ -9,10 +24,35 @@ function getEventSource(): EventSource {
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
function allowSseFallback(): boolean {
|
||||
try {
|
||||
return window.localStorage.getItem('clawx:allow-sse-fallback') === '1';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribeHostEvent<T = unknown>(
|
||||
eventName: string,
|
||||
handler: (payload: T) => void,
|
||||
): () => void {
|
||||
const ipc = window.electron?.ipcRenderer;
|
||||
const ipcChannel = HOST_EVENT_TO_IPC_CHANNEL[eventName];
|
||||
if (ipcChannel && ipc?.on && ipc?.off) {
|
||||
const listener = (payload: unknown) => {
|
||||
handler(payload as T);
|
||||
};
|
||||
ipc.on(ipcChannel, listener);
|
||||
return () => {
|
||||
ipc.off(ipcChannel, listener);
|
||||
};
|
||||
}
|
||||
|
||||
if (!allowSseFallback()) {
|
||||
console.warn(`[host-events] no IPC mapping for event "${eventName}", SSE fallback disabled`);
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const source = getEventSource();
|
||||
const listener = (event: Event) => {
|
||||
const payload = JSON.parse((event as MessageEvent).data) as T;
|
||||
|
||||
Reference in New Issue
Block a user