Skip to content

miozzz/Orc.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Orc.js

Simple worker encapsulation.

  • Creates an Orc worker from a URL
  • Orc class maps onmessage event to custom events
  • Communication with worker is done through this class

Creating Accesible Methods in the Worker

  • In each worker scope there is one instance of an Orc.
  • All methods attached to this Orc instance will be easily accesible in the main thread.
// sampleWorker.js

importScripts('scripts/orc.js');

//This method will be accesible in the main thread.
orc.joinArray = function ([params]) {
   return params.join('')
}

orc.sum = function([a,b]){
   return a + b
}
//This method will be NOT accesible in the main thread
doThis = function(params){
   console.log('do this')
   return
}

Calling Methods from the Main Thread

  • Methods in the worker thread attached to the orc can be accessible in the main thread.
  • You can perform these methods as if the main thread and the worker thread are the same.
let orc = Orc.create('scripts/sampleWorker.js')
    orc.post('joinArray',['a','b','c']) //posts the joinArray method to the worker
    orc.joinArray(['a','b','c'])        //Short version of .post()
    orc.sum(1,2)

Response Handling

Event Style

  • Responses from the worker thread can be handled like events through .on() method
  • The first parameter is the method to listen for.
  • The second parameter is the callback function.
    orc.on('joinArray',function(data){
        console.log(data)
    })
    // Event will fire when orc.joinArray(['a','b','c']) returns from worker thread

Promises

  • Responses from the worker thread can be handled like promises
  • Every time a method is posted to the worker a unique promise is returned
  • This unique promise is resolved when the post returns from the worker thread
    orc.joinArray(['a','b','c']).then((data)=>{
        console.log(data)
    })  

Terminating an Orc

  • In the main thread you can terminate an orc by using the terminate method
orc.terminate()

Reserved Method Names

A list of method names that cannot be attached to the orc in the worker thread

  • self
  • onmessage
  • __initialize__
  • post
  • terminate
  • on
  • post
  • trigger
  • listenToWorker
  • resolveMessage
  • acknowledgeWorker
  • events
  • promises
  • ready

About

web worker encapsulation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published