Skip to content

Commit f23f0a4

Browse files
Upgrade JS SDK to v11, which has clients without bound traffic type
1 parent e8e0d2a commit f23f0a4

11 files changed

+48
-69
lines changed

package-lock.json

+17-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
"homepage": "https://github.com/splitio/react-client#readme",
6565
"dependencies": {
66-
"@splitsoftware/splitio": "10.28.1-rc.2",
66+
"@splitsoftware/splitio": "11.0.0-rc.1",
6767
"memoize-one": "^5.1.1",
6868
"shallowequal": "^1.1.0",
6969
"tslib": "^2.3.1"

src/SplitClient.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useSplitClient } from './useSplitClient';
88
* Children components will have access to the new client when accessing Split Context.
99
*
1010
* The underlying SDK client can be changed during the component lifecycle
11-
* if the component is updated with a different splitKey or trafficType prop.
11+
* if the component is updated with a different splitKey prop.
1212
*
1313
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-instantiate-multiple-sdk-clients}
1414
*/

src/__tests__/SplitClient.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ describe('SplitClient', () => {
344344
function Component({ attributesFactory, attributesClient, splitKey, testSwitch, factory }: TestComponentProps) {
345345
return (
346346
<SplitFactoryProvider factory={factory} attributes={attributesFactory} >
347-
<SplitClient splitKey={splitKey} attributes={attributesClient} trafficType='user' >
347+
<SplitClient splitKey={splitKey} attributes={attributesClient} >
348348
{() => {
349349
testSwitch(done, splitKey);
350350
return null;

src/__tests__/testUtils/mockSplitFactory.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ function parseKey(key: SplitIO.SplitKey): SplitIO.SplitKey {
2626
};
2727
}
2828
}
29-
function buildInstanceId(key: any, trafficType: string | undefined) {
29+
function buildInstanceId(key: any, trafficType?: string) {
3030
return `${key.matchingKey ? key.matchingKey : key}-${key.bucketingKey ? key.bucketingKey : key}-${trafficType !== undefined ? trafficType : ''}`;
3131
}
3232

3333
export function mockSdk() {
3434

3535
return jest.fn((config: SplitIO.IBrowserSettings, __updateModules) => {
3636

37-
function mockClient(_key: SplitIO.SplitKey, _trafficType?: string) {
37+
function mockClient(_key: SplitIO.SplitKey) {
3838
// Readiness
3939
let isReady = false;
4040
let isReadyFromCache = false;
@@ -135,11 +135,10 @@ export function mockSdk() {
135135

136136
// Cache of clients
137137
const __clients__: { [instanceId: string]: any } = {};
138-
const client = jest.fn((key?: string, trafficType?: string) => {
138+
const client = jest.fn((key?: string) => {
139139
const clientKey = key || parseKey(config.core.key);
140-
const clientTT = trafficType || config.core.trafficType;
141-
const instanceId = buildInstanceId(clientKey, clientTT);
142-
return __clients__[instanceId] || (__clients__[instanceId] = mockClient(clientKey, clientTT));
140+
const instanceId = buildInstanceId(clientKey);
141+
return __clients__[instanceId] || (__clients__[instanceId] = mockClient(clientKey));
143142
});
144143

145144
// Factory destroy

src/__tests__/useSplitClient.test.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ describe('useSplitClient', () => {
5656
<SplitFactoryProvider factory={outerFactory} >
5757
{React.createElement(() => {
5858
(outerFactory.client as jest.Mock).mockClear();
59-
client = useSplitClient({ splitKey: 'user2', trafficType: 'user' }).client;
59+
client = useSplitClient({ splitKey: 'user2' }).client;
6060
return null;
6161
})}
6262
</SplitFactoryProvider>
6363
);
64-
expect(outerFactory.client as jest.Mock).toBeCalledWith('user2', 'user');
64+
expect(outerFactory.client as jest.Mock).toBeCalledWith('user2');
6565
expect(outerFactory.client as jest.Mock).toHaveReturnedWith(client);
6666
});
6767

@@ -70,7 +70,7 @@ describe('useSplitClient', () => {
7070
render(
7171
React.createElement(() => {
7272
useSplitClient();
73-
useSplitClient({ splitKey: 'user2', trafficType: 'user' });
73+
useSplitClient({ splitKey: 'user2' });
7474
return null;
7575
})
7676
);
@@ -81,7 +81,7 @@ describe('useSplitClient', () => {
8181

8282
// eslint-disable-next-line react/prop-types
8383
const InnerComponent = ({ splitKey, attributesClient, testSwitch }) => {
84-
useSplitClient({ splitKey, trafficType: 'user', attributes: attributesClient });
84+
useSplitClient({ splitKey, attributes: attributesClient });
8585
testSwitch(done, splitKey);
8686
return null;
8787
};
@@ -112,16 +112,16 @@ describe('useSplitClient', () => {
112112
<SplitContext.Consumer>
113113
{() => countSplitContext++}
114114
</SplitContext.Consumer>
115-
<SplitClient splitKey={sdkBrowser.core.key} trafficType={sdkBrowser.core.trafficType}
115+
<SplitClient splitKey={sdkBrowser.core.key}
116116
/* Disabling update props is ineffective because the wrapping SplitFactoryProvider has them enabled: */
117117
updateOnSdkReady={false} updateOnSdkReadyFromCache={false}
118118
>
119119
{() => { countSplitClient++; return null }}
120120
</SplitClient>
121121
{React.createElement(() => {
122122
// Equivalent to
123-
// - Using config key and traffic type: `const { client } = useSplitClient(sdkBrowser.core.key, sdkBrowser.core.trafficType, { att1: 'att1' });`
124-
// - Disabling update props, since the wrapping SplitFactoryProvider has them enabled: `const { client } = useSplitClient(undefined, undefined, { att1: 'att1' }, { updateOnSdkReady: false, updateOnSdkReadyFromCache: false });`
123+
// - Using config key: `const { client } = useSplitClient({ splitKey: sdkBrowser.core.key, attributes: { att1: 'att1' } });`
124+
// - Disabling update props, since the wrapping SplitFactoryProvider has them enabled: `const { client } = useSplitClient({ attributes: { att1: 'att1' }, updateOnSdkReady: false, updateOnSdkReadyFromCache: false });`
125125
const { client } = useSplitClient({ attributes: { att1: 'att1' } });
126126
expect(client).toBe(mainClient); // Assert that the main client was retrieved.
127127
expect(client!.getAttributes()).toEqual({ att1: 'att1' }); // Assert that the client was retrieved with the provided attributes.
@@ -141,7 +141,7 @@ describe('useSplitClient', () => {
141141
{() => { countSplitClientWithUpdate++; return null }}
142142
</SplitClient>
143143
{React.createElement(() => {
144-
useSplitClient({ splitKey: sdkBrowser.core.key, trafficType: sdkBrowser.core.trafficType, updateOnSdkUpdate: true }).client;
144+
useSplitClient({ splitKey: sdkBrowser.core.key, updateOnSdkUpdate: true }).client;
145145
countUseSplitClientWithUpdate++;
146146
return null;
147147
})}

src/__tests__/useTrack.test.tsx

+7-8
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,30 @@ describe('useTrack', () => {
6262
expect(track).toHaveReturnedWith(trackResult);
6363
});
6464

65-
test('returns the track method bound to a new client given a splitKey and optional trafficType.', () => {
65+
test('returns the track method bound to a new client given a splitKey.', () => {
6666
const outerFactory = SplitFactory(sdkBrowser);
67-
let boundTrack;
6867
let trackResult;
6968

7069
render(
7170
<SplitFactoryProvider factory={outerFactory} >
7271
{React.createElement(() => {
73-
boundTrack = useTrack('user2', tt);
74-
trackResult = boundTrack(eventType, value, properties);
72+
const boundTrack = useTrack('user2');
73+
trackResult = boundTrack(tt, eventType, value, properties);
7574
return null;
7675
})}
7776
</SplitFactoryProvider>,
7877
);
79-
const track = outerFactory.client('user2', tt).track as jest.Mock;
80-
expect(track).toBeCalledWith(eventType, value, properties);
78+
const track = outerFactory.client('user2').track as jest.Mock;
79+
expect(track).toBeCalledWith(tt, eventType, value, properties);
8180
expect(track).toHaveReturnedWith(trackResult);
8281
});
8382

8483
test('throws error if invoked outside of SplitFactoryProvider.', () => {
8584
expect(() => {
8685
render(
8786
React.createElement(() => {
88-
const track = useTrack('user2', tt);
89-
track(eventType, value, properties);
87+
const track = useTrack('user2');
88+
track(tt, eventType, value, properties);
9089
return null;
9190
}),
9291
);

src/types.ts

-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ export interface IUseSplitClientOptions extends IUpdateProps {
145145
*/
146146
splitKey?: SplitIO.SplitKey;
147147

148-
/**
149-
* Traffic type associated with the customer identifier.
150-
* If no provided here or at the config object, it will be required on the client.track() calls.
151-
*/
152-
trafficType?: string;
153-
154148
/**
155149
* An object of type Attributes used to evaluate the feature flags.
156150
*/

src/useSplitClient.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const DEFAULT_UPDATE_OPTIONS = {
1111
};
1212

1313
/**
14-
* 'useSplitClient' is a hook that returns an Split Context object with the client and its status corresponding to the provided key and trafficType.
14+
* 'useSplitClient' is a hook that returns an Split Context object with the client and its status corresponding to the provided key.
1515
* It uses the 'useContext' hook to access the context, which is updated by SplitFactoryProvider and SplitClient components in the hierarchy of components.
1616
*
1717
* @returns A Split Context object
@@ -25,7 +25,7 @@ export const DEFAULT_UPDATE_OPTIONS = {
2525
*/
2626
export function useSplitClient(options?: IUseSplitClientOptions): ISplitContextValues {
2727
const {
28-
updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate, splitKey, trafficType, attributes
28+
updateOnSdkReady, updateOnSdkReadyFromCache, updateOnSdkTimedout, updateOnSdkUpdate, splitKey, attributes
2929
} = { ...DEFAULT_UPDATE_OPTIONS, ...options };
3030

3131
const context = useSplitContext();
@@ -34,7 +34,7 @@ export function useSplitClient(options?: IUseSplitClientOptions): ISplitContextV
3434
let client = contextClient as IClientWithContext;
3535
if (splitKey && factory) {
3636
// @TODO `getSplitClient` starts client sync. Move side effects to useEffect
37-
client = getSplitClient(factory, splitKey, trafficType);
37+
client = getSplitClient(factory, splitKey);
3838
}
3939
initAttributes(client, attributes);
4040

src/useTrack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const noOpFalse = () => false;
1111
*
1212
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#track}
1313
*/
14-
export function useTrack(splitKey?: SplitIO.SplitKey, trafficType?: string): SplitIO.IBrowserClient['track'] {
14+
export function useTrack(splitKey?: SplitIO.SplitKey): SplitIO.IBrowserClient['track'] {
1515
// All update options are false to avoid re-renders. The track method doesn't need the client to be operational.
16-
const { client } = useSplitClient({ splitKey, trafficType, updateOnSdkReady: false, updateOnSdkReadyFromCache: false });
16+
const { client } = useSplitClient({ splitKey, updateOnSdkReady: false, updateOnSdkReadyFromCache: false });
1717
return client ? client.track.bind(client) : noOpFalse;
1818
}

src/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ export function getSplitFactory(config: SplitIO.IBrowserSettings) {
4646
}
4747

4848
// idempotent operation
49-
export function getSplitClient(factory: SplitIO.IBrowserSDK, key?: SplitIO.SplitKey, trafficType?: string): IClientWithContext {
49+
export function getSplitClient(factory: SplitIO.IBrowserSDK, key?: SplitIO.SplitKey): IClientWithContext {
5050
// factory.client is an idempotent operation
51-
const client = (key !== undefined ? factory.client(key, trafficType) : factory.client()) as IClientWithContext;
51+
const client = (key !== undefined ? factory.client(key) : factory.client()) as IClientWithContext;
5252

5353
// Remove EventEmitter warning emitted when using multiple SDK hooks or components.
5454
// Unlike JS SDK, users don't need to access the client directly, making the warning irrelevant.
55-
client.setMaxListeners(0);
55+
client.setMaxListeners && client.setMaxListeners(0);
5656

5757
return client;
5858
}

0 commit comments

Comments
 (0)