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

chore: Provider and hooks #306

Closed
wants to merge 5 commits into from

Conversation

yusinto
Copy link
Contributor

@yusinto yusinto commented Oct 23, 2023

  • Added LDProvider
  • Added hooks
  • Added typed variation[Detail] functions

Please try and run the app, following the README under sdk/react-native to see if this is better than the existing legacy version in terms of brokenness.

@shortcut-integration
Copy link

This pull request has been linked to Shortcut Story #221870: Implement RN provider api.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ordered the props in this interface alphabetically automatically resulting in an ugly diff. The only significant changes are the additions of typed variation functions.

Comment on lines +5 to +8
type ReactSdkContext = {
allFlags?: LDFlagSet;
ldClient?: LDClient;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still in flux and will change to have more/less things exposed.

@@ -0,0 +1,13 @@
import useLDClient from './useLDClient';

export const useBoolVariation = (flagKey: string, defaultValue: boolean) => {
Copy link
Member

Choose a reason for hiding this comment

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

I think these will be very nice for consistent experiment results. (Aside from also allow type safety, which is nice.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Thank you for pushing for these!


export const useBoolVariationDetail = (flagKey: string, defaultValue: boolean) => {
const ldClient = useLDClient();
return ldClient?.boolVariationDetail(flagKey, defaultValue) ?? defaultValue;
Copy link
Member

Choose a reason for hiding this comment

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

Because detailed variations return objects we need to ensure we keep stable object references. Which may happen for flags that exist, because we might be returning a value we have stored.

I am concerned that the variation for a default value will not be stable. Which could lead to the hook triggering more re-renders than desired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok this is definitely a bug. What do you think of a fix like this:

Suggested change
return ldClient?.boolVariationDetail(flagKey, defaultValue) ?? defaultValue;
const def = {
value: defaultValue,
variationIndex: null,
reason: null,
};
return ldClient?.boolVariationDetail(flagKey, defaultValue) ?? def;

Copy link
Member

Choose a reason for hiding this comment

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

So, this fixes a case where the ldclient isn't initialized to have a consistent return.

It doesn't affect the stability of the reference returned by the hook. I think you would need to make a memo.

@yusinto
Copy link
Contributor Author

yusinto commented Oct 25, 2023

I'm closing this PR to make improvements to the provider, hook and error apis.

@yusinto yusinto closed this Oct 25, 2023
yusinto added a commit that referenced this pull request Nov 30, 2023
This is a second attempt of #306 using the newly added event source.

- use StreamingProcessor and event source to fetch flags
- added LDProvider
- added hooks
- added typed variation[Detail] functions

Please read the [example
README](https://github.com/launchdarkly/js-core/blob/9d458f28625c12169df614c8d2289f9c0952cc45/packages/sdk/react-native/example/README.md#L1)
and try running the example app.

---------

Co-authored-by: LaunchDarklyReleaseBot <[email protected]>
Co-authored-by: Ryan Lamb <[email protected]>
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.

2 participants