GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - bubbatls/WorkerAsModule: Allow to use js worker as a module with async methods

BranchesTags
Open more actions menu

Latest commit

 

History

5 Commits
5 Commits

Folders and files

NameName
Last commit message
Last commit date
LICENSE
LICENSE
 
 
README.md
README.md
 
 
WorkerAsModule.js
WorkerAsModule.js
 
 

Repository files navigation

WorkerAsModule

Allow to use js worker as a module with async methods

You might define some methods callable on each side, worker and mainPage can call them transparently, the only limit is that those methods are async, so you need to use await or take the result as a Promise

In your main code you put

import WorkerAsModule from "./WorkerAsModule.js";

const worker = new WorkerAsModule('myWorker.js',{
  progress:function(progess){
    if(!this._span){
      this._span = document.createElement('span')
      document.body.appendChild(this._span)
    }
    this._span.innerHTML = progess;
  }
});
console.log(await worker.hugeTask())

And in myWorker.js

import WorkerAsModule from "./WorkerAsModule.js";

const mainPage = new WorkerAsModule(self,{
  hugeTask:hugeTask
})

function hugeTask(){
  for(let i=0;i<1000000000;i++){
    if(i%10000000===0){
      mainPage.progress(i/10000000+'%');
    }
  }
  return '42';
}

And it works! the progress of the calculation is updated on the fly

If you need to transfer some stuff like a canvas to the worker you can do something like that:

in the worker

const mainPage = new WorkerAsModule(self,{
  takeCanvas(canvas,param2,param3){
    //some stuff
  }
})

and in your page

var toTransfer = mycanvas.transferControlToOffscreen();
worker.takeCanvas.transfer(toTransfer)(toTransfer,param2,param3);

About

Allow to use js worker as a module with async methods

Topics

es6promiseworkers

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.