Skip to content

Capture Zimbra events inside a Zimlet

Barry de Graaff edited this page May 25, 2021 · 8 revisions

The shim exposes zimletEventEmitter utility and ZIMBRA_ZIMLET_EVENTS constant, zimletEventEmitter utility allows zimlet to listen to a given event and ZIMBRA_ZIMLET_EVENTS encapsulates constants for events exposed by Zimbra.

Here is an example of how a zimlet registers listener for a logout event.

import { zimletEventEmitter } from '@zimbra-client/util';
import { ZIMBRA_ZIMLET_EVENTS } from '@zimbra-client/constants';

const onLogoutHandler = () => { /** Do something */ };
zimletEventEmitter.on(ZIMBRA_ZIMLET_EVENTS.LOGOUT, onLogoutHandler);

There can be two types of handlers.

  1. Handler doing synchronous tasks like - calculating something, displaying toast, or updating view/state Here is an example of this kind of handler.
import { zimletEventEmitter } from '@zimbra-client/util';
import { ZIMBRA_ZIMLET_EVENTS } from '@zimbra-client/constants';

const onLogoutHandler = () => { /** Display toast message */ };
zimletEventEmitter.on(ZIMBRA_ZIMLET_EVENTS.LOGOUT, onLogoutHandler);
  1. Handler doing asynchronous tasks like - invoke an API call or display dialog to confirm the action with the user. Here is an example of this kind of handler.
import { zimletEventEmitter } from '@zimbra-client/util';
import { ZIMBRA_ZIMLET_EVENTS } from '@zimbra-client/constants';

const onLogoutHandler = () => new Promise((resolve, reject) => {
    if (window.confirm("Do you really want to logout?")) {
        resolve();
    } else {
        reject();
    }
});
zimletEventEmitter.on(ZIMBRA_ZIMLET_EVENTS.LOGOUT, onLogoutHandler, true); //the 3rd argument makes the handler asynchronous

To remove a listener you can use zimletEventEmitter.off like this:

  zimletEventEmitter.off(ZIMBRA_ZIMLET_EVENTS.LOGOUT, onLogoutHandler);
Clone this wiki locally