Skip to content

SplitProvider - Functional and aware of config changes#2

Merged
samuelcastro merged 9 commits intosamuelcastro:masterfrom
CarsonF:functional-provider
Aug 22, 2019
Merged

SplitProvider - Functional and aware of config changes#2
samuelcastro merged 9 commits intosamuelcastro:masterfrom
CarsonF:functional-provider

Conversation

@CarsonF
Copy link
Copy Markdown
Contributor

@CarsonF CarsonF commented Aug 9, 2019

  • Changed client to be nullable.
    Now we don't have to fake a default client ({} as SplitIO.Client).
    This will prevent error if client is not defined and we try to call getTreatmentsWithConfig.
  • Changed lastUpdate to just be 0 instead of null for default.
    Since it is a unix timestamp check 0 functions fine.
  • I'm also setting lastUpdate to "now" when the client is ready, because I'm pretty sure that's the time it has the treatments loaded from server.
  • I'm re-creating the client if the config options have changed.
    By giving all the config options as a dependency list to useEffect.
  • Converted SplitProvider to a function component.

CarsonF added 3 commits August 9, 2019 12:29
…ill prevent error if client is not defined and we try to call getTreatmentsWithConfig.

Change lastUpdate to just be 0 instead of null for default value.
@CarsonF CarsonF mentioned this pull request Aug 9, 2019
Copy link
Copy Markdown
Owner

@samuelcastro samuelcastro left a comment

Choose a reason for hiding this comment

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

Looks great, just a few small details.

Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx Outdated
Comment thread src/SplitProvider.tsx
Comment thread src/SplitProvider.tsx Outdated
@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 16, 2019

Bleh I did a review instead of single comments 🤦‍♂

This removes the need for us to know about every single config option, which would be a pain to maintain.
This allows us to have an onImpression callback property which is idiomatic for React.

Also because if the function changed in the config the new function wouldn't be called,
because JSON.stringify ignores functions. This is would be confusing for users.
Also the fact that we are using JSON.stringify should be transparent,
a hidden implementation detail, to users.
@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 22, 2019

Ok I'm ready for another review @samuelcastro. I did everything we've talked about so far.

I also added custom handling for debug and impressionListener settings. You can see why in code/commits. If you don't like these or don't think it is necessary, then I can remove.

EDIT: I removed the debug special handling because it was destructuring the config property which would always give it a new identity.

…ether.

I think this is slightly more performant (one less render call).
@CarsonF CarsonF force-pushed the functional-provider branch from 974ff26 to 5ede2df Compare August 22, 2019 17:24
@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 22, 2019

I thought of another caveat. Since we are memoize the config object before JSON.stringify if the user tries to mutate the object the changes won't be applied. Mutating the same object isn't idiomatic for react, but it would be nice not to "do nothing". Maybe Object.freeze can be used...

@samuelcastro
Copy link
Copy Markdown
Owner

samuelcastro commented Aug 22, 2019

The changes looks great, I like the idea of freezing the object, this way users will know mutating is a bad idea. Or we could use immer and this mutation would never happen, however once again, I'm avoid unnecessary dependencies.

Copy link
Copy Markdown
Owner

@samuelcastro samuelcastro left a comment

Choose a reason for hiding this comment

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

🥇

@samuelcastro
Copy link
Copy Markdown
Owner

I'm going to release it as an alpha version after merge and then after make sure everything is good to go we can make a final release. If you want to test it as well I have a Code Sandbox live where we can easily test it: https://codesandbox.io/s/jovial-banzai-3bc81

@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 22, 2019

Ok I added the freeze call. I'm now thinking though that we need to tweak again.

<SplitProvider config={ core: { authorizationKey: '' } } />

This will run the recursive deep freeze and JSON.stringify on every render.
I'm not sure that's a good thing.

The only solution might be to tell users to memoize their config or define outside of component.

@samuelcastro
Copy link
Copy Markdown
Owner

samuelcastro commented Aug 22, 2019

Yeah I don't like idea either, maybe we should leave it as it is and just let users know mutation will produce unexpected results with the client not being re-created.

@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 22, 2019

I liked it more when I didn't think I had to implement recursion myself.

I still think we should tell users the best way to write it so we don't have to do JSON.stringify on every render. And if we are doing that, and assuming users follow it because they care about performance, then the freeze is also not called needlessly. In that case it would be safer to keep the freeze.

Comment thread README.md Outdated
@CarsonF CarsonF force-pushed the functional-provider branch from df4dd25 to 3977119 Compare August 22, 2019 19:14
@CarsonF CarsonF force-pushed the functional-provider branch from 3977119 to a45a85c Compare August 22, 2019 19:16
Copy link
Copy Markdown
Owner

@samuelcastro samuelcastro left a comment

Choose a reason for hiding this comment

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

Looks good.

@CarsonF
Copy link
Copy Markdown
Contributor Author

CarsonF commented Aug 22, 2019

Ship it! :shipit: 😁

I'll rebase the other PR next and update docs.

@samuelcastro samuelcastro merged commit 6580ad7 into samuelcastro:master Aug 22, 2019
@CarsonF CarsonF deleted the functional-provider branch August 22, 2019 19:52
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