Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade version to 1.12.0 #202

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions __tests__/event_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Ain from '../src/ain';
import { FAILED_TO_REGISTER_ERROR_CODE } from '../src/constants';
import { FilterDeletionReasons, TransactionStates } from '../src/types';
import { sleep } from './test_util';
const {
test_node_3,
test_event_handler_node,
Expand All @@ -10,11 +11,27 @@ const {
jest.setTimeout(180000);

describe('Event Handler', function() {
let ain = new Ain(test_node_3, test_event_handler_node);
const ain = new Ain(test_node_3, test_event_handler_node);
let eventFilterId: string;
let connectionCount = 0;
let disconnectionCount = 0;
// Connection callback
const connectionCb = (websocket) => {
connectionCount++;
};
// Disconnection callback
const disconnectionCb = (websocket) => {
disconnectionCount++;
};

beforeAll(async () => {
await ain.em.connect();
expect(connectionCount).toBe(0);
expect(disconnectionCount).toBe(0);

await ain.em.connect(connectionCb, disconnectionCb);

expect(connectionCount).toBe(1);
expect(disconnectionCount).toBe(0);
});

afterEach(async () => {
Expand All @@ -25,8 +42,15 @@ describe('Event Handler', function() {
});
})

afterAll(() => {
afterAll(async () => {
expect(connectionCount).toBe(1);
expect(disconnectionCount).toBe(0);

ain.em.disconnect();

await sleep(3000);
expect(connectionCount).toBe(1);
expect(disconnectionCount).toBe(1);
});

describe('Channel connection', () => {
Expand Down
5 changes: 5 additions & 0 deletions __tests__/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export function eraseStateVersion(result) {
return erased;
}

export function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ainblockchain/ain-js",
"version": "1.11.0",
"version": "1.12.0",
"description": "",
"main": "lib/ain.js",
"scripts": {
Expand Down
9 changes: 8 additions & 1 deletion src/event-manager/event-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EventChannelMessageTypes,
EventChannelMessage,
BlockchainEventTypes,
ConnectionCallback,
DisconnectionCallback,
} from '../types';
import EventFilter from './event-filter';
Expand Down Expand Up @@ -51,10 +52,11 @@ export default class EventChannelClient {

/**
* Opens a new event channel.
* @param {ConnectionCallback} connectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
* @returns {Promise<void>} A promise for the connection success.
*/
connect(disconnectionCallback?: DisconnectionCallback): Promise<any> {
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback): Promise<any> {
return new Promise(async (resolve, reject) => {
if (this.isConnected) {
reject(new Error(`Can't connect multiple channels`));
Expand Down Expand Up @@ -137,11 +139,16 @@ export default class EventChannelClient {
}
// Heartbeat timeout
this.startHeartbeatTimer(DEFAULT_HEARTBEAT_INTERVAL_MS);
// Connection callback
if (connectionCallback) {
connectionCallback(this._ws);
}
resolve(this);
};

this._ws.onclose = () => {
this.disconnect();
// Disconnection callback
if (disconnectionCallback) {
disconnectionCallback(this._ws);
}
Expand Down
21 changes: 15 additions & 6 deletions src/event-manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import Ain from '../ain';
import {
BlockFinalizedEventConfig, BlockFinalizedEvent,
BlockFinalizedEventConfig,
BlockFinalizedEvent,
ErrorFirstCallback,
BlockchainEventConfig, BlockchainEventCallback,
TxStateChangedEventConfig, TxStateChangedEvent,
ValueChangedEventConfig, ValueChangedEvent, DisconnectionCallback, FilterDeletedEventCallback, BlockchainErrorCallback,
BlockchainEventConfig,
BlockchainEventCallback,
TxStateChangedEventConfig,
TxStateChangedEvent,
ValueChangedEventConfig,
ValueChangedEvent,
ConnectionCallback,
DisconnectionCallback,
FilterDeletedEventCallback,
BlockchainErrorCallback,
} from '../types';
import EventChannelClient from './event-channel-client';
import EventCallbackManager from './event-callback-manager';
Expand Down Expand Up @@ -37,10 +45,11 @@ export default class EventManager {

/**
* Opens a new event channel.
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
*/
async connect(disconnectionCallback?: DisconnectionCallback) {
await this._eventChannelClient.connect(disconnectionCallback);
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback) {
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ export type BlockchainErrorCallback = (error: any) => void;
*/
export type FilterDeletedEventCallback = (event: FilterDeletedEvent) => void;

/**
* A type for connection callback functions (blockchain event handler).
*/
export type ConnectionCallback = (webSocket) => void;

/**
* A type for disconnection callback functions (blockchain event handler).
*/
Expand Down
Loading