Skip to content

Commit adf3e03

Browse files
committed
Make listener removal async (it will wait for the API call to complete)
1 parent a964274 commit adf3e03

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

src/PublicHelpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export const addContextMenuItems = async <IdT, EntityIdT extends EntityId | unde
143143
menu
144144
);
145145

146-
return () => {
147-
removeHook();
148-
removeListener();
146+
return async () => {
147+
await removeHook();
148+
await removeListener();
149149
};
150150
};

src/SocketSubscriptionHandler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const SocketSubscriptionHandler = (
4343
// Subscriptions pending to be added in the API
4444
const pendingSubscriptions: { [key: string]: PendingSubscription[] } = {};
4545

46-
const removeSocketListener = (
46+
const removeSocketListener = async (
4747
subscriptionUrl: string,
4848
subscriptionId: string,
4949
callback: Subscriptions.HookCallback<any, any> | Subscriptions.SubscriptionCallback<any>,
@@ -58,11 +58,12 @@ const SocketSubscriptionHandler = (
5858

5959
if (subscriptions[subscriptionId] === 0) {
6060
if (sendApi && socket().isConnected()) {
61-
socket().delete(subscriptionUrl)
62-
.catch(
63-
(error: Requests.ErrorResponse) => {
64-
logger.error('Failed to remove socket listener', subscriptionUrl, error);
65-
});
61+
try {
62+
await socket().delete(subscriptionUrl);
63+
} catch (error) {
64+
const e: Requests.ErrorResponse = error;
65+
logger.error('Failed to remove socket listener', subscriptionUrl, e);
66+
};
6667
}
6768

6869
delete subscriptions[subscriptionId];

src/tests/public_helpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ describe('public helpers', () => {
236236
}
237237

238238
// Remove items
239-
removeMenuItems();
239+
await removeMenuItems();
240240
expect(socket.hasListeners()).toBe(false);
241241
});
242242
});

src/types/subscriptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as API from './api.js';
22

33

44
// SUBSCRIPTIONS
5-
export type SubscriptionRemoveHandler = (sendApi?: boolean) => void;
5+
export type SubscriptionRemoveHandler = (sendApi?: boolean) => Promise<void>;
66

77
export type SubscriptionCallback<
88
DataT extends object | void = object,

0 commit comments

Comments
 (0)