@@ -23,7 +23,7 @@ import { ErrorHandler, LogHandler, LogLevel, LoggerFacade } from './modules/logg
2323import { EventProcessor } from './modules/event_processor' ;
2424
2525import { NotificationCenter as NotificationCenterImpl } from './core/notification_center' ;
26- import { NOTIFICATION_TYPES } from './utils/enums' ;
26+ import { NOTIFICATION_TYPES , DECISION_NOTIFICATION_TYPES , DECISION_SOURCES } from './utils/enums' ;
2727
2828import { IOptimizelyUserContext as OptimizelyUserContext } from './optimizely_user_context' ;
2929
@@ -120,13 +120,106 @@ export interface ListenerPayload {
120120 attributes ?: UserAttributes ;
121121}
122122
123- export type NotificationListener < T extends ListenerPayload > = ( notificationData : T ) => void ;
123+ export type DecisionNotificationType =
124+ | typeof DECISION_NOTIFICATION_TYPES . AB_TEST
125+ | typeof DECISION_NOTIFICATION_TYPES . FEATURE
126+ | typeof DECISION_NOTIFICATION_TYPES . FEATURE_TEST
127+ | typeof DECISION_NOTIFICATION_TYPES . FEATURE_VARIABLE
128+ | typeof DECISION_NOTIFICATION_TYPES . ALL_FEATURE_VARIABLES
129+ | typeof DECISION_NOTIFICATION_TYPES . FLAG ;
124130
125- // NotificationCenter-related types
131+ export type DecisionSource =
132+ | typeof DECISION_SOURCES . FEATURE_TEST
133+ | typeof DECISION_SOURCES . ROLLOUT
134+ | typeof DECISION_SOURCES . EXPERIMENT ;
135+
136+ export type DecisionSourceInfo = {
137+ experimentKey ?: string ;
138+ variationKey ?: string ;
139+ } ;
140+
141+ export type VariablesMap = { [ variableKey : string ] : unknown } ;
142+
143+ export type AbTestDecisionInfo = {
144+ experimentKey : string ;
145+ variationKey : string | null ;
146+ } ;
147+
148+ export type FeatureDecisionInfo = {
149+ featureKey : string ;
150+ featureEnabled : boolean ;
151+ source : DecisionSource ;
152+ sourceInfo : DecisionSourceInfo ;
153+ } ;
154+
155+ export type FeatureTestDecisionInfo = {
156+ experimentKey : string ;
157+ variationKey : string | null ;
158+ } ;
159+
160+ export type FeatureVariableDecisionInfo = {
161+ featureKey : string ;
162+ featureEnabled : boolean ;
163+ source : DecisionSource ;
164+ variableKey : string ;
165+ variableValue : FeatureVariableValue ;
166+ variableType : VariableType ;
167+ sourceInfo : DecisionSourceInfo ;
168+ } ;
169+
170+ export type AllFeatureVariablesDecisionInfo = {
171+ featureKey : string ;
172+ featureEnabled : boolean ;
173+ source : DecisionSource ;
174+ variableValues : VariablesMap ;
175+ sourceInfo : DecisionSourceInfo ;
176+ } ;
177+
178+ export type FlagDecisionInfo = {
179+ flagKey : string ;
180+ enabled : boolean ;
181+ variationKey : string | null ;
182+ ruleKey : string | null ;
183+ variables : VariablesMap ;
184+ reasons : string [ ] ;
185+ decisionEventDispatched : boolean ;
186+ experimentId ?: string ;
187+ variationId ?: string ;
188+ } ;
189+
190+ export type DecisionInfoMap = {
191+ [ DECISION_NOTIFICATION_TYPES . AB_TEST ] : AbTestDecisionInfo ;
192+ [ DECISION_NOTIFICATION_TYPES . FEATURE ] : FeatureDecisionInfo ;
193+ [ DECISION_NOTIFICATION_TYPES . FEATURE_TEST ] : FeatureTestDecisionInfo ;
194+ [ DECISION_NOTIFICATION_TYPES . FEATURE_VARIABLE ] : FeatureVariableDecisionInfo ;
195+ [ DECISION_NOTIFICATION_TYPES . ALL_FEATURE_VARIABLES ] : AllFeatureVariablesDecisionInfo ;
196+ [ DECISION_NOTIFICATION_TYPES . FLAG ] : FlagDecisionInfo ;
197+ } ;
198+
199+ export type DecisionListenerPayloadForType < T extends DecisionNotificationType > = ListenerPayload & {
200+ type : T ;
201+ decisionInfo : DecisionInfoMap [ T ] ;
202+ } ;
203+
204+ export type DecisionListenerPayload = {
205+ [ T in DecisionNotificationType ] : DecisionListenerPayloadForType < T > ;
206+ } [ DecisionNotificationType ] ;
207+
208+ export type LogEventListenerPayload = Event ;
209+
210+ export type NotificationPayloadMap = {
211+ [ NOTIFICATION_TYPES . ACTIVATE ] : ActivateListenerPayload ;
212+ [ NOTIFICATION_TYPES . DECISION ] : DecisionListenerPayload ;
213+ [ NOTIFICATION_TYPES . TRACK ] : TrackListenerPayload ;
214+ [ NOTIFICATION_TYPES . LOG_EVENT ] : LogEventListenerPayload ;
215+ [ NOTIFICATION_TYPES . OPTIMIZELY_CONFIG_UPDATE ] : undefined ;
216+ } ;
217+
218+ export type NotificationListener < T > = ( notificationData : T ) => void ;
126219export interface NotificationCenter {
127- addNotificationListener < T extends ListenerPayload > (
128- notificationType : string ,
129- callback : NotificationListener < T >
220+ addNotificationListener < K extends keyof NotificationPayloadMap > (
221+ notificationType : K ,
222+ callback : NotificationListener < NotificationPayloadMap [ K ] >
130223 ) : number ;
131224 removeNotificationListener ( listenerId : number ) : boolean ;
132225 clearAllNotificationListeners ( ) : void ;
@@ -385,14 +478,14 @@ export interface Client {
385478}
386479
387480export interface ActivateListenerPayload extends ListenerPayload {
388- experiment : import ( './shared_types' ) . Experiment ;
389- variation : import ( './shared_types' ) . Variation ;
481+ experiment ? : import ( './shared_types' ) . Experiment ;
482+ variation ? : import ( './shared_types' ) . Variation ;
390483 logEvent : Event ;
391484}
392485
393486export interface TrackListenerPayload extends ListenerPayload {
394487 eventKey : string ;
395- eventTags : EventTags ;
488+ eventTags ? : EventTags ;
396489 logEvent : Event ;
397490}
398491
0 commit comments