Last active
August 29, 2015 13:55
-
-
Save profelis/8770024 to your computer and use it in GitHub Desktop.
Create flash Worker from single class (inspired by https://github.com/makc/worker-from-class)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-swf bin/Main.swf | |
-main MainTest | |
-swf-version 11.4 | |
-swf-header 800:600:60:FFFFFF | |
-cp src | |
-lib format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package worker; | |
import flash.display.Sprite; | |
import flash.system.*; | |
import haxe.io.*; | |
import format.swf.*; | |
/** | |
* usage: | |
* -lib format | |
* | |
* class Foo extends Sprite {...} | |
* | |
* var worker = HxWorker.workerFromClass(Foo); | |
* | |
* @author Dima Granetchi <[email protected]> | |
*/ | |
class HxWorker { | |
static public function workerFromClass(clazz:Class<Dynamic>, giveAppPrivileges = false, bytes:BytesData = null, domain:WorkerDomain = null):Worker { | |
var type = clazz; | |
while (type != null && type != Sprite) { | |
type = Type.getSuperClass(type); | |
} | |
if (type != Sprite) { | |
throw 'Class ${Type.getClassName(clazz)} must inherit from Sprite to link to the root'; | |
} | |
if (bytes == null) { | |
bytes = flash.Lib.current.loaderInfo.bytes; | |
} | |
var swfReader = new Reader(new BytesInput(Bytes.ofData(bytes))); | |
var swf = swfReader.read(); | |
for (tag in swf.tags) { | |
switch (tag) { | |
case TSymbolClass(refs): | |
for (ref in refs) { | |
if (ref.cid == 0) { | |
ref.className = Type.getClassName(clazz); | |
var swfData = new BytesOutput(); | |
new Writer(swfData).write(swf); | |
if (domain == null) { | |
domain = WorkerDomain.current; | |
} | |
return domain.createWorker(swfData.getBytes().getData(), giveAppPrivileges); | |
} | |
} | |
case _: | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flash.display.Sprite; | |
import flash.system.Worker; | |
import flash.events.Event; | |
/** | |
* @author deep | |
*/class MainTest { | |
public function new(){ | |
} | |
public static function main() { | |
trace("init"); | |
var w = worker.HxWorker.workerFromClass(Foo); | |
trace(w); | |
var fromWorker = w.createMessageChannel(Worker.current); | |
var toWorker = Worker.current.createMessageChannel(w); | |
w.setSharedProperty("fromWorker", fromWorker); | |
w.setSharedProperty("toWorker", toWorker); | |
fromWorker.addEventListener(Event.CHANNEL_MESSAGE, function (_) { | |
trace('answer from worker "${fromWorker.receive()}'); | |
}); | |
w.start(); | |
haxe.Timer.delay(function () { | |
trace("send \"ping\" from Main"); | |
toWorker.send("ping"); | |
}, 300); | |
} | |
} | |
class Bar extends Sprite { | |
function new() { | |
super(); | |
} | |
} | |
class Foo extends Bar { | |
public function new() { | |
super(); | |
var fromWorker = Worker.current.getSharedProperty("fromWorker"); | |
var toWorker = Worker.current.getSharedProperty("toWorker"); | |
toWorker.addEventListener(Event.CHANNEL_MESSAGE, function (_) { | |
var action:String = toWorker.receive(); | |
if (action == "ping") { | |
fromWorker.send("pong"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment