new TransWorker()
TransWorker - Inter thread method invocation helper class for the WebWorker.
This class offers different implementations for its role on the context.
In the main thread, It creates WebWorker instance and creates wrapper functions for all the methods declared in the prototypes of the class given in the parameters.
The wrapper method sends a message to the worker with the method name and all the parameter.
When the worker side instance received the message, it invokes the method specified by the name in the message with the parameters. The return value will be notified by the message to the main thread instance from the worker.
The main thread instance that received the notification notifies the value to the callback function given at first invocation.
LICENSE
Released under the MIT license http://opensource.org/licenses/mit-license.php
Copyright (c) 2017 Koji Takami(vzg03566@gmail.com)
- Source:
Members
(static) SyncTypeCallback :function
A literal for the interface methods to synchronize with a callback.
Type:
- function
- Source:
(static) SyncTypePromise :function
A literal for the interface methods to synchronize with a Promise.
Type:
- function
- Source:
Methods
(static) createInterface(workerUrl, clientCtor, options) → {Transworker}
Create a worker and an interface instance for the thread.
Parameters:
Name | Type | Description |
---|---|---|
workerUrl |
string | A worker url. It must use TransWorker. |
clientCtor |
function | client-class constructor. |
options |
TransWorker.Options | Options to create a wrapper object for the main thread. |
Returns:
The created TransWorker instance.
- Type
- Transworker
(static) createInvoker(workerUrl, clientCtor, thisObject, notifyHandlers) → {Transworker}
Create a main thread instance for dedicated worker.
Parameters:
Name | Type | Description |
---|---|---|
workerUrl |
string | A worker url. It must use TransWorker. |
clientCtor |
function | client-class constructor. |
thisObject |
object | (Optional) A caller of callback and notification. |
notifyHandlers |
object | A map a notification name to the handler. |
Returns:
The created Transworker instance.
- Type
- Transworker
(static) createSharedInvoker(workerUrl, clientCtor, thisObject, notifyHandlers) → {Transworker}
Create a main thread instance for shared worker.
Parameters:
Name | Type | Description |
---|---|---|
workerUrl |
string | A worker url. It must use TransWorker. |
clientCtor |
function | client-class constructor. |
thisObject |
object | (Optional) A caller of callback and notification. |
notifyHandlers |
object | A map a notification name to the handler. |
Returns:
The created Transworker instance.
- Type
- Transworker
(static) createSharedWorker(client) → {TransWorker}
Create a worker side instance of SharedTransWorker.
Parameters:
Name | Type | Description |
---|---|---|
client |
object | An instance of the client class. |
Returns:
an instance of TransWorker.
- Type
- TransWorker
(static) createWorker(client) → {TransWorker}
Create a worker side instance of DedicatedTransWorker.
Parameters:
Name | Type | Description |
---|---|---|
client |
object | An instance of the client class. |
Returns:
an instance of TransWorker.
- Type
- TransWorker
(abstract) connectWorker(workerURL) → {undefined}
Parameters:
Name | Type | Description |
---|---|---|
workerURL |
string | A URL for the worker or server. |
- Source:
Returns:
- Type
- undefined
createCallbackWrapper(methodName) → {function}
Create client method wrapper
Parameters:
Name | Type | Description |
---|---|---|
methodName |
string | A method name to override. |
- Source:
Returns:
A wrapper function.
- Type
- function
createInvoker(workerUrl, clientCtor, thisObject, notifyHandlers) → {undefined}
Create instance for main thread.
Parameters:
Name | Type | Description |
---|---|---|
workerUrl |
string | A worker url. It must use TransWorker. |
clientCtor |
function | client-class constructor. |
thisObject |
object | (Optional) A caller of callback and notification. |
notifyHandlers |
object | A map a notification name to the handler. |
- Source:
Returns:
- Type
- undefined
createPromiseWrapper(methodName) → {function}
Create client method wrapper that returns a promise that will be resolved by a value that remote method returns.
Parameters:
Name | Type | Description |
---|---|---|
methodName |
string | A method name to override. |
- Source:
Returns:
A wrapper function.
- Type
- function
createWorker(client) → {undefined}
Create Worker side TransWorker instance.
Parameters:
Name | Type | Description |
---|---|---|
client |
object | A instance of the client class. |
- Source:
Returns:
- Type
- undefined
invokeMethod(methodName, paramList, transObjList) → {Promise.<any>}
Invoke a remote method and returns a promise object that will resolved with its return value. (for only UI thread)
Parameters:
Name | Type | Description |
---|---|---|
methodName |
string | A name of the object to be transfered |
paramList |
Array.<any> | An array of parameters |
transObjList |
Array.<TransferableObject> | null | An array of objects to be transfered |
- Source:
Returns:
A promise object. The fulfillment value is the return value of the remote method
- Type
- Promise.<any>
listenTransferableObject(objName, handler) → {undefined}
Enter a handler to receive a transferable object. (for only Worker instance)
Parameters:
Name | Type | Description |
---|---|---|
objName |
string | A name of the object to receive |
handler |
function | A callback function to receive the object |
- Source:
Returns:
- Type
- undefined
onTransferableObject(objName, transferableObj) → {undefined}
A primary receiver for a transferable object. (for only Worker instance)
Parameters:
Name | Type | Description |
---|---|---|
objName |
string | A name of the object to be transfered |
transferableObj |
Transferable | An object to be transfered |
- Source:
Returns:
- Type
- undefined
postMessage(message, transObjList) → {undefined}
Post message.
Parameters:
Name | Type | Description |
---|---|---|
message |
object | a message object. |
transObjList |
Array.<TransferableObject> | null | An array of objects to be transfered |
- Source:
Returns:
- Type
- undefined
postNotify(name, param, transObjList) → {undefined}
Post a notify to the UI-thread TransWorker instance
Parameters:
Name | Type | Description |
---|---|---|
name |
string | A message name. |
param |
any | A message parameters. |
transObjList |
Array.<Transferable> | null | A list of transferable objects. |
- Source:
Returns:
- Type
- undefined
(abstract) publishWorkerConsole() → {undefined}
- Source:
Returns:
- Type
- undefined
(abstract) setupOnConnect() → {undefined}
- Source:
Returns:
- Type
- undefined
subscribe(name, handler) → {undefined}
Register a notification to receive a message from the worker thread.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | A notification name. |
handler |
function | A notification handler. |
- Source:
Returns:
- Type
- undefined
(abstract) subscribeWorkerConsole() → {undefined}
- Source:
Returns:
- Type
- undefined
transferObject(objName, transferableObj) → {Promise.<any>}
Transfer an object to the worker. (for only UI thread)
Parameters:
Name | Type | Description |
---|---|---|
objName |
string | A name of the object to be transfered |
transferableObj |
Transferable | An object to be transfered |
- Source:
Returns:
A promise to be resolved a value returned by worker side method
- Type
- Promise.<any>