Skip to content

Commit 145f554

Browse files
authored
Merge pull request #202 from ainblockchain/release/v1.12.0
Upgrade version to 1.12.0
2 parents 58b1ba7 + 2817608 commit 145f554

File tree

6 files changed

+61
-11
lines changed

6 files changed

+61
-11
lines changed

__tests__/event_manager.test.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Ain from '../src/ain';
33
import { FAILED_TO_REGISTER_ERROR_CODE } from '../src/constants';
44
import { FilterDeletionReasons, TransactionStates } from '../src/types';
5+
import { sleep } from './test_util';
56
const {
67
test_node_3,
78
test_event_handler_node,
@@ -10,11 +11,27 @@ const {
1011
jest.setTimeout(180000);
1112

1213
describe('Event Handler', function() {
13-
let ain = new Ain(test_node_3, test_event_handler_node);
14+
const ain = new Ain(test_node_3, test_event_handler_node);
1415
let eventFilterId: string;
16+
let connectionCount = 0;
17+
let disconnectionCount = 0;
18+
// Connection callback
19+
const connectionCb = (websocket) => {
20+
connectionCount++;
21+
};
22+
// Disconnection callback
23+
const disconnectionCb = (websocket) => {
24+
disconnectionCount++;
25+
};
1526

1627
beforeAll(async () => {
17-
await ain.em.connect();
28+
expect(connectionCount).toBe(0);
29+
expect(disconnectionCount).toBe(0);
30+
31+
await ain.em.connect(connectionCb, disconnectionCb);
32+
33+
expect(connectionCount).toBe(1);
34+
expect(disconnectionCount).toBe(0);
1835
});
1936

2037
afterEach(async () => {
@@ -25,8 +42,15 @@ describe('Event Handler', function() {
2542
});
2643
})
2744

28-
afterAll(() => {
45+
afterAll(async () => {
46+
expect(connectionCount).toBe(1);
47+
expect(disconnectionCount).toBe(0);
48+
2949
ain.em.disconnect();
50+
51+
await sleep(3000);
52+
expect(connectionCount).toBe(1);
53+
expect(disconnectionCount).toBe(1);
3054
});
3155

3256
describe('Channel connection', () => {

__tests__/test_util.ts

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export function eraseStateVersion(result) {
1414
return erased;
1515
}
1616

17+
export function sleep(ms) {
18+
return new Promise((resolve) => {
19+
setTimeout(resolve, ms);
20+
});
21+
};

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.11.0",
3+
"version": "1.12.0",
44
"description": "",
55
"main": "lib/ain.js",
66
"scripts": {

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
EventChannelMessageTypes,
77
EventChannelMessage,
88
BlockchainEventTypes,
9+
ConnectionCallback,
910
DisconnectionCallback,
1011
} from '../types';
1112
import EventFilter from './event-filter';
@@ -51,10 +52,11 @@ export default class EventChannelClient {
5152

5253
/**
5354
* Opens a new event channel.
55+
* @param {ConnectionCallback} connectionCallback The connection callback function.
5456
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
5557
* @returns {Promise<void>} A promise for the connection success.
5658
*/
57-
connect(disconnectionCallback?: DisconnectionCallback): Promise<any> {
59+
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback): Promise<any> {
5860
return new Promise(async (resolve, reject) => {
5961
if (this.isConnected) {
6062
reject(new Error(`Can't connect multiple channels`));
@@ -137,11 +139,16 @@ export default class EventChannelClient {
137139
}
138140
// Heartbeat timeout
139141
this.startHeartbeatTimer(DEFAULT_HEARTBEAT_INTERVAL_MS);
142+
// Connection callback
143+
if (connectionCallback) {
144+
connectionCallback(this._ws);
145+
}
140146
resolve(this);
141147
};
142148

143149
this._ws.onclose = () => {
144150
this.disconnect();
151+
// Disconnection callback
145152
if (disconnectionCallback) {
146153
disconnectionCallback(this._ws);
147154
}

src/event-manager/index.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import Ain from '../ain';
22
import {
3-
BlockFinalizedEventConfig, BlockFinalizedEvent,
3+
BlockFinalizedEventConfig,
4+
BlockFinalizedEvent,
45
ErrorFirstCallback,
5-
BlockchainEventConfig, BlockchainEventCallback,
6-
TxStateChangedEventConfig, TxStateChangedEvent,
7-
ValueChangedEventConfig, ValueChangedEvent, DisconnectionCallback, FilterDeletedEventCallback, BlockchainErrorCallback,
6+
BlockchainEventConfig,
7+
BlockchainEventCallback,
8+
TxStateChangedEventConfig,
9+
TxStateChangedEvent,
10+
ValueChangedEventConfig,
11+
ValueChangedEvent,
12+
ConnectionCallback,
13+
DisconnectionCallback,
14+
FilterDeletedEventCallback,
15+
BlockchainErrorCallback,
816
} from '../types';
917
import EventChannelClient from './event-channel-client';
1018
import EventCallbackManager from './event-callback-manager';
@@ -37,10 +45,11 @@ export default class EventManager {
3745

3846
/**
3947
* Opens a new event channel.
48+
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
4049
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
4150
*/
42-
async connect(disconnectionCallback?: DisconnectionCallback) {
43-
await this._eventChannelClient.connect(disconnectionCallback);
51+
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback) {
52+
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback);
4453
}
4554

4655
/**

src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ export type BlockchainErrorCallback = (error: any) => void;
494494
*/
495495
export type FilterDeletedEventCallback = (event: FilterDeletedEvent) => void;
496496

497+
/**
498+
* A type for connection callback functions (blockchain event handler).
499+
*/
500+
export type ConnectionCallback = (webSocket) => void;
501+
497502
/**
498503
* A type for disconnection callback functions (blockchain event handler).
499504
*/

0 commit comments

Comments
 (0)