Skip to content

Commit

Permalink
Merge pull request #50 from notificationapi-com/IepczHxO/2017-websock…
Browse files Browse the repository at this point in the history
…et-connection-for-getuserpreferences

fix: WebSocket connection for getUserPreferences
  • Loading branch information
mbasadi authored May 22, 2024
2 parents cb02900 + de3c005 commit e75712f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notificationapi-js-client-sdk",
"version": "4.6.2",
"version": "4.6.3",
"description": "NotificationAPI client-side library for JavaScript",
"keywords": [
"notificationapi",
Expand Down
20 changes: 17 additions & 3 deletions src/__tests__/getUserPreferences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('given connection is already open', () => {
await server.connected;
await server.nextMessage; // environment/data request
});

test('sends a user_preferences/preferences message', async () => {
notificationapi.getUserPreferences();
const request: WS_UserPreferencesRequest = {
Expand Down Expand Up @@ -98,14 +99,27 @@ describe('given connection is not open yet', () => {
});

describe('given no websocket', () => {
test('rejects the promise', async () => {
test('rejects the promise when websocket is not present', async () => {
notificationapi = new NotificationAPI({
clientId: '',
userId: '',
websocket: false
});
notificationapi.getUserPreferences().catch((error) => {
expect(error).toEqual('Websocket is not present.');
await expect(notificationapi.getUserPreferences()).rejects.toEqual(
'Websocket is not present.'
);
});
test('rejects the promise when websocket fails to open', async () => {
// Mocking a websocket connection that will not open
server.error(); // Simulate an error that prevents websocket from opening
notificationapi = new NotificationAPI({
clientId,
userId,
websocket: 'ws://localhost:1235'
});
// Wait for the getUserPreferences method to be invoked and handle the rejection
await expect(notificationapi.getUserPreferences()).rejects.toEqual(
'Websocket failed to open.'
);
});
});
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1307,13 +1307,26 @@ class NotificationAPIClient implements NotificationAPIClientInterface {

websocketOpened(): Promise<WebSocket> {
return new Promise((resolve, reject) => {
if (!this.websocket) reject('Websocket is not present.');
else {
const ws = this.websocket;
if (ws.readyState == ws.OPEN) {
const ws = this.websocket;
if (!ws) {
reject('Websocket is not present.');
return;
}

const checkState = () => {
if (ws.readyState === ws.OPEN) {
resolve(ws);
} else if (
ws.readyState === ws.CLOSED ||
ws.readyState === ws.CLOSING
) {
reject('Websocket failed to open.');
} else {
setTimeout(checkState, 100); // Check again in 100ms
}
}
};

checkState();
});
}
}
Expand Down

0 comments on commit e75712f

Please sign in to comment.