-
Notifications
You must be signed in to change notification settings - Fork 13
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
feat: add value-independent hook for setting online status message [LIBS-369] #1363
Conversation
This is technically a breaking change. IMO we should always bump the major version, even when we think no lib consumer will break. In case it does affects a consumer, it'd be a PITA to get to the bottom of this.. If you agree with this, we'll have two choices: Either separate that into a separate PR and discuss that particular change itself or make this PR introduce a breaking change. (I'm in favor of the former, but in this case it's such a small thing that I don't care too much) |
if (setOnlineStatusMessage === undefined) { | ||
throw new Error( | ||
'useSetOnlineStatusMessage must be used within an OnlineStatusMessageProvider' | ||
) | ||
} |
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 think setOnlineStatusMessage
will be the default value of the context - eg. ```() => undefined```` and not undefined if there's no context in the tree? But see my comment below, not sure if this is needed.
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.
Ah true - I admit that these checks are a bit of an automatic habit 🤦♂️ I'll remove them as you suggested in your other comment
// note: value is initialized to `null` in provider, not undefined | ||
if (onlineStatusMessage === undefined) { | ||
throw new Error( | ||
'useOnlineStatusMessageValue must be used within an OnlineStatusMessageProvider' | ||
) | ||
} |
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.
This could cause issues if you manually set the status to undefined
. Subsequent calls to this would then throw.
So if we were to do this, I think we should prevent undefined
being passed to setOnlineStatusMessage
. But that would definitely be a breaking change. However, if we just want to check that it's not the default, we could initialize it to a Symbol.
But this is all rendered in the app-shell, right? So this might just create more complexity than it's worth. And calling setOnlineStatusMessage
without a context is fine, since it's just a noop.
So in my opinion I think we could just remove these checks, which would work-around the "potential breaking change".
Initializing with null
or undefined
then doesn't matter, and was never part of the API contract. And no functionality would change. The only problem would be these checks if we kept it like this (mostly due to being able to set it as undefined
through setOnlineStatusMessage
.
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.
If we really wanted to fail early, I think it would look something like this:
const defaultOnlineStatusMessage = Symbol("defaultStatusMessage");
const OnlineStatusMessageValueContext = React.createContext<
ReactNode | typeof defaultOnlineStatusMessage
>(defaultOnlineStatusMessage);
export const useOnlineStatusMessageValue = () => {
const onlineStatusMessage = useContext(OnlineStatusMessageValueContext);
if (onlineStatusMessage === defaultOnlineStatusMessage) {
throw new Error(
"useOnlineStatusMessageValue must be used within an OnlineStatusMessageProvider"
);
}
// this should be inferred to be a ReactNode
return onlineStatusMessage;
};
useSetOnlineStatusMessage
, would be similiar but extracting the "noop" default function to variable and check for that equality.
I think this could be done without a breaking change, since you would keep the public API the same.
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.
That's a solid solution, but I just went with removing the undefined checks to keep it simple -- I think you're right that the app adapter kinda guarantees the provider will be there
# [3.10.0](v3.9.4...v3.10.0) (2023-12-12) ### Features * add value-independent hook for setting online status message [LIBS-369] ([#1363](#1363)) ([a2831e6](a2831e6))
🎉 This PR is included in version 3.10.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Implements LIBS-369
Key features
Adds individual hooks and contexts for getting and setting the online status message. As such, consumers of
useSetOnlineStatusMessage
won't need to rerender when the message value changesuseOnlineStatusMessageValue()
useSetOnlineStatusMessage()
Notes
feat
because the existinguseOnlineStatusMessage
hook is relatively unchanged in behavior, but there are now two more optimal hooks availableset
function won't cause unnecessary rerenders)Checklist