-
Notifications
You must be signed in to change notification settings - Fork 188
PMM-14655 Add listeners for settings change and service added #4858
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
Changes from all commits
9917c4d
4849160
d237ebe
b77da3d
688281f
47645b2
2efd0ef
ae1c7c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @fileoverview | ||
| * Same events as in public/app/percona/shared/core/events.ts in grafana repository | ||
| */ | ||
|
|
||
| import { BusEventBase } from '@grafana/data'; | ||
|
|
||
| export class SettingsUpdatedEvent extends BusEventBase { | ||
| static type = 'settings-updated-event'; | ||
| } | ||
|
|
||
| export class ServiceAddedEvent extends BusEventBase { | ||
| static type = 'service-added-event'; | ||
| } | ||
|
|
||
| export class ServiceDeletedEvent extends BusEventBase { | ||
| static type = 'service-deleted-event'; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ import { useKioskMode } from 'hooks/utils/useKioskMode'; | |
| import { useColorMode } from 'hooks/theme'; | ||
| import { getLocationUrl } from './grafana.utils'; | ||
| import messenger from 'lib/messenger'; | ||
| import { useSettings } from 'hooks/api/useSettings'; | ||
| import { useServiceTypes } from 'hooks/api/useServices'; | ||
|
|
||
| /** Guard DOM usage. */ | ||
| const isBrowser = () => | ||
|
|
@@ -34,6 +36,13 @@ export const GrafanaProvider: FC<PropsWithChildren> = ({ children }) => { | |
| const location = useLocation(); | ||
| const navigate = useNavigate(); | ||
|
|
||
| const { refetch: refetchSettings } = useSettings({ | ||
| enabled: false, | ||
| }); | ||
| const { refetch: refetchServiceTypes } = useServiceTypes({ | ||
| enabled: false, | ||
| }); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main thing I noticed - listener cleanup, the listeners are being added, but I don't see them getting removed: messenger.addListener({ This could cause a memory leak if the component remounts (like during navigation) - you'd keep stacking up listeners. The fix is pretty simple, though, just need to return a cleanup function: useEffect(() => { }, [refetchSettings, refetchServiceTypes]);
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I've updated the unregister method in messenger to clear the listeners. |
||
| const src = location.pathname.replace(PMM_NEW_NAV_PATH, ''); | ||
| const isGrafanaPage = src.startsWith(GRAFANA_SUB_PATH); | ||
|
|
||
|
|
@@ -95,10 +104,27 @@ export const GrafanaProvider: FC<PropsWithChildren> = ({ children }) => { | |
| }, | ||
| }); | ||
|
|
||
| messenger.addListener({ | ||
| type: 'SETTINGS_CHANGED', | ||
| onMessage: () => refetchSettings(), | ||
| }); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few other things to consider (not blockers, just ideas): Error handling on refetch: onMessage: async () => {
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there's a need to notify the user about it failing. |
||
| messenger.addListener({ | ||
| type: 'SERVICE_ADDED', | ||
| onMessage: () => refetchServiceTypes(), | ||
| }); | ||
|
|
||
| messenger.addListener({ | ||
| type: 'SERVICE_DELETED', | ||
| onMessage: () => refetchServiceTypes(), | ||
| }); | ||
|
|
||
| // Cleanup once provider unmounts | ||
| return () => { | ||
| messenger.unregister(); | ||
| }; | ||
|
|
||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [isLoaded, setFromGrafana, navigate]); | ||
|
|
||
| // -------- OUTGOING TO GRAFANA -------- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question about initial data loading
I see you're creating these queries with enabled: false:
const { refetch: refetchSettings } = useSettings({
enabled: false,
});
Just want to make sure I understand - are these queries being run somewhere else on initial load? If not, the UI might not have this data until the first event comes in. Might be worth a quick double-check, or if it's intentional, maybe add a comment so future devs know what's up ))))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial fetch of settings is in
SettingsProvider. This is just used for refetching when triggered from the Grafana side.