Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ServiceWorker] Add verbose mode for debugging #410

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
13 changes: 10 additions & 3 deletions src/service_worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tvmjs from "tvmjs";
import { AppConfig, ChatOptions, MLCEngineConfig, ModelRecord } from "./config";
import { AppConfig, ChatOptions, MLCEngineConfig } from "./config";
import { ReloadParams, WorkerRequest, WorkerResponse } from "./message";
import { MLCEngineInterface, InitProgressReport } from "./types";
import {
Expand Down Expand Up @@ -40,7 +40,7 @@
>();
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.",
Expand Down Expand Up @@ -90,6 +90,9 @@
onError?: () => void,
): void {
const msg = event.data as WorkerRequest;
console.debug(
`ServiceWorker message: [${msg.kind}] ${JSON.stringify(msg.content)}`,
);

if (msg.kind === "keepAlive") {
const reply: WorkerRequest = {
Expand Down Expand Up @@ -193,7 +196,7 @@
const registration = await (navigator.serviceWorker as ServiceWorkerContainer)
.ready;
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(
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
);
serviceWorkerMLCEngine.setInitProgressCallback(
engineConfig?.initProgressCallback,
Expand All @@ -212,7 +215,7 @@
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 @@
"message",
(event: MessageEvent) => {
const msg = event.data;
console.debug(
`MLC client message: [${msg.kind}] ${JSON.stringify(msg.content)}`,
);
try {
if (msg.kind === "heartbeat") {
this.missedHeatbeat = 0;
Expand All @@ -241,6 +247,7 @@
setInterval(() => {
this.worker.postMessage({ kind: "keepAlive", uuid: crypto.randomUUID() });
this.missedHeatbeat += 1;
console.debug("missedHeatbeat", this.missedHeatbeat);
}, keepAliveMs);
}

Expand Down
Loading