Skip to content

Commit

Permalink
slight formatting change but also async on sharepolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed May 15, 2023
1 parent 6e887e3 commit e2a7b9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
14 changes: 6 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ let sharedWorker = await createSharedWorker()
function createSharedWorker(): Promise<SharedWorker> {
return new Promise((resolve) => {
let interval = setInterval(() => {
let worker = new SharedWorker(
new URL("./shared-worker.ts", import.meta.url),
{ type: "module", name: "automerge-repo-shared-worker" }
)
let worker = new SharedWorker(new URL("./shared-worker.ts", import.meta.url), {
type: "module",
name: "automerge-repo-shared-worker",
})
worker.port.onmessage = (e) => {
if (e.data === "READY") {
clearInterval(interval)
Expand All @@ -64,9 +64,7 @@ async function introduceWorkers(sharedWorker: SharedWorker) {
const channel = new MessageChannel()

reg.active.postMessage({ sharedWorkerPort: channel.port1 }, [channel.port1])
sharedWorker.port.postMessage({ serviceWorkerPort: channel.port2 }, [
channel.port2,
])
sharedWorker.port.postMessage({ serviceWorkerPort: channel.port2 }, [channel.port2])
console.log("posted to both workers")
}
introduceWorkers(sharedWorker)
Expand All @@ -79,7 +77,7 @@ function setupSharedWorkerAndRepo() {

const repo = new Repo({
network: [new MessageChannelNetworkAdapter(repoNetworkChannel.port1)],
sharePolicy: (peerId) => peerId.includes("shared-worker"),
sharePolicy: async (peerId) => peerId.includes("shared-worker"),
})

ContentTypes.setRepo(repo)
Expand Down
12 changes: 3 additions & 9 deletions src/shared-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { DocumentId, PeerId, Repo } from "automerge-repo"
import { MessageChannelNetworkAdapter } from "automerge-repo-network-messagechannel"
import { LocalForageStorageAdapter } from "automerge-repo-storage-localforage"
import { BrowserWebSocketClientAdapter } from "automerge-repo-network-websocket"
import {
BinaryDataId,
BinaryObjectDoc,
parseBinaryDataId,
} from "./blobstore/Blob"
import { BinaryDataId, BinaryObjectDoc, parseBinaryDataId } from "./blobstore/Blob"

console.log("shared-worker starting up")
import debug from "debug"
Expand Down Expand Up @@ -44,7 +40,7 @@ const repo = new Repo({
storage: new LocalForageStorageAdapter(),
network: [new BrowserWebSocketClientAdapter(url)],
peerId: ("shared-worker-" + Math.round(Math.random() * 10000)) as PeerId,
sharePolicy: (peerId) => peerId.includes("storage-server"),
sharePolicy: async (peerId) => peerId.includes("storage-server"),
})

self.addEventListener("connect", (e: MessageEvent) => {
Expand Down Expand Up @@ -81,9 +77,7 @@ function configureServiceWorkerPort(port: MessagePort) {
const { mimeType, binary } = doc
var outboundBinary = new ArrayBuffer(binary.byteLength)
new Uint8Array(outboundBinary).set(new Uint8Array(binary))
port.postMessage({ binaryDataId, mimeType, binary: outboundBinary }, [
outboundBinary,
])
port.postMessage({ binaryDataId, mimeType, binary: outboundBinary }, [outboundBinary])
})
// and what if it doesn't work?
}
Expand Down

0 comments on commit e2a7b9d

Please sign in to comment.