-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from splitio/new_SplitFactoryProvider_component
Add `SplitFactoryProvider` component and deprecate `SplitFactory` component and `withSplitFactory` HOC
- Loading branch information
Showing
29 changed files
with
734 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v16.16.0 | ||
lts/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from 'react'; | ||
|
||
import { SplitComponent } from './SplitClient'; | ||
import { ISplitFactoryProps } from './types'; | ||
import { WARN_SF_CONFIG_AND_FACTORY } from './constants'; | ||
import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, getStatus, __factories } from './utils'; | ||
import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient'; | ||
|
||
/** | ||
* SplitFactoryProvider will initialize the Split SDK and its main client when `config` prop is provided or updated, listen for its events in order to update the Split Context, | ||
* and automatically destroy the SDK (shutdown and release resources) when it is unmounted or `config` prop updated. SplitFactoryProvider must wrap other library components and | ||
* functions since they access the Split Context and its properties (factory, client, isReady, etc). | ||
* | ||
* NOTE: Either pass a factory instance or a config object. If both are passed, the config object will be ignored. | ||
* Pass the same reference to the config or factory object rather than a new instance on each render, to avoid unnecessary props changes and SDK reinitializations. | ||
* | ||
* @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client} | ||
*/ | ||
export function SplitFactoryProvider(props: ISplitFactoryProps) { | ||
let { | ||
config, factory: propFactory, | ||
updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate | ||
} = { ...DEFAULT_UPDATE_OPTIONS, ...props }; | ||
|
||
if (config && propFactory) { | ||
console.log(WARN_SF_CONFIG_AND_FACTORY); | ||
config = undefined; | ||
} | ||
|
||
const [configFactory, setConfigFactory] = React.useState<IFactoryWithClients | null>(null); | ||
const factory = propFactory || (configFactory && config === configFactory.config ? configFactory : null); | ||
const client = factory ? getSplitClient(factory) : null; | ||
|
||
// Effect to initialize and destroy the factory | ||
React.useEffect(() => { | ||
if (config) { | ||
const factory = getSplitFactory(config); | ||
|
||
return () => { | ||
destroySplitFactory(factory); | ||
} | ||
} | ||
}, [config]); | ||
|
||
// Effect to subscribe/unsubscribe to events | ||
React.useEffect(() => { | ||
const factory = config && __factories.get(config); | ||
if (factory) { | ||
const client = getSplitClient(factory); | ||
const status = getStatus(client); | ||
|
||
// Unsubscribe from events and update state when first event is emitted | ||
const update = () => { // eslint-disable-next-line no-use-before-define | ||
unsubscribe(); | ||
setConfigFactory(factory); | ||
} | ||
|
||
const unsubscribe = () => { | ||
client.off(client.Event.SDK_READY, update); | ||
client.off(client.Event.SDK_READY_FROM_CACHE, update); | ||
client.off(client.Event.SDK_READY_TIMED_OUT, update); | ||
client.off(client.Event.SDK_UPDATE, update); | ||
} | ||
|
||
if (updateOnSdkReady) { | ||
if (status.isReady) update(); | ||
else client.once(client.Event.SDK_READY, update); | ||
} | ||
if (updateOnSdkReadyFromCache) { | ||
if (status.isReadyFromCache) update(); | ||
else client.once(client.Event.SDK_READY_FROM_CACHE, update); | ||
} | ||
if (updateOnSdkTimedout) { | ||
if (status.hasTimedout) update(); | ||
else client.once(client.Event.SDK_READY_TIMED_OUT, update); | ||
} | ||
if (updateOnSdkUpdate) client.on(client.Event.SDK_UPDATE, update); | ||
|
||
return unsubscribe; | ||
} | ||
}, [config, updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate]); | ||
|
||
return ( | ||
<SplitComponent {...props} factory={factory} client={client} /> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.