Skip to content

Instantly share code, notes, and snippets.

@Sumechoo
Created October 13, 2020 10:48
Show Gist options
  • Save Sumechoo/bb0a19e95985df9398a3cadf3517afbd to your computer and use it in GitHub Desktop.
Save Sumechoo/bb0a19e95985df9398a3cadf3517afbd to your computer and use it in GitHub Desktop.
export class Thread<RT> {
private _worker: Worker;
private _listeners: (data: RT[]) => void;
constructor(lambda: (this: Worker) => void) {
const textContent = lambda.toString().replace(/^function .+\{?|\}$/g, "");
const blob = new Blob([textContent], { type: "application/java-script" });
this._worker = new Worker(URL.createObjectURL(blob));
this._worker.addEventListener("message", this._loger);
}
private _loger = (e: MessageEvent) => {
console.info("Threaded message: ", e.data);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment