-
Notifications
You must be signed in to change notification settings - Fork 186
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
base: v3
Are you sure you want to change the base?
Conversation
| const { refetch: refetchServiceTypes } = useServiceTypes({ | ||
| enabled: false, | ||
| }); | ||
|
|
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 main thing I noticed - listener cleanup, the listeners are being added, but I don't see them getting removed:
messenger.addListener({
type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});
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(() => {
const settingsListener = messenger.addListener({
type: 'SETTINGS_CHANGED',
onMessage: () => refetchSettings(),
});
const serviceListener = messenger.addListener({
type: 'SERVICE_ADDED',
onMessage: () => refetchServiceTypes(),
});
return () => {
messenger.removeListener(settingsListener);
messenger.removeListener(serviceListener);
};
}, [refetchSettings, refetchServiceTypes]);
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.
Good catch, I've updated the unregister method in messenger to clear the listeners.
|
|
||
| const { refetch: refetchSettings } = useSettings({ | ||
| enabled: false, | ||
| }); |
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.
| type: 'SETTINGS_CHANGED', | ||
| onMessage: () => refetchSettings(), | ||
| }); | ||
|
|
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.
A few other things to consider (not blockers, just ideas):
Error handling on refetch:
Right now if the refetch fails, it'll be silent. Maybe worth catching errors and showing a toast or logging them? Totally up to you though:
onMessage: async () => {
try {
await refetchSettings();
} catch (error) {
console.error('Failed to refetch settings:', error);
}
}
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.
I don't think there's a need to notify the user about it failing.
PMM-14655
FB: SUBMODULES-4167
Related PRs: