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] Refine error message and fix lint warning #418

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
1 change: 1 addition & 0 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type RequestKind =
| "keepAlive"
| "heartbeat";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ResponseKind =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this is not used right now, it helps reader understand the event types so we should keep it.

| "return"
| "throw"
Expand Down
20 changes: 14 additions & 6 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";

Check warning on line 2 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

'ModelRecord' is defined but never used
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) {

Check warning on line 43 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

'verbose' is assigned a value but never used
if (!self || !("addEventListener" in self)) {
throw new Error(
"ServiceWorkerGlobalScope is not defined. ServiceWorkerMLCEngineHandler must be created in service worker script.",
Expand Down Expand Up @@ -188,13 +188,21 @@
engineConfig?: MLCEngineConfig,
): Promise<ServiceWorkerMLCEngine> {
if (!("serviceWorker" in navigator)) {
throw new Error("Service worker API is not available");
throw new Error(
"Service worker API is not available in your browser. Please ensure that your browser supports service workers and that you are using a secure context (HTTPS). " +
"Check the browser compatibility and ensure that service workers are not disabled in your browser settings.",
);
}
const registration = await (navigator.serviceWorker as ServiceWorkerContainer)
.ready;
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(
registration.active!,
);
const serviceWorkerAPI = navigator.serviceWorker as ServiceWorkerContainer;
const registration = await serviceWorkerAPI.ready;
const serviceWorker = registration.active || serviceWorkerAPI.controller;
if (!serviceWorker) {
throw new Error(
"Service worker failed to initialize. This could be due to a failure in the service worker registration process or because the service worker is not active. " +
"Please refresh the page to retry initializing the service worker.",
);
}
const serviceWorkerMLCEngine = new ServiceWorkerMLCEngine(serviceWorker);
serviceWorkerMLCEngine.setInitProgressCallback(
engineConfig?.initProgressCallback,
);
Expand All @@ -212,7 +220,7 @@
export class ServiceWorkerMLCEngine extends WebWorkerMLCEngine {
missedHeatbeat = 0;

constructor(worker: IServiceWorker, keepAliveMs = 10000, verbose = false) {

Check warning on line 223 in src/service_worker.ts

View workflow job for this annotation

GitHub Actions / lint

'verbose' is assigned a value but never used
if (!("serviceWorker" in navigator)) {
throw new Error("Service worker API is not available");
}
Expand Down
Loading