-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
export interface IPoorManWorkerScopeEventMap { | ||
message: MessageEvent; | ||
error: ErrorEvent; | ||
} | ||
|
||
export interface IPoorManWorkerScope { | ||
onmessage: ((this: IPoorManWorkerScope, ev: MessageEvent) => any) | null; | ||
onerror: ((this: IPoorManWorkerScope, ev: ErrorEvent) => any) | null; | ||
close(): void; | ||
postMessage(message: any, transfer?: Transferable[]): void; | ||
addEventListener<K extends keyof IPoorManWorkerScopeEventMap>(type: K, listener: (this: IPoorManWorkerScope, ev: IPoorManWorkerScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; | ||
removeEventListener<K extends keyof IPoorManWorkerScopeEventMap>(type: K, listener: (this: IPoorManWorkerScope, ev: IPoorManWorkerScopeEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; | ||
} | ||
|
||
function workerMain(self: IPoorManWorkerScope) { | ||
self.addEventListener('message', (evt) => { | ||
self.postMessage(`Worker: ${evt.data} - Polo`); | ||
}); | ||
} | ||
|
||
function toFunctionBody(f: Function) { | ||
const source = f.toString(); | ||
return source.slice(source.indexOf('{') + 1, source.lastIndexOf('}')); | ||
} | ||
|
||
export function createWorker(fs: Function[]) { | ||
const sources: string[] = fs.map(toFunctionBody); | ||
|
||
const blob = new Blob(sources, {type: 'application/javascript'}); | ||
|
||
return new Worker(URL.createObjectURL(blob)); | ||
} |