How to create a new event handler in apps-engine repo?

How to create a new event handler in apps-engine repo?

Here, we put LivechatRoomClosed event as an example:

Apps-engine side

  1. Update the AppMethod enum which is under the definition/metadata directory. We will add a new method EXECUTELIVECHATROOMCLOSEDHANDLER:
1
2
3
4
...
// Livechat
EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER = 'executeLivechatRoomClosedHandler',
...
  1. Also we need to update the AppInterface enum which is under the server/compiler/AppImplements directory:
1
2
3
4
...
// Livechat
ILivechatRoomClosedHander = 'ILivechatRoomClosedHander',
...
  1. Create an interface called ILivechatRoomClosedHander.ts under the definition/livechat, its content can be similar to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { IHttp, IPersistence, IRead } from '../accessors';
import { AppMethod } from '../metadata';
import { ILivechatRoom } from './ILivechatRoom';

/**
* Handler called after a livechat room is closed.
*/
export interface ILivechatRoomClosedHander {
/**
* Method called *after* a livechat room is closed.
*
* @param livechatRoom The livechat room which is closed.
* @param read An accessor to the environment
* @param http An accessor to the outside world
* @param persistence An accessor to the App's persistence
*/
[AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER](data: ILivechatRoom, read: IRead, http: IHttp, persistence: IPersistence): Promise<void>;
}
  1. Edit AppListenerManager which is under the server/manager directory:

    • Add a new case into executeListener method:

    1
    2
    3
    4
    5
    6
    ...
    // Livechat
    case AppInterface.ILivechatRoomClosedHander:
    this.executeLivechatRoomClosed(data as ILivechatRoom);
    return;
    ...

    • Add executeLivechatRoomClosed method to resolve the events:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    private async executeLivechatRoomClosed(data: ILivechatRoom): Promise<void> {
    const cfLivechatRoom = Utilities.deepCloneAndFreeze(data);

    for (const appId of this.listeners.get(AppInterface.ILivechatRoomClosedHander)) {
    const app = this.manager.getOneById(appId);

    if (!app.hasMethod(AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER)) {
    continue;
    }

    await app.call(AppMethod.EXECUTE_LIVECHAT_ROOM_CLOSED_HANDLER,
    cfLivechatRoom,
    this.am.getReader(appId),
    this.am.getHttp(appId),
    this.am.getPersistence(appId),
    );
    }
    }