-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtrackMethod.ts
More file actions
59 lines (51 loc) · 2.7 KB
/
Copy pathtrackMethod.ts
File metadata and controls
59 lines (51 loc) · 2.7 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
import { thenable } from '../utils/promise/thenable';
import { getMatching } from '../utils/key';
import { validateTrafficTypeExistence } from '../utils/inputValidation/trafficTypeExistence';
import { validateKey, validateTrafficType, validateEvent, validateEventValue, validateEventProperties, validateIfNotDestroyed } from '../utils/inputValidation';
import { TRACK, TRACK_FN_LABEL } from '../utils/constants';
import { isConsumerMode } from '../utils/settingsValidation/mode';
import SplitIO from '../../types/splitio';
import { ISdkFactoryContext } from '../sdkFactory/types';
/**
* Creates a standalone `track` function with input validation.
* Reusable by FF SDK client, Configs SDK, and thin-client SDK.
*/
export function trackMethodFactory(params: Pick<ISdkFactoryContext, 'settings' | 'eventTracker' | 'telemetryTracker' | 'storage' | 'sdkReadinessManager'>, warnTTExistence = true) {
const { settings, storage: { definitions }, telemetryTracker, eventTracker, sdkReadinessManager: { readinessManager } } = params;
const { log, mode } = settings;
const isAsync = isConsumerMode(mode);
return function track(maybeKey: SplitIO.SplitKey, maybeTT: string, maybeEvent: string, maybeEventValue?: number, maybeProperties?: SplitIO.Properties) {
// Input validation
const key = validateKey(log, maybeKey, TRACK_FN_LABEL);
const trafficTypeName = validateTrafficType(log, maybeTT, TRACK_FN_LABEL);
const eventTypeId = validateEvent(log, maybeEvent, TRACK_FN_LABEL);
const value = validateEventValue(log, maybeEventValue, TRACK_FN_LABEL);
const { properties, size } = validateEventProperties(log, maybeProperties, TRACK_FN_LABEL);
const isNotDestroyed = validateIfNotDestroyed(log, readinessManager, TRACK_FN_LABEL);
if (!(isNotDestroyed && key && trafficTypeName && eventTypeId && value !== false && properties !== false)) {
return isAsync ? Promise.resolve(false) : false;
}
// Core logic
const stopTelemetryTracker = telemetryTracker.trackEval(TRACK);
const eventData: SplitIO.EventData = {
eventTypeId,
trafficTypeName,
value,
timestamp: Date.now(),
key: getMatching(key),
properties: properties as SplitIO.Properties | undefined
};
// This may be async but we only warn, we don't actually care if it is valid or not in terms of queueing the event.
if (warnTTExistence) validateTrafficTypeExistence(log, readinessManager, definitions, mode, trafficTypeName, TRACK_FN_LABEL);
const result = eventTracker.track(eventData, size);
if (thenable(result)) {
return result.then((result) => {
stopTelemetryTracker();
return result;
});
} else {
stopTelemetryTracker();
return result;
}
};
}