Skip to content

Conversation

@FredrikOseberg
Copy link
Contributor

About the changes

This PR extracts handlers into their own functions and adds a returning function in the useEffect that cleans up the event listeners on unmount.

Closes #90
Closes #91

Important files

Discussion points

@kwasniew kwasniew self-requested a review December 15, 2022 07:57
Copy link
Contributor

@thomasheartman thomasheartman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This looks great to me 😄 Just one small question: It was pointed out in this comment on #90, that there might be a bug in checking the flag and having different handlers for ready and update:

I think there's actually a second (unrelated) small bug where the value of flagRef isn't being updated in the ready function. I think the impact would be that if a useFlag is used before the client is ready, and the flag is currently turned ON, then it won't respond if the flag later gets turned OFF (unless it's turned OFF then ON then OFF again).

It's then suggested to instead do this:

  useEffect(() => {
    if (!client) return;
    const updateFn = () => {
      const enabled = isEnabled(name);
      if (enabled !== flagRef.current) {
        flagRef.current = enabled;
        setFlag(enabled);
      }
    };
    client.on('update', updateFn);
    client.on('ready', updateFn);

    return () => {
      client.off('update', updateFn);
      client.off('ready', updateFn);
    };
  }, [client]);

and (from what I can tell) use the same function for both events. What do you think about that? Is that an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Props of FlagProvider is not exported Leak in useFlag

3 participants