Created
August 29, 2012 15:36
-
-
Save silviopaganini/3514498 to your computer and use it in GitHub Desktop.
Test Worker using https://github.com/bortsen/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
package cacilds | |
{ | |
import cacilds.workers.WorkerEnterFrame; | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.system.MessageChannel; | |
import flash.system.Worker; | |
import flash.system.WorkerDomain; | |
/** | |
* @author @silviopaganini | s2paganini.com | |
* unit9 2012 | |
* All rights reserved. | |
* example using https://github.com/bortsen/worker-from-class | |
*/ | |
public class TestWorker extends Sprite | |
{ | |
private var worker : Worker; | |
private var mainWorker : Worker; | |
private var workerToMain : MessageChannel; | |
private var bgWorkerCommandChannel : MessageChannel; | |
public function TestWorker() | |
{ | |
super(); | |
// create main worker | |
mainWorker = WorkerDomain.current.createWorker(loaderInfo.bytes); | |
// create background worker | |
worker = WorkerFactory.getWorkerFromClass(WorkerEnterFrame, loaderInfo.bytes); | |
// create background worker channel -> worker to main communication | |
workerToMain = worker.createMessageChannel(Worker.current); | |
// create background worker channel -> main to worker communication | |
bgWorkerCommandChannel = Worker.current.createMessageChannel(worker); | |
// set shared workers channels | |
worker.setSharedProperty("incomingCommandChannel", bgWorkerCommandChannel); | |
worker.setSharedProperty('channel', workerToMain); | |
// listen for the message from the worker | |
workerToMain.addEventListener(Event.CHANNEL_MESSAGE, workerHandler); | |
// listen for the work to start | |
worker.addEventListener(Event.WORKER_STATE, workerStateHandler); | |
// start the worker | |
worker.start(); | |
} | |
/** | |
* Worker Status Handler | |
*/ | |
private function workerStateHandler(event : Event) : void | |
{ | |
if(worker.state == "running") | |
{ | |
// tell worker to start enter frame listener | |
bgWorkerCommandChannel.send("startEnterFrame"); | |
} | |
} | |
/** | |
* Worker Channel Handler | |
*/ | |
private function workerHandler(event : Event) : void | |
{ | |
trace("main", workerToMain.receive()); | |
} | |
} | |
} |
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 cacilds.workers | |
{ | |
import flash.events.Event; | |
import flash.display.Sprite; | |
import flash.system.MessageChannel; | |
import flash.system.Worker; | |
/** | |
* @author @silviopaganini | s2paganini.com | |
* unit9 2012 | |
* All rights reserved. | |
*/ | |
public class WorkerEnterFrame extends Sprite | |
{ | |
public var counter : int = 0; | |
private var commandChannel : MessageChannel; | |
private var output : MessageChannel; | |
public function WorkerEnterFrame() | |
{ | |
output = Worker.current.getSharedProperty("channel") as MessageChannel; | |
commandChannel = Worker.current.getSharedProperty("incomingCommandChannel") as MessageChannel; | |
commandChannel.addEventListener(Event.CHANNEL_MESSAGE, handleCommandMessage); | |
} | |
private function handleCommandMessage(event : Event) : void | |
{ | |
var message : String = String(commandChannel.receive()); | |
switch(message) | |
{ | |
case "startEnterFrame": | |
addEventListener(Event.ENTER_FRAME, ef); | |
break; | |
} | |
} | |
private function ef(event : Event) : void | |
{ | |
counter++; | |
output.send(counter); | |
} | |
} | |
} |
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 com.codeazur.as3swf.SWF; | |
import com.codeazur.as3swf.data.SWFSymbol; | |
import com.codeazur.as3swf.tags.ITag; | |
import com.codeazur.as3swf.tags.TagDoABC; | |
import com.codeazur.as3swf.tags.TagEnableDebugger2; | |
import com.codeazur.as3swf.tags.TagEnd; | |
import com.codeazur.as3swf.tags.TagFileAttributes; | |
import com.codeazur.as3swf.tags.TagShowFrame; | |
import com.codeazur.as3swf.tags.TagSymbolClass; | |
import flash.system.Worker; | |
import flash.system.WorkerDomain; | |
import flash.utils.ByteArray; | |
import flash.utils.getQualifiedClassName; | |
public class WorkerFactory | |
{ | |
/** | |
* Creates a Worker from a Class. | |
* @param clazz the Class to create a Worker from | |
* @param bytes SWF ByteArray which must contain the Class definition (usually loaderInfo.bytes) | |
* @param debug set to tru if you want to debug the Worker | |
* @param domain the WorkerDomain to create the Worker in | |
* @return the new Worker | |
*/ | |
public static function getWorkerFromClass(clazz:Class, bytes:ByteArray, debug:Boolean = true, domain:WorkerDomain = null):Worker | |
{ | |
var swf:SWF = new SWF(bytes); | |
var tags:Vector.<ITag> = swf.tags; | |
var className:String = getQualifiedClassName(clazz).replace(/::/g, "."); | |
var abcName:String = className.replace(/\./g, "/"); | |
var classTag:ITag; | |
for each (var tag:ITag in tags) | |
{ | |
if (tag is TagDoABC && TagDoABC(tag).abcName == abcName) | |
{ | |
classTag = tag; | |
break; | |
} | |
} | |
if (classTag) | |
{ | |
swf = new SWF(); | |
swf.version = 17; | |
swf.tags.push(new TagFileAttributes()); | |
if (debug) | |
swf.tags.push(new TagEnableDebugger2()); | |
swf.tags.push(classTag); | |
var symbolTag:TagSymbolClass = new TagSymbolClass(); | |
symbolTag.symbols.push(SWFSymbol.create(0, className)); | |
swf.tags.push(symbolTag); | |
swf.tags.push(new TagShowFrame()); | |
swf.tags.push(new TagEnd()); | |
var swfBytes:ByteArray = new ByteArray(); | |
swf.publish(swfBytes); | |
swfBytes.position = 0; | |
if (!domain) | |
domain = WorkerDomain.current; | |
return domain.createWorker(swfBytes); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment