register

[ Function ]

static register(
    name: string,
    callback: (e: EventProvider.Event, ...data: any[]) => void,
    options?: object
): TcHmi.DestroyFunction;

Registers a callback to an event and returns a function for deregistering the event.

If an event is no longer required, it should be logged off to avoid memory leaks (mem leaks).

The system offers two options for logging out:

Both options are used in the sample below.

Parameter

Name

Type

Description

name

string

Name of the event

callback

(e: EventProvider.Event, ...data: any[]) => void;

Callback function which should be called for these events.

This function receives an event object and optional further parameters. Not all events supplies these.

options [ optional ]

Available since version 1.10

object

JSON object with setting values.
Options depend on the event.

Return value

Type

Description

TcHmi.DestroyFunction

Deregisters the event and releases all associated resources when called.

register 1:

Available from 1.8

Sample – JavaScript

var destroyEvent = TcHmi.EventProvider.register(
    'TcHmiButton.onPressed',
    function (evt, data) {
        // Do stuff
        console.log(evt);
        if(data){
            // some events have custom data
            console.log(data);
        }
        // …
        // Destroy to free event resources if event is no longer needed.
        evt.destroy();
    }
);
var timeoutID = setTimeout(function () {
    // Remove register after 5 seconds as an example
    destroyEvent();
}, 5000);

Sample – TypeScript

The event name can also be accessed and responded to within the Callback:

// get somehow the control instances of the controls
const controlA = TcHmi.Controls.get('ControlNameA')!;
const controlB = TcHmi.Controls.get('ControlNameB')!;

const genericHandler: Parameters<typeof TcHmi.EventProvider.register>[1] = (event, data) => {
    const ctrlName = event.name.split('.')[0]; // event.name is '<controlname>.onPressed'
    if (ctrlName === 'ControlA') {
        // do smart things with Control A
        TcHmi.Log.infoEx(controlA.getId() + ' clicked' );
    } else if (ctrlName === 'ControlB') {
        // do smart things with Control B
        TcHmi.Log.infoEx(controlB.getId() + ' clicked');
    }
};

const destroyRegPressedA = TcHmi.EventProvider.register(controlA.getId() + '.onPressed', genericHandler);
const destroyRegPressedB =TcHmi.EventProvider.register(controlB.getId() + '.onPressed', genericHandler);

// both destroyRegPressed destroy callbacks should be called at the correct time to prevent memleaks.
// if register was done in __attach destroy it in __detach
// if register was done before __attach destroy it in __destroy