1 | ;
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.Events = void 0;
|
9 |
|
10 | var _eventemitter = _interopRequireDefault(require("eventemitter3"));
|
11 |
|
12 | // Copyright 2017-2022 @polkadot/api authors & contributors
|
13 | // SPDX-License-Identifier: Apache-2.0
|
14 | class Events {
|
15 | #eventemitter = new _eventemitter.default();
|
16 |
|
17 | emit(type) {
|
18 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
19 | args[_key - 1] = arguments[_key];
|
20 | }
|
21 |
|
22 | return this.#eventemitter.emit(type, ...args);
|
23 | }
|
24 | /**
|
25 | * @description Attach an eventemitter handler to listen to a specific event
|
26 | *
|
27 | * @param type The type of event to listen to. Available events are `connected`, `disconnected`, `ready` and `error`
|
28 | * @param handler The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments.
|
29 | *
|
30 | * @example
|
31 | * <BR>
|
32 | *
|
33 | * ```javascript
|
34 | * api.on('connected', (): void => {
|
35 | * console.log('API has been connected to the endpoint');
|
36 | * });
|
37 | *
|
38 | * api.on('disconnected', (): void => {
|
39 | * console.log('API has been disconnected from the endpoint');
|
40 | * });
|
41 | * ```
|
42 | */
|
43 |
|
44 |
|
45 | on(type, handler) {
|
46 | this.#eventemitter.on(type, handler);
|
47 | return this;
|
48 | }
|
49 | /**
|
50 | * @description Remove the given eventemitter handler
|
51 | *
|
52 | * @param type The type of event the callback was attached to. Available events are `connected`, `disconnected`, `ready` and `error`
|
53 | * @param handler The callback to unregister.
|
54 | *
|
55 | * @example
|
56 | * <BR>
|
57 | *
|
58 | * ```javascript
|
59 | * const handler = (): void => {
|
60 | * console.log('Connected !);
|
61 | * };
|
62 | *
|
63 | * // Start listening
|
64 | * api.on('connected', handler);
|
65 | *
|
66 | * // Stop listening
|
67 | * api.off('connected', handler);
|
68 | * ```
|
69 | */
|
70 |
|
71 |
|
72 | off(type, handler) {
|
73 | this.#eventemitter.removeListener(type, handler);
|
74 | return this;
|
75 | }
|
76 | /**
|
77 | * @description Attach an one-time eventemitter handler to listen to a specific event
|
78 | *
|
79 | * @param type The type of event to listen to. Available events are `connected`, `disconnected`, `ready` and `error`
|
80 | * @param handler The callback to be called when the event fires. Depending on the event type, it could fire with additional arguments.
|
81 | *
|
82 | * @example
|
83 | * <BR>
|
84 | *
|
85 | * ```javascript
|
86 | * api.once('connected', (): void => {
|
87 | * console.log('API has been connected to the endpoint');
|
88 | * });
|
89 | *
|
90 | * api.once('disconnected', (): void => {
|
91 | * console.log('API has been disconnected from the endpoint');
|
92 | * });
|
93 | * ```
|
94 | */
|
95 |
|
96 |
|
97 | once(type, handler) {
|
98 | this.#eventemitter.once(type, handler);
|
99 | return this;
|
100 | }
|
101 |
|
102 | }
|
103 |
|
104 | exports.Events = Events; |
\ | No newline at end of file |