WebWorker Syntax

brett hartshorn edited this page Jul 30, 2015 · 5 revisions

The JavaScript backend uses special syntax inspired by Golang to make working with WebWorkers clean and simple.

Define WebWorker Code

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):
       ...

Create a new Worker

myworker = spawn( Something() )

Send Data to Worker

sends object to webworker. inside the webworker this calls the send method of the spawned class.

myworker <- object

Blocking Assignment

Waits until the next message comes from myworker.

result = <- myworker

Calling Worker Functions

result = <- mywebworkerfunction(x)

Calling Worker Methods

result = <- myworker.somemethod(x)

Non-blocking Select

Continues if worker currently has no new messages.

select:
  case result = <- myworker1:
    print result
  case result = <- myworker2:
    print result

Multiple Return Data

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'

Sending and Receiving Class Instances

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.

Gotchas

  • 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

Examples