-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStatsigClientEventEmitter.ts
111 lines (95 loc) · 3.31 KB
/
StatsigClientEventEmitter.ts
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
111
import { DataAdapterResult } from './StatsigDataAdapter';
import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
import { Flatten } from './TypingUtils';
export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready';
export const ErrorTag = {
NetworkError: 'NetworkError',
} as const;
export type ErrorTag = (typeof ErrorTag)[keyof typeof ErrorTag];
type ErrorEventData =
| {
error: unknown;
tag: ErrorTag | string;
}
| {
error: unknown;
tag: 'NetworkError';
requestArgs: Record<string, unknown>;
};
type EventNameToEventDataMap = {
values_updated: {
status: StatsigLoadingStatus;
values: DataAdapterResult | null;
};
session_expired: object;
error: ErrorEventData;
pre_logs_flushed: { events: Record<string, unknown>[] };
logs_flushed: { events: Record<string, unknown>[] };
pre_shutdown: object;
initialization_failure: object;
gate_evaluation: { gate: FeatureGate };
dynamic_config_evaluation: { dynamicConfig: DynamicConfig };
experiment_evaluation: { experiment: Experiment };
layer_evaluation: { layer: Layer };
};
/**
* Type representing various events emitted by a Statsig client.
*
* `values_updated` - When the Statsig clients internal values change as the result of an initialize/update operation.
*
* `session_expired` - When the current session has expired.
*
* `error` - When an unexpected error occurs within the Statsig client.
*
* `pre_logs_flushed` - Fired just before queued StatsigEvents are flushed to Statsig servers.
*
* `logs_flushed` - When queued StatsigEvents are flushed to Statsig servers.
*
* `pre_shutdown` - Fired just before the SDK is shutdown
*
* `initialization_failure` - Fired when the client fails to initialize.
*
* `gate_evaluation` - Fired when any gate is checked from the Statsig client.
*
* `dynamic_config_evaluation` - Fired when any dyanamic config is checked from the Statsig client.
*
* `experiment_evaluation` - Fired when any experiment is checked from the Statsig client.
*
* `layer_evaluation` - Fired when any layer is checked from the Statsig client.
*/
export type AnyStatsigClientEvent = Flatten<
{
[K in keyof EventNameToEventDataMap]: {
name: K;
} & EventNameToEventDataMap[K];
}[keyof EventNameToEventDataMap]
>;
export type StatsigClientEvent<T> = Extract<AnyStatsigClientEvent, { name: T }>;
export type AnyStatsigClientEventListener =
StatsigClientEventCallback<StatsigClientEventName>;
export type StatsigClientEventName = AnyStatsigClientEvent['name'] | '*';
export type StatsigClientEventCallback<T extends StatsigClientEventName> = (
event: T extends '*' ? AnyStatsigClientEvent : StatsigClientEvent<T>,
) => void;
export interface StatsigClientEventEmitterInterface {
readonly loadingStatus: StatsigLoadingStatus;
on<T extends StatsigClientEventName>(
event: T,
listener: StatsigClientEventCallback<T>,
): void;
off<T extends StatsigClientEventName>(
event: T,
listener: StatsigClientEventCallback<T>,
): void;
/**
* (Statsig Use Only) - Same as .on() but logs errors to sdk_exception
*/
$on<T extends StatsigClientEventName>(
event: T,
listener: StatsigClientEventCallback<T>,
): void;
/**
* (Statsig Use Only) - Emit StatsigClientEvents
*/
$emt(event: AnyStatsigClientEvent): void;
}