microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fedimser/permutation

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/npm/qsharp/src/workers/adapters/browser.ts

37lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4import type { IWorkerHost } from "./types.js";
5
6export class BrowserWorkerHost implements IWorkerHost {
7 private worker: Worker;
8
9 constructor(url: string | URL) {
10 const scriptUrl = typeof url === "string" ? url : url.href;
11 const bootstrap = `
12 self.WorkerSelf = {
13 postMessage(msg) { self.postMessage(msg); },
14 onMessage(handler) { self.addEventListener("message", handler); }
15 };
16 importScripts("${scriptUrl}");
17 `;
18 const blob = new Blob([bootstrap], { type: "application/javascript" });
19 this.worker = new Worker(URL.createObjectURL(blob));
20 }
21
22 postMessage(msg: unknown): void {
23 this.worker.postMessage(msg);
24 }
25
26 onMessage(handler: (e: MessageEvent) => void): void {
27 this.worker.onmessage = handler;
28 }
29
30 onError(handler: (e: Event) => void): void {
31 this.worker.onerror = handler;
32 }
33
34 terminate(): void {
35 this.worker.terminate();
36 }
37}
38