Skip to content
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

fix: extract handlers and add cleanup function #96

Merged
merged 3 commits into from
Dec 15, 2022

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
Archived in project
Development

Successfully merging this pull request may close these issues.

Props of FlagProvider is not exported Leak in useFlag
3 participants