Dart API Referencedart:htmlWorker

Worker class

Workers are background tasks that can be easily created and can send messages back to their creators. Creating a worker is as simple as calling the Worker() constructor, specifying a script to be run in the worker thread.

Of note is the fact that workers may in turn spawn new workers as long as those workers are hosted within the same origin as the parent page.  In addition, workers may use XMLHttpRequest for network I/O, with the exception that the responseXML and channel attributes on XMLHttpRequest always return null.

For a list of global functions available to workers, see Functions available to workers.

Gecko 2.0 note
(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

If you want to use workers in extensions, and would like to have access to js-ctypes, you should use the ChromeWorker object instead.

See Using web workers for examples and details.

@DocsEditable
@DomName('Worker')
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.FIREFOX)
@SupportedBrowser(SupportedBrowser.IE, '10')
@SupportedBrowser(SupportedBrowser.SAFARI)
class Worker extends AbstractWorker native "Worker" {

 @DomName('Worker.messageEvent')
 @DocsEditable
 static const EventStreamProvider<MessageEvent> messageEvent = const EventStreamProvider<MessageEvent>('message');

 @DomName('Worker.Worker')
 @DocsEditable
 factory Worker(String scriptUrl) {
   return Worker._create_1(scriptUrl);
 }
 static Worker _create_1(scriptUrl) => JS('Worker', 'new Worker(#)', scriptUrl);

 /// Checks if this type is supported on the current platform.
 static bool get supported => JS('bool', '(typeof window.Worker != "undefined")');

 @DomName('Worker.postMessage')
 @DocsEditable
 void postMessage(/*SerializedScriptValue*/ message, [List messagePorts]) native;

 @DomName('Worker.terminate')
 @DocsEditable
 void terminate() native;

 @DomName('Worker.onmessage')
 @DocsEditable
 Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);
}

Extends

Interceptor > EventTarget > AbstractWorker > Worker

Static Properties

const EventStreamProvider<MessageEvent> messageEvent #

static const EventStreamProvider<MessageEvent> messageEvent = const EventStreamProvider<MessageEvent>('message')

final bool supported #

Checks if this type is supported on the current platform.

static bool get supported => JS('bool', '(typeof window.Worker != "undefined")');

Constructors

factory Worker(String scriptUrl) #

@DomName('Worker.Worker')
@DocsEditable
factory Worker(String scriptUrl) {
 return Worker._create_1(scriptUrl);
}

Properties

final int hashCode #

inherited from Interceptor

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

docs inherited from Object
int get hashCode => Primitives.objectHashCode(this);

final Events on #

inherited from EventTarget

This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.

Events get on => new Events(this);

final Stream<ErrorEvent> onError #

inherited from AbstractWorker
@DomName('AbstractWorker.onerror')
@DocsEditable
Stream<ErrorEvent> get onError => errorEvent.forTarget(this);

final Stream<MessageEvent> onMessage #

An event listener that is called whenever a MessageEvent with type message bubbles through the worker. The message is stored in the event's data member.
@DomName('Worker.onmessage')
@DocsEditable
Stream<MessageEvent> get onMessage => messageEvent.forTarget(this);

final Type runtimeType #

inherited from Interceptor

A representation of the runtime type of the object.

docs inherited from Object
Type get runtimeType => getRuntimeType(this);

Operators

bool operator ==(other) #

inherited from Interceptor

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

docs inherited from Object
bool operator ==(other) => identical(this, other);

Methods

bool dispatchEvent(Event evt) #

inherited from AbstractWorker
@DomName('AbstractWorker.dispatchEvent')
@DocsEditable
bool dispatchEvent(Event evt) native;

dynamic noSuchMethod(Invocation invocation) #

inherited from Interceptor

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod in an Invocation. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

docs inherited from Object
dynamic noSuchMethod(Invocation invocation) {
 throw new NoSuchMethodError(
     this,
     _symbolToString(invocation.memberName),
     invocation.positionalArguments,
     _symbolMapToStringMap(invocation.namedArguments));
}

void postMessage(message, [List messagePorts]) #

Sends a message to the worker's inner scope. This accepts a single parameter, which is the data to send to the worker. The data may be any value or JavaScript object that does not contain functions or cyclical references (since the object is converted to JSON internally).

Parameters
aMessage
The object to deliver to the worker; this will be in the data field in the event delivered to the onmessage handler. This may be any value or JavaScript object that does not contain functions or cyclical references (since the object is converted to JSON internally).
@DomName('Worker.postMessage')
@DocsEditable
void postMessage(/*SerializedScriptValue*/ message, [List messagePorts]) native;

void terminate() #

Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once.

void terminate();
@DomName('Worker.terminate')
@DocsEditable
void terminate() native;

String toString() #

inherited from Interceptor

Returns a string representation of this object.

docs inherited from Object
String toString() => Primitives.objectToString(this);

void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) #

inherited from AbstractWorker
@JSName('addEventListener')
@DomName('AbstractWorker.addEventListener')
@DocsEditable
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) native;

void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) #

inherited from AbstractWorker
@JSName('removeEventListener')
@DomName('AbstractWorker.removeEventListener')
@DocsEditable
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) native;

This page includes content from the Mozilla Foundation that is graciously licensed under a Creative Commons: Attribution-Sharealike license. Mozilla has no other association with Dart or dartlang.org. We encourage you to improve the web by contributing to The Mozilla Developer Network.