-
Notifications
You must be signed in to change notification settings - Fork 13k
fix: 'room-changed' event race condition #35371
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
69e0cbe
refactor fireGlobalEvent
gabriellsh a4b089d
implement useFireGlobalEvent
gabriellsh 27ff072
fire event from roomprovider
gabriellsh 6fd6d4d
Create odd-squids-repeat.md
gabriellsh c94c232
Update apps/meteor/client/hooks/useFireGlobalEvent.spec.ts
gabriellsh efea3e4
Merge branch 'develop' into fix/room-opened
kodiakhq[bot] a1d4e46
Merge branch 'develop' into fix/room-opened
kodiakhq[bot] 5771d5d
Merge branch 'develop' into fix/room-opened
kodiakhq[bot] ab6bf4d
Merge branch 'develop' into fix/room-opened
gabriellsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@rocket.chat/meteor": patch | ||
| --- | ||
|
|
||
| Fixes an issue with `room-changed` event not being fired properly when switching between rooms that are available on cache. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
|
|
||
| import { useFireGlobalEvent } from './useFireGlobalEvent'; | ||
| import { fireGlobalEventBase } from '../lib/utils/fireGlobalEventBase'; | ||
|
|
||
| jest.mock('../lib/utils/fireGlobalEventBase', () => ({ | ||
| fireGlobalEventBase: jest.fn(() => () => undefined), | ||
| })); | ||
|
|
||
| const fireGlobalMock = fireGlobalEventBase as jest.MockedFunction<typeof fireGlobalEventBase>; | ||
|
|
||
| describe('useFireGlobalEvent', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should dispatch event only once if scope is defined', async () => { | ||
| const scope = 'scope'; | ||
| const { result } = renderHook(({ scope }) => useFireGlobalEvent('room-opened', scope), { | ||
| initialProps: { scope }, | ||
|
|
||
| wrapper: mockAppRoot() | ||
| .withSetting('Iframe_Integration_send_enable', true) | ||
| .withSetting('Iframe_Integration_send_target_origin', '') | ||
| .build(), | ||
| }); | ||
| result.current.mutate(null); | ||
|
|
||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
|
|
||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
|
|
||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
|
|
||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should dispatch event only once for each (eventName/scope)', async () => { | ||
| const { result, rerender } = renderHook(({ scope }) => useFireGlobalEvent('room-opened', scope), { | ||
| initialProps: { scope: 'scope' }, | ||
| wrapper: mockAppRoot() | ||
| .withSetting('Iframe_Integration_send_enable', true) | ||
| .withSetting('Iframe_Integration_send_target_origin', '') | ||
| .build(), | ||
| }); | ||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| rerender({ scope: 'another' }); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(2); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('should dispatch event multiple times if scope is not defined', async () => { | ||
| const { result } = renderHook(() => useFireGlobalEvent('room-opened'), { | ||
| wrapper: mockAppRoot() | ||
| .withSetting('Iframe_Integration_send_enable', true) | ||
| .withSetting('Iframe_Integration_send_target_origin', '') | ||
| .build(), | ||
| }); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(2); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(3); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(4); | ||
| }); | ||
|
|
||
| it('should pass required settings to postMessage', async () => { | ||
| const { result } = renderHook(() => useFireGlobalEvent('room-opened'), { | ||
| wrapper: mockAppRoot() | ||
| .withSetting('Iframe_Integration_send_enable', true) | ||
| .withSetting('Iframe_Integration_send_target_origin', 'origin') | ||
| .build(), | ||
| }); | ||
|
|
||
| const postMessage = jest.fn(); | ||
|
|
||
| fireGlobalMock.mockImplementation(() => postMessage); | ||
|
|
||
| result.current.mutate(null); | ||
| await waitFor(() => expect(result.current.status).toBe('success')); | ||
| expect(fireGlobalMock).toHaveBeenCalledTimes(1); | ||
| expect(postMessage).toHaveBeenCalledWith(true, 'origin'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { useSetting } from '@rocket.chat/ui-contexts'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| import { fireGlobalEventBase } from '../lib/utils/fireGlobalEventBase'; | ||
|
|
||
| const getScopeForEvent = (eventName: string, scope?: string) => (scope ? `${eventName}/${scope}` : eventName); | ||
|
|
||
| export const useFireGlobalEvent = (eventName: string, scope?: string) => { | ||
| const sendEnabled = useSetting('Iframe_Integration_send_enable'); | ||
| const origin = useSetting('Iframe_Integration_send_target_origin'); | ||
|
|
||
| const dispatchedRef = useRef({ scope: getScopeForEvent(eventName, scope), dispatched: false }); | ||
|
|
||
| useEffect(() => { | ||
| const newScope = getScopeForEvent(eventName, scope); | ||
| if (dispatchedRef.current?.scope !== newScope) { | ||
| dispatchedRef.current = { scope: newScope, dispatched: false }; | ||
| } | ||
| }, [scope, eventName]); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: async (data?: unknown) => { | ||
| if (scope && dispatchedRef.current.dispatched) { | ||
| return; | ||
| } | ||
|
|
||
| const postMessage = fireGlobalEventBase(eventName, data); | ||
| postMessage(sendEnabled as boolean, origin as string); | ||
| dispatchedRef.current.dispatched = true; | ||
| }, | ||
| scope: scope ? { id: getScopeForEvent(eventName, scope) } : undefined, | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { fireGlobalEventBase } from './fireGlobalEventBase'; | ||
|
|
||
| const postMessageMock = jest.fn(); | ||
| const dispatchEventMock = jest.fn(); | ||
|
|
||
| const originalDispatch = window.dispatchEvent; | ||
| const originalPostMessage = parent.postMessage; | ||
|
|
||
| beforeAll(() => { | ||
| window.dispatchEvent = dispatchEventMock; | ||
| parent.postMessage = postMessageMock; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| postMessageMock.mockClear(); | ||
| dispatchEventMock.mockClear(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| window.dispatchEvent = originalDispatch; | ||
| parent.postMessage = originalPostMessage; | ||
| }); | ||
|
|
||
| it('should dispatch event but not post message', () => { | ||
| const detail = 'test-detail'; | ||
| const postMessage = fireGlobalEventBase('test-event', detail); | ||
| postMessage(false, ''); | ||
|
|
||
| expect(postMessageMock).not.toHaveBeenCalled(); | ||
|
|
||
| expect(dispatchEventMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| const result = dispatchEventMock.mock.lastCall[0]; | ||
| expect(result).toBeInstanceOf(CustomEvent); | ||
| expect(result.detail).toBe(detail); | ||
| expect(result.type).toBe('test-event'); | ||
| }); | ||
|
|
||
| it('should dispatch event and post message', () => { | ||
| const detail = 'test-detail'; | ||
| const origin = 'test-origin'; | ||
| const postMessage = fireGlobalEventBase('test-event', detail); | ||
| postMessage(true, origin); | ||
|
|
||
| expect(postMessageMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| expect(dispatchEventMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| const dispatchResult = dispatchEventMock.mock.lastCall[0]; | ||
| expect(dispatchResult).toBeInstanceOf(CustomEvent); | ||
| expect(dispatchResult.detail).toBe(detail); | ||
| expect(dispatchResult.type).toBe('test-event'); | ||
|
|
||
| const [postEventResult, originResult] = postMessageMock.mock.lastCall; | ||
| expect(postEventResult).toBeInstanceOf(Object); | ||
| expect(originResult).toBe(origin); | ||
| expect(postEventResult.data).toBe(detail); | ||
| expect(postEventResult.eventName).toBe('test-event'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export const fireGlobalEventBase = (eventName: string, detail?: unknown) => { | ||
| window.dispatchEvent(new CustomEvent(eventName, { detail })); | ||
|
|
||
| const dispatchMessage = (iframeSendEnabled: boolean, sendTargetOrigin: string) => { | ||
| if (!iframeSendEnabled) { | ||
| return; | ||
| } | ||
| parent.postMessage( | ||
| { | ||
| eventName, | ||
| data: detail, | ||
| }, | ||
| sendTargetOrigin, | ||
| ); | ||
| }; | ||
|
|
||
| return dispatchMessage; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.