- Creates an Orc worker from a URL
- Orc class maps onmessage event to custom events
- Communication with worker is done through this class
- 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
}
- 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)
- 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
- 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)
})
- In the main thread you can terminate an orc by using the terminate method
orc.terminate()
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