Created
October 13, 2020 10:48
-
-
Save Sumechoo/bb0a19e95985df9398a3cadf3517afbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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