Skip to content

Instantly share code, notes, and snippets.

@swayf
Forked from clemos/MyWorkerScript.hx
Last active August 29, 2015 14:17
Show Gist options
  • Save swayf/fad21b2bdc62984d39ee to your computer and use it in GitHub Desktop.
Save swayf/fad21b2bdc62984d39ee to your computer and use it in GitHub Desktop.
-main MyWorkerScript
-js myworkerscript.js
--next
-main MyWorkerTest
-js myworkertest.js
using WorkerScript;
class MyWorkerScript extends WorkerScript {
public override function onMessage(e){
trace("on message triggered");
postMessage("ola");
}
static function main(){
new MyWorkerScript().export();
}
}
class MyWorkerTest {
public static function main(){
var worker = new Worker("myworkerscript.js");
worker.postMessage("coucou");
}
}
@:native("Worker")
extern class Worker {
public function new( script : String ) : Void;
public function postMessage( m : Dynamic ) : Void;
public function addEventListener( type : Dynamic , cb : Dynamic -> Void ) : Void;
}
class WorkerScript {
public function new(){}
public function onMessage( e : Dynamic ) : Void { }
public inline function postMessage( m : Dynamic ) : Void {
untyped __js__("self.postMessage( m )");
}
public static function export( script : WorkerScript ){
untyped __js__("self.onmessage = script.onMessage");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment