Skip to content

Commit e1c5d17

Browse files
authored
Merge pull request #207 from ainblockchain/release/v1.13.0
Upgrade version to 1.13.0
2 parents 145f554 + 01589a9 commit e1c5d17

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

__tests__/event_manager.test.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.setTimeout(180000);
1212

1313
describe('Event Handler', function() {
1414
const ain = new Ain(test_node_3, test_event_handler_node);
15+
const customClientId = 'myCustomClientId';
1516
let eventFilterId: string;
1617
let connectionCount = 0;
1718
let disconnectionCount = 0;
@@ -28,7 +29,7 @@ describe('Event Handler', function() {
2829
expect(connectionCount).toBe(0);
2930
expect(disconnectionCount).toBe(0);
3031

31-
await ain.em.connect(connectionCb, disconnectionCb);
32+
await ain.em.connect(connectionCb, disconnectionCb, customClientId);
3233

3334
expect(connectionCount).toBe(1);
3435
expect(disconnectionCount).toBe(0);
@@ -59,6 +60,12 @@ describe('Event Handler', function() {
5960
});
6061
});
6162

63+
describe('Custom client id setting', () => {
64+
it('getCustomClientId()', async () => {
65+
expect(ain.em.getCustomClientId()).toBe(customClientId);
66+
});
67+
});
68+
6269
describe('BLOCK_FINALIZED', () => {
6370
it('Subscribe to BLOCK_FINALIZED', (done) => {
6471
eventFilterId = ain.em.subscribe('BLOCK_FINALIZED', {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ainblockchain/ain-js",
3-
"version": "1.12.0",
3+
"version": "1.13.0",
44
"description": "",
55
"main": "lib/ain.js",
66
"scripts": {

src/event-manager/event-channel-client.ts

+38-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default class EventChannelClient {
2727
private _ws?: WebSocket | WebSocketBE;
2828
/** Whether it's connected or not. */
2929
private _isConnected: boolean;
30+
/** The custom client id of the event channel. */
31+
private _customClientId: string;
3032
/** The handshake timeout object. */
3133
private _handshakeTimeout?: ReturnType<typeof setTimeout> | null;
3234
/** The heartbeat timeout object. */
@@ -42,6 +44,7 @@ export default class EventChannelClient {
4244
this._eventCallbackManager = eventCallbackManager;
4345
this._ws = undefined;
4446
this._isConnected = false;
47+
this._customClientId = '';
4548
this._handshakeTimeout = undefined;
4649
this._heartbeatTimeout = undefined;
4750
}
@@ -54,9 +57,10 @@ export default class EventChannelClient {
5457
* Opens a new event channel.
5558
* @param {ConnectionCallback} connectionCallback The connection callback function.
5659
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
60+
* @param {string} customClientId The custom client id to set.
5761
* @returns {Promise<void>} A promise for the connection success.
5862
*/
59-
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback): Promise<any> {
63+
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string): Promise<any> {
6064
return new Promise(async (resolve, reject) => {
6165
if (this.isConnected) {
6266
reject(new Error(`Can't connect multiple channels`));
@@ -139,6 +143,10 @@ export default class EventChannelClient {
139143
}
140144
// Heartbeat timeout
141145
this.startHeartbeatTimer(DEFAULT_HEARTBEAT_INTERVAL_MS);
146+
// Custom client id
147+
if (customClientId) {
148+
this.setCustomClientId(customClientId);
149+
}
142150
// Connection callback
143151
if (connectionCallback) {
144152
connectionCallback(this._ws);
@@ -291,30 +299,48 @@ export default class EventChannelClient {
291299
}
292300

293301
/**
294-
* Sends a register-event-filter messsage to the event channel.
302+
* Sends a SET_CUSTOM_CLIENT_ID messsage to the event channel.
303+
* @param {string} customClientId The custom client id to set.
304+
*/
305+
setCustomClientId(customClientId: string) {
306+
this._customClientId = customClientId;
307+
const data = { customClientId };
308+
const message = this.buildMessage(EventChannelMessageTypes.SET_CUSTOM_CLIENT_ID, data);
309+
this.sendMessage(message);
310+
}
311+
312+
/**
313+
* Returns the custom client id saved on the client side.
314+
*/
315+
getCustomClientId() {
316+
return this._customClientId;
317+
}
318+
319+
/**
320+
* Sends a REGISTER_FILTER messsage to the event channel.
295321
* @param {EventFilter} filter The event filter to register.
296322
*/
297323
registerFilter(filter: EventFilter) {
298-
const filterObj = filter.toObject();
299-
const registerMessage = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, filterObj);
300-
this.sendMessage(registerMessage);
324+
const data = filter.toObject();
325+
const message = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, data);
326+
this.sendMessage(message);
301327
}
302328

303329
/**
304-
* Sends a deregister-event-filter messsage to the event channel.
330+
* Sends a DEREGISTER_FILTER messsage to the event channel.
305331
* @param {EventFilter} filter The event filter to deregister.
306332
*/
307333
deregisterFilter(filter: EventFilter) {
308-
const filterObj = filter.toObject();
309-
const deregisterMessage = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, filterObj);
310-
this.sendMessage(deregisterMessage);
334+
const data = filter.toObject();
335+
const message = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, data);
336+
this.sendMessage(message);
311337
}
312338

313339
/**
314-
* Sends a pong message.
340+
* Sends a PONG message.
315341
*/
316342
sendPong() {
317-
const pongMessage = this.buildMessage(EventChannelMessageTypes.PONG, {});
318-
this.sendMessage(pongMessage);
343+
const message = this.buildMessage(EventChannelMessageTypes.PONG, {});
344+
this.sendMessage(message);
319345
}
320346
}

src/event-manager/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export default class EventManager {
4747
* Opens a new event channel.
4848
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
4949
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
50+
* @param {string} customClientId The custom client id to set.
5051
*/
51-
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback) {
52-
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback);
52+
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string) {
53+
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback, customClientId);
5354
}
5455

5556
/**
@@ -59,6 +60,13 @@ export default class EventManager {
5960
this._eventChannelClient.disconnect();
6061
}
6162

63+
/**
64+
* Returns the custom client id of the event channel saved on the client side.
65+
*/
66+
getCustomClientId() {
67+
return this._eventChannelClient.getCustomClientId();
68+
}
69+
6270
subscribe(
6371
eventType: 'BLOCK_FINALIZED',
6472
config: BlockFinalizedEventConfig,

src/types.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ export enum BlockchainEventTypes {
346346
* Event channel message types for blockchain event handler.
347347
*/
348348
export enum EventChannelMessageTypes {
349+
SET_CUSTOM_CLIENT_ID = 'SET_CUSTOM_CLIENT_ID',
349350
REGISTER_FILTER = 'REGISTER_FILTER',
350351
DEREGISTER_FILTER = 'DEREGISTER_FILTER',
351352
EMIT_EVENT = 'EMIT_EVENT',
@@ -497,9 +498,9 @@ export type FilterDeletedEventCallback = (event: FilterDeletedEvent) => void;
497498
/**
498499
* A type for connection callback functions (blockchain event handler).
499500
*/
500-
export type ConnectionCallback = (webSocket) => void;
501+
export type ConnectionCallback = (webSocket: any) => void;
501502

502503
/**
503504
* A type for disconnection callback functions (blockchain event handler).
504505
*/
505-
export type DisconnectionCallback = (webSocket) => void;
506+
export type DisconnectionCallback = (webSocket: any) => void;

0 commit comments

Comments
 (0)