Skip to content

Commit

Permalink
provideWorkerPort
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Mar 20, 2024
1 parent 5045ac6 commit cc372bf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/demo-app-worker/src/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Chat() {
worker.current = new Worker(new URL('./worker.ts', import.meta.url), {
type: 'module',
});
aiMaskClient.sendWorkerPort(worker.current)
aiMaskClient.provideWorkerPort(worker.current)
setLoading(false);
}
}, []);
Expand Down
1 change: 0 additions & 1 deletion examples/demo-app-worker/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
base: '/ai-mask/',
})
6 changes: 6 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ai-mask/sdk

## 0.7.2

### Patch Changes

- passive port providing for workers

## 0.7.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai-mask/sdk",
"version": "0.7.1",
"version": "0.7.2",
"repository": {
"type": "git",
"url": "git+https://github.com/pacoccino/ai-mask"
Expand Down
35 changes: 21 additions & 14 deletions packages/sdk/src/AIMaskClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,34 +115,41 @@ export class AIMaskClient {
}

// TODO: only one worker can be used with AIMask atm
sendWorkerPort(worker: Worker) {
const { port1: mainPort, port2: workerPort } = new MessageChannel();
provideWorkerPort(worker: Worker) {
worker.addEventListener('message', ({ data }) => {
if (data._AIMaskWorkerPortRequest) {
const { port1: mainPort, port2: workerPort } = new MessageChannel();

this.messager.setPassthroughListener((message) => {
mainPort.postMessage(message)
})

mainPort.onmessage = (event) => {
const message = event.data
this.messager.passthroughRequest(message)
}

this.messager.setPassthroughListener((message) => {
mainPort.postMessage(message)
worker.postMessage({ _AIMaskWorkerPort: workerPort }, [workerPort]);
}
})

mainPort.onmessage = (event) => {
const message = event.data
this.messager.passthroughRequest(message)
}

worker.postMessage({ _AIMaskWorkerPort: workerPort }, [workerPort]);
}

static async getWorkerClient() {
return new Promise<AIMaskClient>((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('[AIMaskWorkerClient] Did not receive worker port, did you send from main thread with aiMask.sendWorkerPort(worker) ?'))
}, 2000)
self.addEventListener("message", async ({ data }) => {
const listener = ({ data }: any) => {
if (data._AIMaskWorkerPort) {
clearTimeout(timeout)
self.removeEventListener("message", listener)
clearInterval(timeout)
const fakePort = createFakePort(data._AIMaskWorkerPort)
const aiMaskClient = new AIMaskClient({ port: fakePort })
resolve(aiMaskClient)
}
});
}
self.addEventListener("message", listener)
self.postMessage({ _AIMaskWorkerPortRequest: true })
})
}
}

0 comments on commit cc372bf

Please sign in to comment.