Documentation

Event.EventEmitter

This class realizes EventEmitter interface for handling custom events.

import {Event} from 'main.core';

// Sending events in the descendant class 
class Button extends Event.EventEmitter
{
	constructor()
	{
		super();
		// Sending event BX.MyModule.MyClass:ready
		this.emit('BX.UI.Button:ready');
	}
	
	render()
	{
		// Sending event BX.MyModule.MyClass:beforeRender
		this.emit('BX.UI.Button:beforeRender');
		// ... 
		// Sending event BX.MyModule.MyClass:render
		this.emit('BX.UI.Button:render');
	}
	
	onClick()
	{
		const event = new Event.BaseEvent({data: {...}});
		// Sending event BX.MyModule.MyClass:render
		this.emit('BX.UI.Button:click', event);
	}
}

Subscription to object events

const button = new Button();

button
	.subscribe('BX.UI.Button:ready', (event) => {
		console.log('Button is ready', event);
	})
	.subscribe('BX.UI.Button:render', (event) => {
		console.log('Button is rendered', event);
	})
	.subscribe('BX.UI.Button:click', (event) => {
		console.log('Button is clicked', event);
	});

Global subscription to events of all objects

Event.EventEmitter
	.subscribe('BX.UI.Button:ready', (event) => {
		console.log('Button is ready', event);
	})
	.subscribe('BX.UI.Button:render', (event) => {
		console.log('Button is rendered', event);
	})
	.subscribe('BX.UI.Button:click', (event) => {
		console.log('Button is clicked', event);
	});

Global sending of events

Event.EventEmitter
	.emit('BX.UI.Button:click');

Calls all BX.UI.Button:click event handlers.


Event.EventEmitter.subscribe

Event.EventEmitter.subscribe(event: any, listener: (event: BaseEvent) => void): EventEmitter

Adds handler for specified event. BaseEvent object type is passed to the handler as first parameter.

import {Event} from 'main.core';

class Button extends Event.EventEmitter {}

const emitter = new Button();

emitter.subscribe('BX.UI.Button:click', (event) => {
	// ...
});

Event.EventEmitter.subscribeOnce

Event.EventEmitter.subscribeOnce(event: any, listener: (event: BaseEvent) => void): EventEmitter

Adds handler for the specified event. This handler will be called only once.

import {Event} from 'main.core';

class Button extends Event.EventEmitter {}

const emitter = new Button();

emitter.subscribeOnce('BX.UI.Button:click', (event) => {
	// ...
});

Event.EventEmitter.unsubscribe

Event.EventEmitter.unsubscribe(event: any, listener: Function) => void): EventEmitter

Deletes event handler.

import {Event} from 'main.core';

class Button extends Event.EventEmitter {}

const emitter = new Button();

emitter.subscribeOnce('BX.UI.Button:click', clickHandler);

Event.EventEmitter.emit

Event.EventEmitter.emit(eventName: any, event?: BaseEvent | {[key: string]: any}): EventEmitter

Sends event.

import {Event} from 'main.core';

// Sending events in the descendant class 
class Button extends Event.EventEmitter
{
	constructor()
	{
		super();
		// Sending event BX.MyModule.MyClass:ready
		this.emit('BX.UI.Button:ready');
	}
	
	// ...
}

Event.EventEmitter.emitAsync

Event.EventEmitter.emitAsync(eventName: any, event?: BaseEvent | {[key: string]: any}): Promise

Sends event and returns Promise to be resolved when all promises, returned from handlers are resolved, or Promise to be rejected when minimum one of returned promises is rejected.

Please note, that promise is resolved with Array type value, containing values of promises returned from handlers. Value sorting order corresponds with handler subscription order, but not to the resolved promises sort order.

Also, please note that it is not mandatory to return promise from synchronous handlers, you can simply return value of any type. Or you may not return anything.

import {Event} from 'main.core';

class Button extends Event.EventEmitter {}

const button = new Button();

button.subscribe('click', () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve('value 1');
        }, 1000);  
    });
});

button.subscribe('click', () => {
    return 'value 2';
});

button
    .emitAsync('click')
    .then((results) => {
        console.log(result[0]); // => 'value 1'
        console.log(result[1]); // => 'value 2'
    });

Event.EventEmitter.setMaxListeners

Event.EventEmitter.setMaxListeners(count: number): EventEmitter

Sets maximum number of handlers for each event. Default number is 10. Console shows a warning when the number of handlers exceeds the maximum number.

import {Event} from 'main.core';

class Button extends Event.EventEmitter {}

const emitter = new Button();

emitter.setMaxListeners(2);

emitter
	.subscribe('BX.UI.Button:click', () => {})
	.subscribe('BX.UI.Button:click', () => {})
	.subscribe('BX.UI.Button:click', () => {});

// => Possible BX.Event.EventEmitter memory leak detected. 3 BX.UI.Button:click listeners added. Use emitter.setMaxListeners() to increase limit

Event.EventEmitter.getMaxListeners

Event.EventEmitter.getMaxListeners(): Number

Returns current limit for the number of handlers for each event.


Event.EventEmitter.getListeners

Event.EventEmitter.getListeners(eventName: any): Set | null

Returns list of handlers for the specified event.



© «Bitrix24», 2001-2022
Up