-
Notifications
You must be signed in to change notification settings - Fork 49
WebWorker Syntax
The JavaScript backend uses special syntax inspired by Golang to make working with WebWorkers clean and simple.
Any code blocked under with webworker:
is split off and injected into the webworker when it is spawned. Note that the webworker also copies all functions and classes from the main thread's namespace into the webworker, this way you do not need worry about splitting apart your code and using importScripts inside the webworker.
with webworker:
def mywebworkerfunction(a):
...
class Something():
def send(self, ob):
...
myworker = spawn( Something() )
sends object to webworker. inside the webworker this calls the send
method of the spawned class.
myworker <- object
Waits until the next message comes from myworker.
result = <- myworker
result = <- mywebworkerfunction(x)
result = <- myworker.somemethod(x)
Continues if worker currently has no new messages.
select:
case result = <- myworker1:
print result
case result = <- myworker2:
print result
Spawned objects can return a data stream to the main thread using self <- object
.
The send
method is called the worker is written to from the main thread using worker <- object
.
Note: if sending objects from the main thread to the worker, you must restore the __proto__
property of the object sent, if you will call methods on it in the webworker.
with webworker:
class Worker:
def send(self, input):
self <- 'hello'
self <- 'world'
return 'foobar'
When sending or getting data from the worker, data is actually cloned by the browser (using structured cloning), this strips away the __proto__
attribute of the object and causes it to lose its class prototype, preventing you from calling methods on the sent object.
The __proto__
and class of a sent object can automatically be restored, when you statically type the input arguments and return type of the function, the transpiler and runtime will automatically restore the class type, so that you can call methods on the object.
- restoring the
__proto__
attribute only happens on the toplevel object, so any sub-objects it may contain will not have__proto__
restored. - when static types are used outside of the webworker, they are instead used for runtime type checking, and not for restoring the
__proto__
attribute and the class of the object. https://github.com/rusthon/Rusthon/wiki/JavaScript-Static-Types - webworkers are not builtin to NodeJS, so they do not work server side.
class SharedClass:
def foobar(self, x,y,z):
return x+y+z
with webworker:
def somefunction() -> SharedClass:
s = SharedClass(10,20,30)
return s
class MyWorker:
def send(self, obj:SharedClass ) -> SharedClass:
print obj.foobar(1,2,3)
return obj
- https://github.com/rusthon/Rusthon/blob/master/examples/javascript_webworkers.md
- https://github.com/rusthon/Rusthon/blob/master/examples/javascript_webworkers_advanced.md
- https://github.com/rusthon/Rusthon/blob/master/examples/javascript_webworkers_classes.md
- https://github.com/rusthon/Rusthon/blob/master/examples/javascript_timeout_webworkers.md