Skip to content

Commit

Permalink
web: fix web app not working on safari
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed May 16, 2024
1 parent 8de98d9 commit 62d677f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions apps/web/src/common/sqlite/shared-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class SharedService<T extends object> extends EventTarget {
}

activate(
portProviderFunc: () => MessagePort | Promise<MessagePort>,
portProviderFunc: () => Promise<{ port: MessagePort; onclose: () => void }>,
onClientConnected: () => Promise<void>
) {
if (this.#onDeactivate) return;
Expand All @@ -97,7 +97,7 @@ export class SharedService<T extends object> extends EventTarget {
navigator.locks
.request(LOCK_NAME, { signal: this.#onDeactivate.signal }, async () => {
// Get the port to request client ports.
const port = await portProviderFunc();
const { port, onclose } = await portProviderFunc();
port.start();

// Listen for client requests. A separate BroadcastChannel
Expand Down Expand Up @@ -159,6 +159,7 @@ export class SharedService<T extends object> extends EventTarget {
// Release the lock only on user abort or context destruction.
return new Promise((_, reject) => {
this.#onDeactivate?.signal.addEventListener("abort", () => {
onclose();
broadcastChannel.close();
reject(this.#onDeactivate?.signal.reason);
});
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/common/sqlite/sqlite.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ addEventListener("message", async (event) => {
await worker.open(event.data.dbName, event.data.async, event.data.uri);
const providerPort = createSharedServicePort(worker);
postMessage(null, [providerPort]);

self.addEventListener("beforeunload", () => worker.close());
}
});

Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/common/sqlite/wa-sqlite-kysely-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ export class WaSqliteWorkerDriver implements Driver {

service.activate(
() =>
new Promise<MessagePort>((resolve) => {
new Promise<{ port: MessagePort; onclose: () => void }>((resolve) => {
console.log("initializing worker");
this.needsInitialization = true;

const worker = new Worker();
worker.addEventListener(
"message",
(event) => resolve(event.ports[0]),
(event) =>
resolve({
port: event.ports[0],
onclose: () => worker.terminate()
}),
{ once: true }
);
worker.postMessage({
Expand Down

0 comments on commit 62d677f

Please sign in to comment.