-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.ts
More file actions
110 lines (94 loc) · 4.32 KB
/
utils.ts
File metadata and controls
110 lines (94 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import memoizeOne from 'memoize-one';
import shallowEqual from 'shallowequal';
import { CONTROL_WITH_CONFIG } from './constants';
import { ISplitStatus } from './types';
// Utils used to access singleton instances of Split factories and clients, and to gracefully shutdown all clients together.
/**
* ClientWithContext interface.
*/
interface IClientWithContext extends SplitIO.IBrowserClient {
__getStatus(): {
isReady: boolean;
isReadyFromCache: boolean;
isTimedout: boolean;
hasTimedout: boolean;
isDestroyed: boolean;
isOperational: boolean;
lastUpdate: number;
};
}
export interface IFactoryWithLazyInit extends SplitIO.IBrowserSDK {
config: SplitIO.IBrowserSettings;
init(): void;
}
// idempotent operation
export function getSplitClient(factory: SplitIO.IBrowserSDK, key?: SplitIO.SplitKey): IClientWithContext {
// factory.client is an idempotent operation
const client = (key !== undefined ? factory.client(key) : factory.client()) as IClientWithContext;
// Remove EventEmitter warning emitted when using multiple SDK hooks or components.
// Unlike JS SDK, users don't need to access the client directly, making the warning irrelevant.
client.setMaxListeners && client.setMaxListeners(0);
return client;
}
// Util used to get client status.
// It might be removed in the future, if the JS SDK extends its public API with a `getStatus` method
export function getStatus(client?: SplitIO.IBrowserClient): ISplitStatus {
const status = client && (client as IClientWithContext).__getStatus();
return {
isReady: status ? status.isReady : false,
isReadyFromCache: status ? status.isReadyFromCache : false,
isTimedout: status ? status.isTimedout : false,
hasTimedout: status ? status.hasTimedout : false,
isDestroyed: status ? status.isDestroyed : false,
lastUpdate: status ? status.lastUpdate : 0,
};
}
/**
* Manage client attributes binding
*/
// @TODO should reset attributes rather than set/merge them, to keep SFP and hooks pure.
export function initAttributes(client?: SplitIO.IBrowserClient, attributes?: SplitIO.Attributes) {
if (client && attributes) client.setAttributes(attributes);
}
export function getControlTreatmentsWithConfig(featureFlagNames: unknown): SplitIO.TreatmentsWithConfig {
if (!Array.isArray(featureFlagNames)) return {};
featureFlagNames = featureFlagNames
.filter((featureFlagName) => isString(featureFlagName))
.map((featureFlagName) => featureFlagName.trim())
.filter((featureFlagName) => featureFlagName.length > 0);
// return control treatments for each validated feature flag name
return (featureFlagNames as string[]).reduce((pValue: SplitIO.TreatmentsWithConfig, cValue: string) => {
pValue[cValue] = CONTROL_WITH_CONFIG;
return pValue;
}, {});
}
/**
* Checks if a given value is a string.
*/
function isString(val: unknown): val is string {
return typeof val === 'string' || val instanceof String;
}
/**
* Gets a memoized version of the `client.getTreatmentsWithConfig` method.
* It is used to avoid duplicated impressions, because the result treatments are the same given the same `client` instance, `lastUpdate` timestamp, and list of feature flag `names` and `attributes`.
*/
export function memoizeGetTreatmentsWithConfig() {
return memoizeOne(evaluateFeatureFlags, argsAreEqual);
}
function argsAreEqual(newArgs: any[], lastArgs: any[]): boolean {
return newArgs[0] === lastArgs[0] && // client
newArgs[1] === lastArgs[1] && // lastUpdate
shallowEqual(newArgs[2], lastArgs[2]) && // names
shallowEqual(newArgs[3], lastArgs[3]) && // attributes
shallowEqual(newArgs[4], lastArgs[4]) && // client attributes
shallowEqual(newArgs[5], lastArgs[5]); // flagSets
}
function evaluateFeatureFlags(client: SplitIO.IBrowserClient | undefined, _lastUpdate: number, names?: SplitIO.SplitNames, attributes?: SplitIO.Attributes, _clientAttributes?: SplitIO.Attributes, flagSets?: string[], options?: SplitIO.EvaluationOptions) {
return client && (client as IClientWithContext).__getStatus().isOperational && (names || flagSets) ?
names ?
client.getTreatmentsWithConfig(names, attributes, options) :
client.getTreatmentsWithConfigByFlagSets(flagSets!, attributes, options) :
names ?
getControlTreatmentsWithConfig(names) :
{} // empty object when evaluating with flag sets and client is not ready
}