Skip to content

Commit 4204a35

Browse files
authored
feat: make sdk disconnected on Sendbird component umount (#636)
Made sdk.disconnect() is called when lib/Sendbird component is unmounted https://sendbird.atlassian.net/browse/UIKIT-4132
1 parent 7518a2a commit 4204a35

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/hooks/useUnmount.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useLayoutEffect } from 'react';
1+
import { DependencyList, useLayoutEffect } from 'react';
22

33
// this hook accepts a callback to run component is unmounted
4-
export function useUnmount(callback: () => void) {
4+
export function useUnmount(callback: () => void, deps: DependencyList = []) {
55
useLayoutEffect(() => {
66
return () => {
77
callback();
88
};
9-
}, []);
9+
}, deps);
1010
}

src/lib/Sendbird.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import { useMarkAsReadScheduler } from './hooks/useMarkAsReadScheduler';
3131
import { ConfigureSessionTypes } from './hooks/useConnect/types';
3232
import { useMarkAsDeliveredScheduler } from './hooks/useMarkAsDeliveredScheduler';
3333
import { getCaseResolvedReplyType, getCaseResolvedThreadReplySelectType } from './utils/resolvedReplyType';
34+
import { useUnmount } from '../hooks/useUnmount';
35+
import { disconnectSdk } from './hooks/useConnect/disconnectSdk';
3436

3537
export type UserListQueryType = {
3638
hasNext?: boolean;
@@ -192,6 +194,17 @@ const SendbirdSDK = ({
192194
initDashboardConfigs,
193195
});
194196

197+
useUnmount(() => {
198+
if (typeof sdk.disconnect === 'function') {
199+
disconnectSdk({
200+
logger,
201+
sdkDispatcher,
202+
userDispatcher,
203+
sdk,
204+
});
205+
}
206+
}, [sdk.disconnect]);
207+
195208
// to create a pubsub to communicate between parent and child
196209
useEffect(() => {
197210
setLogger(LoggerFactory(logLevel as LogLevel));

0 commit comments

Comments
 (0)