Skip to content

Commit 327a8da

Browse files
Bugfix: hook doesn't re-render when an SDK event is emitted between the hook call and the internal effect execution.
1 parent c22e8cd commit 327a8da

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
1.10.1 (November 21, 2023)
2+
- Bugfixing - Resolved an issue with `useSplitClient` hook, which was not re-rendering when an SDK client event was emitted between the hook's call and the execution of its internal effect.
3+
14
1.10.0 (November 16, 2023)
25
- Added support for Flag Sets on the SDK, which enables grouping feature flags and interacting with the group rather than individually (more details in our documentation):
36
- Added a new `flagSets` prop to the `SplitTreatments` component and `flagSets` option to the `useSplitTreatments` hook options object, to support evaluating flags in given flag set/s. Either `names` or `flagSets` must be provided to the component and hook. If both are provided, `names` will be used.

src/SplitClient.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,21 @@ export class SplitComponent extends React.Component<IUpdateProps & { factory: Sp
6363
// The listeners take into account the value of `updateOnSdk***` props.
6464
subscribeToEvents(client: SplitIO.IBrowserClient | null) {
6565
if (client) {
66-
const status = getStatus(client);
67-
if (this.props.updateOnSdkReady && !status.isReady) client.once(client.Event.SDK_READY, this.update);
68-
if (this.props.updateOnSdkReadyFromCache && !status.isReadyFromCache) client.once(client.Event.SDK_READY_FROM_CACHE, this.update);
69-
if (this.props.updateOnSdkTimedout && !status.hasTimedout) client.once(client.Event.SDK_READY_TIMED_OUT, this.update);
66+
const statusOnEffect = getStatus(client);
67+
const status = this.state;
68+
69+
if (this.props.updateOnSdkReady) {
70+
if (!statusOnEffect.isReady) client.once(client.Event.SDK_READY, this.update);
71+
else if (!status.isReady) this.update();
72+
}
73+
if (this.props.updateOnSdkReadyFromCache) {
74+
if (!statusOnEffect.isReadyFromCache) client.once(client.Event.SDK_READY_FROM_CACHE, this.update);
75+
else if (!status.isReadyFromCache) this.update();
76+
}
77+
if (this.props.updateOnSdkTimedout) {
78+
if (!statusOnEffect.hasTimedout) client.once(client.Event.SDK_READY_TIMED_OUT, this.update);
79+
else if (!status.hasTimedout) this.update();
80+
}
7081
if (this.props.updateOnSdkUpdate) client.on(client.Event.SDK_UPDATE, this.update);
7182
}
7283
}

src/useSplitClient.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ export function useSplitClient(options?: IUseSplitClientOptions): ISplitContextV
4747
const update = () => setLastUpdate(client.lastUpdate);
4848

4949
// Subscribe to SDK events
50-
const status = getStatus(client);
51-
if (updateOnSdkReady && !status.isReady) client.once(client.Event.SDK_READY, update);
52-
if (updateOnSdkReadyFromCache && !status.isReadyFromCache) client.once(client.Event.SDK_READY_FROM_CACHE, update);
53-
if (updateOnSdkTimedout && !status.hasTimedout) client.once(client.Event.SDK_READY_TIMED_OUT, update);
50+
const statusOnEffect = getStatus(client); // Effect call is not synchronous, so the status may have changed
51+
52+
if (updateOnSdkReady) {
53+
if (!statusOnEffect.isReady) client.once(client.Event.SDK_READY, update);
54+
else if (!status.isReady) update();
55+
}
56+
if (updateOnSdkReadyFromCache) {
57+
if (!statusOnEffect.isReadyFromCache) client.once(client.Event.SDK_READY_FROM_CACHE, update);
58+
else if (!status.isReadyFromCache) update();
59+
}
60+
if (updateOnSdkTimedout) {
61+
if (!statusOnEffect.hasTimedout) client.once(client.Event.SDK_READY_TIMED_OUT, update);
62+
else if (!status.hasTimedout) update();
63+
}
5464
if (updateOnSdkUpdate) client.on(client.Event.SDK_UPDATE, update);
5565

5666
return () => {
@@ -60,7 +70,7 @@ export function useSplitClient(options?: IUseSplitClientOptions): ISplitContextV
6070
client.off(client.Event.SDK_READY_TIMED_OUT, update);
6171
client.off(client.Event.SDK_UPDATE, update);
6272
}
63-
}, [client, updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate]);
73+
}, [client, updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate, status]);
6474

6575
return {
6676
factory, client, ...status

0 commit comments

Comments
 (0)