1. Miscellaneous
  2. Using Event Listeners

Miscellaneous

Using Event Listeners

ToDesktop Event Listeners allow you to subscribe to events from the underlying desktop instance.

Subscribing to namespace events

WARNING

This approach will soon be deprecated. Use the API under the next section, Subscribing to custom events, with newer versions of ToDesktop Builder.

ToDesktop APIs expose an on function for interacting with desktop events. For example, we can use the appMenu namespace to listen to custom menu events:

        import { appMenu } from '@todesktop/client-core';

const unsubscribe = appMenu.on('new-draft', () => {
  // ...
});

await unsubscribe();

      

Subscribing to custom events

INFO

This functionality was introduced in v0.18.0 of ToDesktop Builder.

Events that are specified through ToDesktop Builder can be subscribed to via the object namespace. For example, we can specify an event called context-tray-clicked that triggers whenever a specific tray has been clicked.

        import { object } from '@todesktop/client-core';

const unsubscribe = await object.on('context-tray-clicked', () => {
  // ...
});

await unsubscribe();

      

Subscribing to instance events

ToDesktop also supports listening to instance events from created objects. For example, we can listen to notification events from a specific notification:

        import { notification } from '@todesktop/client-core';

// create the notification
const createdNotification = await notification.create({
  title: 'Notification',
  actions: [{ type: 'button', text: 'Click Me!' }]
});

// listen to click events, passing the target notification as the third argument
const unsubscribe = await notification.on(
  'click',
  () => {
    // ...
  },
  { ref: createdNotification }
);

// unsubscribing from events on the target notification
await unsubscribe();