Skip to content

Commit

Permalink
explorations on opfs availability
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Jun 20, 2023
1 parent c62a993 commit c524862
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 39 deletions.
14 changes: 8 additions & 6 deletions js/packages/sandbox/src/DB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import serviceWorkerUrl from "./service-worker.js?url";

const registrationPromise = navigator.serviceWorker
const swRegistrationPromise = navigator.serviceWorker
.register(serviceWorkerUrl, {
scope: "/src/@vlcn.io_db-coordinator",
})
Expand Down Expand Up @@ -30,21 +30,23 @@ type CoordinateMsg =
};

class Connection {
#bc = new BroadcastChannel("connection-coordinate");
#bc = new BroadcastChannel("@vlcn.io/connection-coordinate");
#usingWorker: WorkerId | null = null;
#messageChannel: MessageChannel | null = null;
#swRegistration: ServiceWorkerRegistration | null = null;
#sw: ServiceWorker | null = null;

constructor(private filename: string) {
this.#bc.onmessage = this.#broadcastReceived;
}

async open() {
this.#swRegistration = await registrationPromise;
this.#swRegistration = await swRegistrationPromise;
this.#swRegistration.addEventListener;
console.log("Installing?", this.#swRegistration.installing);
console.log("Active?", this.#swRegistration.active);
console.log("Waiting?", this.#swRegistration.waiting);
this.#sw =
this.#swRegistration.installing ||
this.#swRegistration.active ||
this.#swRegistration.waiting;
// Workers will process this message,
// try to acquire the weblock for the filename,
// if successful (or already hold), report that it is available
Expand Down
3 changes: 3 additions & 0 deletions js/packages/sandbox/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import DB from "./DB";
import sharedWorkerUrl from "./shared-worker.js?url";

const db = DB.open("a-file");

console.log("start sared");
const sharedWorker = new SharedWorker(sharedWorkerUrl, { type: "module" });
/**
* Algorithm:
* 1. Each tab spawns a dedicated worker
Expand Down
70 changes: 37 additions & 33 deletions js/packages/sandbox/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,44 @@ const mapNameToProviderPort = new Map<string, MessagePort>();

onmessage = (event) => {
console.log("sw got a msg", event);
if (event.data.type === "worker_connect") {
onWorkerConnected(event.ports[0]);
}
// we will want to `event.awaitUntil` as we handle port passing.
};

function onWorkerConnected(workerPort: MessagePort) {
workerPort.onmessage = async (event) => {
if (event.ports.length) {
// Register new port provider.
const name = event.data;
const providerPort = event.ports[0];
providerPort.start();
mapNameToProviderPort.get(name)?.close();
mapNameToProviderPort.set(name, providerPort);
// function onWorkerConnected(workerPort: MessagePort) {
// workerPort.onmessage = async (event) => {
// if (event.ports.length) {
// // Register new port provider.
// const name = event.data;
// const providerPort = event.ports[0];
// providerPort.start();
// mapNameToProviderPort.get(name)?.close();
// mapNameToProviderPort.set(name, providerPort);

new BroadcastChannel("SharedService").postMessage(name);
} else {
// Handle port provider request.
const { name, lockId } = event.data;
const providerPort = mapNameToProviderPort.get(name);
if (providerPort) {
providerPort.addEventListener(
"message",
(event) => {
event.stopImmediatePropagation();
// @ts-ignore
workerPort.postMessage(null, event.ports);
},
{ once: true }
);
providerPort.postMessage(lockId);
}
}
};
}
// new BroadcastChannel("SharedService").postMessage(name);
// } else {
// // Handle port provider request.
// const { name, lockId } = event.data;
// const providerPort = mapNameToProviderPort.get(name);
// if (providerPort) {
// providerPort.addEventListener(
// "message",
// (event) => {
// event.stopImmediatePropagation();
// // @ts-ignore
// workerPort.postMessage(null, event.ports);
// },
// { once: true }
// );
// providerPort.postMessage(lockId);
// }
// }
// };
// }

// Test that we can actually pass a message port from tab to service worker to worker first.
// console.log("do we have opfs access?!");
// // Test that we can actually pass a message port from tab to service worker to worker first.
// navigator.storage.getDirectory().then((opfsRoot) => {
// console.log(opfsRoot);
// });
// // A FileSystemDirectoryHandle whose type is "directory"
// // and whose name is "".
8 changes: 8 additions & 0 deletions js/packages/sandbox/src/shared-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
console.log("in shared...");
console.log("do we have opfs access?!");
// Test that we can actually pass a message port from tab to service worker to worker first.
navigator.storage.getDirectory().then((opfsRoot) => {
console.log(opfsRoot);
});
// A FileSystemDirectoryHandle whose type is "directory"
// and whose name is "".
2 changes: 2 additions & 0 deletions js/packages/sandbox/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import workerUrl from "./service-worker.js?url";

// navigator.serviceWorker.getRegistration()

// const container = navigator.serviceWorker;
// container.onmessage = (msg) => {
// console.log("Dedicated worker got msg from SW", msg);
Expand Down

0 comments on commit c524862

Please sign in to comment.