Giter Site home page Giter Site logo

grapemix / angular2-web-worker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haochi/angular2-web-worker

0.0 3.0 0.0 14 KB

Web worker for Angular 2

Home Page: https://www.npmjs.com/package/angular2-web-worker

TypeScript 90.06% JavaScript 0.60% HTML 9.34%

angular2-web-worker's Introduction

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>;
}
  • run
    • workerFunction:
      • Must be a self-contained function. Cannot reference outside variables.
      • You can import other libraries with importScripts though
      • These are okay:
      • run(input => input * input, 10);
        
        run(input => {
            const square = num => num * num;
            return square(input);
        }, 10);
        
        const someFunction = (input) => input * input);
        run(someFunction, 10);
        
        class Runner {
            private webWorkerService = new WebWorkerService();
            constructor() {
                this.webWorkerService.run(this.someFunction, 10);
            }
            someFunction() {
                return input * input;
            }
        }
      • These will probably NOT work:
      • // this is not okay because inside the context of the web worker `this` is not the same `this` as here.
        run(input => this.square(input), 10); 
        
        // this is not okay because `_` doesn't exist in the web worker context (assuming tht `_` is available here to begin with)
        run(input => {
            return _.uniqueId() * input;
        }, 10);
    • data: serializable data
  • runUrl: Basically the same as
    • url: The url you would use to create a Worker instance
    • data: Same as the run method
  • terminate: Calling this will terminate the web worker, if it is still running.
    • promise: The Promise instance returned by run or runUrl.

Example

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;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.