Skip to content

Commit 017f8b2

Browse files
committed
make Activate listener payload type backwards compatible
1 parent 017e10b commit 017f8b2

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

lib/notification_center/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export type UserEventListenerPayload = {
3333
}
3434

3535
export type ActivateListenerPayload = UserEventListenerPayload & {
36-
experiment: Experiment | Holdout | null;
36+
experiment: Experiment | null;
37+
holdout: Holdout | null;
3738
variation: Variation | null;
3839
logEvent: LogEvent;
3940
}

lib/optimizely/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
OptimizelyDecision,
3838
Client,
3939
UserProfileServiceAsync,
40+
isHoldout,
4041
} from '../shared_types';
4142
import { newErrorDecision } from '../optimizely_decision';
4243
import OptimizelyUserContext from '../optimizely_user_context';
@@ -62,7 +63,7 @@ import {
6263
import { Fn, Maybe, OpType } from '../utils/type';
6364
import { resolvablePromise } from '../utils/promise/resolvablePromise';
6465

65-
import { NOTIFICATION_TYPES, DecisionNotificationType, DECISION_NOTIFICATION_TYPES } from '../notification_center/type';
66+
import { NOTIFICATION_TYPES, DecisionNotificationType, DECISION_NOTIFICATION_TYPES, ActivateListenerPayload } from '../notification_center/type';
6667
import {
6768
FEATURE_NOT_IN_DATAFILE,
6869
INVALID_INPUT_FORMAT,
@@ -382,17 +383,29 @@ export default class Optimizely extends BaseService implements Client {
382383
this.eventProcessor.process(impressionEvent);
383384

384385
const logEvent = buildLogEvent([impressionEvent]);
385-
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.ACTIVATE, {
386-
experiment: decisionObj.experiment,
386+
387+
const activateNotificationPayload: ActivateListenerPayload = {
388+
experiment: null,
389+
holdout: null,
387390
userId: userId,
388391
attributes: attributes,
389392
variation: decisionObj.variation,
390393
logEvent,
391-
});
394+
};
395+
396+
if (decisionObj.experiment) {
397+
if (isHoldout(decisionObj.experiment)) {
398+
activateNotificationPayload.holdout = decisionObj.experiment;
399+
} else {
400+
activateNotificationPayload.experiment = decisionObj.experiment;
401+
}
402+
}
403+
404+
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.ACTIVATE, activateNotificationPayload);
392405
}
393406

394407
/**
395-
* Sends conversion event to Optimizely.
408+
* Sends conversion event to Optimizely.`
396409
* @param {string} eventKey
397410
* @param {string} userId
398411
* @param {UserAttributes} attributes

lib/shared_types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ export interface Holdout extends ExperimentCore {
181181
excludedFlags: string[];
182182
}
183183

184+
export function isHoldout(obj: Experiment | Holdout): obj is Holdout {
185+
// Holdout has 'status', 'includedFlags', and 'excludedFlags' properties
186+
return (
187+
(obj as Holdout).status !== undefined &&
188+
Array.isArray((obj as Holdout).includedFlags) &&
189+
Array.isArray((obj as Holdout).excludedFlags)
190+
);
191+
}
192+
184193
export enum VariableType {
185194
BOOLEAN = 'boolean',
186195
DOUBLE = 'double',

memory-test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)