Skip to main content

Class: PlayerEvents

The Event API for Player apps has methods for adding and removing event handlers. Events include system events as well as custom app events generated via push notifications. The Player SDK has a few events that are fired during specific moments of the application life-cycle. Below you can find the various events and what to expect to be passed to the event handler.


The destroy event

The destroy event is fired anytime the application is going to be disposed by the player. When developing a player application with the Player SDK, you should be prepared for your app to be destroyed anytime it is taken off of the display. The player will fire the destroy event and then wait for a small amount of time to allow you to do any cleanup. To notify the player you are done with any final processes, a callback is passed to the event handler. If you do not call the callback, your app will still be removed once the allotted cleanup time has passed. Every JavaScript Player application should attach a handler, if only to call the done callback.

enplug.on('destroy', (done) => {  // perform some synchronous action  localStorage.setItem('last-viewed', view.id);  done(); // ok! I'm ready to be destroyed...});

Useful commands:

Methods#

fireEvent#

fireEvent(eventName, ...args): void

Calls all handler functions associated with the event.

Parameters#

NameTypeDescription
eventNamestringName of the event.
...argsany[]All arguments passed to this function besides eventName.

Returns#

void


off#

off(eventName, eventHandler): void

Removes a handler function from listening to the event.

Can be used to attach a new handler for a specific event by that event’s name. If you wish to remove this event handler at any time, you will need to keep a reference to the handler function and pass it into the off function.

see on to add an event handler

Adding and removing event handler
function handler(eventData) {  // ...}enplug.on('my-event', handler);enplug.off('my-event', handler);

Parameters#

NameTypeDescription
eventNamestringName of the event.
eventHandler(...args: any[]) => anyHandler function to remove.

Returns#

void


on#

on(eventName, eventHandler): Promise<void>

Lets apps listen for events. The events are triggered by messages with service == 'event' coming from the Player through the bridge.

Can be used to attach a new handler for a specific event by that event’s name.

Adding event handler
enplug.on('my-event', (eventData) => {  // ...});

If you wish to remove this event handler at any time, you will need to keep a reference to the handler function and pass it into the off function.

see off to remove an event handler

Parameters#

NameTypeDescription
eventNamestringName of the event.
eventHandler(...args: any[]) => anyAn event handler, called when the event occurs.

Returns#

Promise<void>


once#

once(eventName, eventHandler): Promise<void>

Lets apps listen for an event. After it occurs once, it removes the listener.

The once function is a convenience function for adding an event handler that gets automatically removed after the first time it is fired. The code below is the functional equivalent of the once function’s functionality.

function handler(eventData) {  enplug.off('my-event', handler);  // your handler run here}enplug.on('my-event', handler);

Parameters#

NameTypeDescription
eventNamestringName of the event.
eventHandler(...args: any[]) => anyHandler function to fire once when the event occurs.

Returns#

Promise<void>