Worker from Class is a small library to use create Workers from Class definitions. By the default, the Flash Worker’s API receives the content of a SWF file to create a new thread. The whole SWF file is seen as the code to be executed, which prevents a developer from choosing a specific class to run in the newly created thread. Some Worker’s management code must be added to the SWF to execute a specific class or method.

This lib abstracts all that process and allows a developer to create a new Worker based on a specific class. The lib handles the worker instantiation and ensures that the specified class will run in the thread instead of the whole SWF. The content of the SWF must still be provided, but this time the lib will inspect it look up the desired class. The SWF inspection is powered by as3swf.

Sample

package workers {
  import flash.display.Sprite;
  import flash.system.Worker;

  public class MyWorker extends Sprite {
    public function MyWorker() {
      super();
      trace("Hello from the Worker!");
      trace("isPrimordial: " + Worker.current.isPrimordial)
    }
  }
}

package {
  import flash.display.Sprite;
  import flash.system.Worker;

  import workers.MyWorker;

  public class WorkerFromClass extends Sprite {
    public function WorkerFromClass() {
      var worker:Worker = WorkerFactory.getWorkerFromClass(MyWorker, loaderInfo.bytes);
      worker.start();
    }
  }
}
Misc. URL.