Skip to content

Commit

Permalink
[ServiceWorker] Add debugging outputs for event traces
Browse files Browse the repository at this point in the history
  • Loading branch information
Neet-Nestor committed May 22, 2024
1 parent f228d05 commit 1d4fdcd
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/service_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export class ServiceWorkerMLCEngineHandler extends MLCEngineWorkerHandler {
>();
private initReuqestUuid?: string;

constructor(engine: MLCEngineInterface, verbose = false) {
constructor(engine: MLCEngineInterface) {
if (!self || !("addEventListener" in self)) {
throw new Error(
"ServiceWorkerGlobalScope is not defined. ServiceWorkerMLCEngineHandler must be created in service worker script.",
"ServiceWorkerGlobalScope is not defined. ServiceWorkerMLCEngineHandler must be created in service worker script."

Check failure on line 46 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
}
const postMessageHandler = {
Expand Down Expand Up @@ -79,17 +79,20 @@ export class ServiceWorkerMLCEngineHandler extends MLCEngineWorkerHandler {
message.waitUntil(
new Promise((resolve, reject) => {
onmessage(message, resolve, reject);
}),
})

Check failure on line 82 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
});
}

onmessage(
event: ExtendableMessageEvent,
onComplete?: (value: any) => void,
onError?: () => void,
onError?: () => void

Check failure on line 90 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
): void {
const msg = event.data as WorkerRequest;
console.debug(
`ServiceWorker message: [${msg.kind}] ${JSON.stringify(msg.content)}`

Check failure on line 94 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);

if (msg.kind === "keepAlive") {
const reply: WorkerRequest = {
Expand Down Expand Up @@ -135,7 +138,7 @@ export class ServiceWorkerMLCEngineHandler extends MLCEngineWorkerHandler {
await this.engine.reload(
params.modelId,
params.chatOpts,
params.appConfig,
params.appConfig

Check failure on line 141 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
this.modelId = params.modelId;
this.chatOpts = params.chatOpts;
Expand Down Expand Up @@ -185,23 +188,23 @@ export class ServiceWorker implements ChatWorker {
*/
export async function CreateServiceWorkerMLCEngine(
modelId: string,
engineConfig?: MLCEngineConfig,
engineConfig?: MLCEngineConfig

Check failure on line 191 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
): Promise<ServiceWorkerMLCEngine> {
if (!("serviceWorker" in navigator)) {
throw new Error("Service worker API is not available");
}
const registration = await (navigator.serviceWorker as ServiceWorkerContainer)
.ready;
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(
registration.active!,
registration.active!

Check warning on line 199 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

Check failure on line 199 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
serviceWorkerMLCEngine.setInitProgressCallback(
engineConfig?.initProgressCallback,
engineConfig?.initProgressCallback

Check failure on line 202 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
await serviceWorkerMLCEngine.init(
modelId,
engineConfig?.chatOpts,
engineConfig?.appConfig,
engineConfig?.appConfig

Check failure on line 207 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
return serviceWorkerMLCEngine;
}
Expand All @@ -212,7 +215,7 @@ export async function CreateServiceWorkerMLCEngine(
export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
missedHeatbeat = 0;

constructor(worker: IServiceWorker, keepAliveMs = 10000, verbose = false) {
constructor(worker: IServiceWorker, keepAliveMs = 10000) {
if (!("serviceWorker" in navigator)) {
throw new Error("Service worker API is not available");
}
Expand All @@ -223,6 +226,9 @@ export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
"message",
(event: MessageEvent) => {
const msg = event.data;
console.debug(
`MLC client message: [${msg.kind}] ${JSON.stringify(msg.content)}`

Check failure on line 230 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
try {
if (msg.kind === "heartbeat") {
this.missedHeatbeat = 0;
Expand All @@ -235,12 +241,13 @@ export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
console.error("CreateWebServiceWorkerMLCEngine.onmessage", err);
}
}
},
}
);

setInterval(() => {
this.worker.postMessage({ kind: "keepAlive", uuid: crypto.randomUUID() });
this.missedHeatbeat += 1;
console.debug("missedHeatbeat", this.missedHeatbeat);
}, keepAliveMs);
}

Expand All @@ -258,7 +265,7 @@ export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
async init(
modelId: string,
chatOpts?: ChatOptions,
appConfig?: AppConfig,
appConfig?: AppConfig
): Promise<void> {
const msg: WorkerRequest = {
kind: "init",
Expand Down

0 comments on commit 1d4fdcd

Please sign in to comment.