Skip to content

Commit 39f2a92

Browse files
Merge pull request #321 from splitio/refactors
Refactors
2 parents aed81d9 + 5195943 commit 39f2a92

File tree

7 files changed

+18
-16
lines changed

7 files changed

+18
-16
lines changed

src/logger/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export const ENGINE_MATCHER_ERROR = 329;
120120
// Log prefixes (a.k.a. tags or categories)
121121
export const LOG_PREFIX_SETTINGS = 'settings';
122122
export const LOG_PREFIX_INSTANTIATION = 'Factory instantiation';
123+
export const LOG_PREFIX_CLIENT_INSTANTIATION = 'Client instantiation';
123124
export const LOG_PREFIX_ENGINE = 'engine';
124125
export const LOG_PREFIX_ENGINE_COMBINER = LOG_PREFIX_ENGINE + ':combiner: ';
125126
export const LOG_PREFIX_ENGINE_MATCHER = LOG_PREFIX_ENGINE + ':matcher: ';

src/sdkClient/sdkClientMethodCS.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getMatching, keyParser } from '../utils/key';
55
import { sdkClientFactory } from './sdkClient';
66
import { ISyncManagerCS } from '../sync/types';
77
import { objectAssign } from '../utils/lang/objectAssign';
8-
import { RETRIEVE_CLIENT_DEFAULT, NEW_SHARED_CLIENT, RETRIEVE_CLIENT_EXISTING } from '../logger/constants';
8+
import { RETRIEVE_CLIENT_DEFAULT, NEW_SHARED_CLIENT, RETRIEVE_CLIENT_EXISTING, LOG_PREFIX_CLIENT_INSTANTIATION } from '../logger/constants';
99
import { SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
1010
import { ISdkFactoryContext } from '../sdkFactory/types';
1111

@@ -14,8 +14,6 @@ function buildInstanceId(key: SplitIO.SplitKey) {
1414
return `${key.matchingKey ? key.matchingKey : key}-${key.bucketingKey ? key.bucketingKey : key}-`;
1515
}
1616

17-
const method = 'Client instantiation';
18-
1917
/**
2018
* Factory of client method for the client-side API variant where TT is ignored.
2119
* Therefore, clients don't have a bound TT for the track method.
@@ -43,7 +41,7 @@ export function sdkClientMethodCSFactory(params: ISdkFactoryContext): (key?: Spl
4341
}
4442

4543
// Validate the key value. The trafficType (2nd argument) is ignored
46-
const validKey = validateKey(log, key, method);
44+
const validKey = validateKey(log, key, LOG_PREFIX_CLIENT_INSTANTIATION);
4745
if (validKey === false) {
4846
throw new Error('Shared Client needs a valid key.');
4947
}

src/sdkClient/sdkClientMethodCSWithTT.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getMatching, keyParser } from '../utils/key';
66
import { sdkClientFactory } from './sdkClient';
77
import { ISyncManagerCS } from '../sync/types';
88
import { objectAssign } from '../utils/lang/objectAssign';
9-
import { RETRIEVE_CLIENT_DEFAULT, NEW_SHARED_CLIENT, RETRIEVE_CLIENT_EXISTING } from '../logger/constants';
9+
import { RETRIEVE_CLIENT_DEFAULT, NEW_SHARED_CLIENT, RETRIEVE_CLIENT_EXISTING, LOG_PREFIX_CLIENT_INSTANTIATION } from '../logger/constants';
1010
import { SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
1111
import { ISdkFactoryContext } from '../sdkFactory/types';
1212

@@ -15,8 +15,6 @@ function buildInstanceId(key: SplitIO.SplitKey, trafficType?: string) {
1515
return `${key.matchingKey ? key.matchingKey : key}-${key.bucketingKey ? key.bucketingKey : key}-${trafficType !== undefined ? trafficType : ''}`;
1616
}
1717

18-
const method = 'Client instantiation';
19-
2018
/**
2119
* Factory of client method for the client-side (browser) variant of the Isomorphic JS SDK,
2220
* where clients can have a bound TT for the track method, which is provided via the settings
@@ -46,14 +44,14 @@ export function sdkClientMethodCSFactory(params: ISdkFactoryContext): (key?: Spl
4644
}
4745

4846
// Validate the key value
49-
const validKey = validateKey(log, key, `Shared ${method}`);
47+
const validKey = validateKey(log, key, LOG_PREFIX_CLIENT_INSTANTIATION);
5048
if (validKey === false) {
5149
throw new Error('Shared Client needs a valid key.');
5250
}
5351

5452
let validTrafficType;
5553
if (trafficType !== undefined) {
56-
validTrafficType = validateTrafficType(log, trafficType, `Shared ${method}`);
54+
validTrafficType = validateTrafficType(log, trafficType, LOG_PREFIX_CLIENT_INSTANTIATION);
5755
if (validTrafficType === false) {
5856
throw new Error('Shared Client needs a valid traffic type or no traffic type at all.');
5957
}

src/storages/AbstractSplitsCacheSync.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ISplitsCacheSync } from './types';
22
import { ISplit } from '../dtos/types';
33
import { objectAssign } from '../utils/lang/objectAssign';
44
import { ISet } from '../utils/lang/sets';
5+
import { IN_SEGMENT } from '../utils/constants';
56

67
/**
78
* This class provides a skeletal implementation of the ISplitsCacheSync interface
@@ -93,7 +94,7 @@ export function usesSegments(split: ISplit) {
9394
const matchers = conditions[i].matcherGroup.matchers;
9495

9596
for (let j = 0; j < matchers.length; j++) {
96-
if (matchers[j].matcherType === 'IN_SEGMENT') return true;
97+
if (matchers[j].matcherType === IN_SEGMENT) return true;
9798
}
9899
}
99100

src/sync/polling/updaters/splitChangesUpdater.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { SDK_SPLITS_ARRIVED, SDK_SPLITS_CACHE_LOADED } from '../../../readiness/
88
import { ILogger } from '../../../logger/types';
99
import { SYNC_SPLITS_FETCH, SYNC_SPLITS_NEW, SYNC_SPLITS_REMOVED, SYNC_SPLITS_SEGMENTS, SYNC_SPLITS_FETCH_FAILS, SYNC_SPLITS_FETCH_RETRY } from '../../../logger/constants';
1010
import { startsWith } from '../../../utils/lang';
11+
import { IN_SEGMENT } from '../../../utils/constants';
1112

1213
type ISplitChangesUpdater = (noCache?: boolean, till?: number, splitUpdateNotification?: { payload: ISplit, changeNumber: number }) => Promise<boolean>
1314

@@ -33,7 +34,7 @@ export function parseSegments({ conditions }: ISplit): ISet<string> {
3334
const matchers = conditions[i].matcherGroup.matchers;
3435

3536
matchers.forEach(matcher => {
36-
if (matcher.matcherType === 'IN_SEGMENT') segments.add(matcher.userDefinedSegmentMatcherData.segmentName);
37+
if (matcher.matcherType === IN_SEGMENT) segments.add(matcher.userDefinedSegmentMatcherData.segmentName);
3738
});
3839
}
3940

@@ -54,7 +55,7 @@ interface ISplitMutations {
5455
* @param filters splitFiltersValidation bySet | byName
5556
*/
5657
function matchFilters(featureFlag: ISplit, filters: ISplitFiltersValidation) {
57-
const { bySet: setsFilter, byName: namesFilter, byPrefix: prefixFilter} = filters.groupedFilters;
58+
const { bySet: setsFilter, byName: namesFilter, byPrefix: prefixFilter } = filters.groupedFilters;
5859
if (setsFilter.length > 0) return featureFlag.sets && featureFlag.sets.some((featureFlagSet: string) => setsFilter.indexOf(featureFlagSet) > -1);
5960

6061
const namesFilterConfigured = namesFilter.length > 0;
@@ -129,7 +130,7 @@ export function splitChangesUpdaterFactory(
129130

130131
/** Returns true if at least one split was updated */
131132
function isThereUpdate(flagsChange: [boolean | void, void | boolean[], void | boolean[], boolean | void] | [any, any, any]) {
132-
const [, added, removed, ] = flagsChange;
133+
const [, added, removed] = flagsChange;
133134
// There is at least one added or modified feature flag
134135
if (added && added.some((update: boolean) => update)) return true;
135136
// There is at least one removed feature flag

src/utils/constants/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,6 @@ export const ENABLED = 1;
106106
export const PAUSED = 2;
107107

108108
export const FLAG_SPEC_VERSION = '1.1';
109+
110+
// Matcher types
111+
export const IN_SEGMENT = 'IN_SEGMENT';

src/utils/settingsValidation/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ISettingsValidationParams } from './types';
77
import { ISettings } from '../../types';
88
import { validateKey } from '../inputValidation/key';
99
import { validateTrafficType } from '../inputValidation/trafficType';
10-
import { ERROR_MIN_CONFIG_PARAM } from '../../logger/constants';
10+
import { ERROR_MIN_CONFIG_PARAM, LOG_PREFIX_CLIENT_INSTANTIATION } from '../../logger/constants';
1111

1212
// Exported for telemetry
1313
export const base = {
@@ -166,13 +166,13 @@ export function settingsValidation(config: unknown, validationParams: ISettingsV
166166
// Keeping same behaviour than JS SDK: if settings key or TT are invalid,
167167
// `false` value is used as bound key/TT of the default client, which leads to some issues.
168168
// @ts-ignore, @TODO handle invalid keys as a non-recoverable error?
169-
withDefaults.core.key = validateKey(log, maybeKey, 'Client instantiation');
169+
withDefaults.core.key = validateKey(log, maybeKey, LOG_PREFIX_CLIENT_INSTANTIATION);
170170
}
171171

172172
if (validationParams.acceptTT) {
173173
const maybeTT = withDefaults.core.trafficType;
174174
if (maybeTT !== undefined) { // @ts-ignore
175-
withDefaults.core.trafficType = validateTrafficType(log, maybeTT, 'Client instantiation');
175+
withDefaults.core.trafficType = validateTrafficType(log, maybeTT, LOG_PREFIX_CLIENT_INSTANTIATION);
176176
}
177177
}
178178
} else {

0 commit comments

Comments
 (0)