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:
- Return value of the register call: The return value is saved and called up in the destroy of a control, for example.
- First parameter in the callback. For example, if a callback is to be called only once, the registration can be terminated within the callback.
Both options are used in the sample below.
Parameter
Name | Type | Description |
---|---|---|
name | 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. |
Return value
Type | Description |
---|---|
Deregisters the event and releases all associated resources when called. |
![]() | 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