haochi / angular2-web-worker

Web worker for Angular 2
https://www.npmjs.com/package/angular2-web-worker
51 stars 10 forks source link
angular2 typescript web-worker

What is this?

Web worker service for Angular 2.

Install

npm i angular2-web-worker

API

export interface IWebWorkerService {
    run<T>(workerFunction: (any) => T, data?: any): Promise<T>;
    runUrl(url: string, data?: any): Promise<any>;
    terminate<T>(promise: Promise<T>): Promise<T>;
}

Example

Check out angular2-web-worker-example for a sample project, or see app/app.component.ts for usage with an Angular 2 application.

export class AppComponent implements OnInit {
    constructor(private _webWorkerService: WebWorkerService) {
    }

    ngOnInit() {
        const input = 100;
        const promise = this._webWorkerService.run(this.someCPUHeavyFunction, input);
        promise.then(result => console.log(result));
    }

    someCPUHeavyFunction (input) {
        return input * 10;
    }
}