My inspiration how we can use React, Web Workers, and AI frontend technologies
- Smooth UI when computing something heavy.
- Predefined React hooks to create a background task.
- Event-based replies from a task function.
- Possibility to use promise-based and generator task functions.
- 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.
- You cannot use DOM manipulations.
- 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.
-
Install npm package
npm i @tynik/web-workers
-
File
worker.worker.js
is required by a worker and is fetched when the worker is initiated. You should copyworker.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' ] }) ] }
All examples source directory here.
Also, you can see deployed examples here.
React
- Pure. Implemented with using base
Task
class. - Base.
- Tasks queue. Calling the heavy task with different arguments at the same time.
- Promise-based result.
- Simple generator.
- Infinity generator. Task function as infinity generator that automatically stopped after some time.
- Files processing.
- Brain.js XOR.
- High resolution time metrics. Please, see reduced time precision in Firefox.
Task class
constructor(func: TaskFunction, { deps?: string[] })
- Initiating a task with creating a new Worker. The task function currently is not run.run(...args: Params): RunTaskAPI
- Transfer and run a task function inside created Worker. If you callrun
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.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.
whenSent: Promise<Meta>
- Property to subscribe onsent
event.whenStarted: Promise<Meta>
- Property to subscribe onstarted
event.whenCompleted: Promise<{ result: Result } & Meta>
- Property to subscribe oncompleted
event.whenError: Promise<{ result: string } & Meta>
- Property to subscribe onerror
event.next(...args: Params): Promise<{ result: Result } & Meta>
- Run the next iteration for a task generator function with transferring arguments.return(value?: any): void
- Stop a task generator function with passingvalue
as a result if needed. In common can be used to stop infinity generators.throw(e?: any): void
- Throw an error inside a task generator function. The argumente
can accept only cloneable objects. To more know about that you can read The structured clone algorithm.