infinity.event¶
Provides functionality for communicating between the main program, threads and scheduler tasks by registering event handlers, sending events into queues and processing them.
Module: none (built-in)
Every runtime (the main program, every infinity.thread and every infinity.scheduler task) has its own event queue. send() puts the event into the queue of every one of these runtimes (including the sending one) that has registered a handler for the event name at that moment; runtimes without a handler for that name do not receive the event. The handlers are only called when the receiving runtime calls processQueue(). The parameters are passed as a JSON copy, so only JSON-serializable data can be transferred. Short-lived runtimes like HTTP request handlers do not have an event queue: register(), unregister() and processQueue() throw an error there (since 3.11; before, an access violation), send() works.
Example:
main.ts:
infinity.loadModule('infinity.thread');
let thread = new infinity.thread('thread.js');
thread.start();
infinity.sleep(100);
infinity.event.send('message', 'Hello from main.js');
infinity.sleep(1000);
thread.stop();
thread.ts:
infinity.event.register('message',
function(params: any): void {
console.debug('thread.js: Message received: ' + JSON.stringify(params));
}
);
console.info( 'thread.js: Started' );
while (!infinity.threadTerminated) {
infinity.sleep(100);
infinity.event.processQueue();
}
console.info( 'thread.js: Terminated' );
Console output:
[I 2021-04-01 18:41:04,702] thread.js: Message received: "Hello from main.js"
processQueue()¶
Processes all events collected in the queue of the current runtime: for every queued event, the registered callback is called synchronously with the event's parameters. Returns when the queue is empty.
Signature:
processQueue(): void
Example:
infinity.event.processQueue();
register()¶
Registers an event handler under the specified name with the provided callback function in the current runtime. Only one handler can be registered per event name and runtime; registering a second handler for the same name throws an error.
Signature:
register( name: string, callback: (params: any)=>void ): void
Parameters:
-
name:
stringThe name for the event handler to register.
-
callback:
(params: any)=>voidThe callback function that will be executed in case of the event getting triggered.
Example:
infinity.event.register('message',
function(params: any): void {
console.debug('event triggered: ' + JSON.stringify(params));
}
);
send()¶
Sends the specified event with the provided parameters to the queues of all runtimes that have registered a handler for it (see above). The callbacks are executed when the receiving runtimes call processQueue().
Signature:
send( name: string, params?: any ): void
Parameters:
-
name:
stringThe event to be triggered.
-
params:
any, optionalThe value to be passed to the callback function of the event handler. It is converted to JSON and back, so it must be JSON-serializable. If omitted, the callback is called without argument.
Example:
infinity.event.send('message');
unregister()¶
Unregisters the specified event handler of the current runtime. Throws an error if no handler is registered for this name.
Signature:
unregister( name: string ): void
Parameters:
- name:
stringThe name of the event handler to unregister.
Example:
infinity.event.unregister('message');