-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
108 lines (102 loc) · 4.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import Ain from '../ain';
import {
BlockFinalizedEventConfig, BlockFinalizedEvent,
ErrorFirstCallback,
BlockchainEventConfig, BlockchainEventCallback,
TxStateChangedEventConfig, TxStateChangedEvent,
ValueChangedEventConfig, ValueChangedEvent, DisconnectionCallback, FilterDeletedEventCallback, BlockchainErrorCallback,
} from '../types';
import EventChannelClient from './event-channel-client';
import EventCallbackManager from './event-callback-manager';
import EventFilter from './event-filter';
/**
* A class for managing blockchain events.
*/
export default class EventManager {
/** The event callback manager. */
private readonly _eventCallbackManager: EventCallbackManager;
/** The event channel client. */
private readonly _eventChannelClient: EventChannelClient;
/**
* Creates a new EventManager object.
* @param {Ain} ain The Ain object.
*/
constructor(ain: Ain) {
this._eventCallbackManager = new EventCallbackManager();
this._eventChannelClient = new EventChannelClient(ain, this._eventCallbackManager);
}
/**
* Opens a new event channel.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
*/
async connect(disconnectionCallback?: DisconnectionCallback) {
await this._eventChannelClient.connect(disconnectionCallback);
}
/**
* Closes the current event channel.
*/
disconnect() {
this._eventChannelClient.disconnect();
}
subscribe(
eventType: 'BLOCK_FINALIZED',
config: BlockFinalizedEventConfig,
eventCallback?: (event: BlockFinalizedEvent) => void,
errorCallback?: BlockchainErrorCallback
): string;
subscribe(
eventType: 'VALUE_CHANGED',
config: ValueChangedEventConfig,
eventCallback?: (event: ValueChangedEvent) => void,
errorCallback?: BlockchainErrorCallback
): string;
subscribe(
eventType: 'TX_STATE_CHANGED',
config: TxStateChangedEventConfig,
eventCallback?: (event: TxStateChangedEvent) => void,
errorCallback?: BlockchainErrorCallback,
filterDeletedEventCallback?: FilterDeletedEventCallback
): string;
/**
* Subscribes to blockchain events.
* @param {string} eventTypeStr The event type.
* @param {BlockchainEventConfig} config The blockchain event configuraiton.
* @param {BlockchainEventCallback} eventCallback The blockchain event callback function.
* @param {BlockchainErrorCallback} errorCallback The blockchain error callback function.
* @param {FilterDeletedEventCallback} filterDeletedEventCallback The filter-deleted event callback function.
* @returns {string} The created filter ID.
*/
subscribe(
eventTypeStr: string,
config: BlockchainEventConfig,
eventCallback?: BlockchainEventCallback,
errorCallback?: BlockchainErrorCallback,
filterDeletedEventCallback?: FilterDeletedEventCallback
): string {
if (!this._eventChannelClient.isConnected) {
throw Error(`Event channel is not connected! You must call ain.eh.connect() before using subscribe()`);
}
const filter = this._eventCallbackManager.createFilter(eventTypeStr, config);
this._eventChannelClient.registerFilter(filter);
this._eventCallbackManager.createSubscription(filter, eventCallback, errorCallback, filterDeletedEventCallback);
return filter.id;
}
/**
* Cancel a subscription.
* @param {string} filterId The filter ID of the subscription to cancel.
* @param {ErrorFirstCallback} callback The error handling callback function.
*/
unsubscribe(filterId: string, callback: ErrorFirstCallback<EventFilter>) {
try {
if (!this._eventChannelClient.isConnected) {
throw Error(`Event channel is not connected! You must call ain.eh.connect() before using unsubscribe()`);
}
const filter = this._eventCallbackManager.getFilter(filterId);
this._eventChannelClient.deregisterFilter(filter);
// NOTE(ehgmsdk20): This does not mean filter is deleted. It just means that delete request is successfully sent.
callback(null, filter);
} catch (err) {
callback(err, null);
}
}
}