Code Monkey home page Code Monkey logo

web-workers's Introduction

Web Workers

Latest version Publish status Package size Downloads statistic Commit activity Licence

My inspiration how we can use React, Web Workers, and AI frontend technologies

Features

  1. Smooth UI when computing something heavy.
  2. Predefined React hooks to create a background task.
  3. Event-based replies from a task function.
  4. Possibility to use promise-based and generator task functions.

Limitations inside task function

  1. You cannot use the outer scope because task function is isolated and is run in another thread, but you can pass arguments to task function.
  2. You cannot use DOM manipulations.
  3. You cannot use recursion inside passed function as argument for a task function. All passed functions become anonymous functions (do not have a name) that's why a function cannot call itself. You can declare a recursive function inside task function or import it via dependencies.

Installation

  1. Install npm package

    npm i @tynik/web-workers
  2. File worker.worker.js is required by a worker and is fetched when the worker is initiated. You should copy worker.worker.js file from @tynik/web-workers package before you will start your own project. If you use Webpack you can install copy-webpack-plugin package and will add the next configuration to Webpack config file.

    // webpack.config.js
    
    const CopyWebpackPlugin = require('copy-webpack-plugin');
    
    module.exports = {
      // webpack settings...
      plugins: [
        new CopyWebpackPlugin({
          patterns: [
            'node_modules/@tynik/web-workers/dist/worker.worker.js'
          ]
        })
      ]
    }

Examples

All examples source directory here.

Also, you can see deployed examples here.

React

  1. Pure. Implemented with using base Task class.
  2. Base.
  3. Tasks queue. Calling the heavy task with different arguments at the same time.
  4. Promise-based result.
  5. Simple generator.
  6. Infinity generator. Task function as infinity generator that automatically stopped after some time.
  7. Files processing.
  8. Brain.js XOR.

Notes

  1. High resolution time metrics. Please, see reduced time precision in Firefox.

API

Task class

  1. constructor(func: TaskFunction, { deps?: string[] }) - Initiating a task with creating a new Worker. The task function currently is not run.
  2. run(...args: Params): RunTaskAPI - Transfer and run a task function inside created Worker. If you call run many times simultaneously and the previous task function was not completed then a new call will be pushed to the queue and be run when the previous task is finished.
  3. stop(): void - Stop the task function executing. Also, the tasks queue will be erased.

RunTaskAPI

The RunTaskAPI interface it is the result of executing run() method.

  1. whenSent: Promise<Meta> - Property to subscribe on sent event.
  2. whenStarted: Promise<Meta> - Property to subscribe on started event.
  3. whenCompleted: Promise<{ result: Result } & Meta> - Property to subscribe on completed event.
  4. whenError: Promise<{ result: string } & Meta> - Property to subscribe on error event.
  5. next(...args: Params): Promise<{ result: Result } & Meta> - Run the next iteration for a task generator function with transferring arguments.
  6. return(value?: any): void - Stop a task generator function with passing value as a result if needed. In common can be used to stop infinity generators.
  7. throw(e?: any): void - Throw an error inside a task generator function. The argument e can accept only cloneable objects. To more know about that you can read The structured clone algorithm.

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.