Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ const usePublisher = (): PublisherContextType => {
return;
}

sessionPublish(publisherRef.current);
setIsPublishingToSession(true);
setIsPublishingToSession(true); // Avoid multiple simultaneous publish attempts
await sessionPublish(publisherRef.current);
} catch (err: unknown) {
if (err instanceof Error) {
console.warn(err.stack);
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/Context/SessionProvider/session.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('SessionProvider', () => {
const TestComponent = () => {
const {
activeSpeakerId,
publish,
unpublish,
joinRoom,
disconnect,
Expand All @@ -69,6 +70,17 @@ describe('SessionProvider', () => {

return (
<div>
<button
data-testid="publish"
onClick={() => {
if (publish) {
publish({} as unknown as Publisher);
}
}}
type="button"
>
Publish
</button>
<button
data-testid="unpublish"
onClick={() => {
Expand Down Expand Up @@ -143,6 +155,7 @@ describe('SessionProvider', () => {
};
vonageVideoClient = Object.assign(new EventEmitter(), {
unpublish: vi.fn(),
publish: vi.fn().mockResolvedValue(undefined),
connect: vi.fn().mockReturnValue(Promise.resolve()),
disconnect: vi.fn(),
forceMuteStream: vi.fn(),
Expand Down Expand Up @@ -197,6 +210,30 @@ describe('SessionProvider', () => {
await waitFor(() => expect(getByTestId('activeSpeaker')).toHaveTextContent('sub2'));
});

describe('publish', () => {
it('should call publish on VonageVideoClient when connected', async () => {
await act(async () => {
getByTestId('publish').click();
});

expect(vonageVideoClient.publish).toHaveBeenCalledTimes(1);
});

it('should not call publish on VonageVideoClient if not connected', async () => {
act(() => {
getByTestId('disconnect').click();
});

await waitFor(() => expect(getByTestId('connected')).toHaveTextContent('false'));

await act(async () => {
getByTestId('publish').click();
});

expect(vonageVideoClient.publish).toHaveBeenCalledTimes(0);
});
});

describe('unpublish', () => {
it('should call unpublish on VonageVideoClient', () => {
act(() => {
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/Context/SessionProvider/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,10 @@ const SessionProvider = ({ children }: SessionProviderProps): ReactElement => {
* @returns {Promise<void>} A promise that resolves when the stream is successfully published.
*/
const publish = useCallback(async (publisher: Publisher): Promise<void> => {
if (vonageVideoClient.current) {
vonageVideoClient.current.publish(publisher);
if (!vonageVideoClient.current) {
return;
}
await vonageVideoClient.current.publish(publisher);
}, []);

/**
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/useScreenShare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const useScreenShare = (): UseScreenShareType => {
});

// Publishing the screen sharing stream
publish(screenSharingPub.current);
await publish(screenSharingPub.current);

vonageVideoClient?.on('screenshareStreamCreated', handleStreamCreated);
} else if (screenSharingPub.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const cleanAndDedupeDeviceLabels = (
devices: (Device | AudioOutputDevice)[]
): Array<Device | AudioOutputDevice> => {
const labelCounts = new Map<string, number>();
console.log({ devices });

return devices.map((device) => {
if (!device.label) {
return device;
Expand Down
Loading