UNPKG

2.65 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Events = void 0;
4const eventemitter3_1 = require("eventemitter3");
5class Events {
6 __internal__eventemitter = new eventemitter3_1.EventEmitter();
7 emit(type, ...args) {
8 return this.__internal__eventemitter.emit(type, ...args);
9 }
10 /**
11 * @description Attach an eventemitter handler to listen to a specific event
12 *
13 * @param type The type of event to listen to. Available events are `connected`, `disconnected`, `ready` and `error`
14 * @param handler The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments.
15 *
16 * @example
17 * <BR>
18 *
19 * ```javascript
20 * api.on('connected', (): void => {
21 * console.log('API has been connected to the endpoint');
22 * });
23 *
24 * api.on('disconnected', (): void => {
25 * console.log('API has been disconnected from the endpoint');
26 * });
27 * ```
28 */
29 on(type, handler) {
30 this.__internal__eventemitter.on(type, handler);
31 return this;
32 }
33 /**
34 * @description Remove the given eventemitter handler
35 *
36 * @param type The type of event the callback was attached to. Available events are `connected`, `disconnected`, `ready` and `error`
37 * @param handler The callback to unregister.
38 *
39 * @example
40 * <BR>
41 *
42 * ```javascript
43 * const handler = (): void => {
44 * console.log('Connected !);
45 * };
46 *
47 * // Start listening
48 * api.on('connected', handler);
49 *
50 * // Stop listening
51 * api.off('connected', handler);
52 * ```
53 */
54 off(type, handler) {
55 this.__internal__eventemitter.removeListener(type, handler);
56 return this;
57 }
58 /**
59 * @description Attach an one-time eventemitter handler to listen to a specific event
60 *
61 * @param type The type of event to listen to. Available events are `connected`, `disconnected`, `ready` and `error`
62 * @param handler The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments.
63 *
64 * @example
65 * <BR>
66 *
67 * ```javascript
68 * api.once('connected', (): void => {
69 * console.log('API has been connected to the endpoint');
70 * });
71 *
72 * api.once('disconnected', (): void => {
73 * console.log('API has been disconnected from the endpoint');
74 * });
75 * ```
76 */
77 once(type, handler) {
78 this.__internal__eventemitter.once(type, handler);
79 return this;
80 }
81}
82exports.Events = Events;