Object built-in object

ObjectWorker

The Worker object is an object used to create child threads, which can be created and processed in the program. A Worker object can be understood as a JavaScript process that executes in a thread different from the main thread. Worker does not share memory with the main thread and does not block the main thread. It is a mainstream asynchronous programming method.

The constructor of the Worker object is as follows:

1
new Worker(String path, Object opts = {})

in,pathThe parameter specifies the JavaScript file path of the new thread. For example, you can write a work.js file with the following content:

1 2 3 4
const { Worker } = require('worker_threads'); console.log('Hi from worker');

In the main program, run work.js with the following code:

1 2 3 4
const { Worker } = require('worker_threads'); const worker = new Worker('path/to/work.js');

After running, you can see the output "Hi from worker" on the console of the main program.

In the following example, suppose we have a long-running calculation and we want to put it into another thread to process without being blocked by this calculation in the main thread. code show as below:

Main thread:

1 2 3 4 5 6 7 8 9 10 11 12
const { Worker } = require('worker_threads'); // create a worker thread const fib = new Worker(__dirname + '/fib-worker.js'); // receive the result from the worker thread fib.onmessage = (ev) => { console.log('result: ', ev.data); }; fib.postMessage(40); console.log('main thread still working');

In this example, we create a worker thread through the constructor of the Worker object to process the calculation of the Fibonacci sequence. The main thread passes data to the worker thread through the postMessage() method, and obtains the processing results through the onmessage event. At the same time, the main thread displays the 'still working' message to prove that this computing task has been 'delegated' to the worker thread and can continue to process other things.

The worker thread code style is as follows:

1 2 3 4 5 6 7 8 9 10 11 12
// fib-worker.js Master.onmessage = (ev) => { const n = ev.data; const result = fib(n); // Once the calculation has been completed, the result is sent back to the main thread. Master.postMessage(result); }; function fib(n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); }

In the worker thread, we listened to the messages sent by the main thread through the entry parameter postMessage(), calculated the specified Fibonacci sequence and sent the calculation results back to the main thread through the Master.postMessage() method.

This is the most basic Worker example. When developing using Worker objects, the main thread and the worker thread are completely asynchronous. Each Worker object is a separate thread. The Worker object instantiated in the main thread will not generate any block.

inheritance relationship

Constructor

Worker

Worker object constructor

1 2
new Worker(String path, Object opts = {});

Call parameters:

  • path: String, specifies the Worker entry script, only accepts absolute paths
  • opts: Object, construction option, not supported yet

static properties

defaultMaxListeners

Integer, the default global maximum number of listeners

1
static Integer Worker.defaultMaxListeners;

member properties

onload

Function, query and binding accept the load message event, which is equivalent to on("load", func);

1
Function Worker.onload;

onmessage

Function, query and binding accept postMessage message events, equivalent to on("message", func);

1
Function Worker.onmessage;

onerror

Function, query and binding accept error message events, equivalent to on("error", func);

1
Function Worker.onerror;

member function

postMessage

Send a message to Master or Worker,

1
Worker.postMessage(Value data);

Call parameters:

  • data: Value, specifies the content of the message sent

on

Bind an event handler to the object

1 2
Object Worker.on(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object Worker.on(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

addListener

Bind an event handler to the object

1 2
Object Worker.addListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object

1
Object Worker.addListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependListener

Bind an event handler to the object's origin

1 2
Object Worker.prependListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object Worker.prependListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

once

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1 2
Object Worker.once(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind a one-time event handler to the object. The one-time handler will only be triggered once.

1
Object Worker.once(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

prependOnceListener

Bind an event handler to the object's origin

1 2
Object Worker.prependOnceListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Bind an event handler to the object's origin

1
Object Worker.prependOnceListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name will be used as the event name, and the value of the attribute will be used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

off

Unassign a function from the object processing queue

1 2
Object Worker.off(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object Worker.off(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object Worker.off(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeListener

Unassign a function from the object processing queue

1 2
Object Worker.removeListener(String ev, Function func);

Call parameters:

  • ev: String, specifies the name of the event
  • func: Function, specify the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancel all functions in the object processing queue

1
Object Worker.removeListener(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Unassign a function from the object processing queue

1
Object Worker.removeListener(Object map);

Call parameters:

  • map: Object, specifies the event mapping relationship, the object attribute name is used as the event name, and the value of the attribute is used as the event processing function

Return results:

  • Object, returns the event object itself to facilitate chain calls

removeAllListeners

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object Worker.removeAllListeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

Cancels all listeners for all events from the object's processing queue. If an event is specified, removes all listeners for the specified event.

1
Object Worker.removeAllListeners(Array evs = []);

Call parameters:

  • evs: Array, specify the name of the event

Return results:

  • Object, returns the event object itself to facilitate chain calls

setMaxListeners

The default limit on the number of listeners, for compatibility only

1
Worker.setMaxListeners(Integer n);

Call parameters:

  • n: Integer, specify the number of events

getMaxListeners

Gets the default limit number of listeners, for compatibility only

1
Integer Worker.getMaxListeners();

Return results:

  • Integer, returns the default limit quantity

listeners

Query the listener array for the specified event of the object

1
Array Worker.listeners(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Array, returns the listener array for the specified event

listenerCount

Query the number of listeners for the specified event of the object

1
Integer Worker.listenerCount(String ev);

Call parameters:

  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

Query the number of listeners for the specified event of the object

1 2
Integer Worker.listenerCount(Value o, String ev);

Call parameters:

  • o: Value, specifies the object of the query
  • ev: String, specifies the name of the event

Return results:

  • Integer, returns the number of listeners for the specified event

eventNames

Query listener event name

1
Array Worker.eventNames();

Return results:

  • Array, returns an array of event names

emit

Actively trigger an event

1 2
Boolean Worker.emit(String ev, ...args);

Call parameters:

  • ev: String, event name
  • args: ..., event parameters will be passed to the event processing function

Return results:

  • Boolean, returns the event trigger status, returns true if there is a response event, otherwise returns false

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Worker.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value Worker.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable