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:
Lingxuan Zuo
2026-03-09 19:04:00 +08:00
committed by GitHub
Unverified
parent 8b45960662
commit e28eba01e1
47 changed files with 4160 additions and 543 deletions

View File

@@ -1,6 +1,17 @@
type TelemetryPayload = Record<string, unknown>;
export type UiTelemetryEntry = {
id: number;
event: string;
payload: TelemetryPayload;
count: number;
ts: string;
};
const counters = new Map<string, number>();
const history: UiTelemetryEntry[] = [];
const listeners = new Set<(entry: UiTelemetryEntry) => void>();
let nextEntryId = 1;
const MAX_HISTORY = 500;
function safeStringify(payload: TelemetryPayload): string {
try {
@@ -14,10 +25,29 @@ export function trackUiEvent(event: string, payload: TelemetryPayload = {}): voi
const count = (counters.get(event) ?? 0) + 1;
counters.set(event, count);
const logPayload = {
const normalizedPayload = {
...payload,
};
const ts = new Date().toISOString();
const entry: UiTelemetryEntry = {
id: nextEntryId,
event,
payload: normalizedPayload,
count,
ts: new Date().toISOString(),
ts,
};
nextEntryId += 1;
history.push(entry);
if (history.length > MAX_HISTORY) {
history.splice(0, history.length - MAX_HISTORY);
}
listeners.forEach((listener) => listener(entry));
const logPayload = {
...normalizedPayload,
count,
ts,
};
// Local-only telemetry for UX diagnostics.
@@ -27,3 +57,54 @@ export function trackUiEvent(event: string, payload: TelemetryPayload = {}): voi
export function getUiCounter(event: string): number {
return counters.get(event) ?? 0;
}
export function trackUiTiming(
event: string,
durationMs: number,
payload: TelemetryPayload = {},
): void {
trackUiEvent(event, {
...payload,
durationMs: Math.round(durationMs),
});
}
export function startUiTiming(
event: string,
payload: TelemetryPayload = {},
): (nextPayload?: TelemetryPayload) => number {
const start = typeof performance !== 'undefined' && typeof performance.now === 'function'
? performance.now()
: Date.now();
return (nextPayload: TelemetryPayload = {}): number => {
const end = typeof performance !== 'undefined' && typeof performance.now === 'function'
? performance.now()
: Date.now();
const durationMs = Math.max(0, end - start);
trackUiTiming(event, durationMs, { ...payload, ...nextPayload });
return durationMs;
};
}
export function getUiTelemetrySnapshot(limit = 200): UiTelemetryEntry[] {
if (!Number.isFinite(limit) || limit <= 0) {
return [];
}
if (limit >= history.length) {
return [...history];
}
return history.slice(-limit);
}
export function clearUiTelemetry(): void {
counters.clear();
history.length = 0;
}
export function subscribeUiTelemetry(listener: (entry: UiTelemetryEntry) => void): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}