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.13.0 #207

Merged
merged 8 commits into from
Aug 12, 2024
9 changes: 8 additions & 1 deletion __tests__/event_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.setTimeout(180000);

describe('Event Handler', function() {
const ain = new Ain(test_node_3, test_event_handler_node);
const customClientId = 'myCustomClientId';
let eventFilterId: string;
let connectionCount = 0;
let disconnectionCount = 0;
Expand All @@ -28,7 +29,7 @@ describe('Event Handler', function() {
expect(connectionCount).toBe(0);
expect(disconnectionCount).toBe(0);

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

expect(connectionCount).toBe(1);
expect(disconnectionCount).toBe(0);
Expand Down Expand Up @@ -59,6 +60,12 @@ describe('Event Handler', function() {
});
});

describe('Custom client id setting', () => {
it('getCustomClientId()', async () => {
expect(ain.em.getCustomClientId()).toBe(customClientId);
});
});

describe('BLOCK_FINALIZED', () => {
it('Subscribe to BLOCK_FINALIZED', (done) => {
eventFilterId = ain.em.subscribe('BLOCK_FINALIZED', {
Expand Down
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.12.0",
"version": "1.13.0",
"description": "",
"main": "lib/ain.js",
"scripts": {
Expand Down
50 changes: 38 additions & 12 deletions src/event-manager/event-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default class EventChannelClient {
private _ws?: WebSocket | WebSocketBE;
/** Whether it's connected or not. */
private _isConnected: boolean;
/** The custom client id of the event channel. */
private _customClientId: string;
/** The handshake timeout object. */
private _handshakeTimeout?: ReturnType<typeof setTimeout> | null;
/** The heartbeat timeout object. */
Expand All @@ -42,6 +44,7 @@ export default class EventChannelClient {
this._eventCallbackManager = eventCallbackManager;
this._ws = undefined;
this._isConnected = false;
this._customClientId = '';
this._handshakeTimeout = undefined;
this._heartbeatTimeout = undefined;
}
Expand All @@ -54,9 +57,10 @@ export default class EventChannelClient {
* Opens a new event channel.
* @param {ConnectionCallback} connectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
* @param {string} customClientId The custom client id to set.
* @returns {Promise<void>} A promise for the connection success.
*/
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback): Promise<any> {
connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string): Promise<any> {
return new Promise(async (resolve, reject) => {
if (this.isConnected) {
reject(new Error(`Can't connect multiple channels`));
Expand Down Expand Up @@ -139,6 +143,10 @@ export default class EventChannelClient {
}
// Heartbeat timeout
this.startHeartbeatTimer(DEFAULT_HEARTBEAT_INTERVAL_MS);
// Custom client id
if (customClientId) {
this.setCustomClientId(customClientId);
}
// Connection callback
if (connectionCallback) {
connectionCallback(this._ws);
Expand Down Expand Up @@ -291,30 +299,48 @@ export default class EventChannelClient {
}

/**
* Sends a register-event-filter messsage to the event channel.
* Sends a SET_CUSTOM_CLIENT_ID messsage to the event channel.
* @param {string} customClientId The custom client id to set.
*/
setCustomClientId(customClientId: string) {
this._customClientId = customClientId;
const data = { customClientId };
const message = this.buildMessage(EventChannelMessageTypes.SET_CUSTOM_CLIENT_ID, data);
this.sendMessage(message);
}

/**
* Returns the custom client id saved on the client side.
*/
getCustomClientId() {
return this._customClientId;
}

/**
* Sends a REGISTER_FILTER messsage to the event channel.
* @param {EventFilter} filter The event filter to register.
*/
registerFilter(filter: EventFilter) {
const filterObj = filter.toObject();
const registerMessage = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, filterObj);
this.sendMessage(registerMessage);
const data = filter.toObject();
const message = this.buildMessage(EventChannelMessageTypes.REGISTER_FILTER, data);
this.sendMessage(message);
}

/**
* Sends a deregister-event-filter messsage to the event channel.
* Sends a DEREGISTER_FILTER messsage to the event channel.
* @param {EventFilter} filter The event filter to deregister.
*/
deregisterFilter(filter: EventFilter) {
const filterObj = filter.toObject();
const deregisterMessage = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, filterObj);
this.sendMessage(deregisterMessage);
const data = filter.toObject();
const message = this.buildMessage(EventChannelMessageTypes.DEREGISTER_FILTER, data);
this.sendMessage(message);
}

/**
* Sends a pong message.
* Sends a PONG message.
*/
sendPong() {
const pongMessage = this.buildMessage(EventChannelMessageTypes.PONG, {});
this.sendMessage(pongMessage);
const message = this.buildMessage(EventChannelMessageTypes.PONG, {});
this.sendMessage(message);
}
}
12 changes: 10 additions & 2 deletions src/event-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export default class EventManager {
* Opens a new event channel.
* @param {ConnectionCallback} ConnectionCallback The connection callback function.
* @param {DisconnectionCallback} disconnectionCallback The disconnection callback function.
* @param {string} customClientId The custom client id to set.
*/
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback) {
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback);
async connect(connectionCallback?: ConnectionCallback, disconnectionCallback?: DisconnectionCallback, customClientId?: string) {
await this._eventChannelClient.connect(connectionCallback, disconnectionCallback, customClientId);
}

/**
Expand All @@ -59,6 +60,13 @@ export default class EventManager {
this._eventChannelClient.disconnect();
}

/**
* Returns the custom client id of the event channel saved on the client side.
*/
getCustomClientId() {
return this._eventChannelClient.getCustomClientId();
}

subscribe(
eventType: 'BLOCK_FINALIZED',
config: BlockFinalizedEventConfig,
Expand Down
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ export enum BlockchainEventTypes {
* Event channel message types for blockchain event handler.
*/
export enum EventChannelMessageTypes {
SET_CUSTOM_CLIENT_ID = 'SET_CUSTOM_CLIENT_ID',
REGISTER_FILTER = 'REGISTER_FILTER',
DEREGISTER_FILTER = 'DEREGISTER_FILTER',
EMIT_EVENT = 'EMIT_EVENT',
Expand Down Expand Up @@ -497,9 +498,9 @@ export type FilterDeletedEventCallback = (event: FilterDeletedEvent) => void;
/**
* A type for connection callback functions (blockchain event handler).
*/
export type ConnectionCallback = (webSocket) => void;
export type ConnectionCallback = (webSocket: any) => void;

/**
* A type for disconnection callback functions (blockchain event handler).
*/
export type DisconnectionCallback = (webSocket) => void;
export type DisconnectionCallback = (webSocket: any) => void;
Loading