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

Add customClientId param to event channel connect() #206

Merged
merged 1 commit into from
Aug 12, 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
5 changes: 2 additions & 3 deletions __tests__/event_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,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 @@ -61,8 +61,7 @@ describe('Event Handler', function() {
});

describe('Custom client id setting', () => {
it('setCustomClientId() / getCustomClientId()', async () => {
await ain.em.setCustomClientId(customClientId);
it('getCustomClientId()', async () => {
expect(ain.em.getCustomClientId()).toBe(customClientId);
});
});
Expand Down
7 changes: 6 additions & 1 deletion src/event-manager/event-channel-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,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 @@ -142,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
13 changes: 3 additions & 10 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,14 +60,6 @@ export default class EventManager {
this._eventChannelClient.disconnect();
}

/**
* Sets the custom client id of the event channel.
* @param {string} customClientId The custom client id.
*/
setCustomClientId(customClientId: string) {
this._eventChannelClient.setCustomClientId(customClientId);
}

/**
* Returns the custom client id of the event channel saved on the client side.
*/
Expand Down
Loading