Skip to content

fsx950223/worker-run

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Principle

Use Browser File System generate a blob file to create a worker and run function in the worker, you can concurrent exec function in different workers.

Target

To solve webpack problems with worker-loader,just like bundle size and window is not defined,etc.

Usage

npm install worker-run

simple

import {run} from 'worker-run'
function a(){console.log(123);return 123}
run(a).then(val=>console.log(val))

keepAlive

When you set keepAlive to true,worker will run until page closed.

import {run} from 'worker-run'
function a(val){setInterval(()=>console.log(val),1000);return val+1}
run({exec:a,keepAlive:true},123).then(val=>console.log(val))

callback

Callback will be called when worker received message event

import {run} from 'worker-run'
function a(val){setInterval(()=>console.log(val),1000);return val+1}
function b(event){console.log(event)}
run({exec:a,keepAlive:true,callback:b},123).then(val=>console.log(val))