Created
May 28, 2019 07:26
-
-
Save Rwin90/3a27ddf4064cd3876011eacffd9fd0dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Injectable } from '@angular/core'; | |
import {Subject} from 'rxjs/Rx'; | |
import {PlatformLocation} from '@angular/common'; | |
@Injectable() | |
export class WebWorkerService { | |
worker; | |
workerResult: {[key: string]: Subject<any> } = {} ; | |
workerUrl = window.location.origin +this.platformLocation.getBaseHrefFromDOM() + 'assets/scripts/worker.js'; | |
constructor(private platformLocation: PlatformLocation) { | |
this.workerInit(this.workerUrl); | |
this.onmessage(); | |
} | |
newWidgetResult(widgetNumber) { | |
this.workerResult[widgetNumber] = new Subject<any>(); | |
} | |
workerInit(fileUrl: string): void { | |
this.worker = new Worker(fileUrl); | |
} | |
postMessage(data: any): void { | |
this.worker.postMessage(data); | |
} | |
onmessage(): void { | |
const that = this; | |
this.worker.onmessage = function (data: any) { | |
that.workerResult[data.data.widget].next(data.data.data) | |
}; | |
} | |
terminate(): void { | |
this.worker.terminate(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment