events

October 22, 2022

events

Stability: 2-Stable

The events module provides an interface for monitoring mobile phone notifications, buttons, and touches. You can use it to cooperate with the automatic operation function to complete the automation work.

The events itself is an EventEmitter, but there are some built-in events, including key events, notification events, Toast events, etc.

It should be noted that the event processing is single-threaded and is still executed in the original thread. If the script body or other event processing has time-consuming operations, polling, etc., the event will not be processed in time (it will enter the event queue to wait for the script The main body or other event processing is completed before execution).

events.emitter()

Return a new EventEmitter. This EventEmitter does not have any built-in events.

Event:'exit`

This event is triggered when the script exits normally or abnormally. If an exception is thrown during event processing, the processing of the exit event will be immediately suspended (even if the exit event has multiple processing functions) and the exception will be printed in the console and log.

When a script stops running, all floating windows of the script will be closed, an exit event will be triggered, and then resources will be recovered. If there is an infinite loop in the processing of the exit event, subsequent resources cannot be recovered in time. At this time, the script will stay in the task list. If it is closed in the task list, the processing of the exit event will be forcibly ended and subsequent resources will be recycled.

log("Start running")
events.on("exit", function(){
    log("End of operation");
});
log("The operation is about to end");

EventEmitter

Stability: 2-Stable

EventEmitter.defaultMaxListeners

By default, up to 10 listeners can be registered for each event. The limit of a single EventEmitter instance can be changed using the emitter.setMaxListeners(n) method. The default value of all EventEmitter instances can be changed using the EventEmitter.defaultMaxListeners property.

Setting EventEmitter.defaultMaxListeners should be cautious because it will affect all EventEmitter instances, including those created before. Therefore, calling emitter.setMaxListeners(n) takes precedence over EventEmitter.defaultMaxListeners.

Note that unlike Node.js, this is a hard limit. The EventEmitter instance does not allow adding more listeners, and a TooManyListenersException will be thrown when the number of listeners exceeds the maximum.

emitter.setMaxListeners(emitter.getMaxListeners() + 1);
emitter.once('event', () => {
  // do something
  emitter.setMaxListeners(Math.max(emitter.getMaxListeners()-1, 0));
});

EventEmitter.addListener(eventName, listener)

  • eventName {any}
  • listener {Function}

Alias ​​of emitter.on(eventName, listener).

EventEmitter.emit(eventName[, ...args])

  • eventName {any}
  • args {any}

According to the registration order of the listeners, call each listener registered to the event named eventName synchronously, and pass in the provided parameters.

If the event has a listener, it returns true, otherwise it returns false.

EventEmitter.eventNames()

Returns an array listing the events of the trigger registered listener. The values ​​in the array are strings or symbols.

const myEE = events.emitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Print: ['foo','bar', Symbol(symbol)]

EventEmitter.getMaxListeners()

Returns the current maximum listener limit value of EventEmitter. This value can be set by emitter.setMaxListeners(n) or the default is EventEmitter.defaultMaxListeners.

EventEmitter.listenerCount(eventName)

  • eventName {string} The name of the event being monitored

Returns the number of listeners that are listening to the event named eventName.

EventEmitter.listeners(eventName)

  • eventName {string}

Returns a copy of the array of listeners for the event named eventName.

server.on('connection', (stream) => {
  console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Print: [[Function]]

EventEmitter.on(eventName, listener)

  • eventName {any} event name
  • listener {Function} callback function

Add the listener function to the end of the listener array for the event named eventName. It does not check whether the listener has been added. Calling multiple times and passing in the same eventName and listener will cause the listener to be added and called multiple times.

server.on('connection', (stream) => {
  console.log('There is a connection!');
});

Returns an EventEmitter reference, which can be chained.

By default, event listeners will be called sequentially in the order they were added. The emitter.prependListener() method can be used to add event listeners to the beginning of the listener array.

const myEE = events.emitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// print:
// b
// a

EventEmitter.once(eventName, listener)#

  • eventName {any} event name
  • listener {Function} callback function

Add a one-shot listener function to the event named eventName. The next time the eventName event is triggered, the listener will be removed and then called.

server.once('connection', (stream) => {
  console.log('First call!');
});

Returns an EventEmitter reference, which can be chained.

By default, event listeners will be called sequentially in the order they were added. The emitter.prependOnceListener() method can be used to add event listeners to the beginning of the listener array.

const myEE = events.emitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// print:
// b
// a

EventEmitter.prependListener(eventName, listener)

  • eventName {any} event name
  • listener {Function} callback function

Add the listener function to the beginning of the listener array for the event named eventName. It does not check whether the listener has been added. Calling multiple times and passing in the same eventName and listener will cause the listener to be added and called multiple times.

server.prependListener('connection', (stream) => {
  console.log('There is a connection!');});

Returns an EventEmitter reference, which can be chained.

EventEmitter.prependOnceListener(eventName, listener)

  • eventName {any} event name
  • listener {Function} callback function

Add a one-shot listener function to the beginning of the listener array for the event named eventName. The next time the eventName event is triggered, the listener will be removed and then called.

server.prependOnceListener('connection', (stream) => {
  console.log('First call!');
});

Returns an EventEmitter reference, which can be chained.

EventEmitter.removeAllListeners([eventName])

  • eventName {any}

Remove all or the listeners of the specified eventName.

Note that it is a bad practice to remove listeners added elsewhere in the code, especially when the EventEmitter instance is created by other components or modules.

Returns an EventEmitter reference, which can be chained.

EventEmitter.removeListener(eventName, listener)

  • eventName {any}
  • listener {Function}

Remove the specified listener from the listener array for the event named eventName.

const callback = (stream) => {
  console.log('There is a connection!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

removeListener will only remove at most one listener instance from the listener array. If any single listener is added to the listener array of the specified eventName multiple times, removeListener must be called multiple times to remove each instance.

Note that once an event is triggered, all listeners bound to it will be triggered in sequence. This means that any call to removeListener() or removeAllListeners() will not remove them from emit() after the event is triggered and before the last listener has finished executing. Subsequent events will happen as expected.

const myEmitter = events.emitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes the listener callbackB, but it will still be called.
// The trigger is the internal listener array is [callbackA, callbackB]
myEmitter.emit('event');
// print:
// A
// B

// callbackB is removed.
// The internal listener array is [callbackA]
myEmitter.emit('event');
// print:
// A

Because the listener is managed using an internal array, calling it will change the position index of any listener registered after the listener is removed. Although this does not affect the call sequence of the listeners, it means that a copy of the listener array returned by the emitter.listeners() method needs to be recreated.

Returns an EventEmitter reference, which can be chained.

EventEmitter.setMaxListeners(n)

  • n {number}

By default, if more than 10 listeners are added for a particular event, EventEmitter will print a warning. This limitation helps to find memory leaks. However, not all events are limited to 10. The emitter.setMaxListeners() method allows to modify the limits of the specified EventEmitter instance. A value of Infinity (or 0) indicates that the number of listeners is not limited.

Returns an EventEmitter reference, which can be chained.

events.broadcast: Broadcast between scripts

In addition to the ScriptEngine.emit() method provided by the engines module, the communication between scripts can also use the broadcast provided by the events module.

events.broadcast itself is an EventEmitter, but its events are shared between scripts, all scripts can send and listen to these events; event processing will be executed in the main thread of the script (the function onThisThread(eventName, ... may be added later) args) to provide the ability to execute in other threads).

For example, sending a broadcast hello in a script:

events.broadcast.emit("hello", "小明");

Monitor and process in other scripts:

events.broadcast.on("hello", function(name){
    toast("Hello, "+ name);
});
//Keep the script running
setInterval(()=>{}, 1000);
Last update:
Contributors: Bruce