Skip to content

Commit

Permalink
refactor: remove OdpMgr.enabled + move instantiation up
Browse files Browse the repository at this point in the history
Also remove some throws & linting
  • Loading branch information
Mike Chu committed Feb 7, 2024
1 parent 5775e15 commit b1dc7a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 66 deletions.
43 changes: 9 additions & 34 deletions lib/core/odp/odp_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2023, Optimizely
* Copyright 2023-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,8 +34,6 @@ import { OdpEvent } from './odp_event';
export interface IOdpManager {
initPromise?: Promise<void>;

enabled: boolean;

segmentManager: IOdpSegmentManager | undefined;

eventManager: IOdpEventManager | undefined;
Expand Down Expand Up @@ -64,11 +62,6 @@ export abstract class OdpManager implements IOdpManager {
*/
initPromise?: Promise<void>;

/**
* Switch to enable/disable ODP Manager functionality
*/
enabled = true;

/**
* ODP Segment Manager which provides an interface to the remote ODP server (GraphQL API) for audience segments mapping.
* It fetches all qualified segments for the given user context and manages the segments cache for all user contexts.
Expand Down Expand Up @@ -98,10 +91,6 @@ export abstract class OdpManager implements IOdpManager {
* Provides a method to update ODP Manager's ODP Config API Key, API Host, and Audience Segments
*/
updateSettings({ apiKey, apiHost, pixelUrl, segmentsToCheck }: OdpConfig): boolean {
if (!this.enabled) {
return false;
}

if (!this.eventManager) {
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_MANAGER_UPDATE_SETTINGS_FAILED_EVENT_MANAGER_MISSING);
return false;
Expand Down Expand Up @@ -130,10 +119,6 @@ export abstract class OdpManager implements IOdpManager {
* Attempts to stop the current instance of ODP Manager's event manager, if it exists and is running.
*/
close(): void {
if (!this.enabled) {
return;
}

this.eventManager?.stop();
}

Expand All @@ -145,11 +130,6 @@ export abstract class OdpManager implements IOdpManager {
* @returns {Promise<string[] | null>} A promise holding either a list of qualified segments or null.
*/
async fetchQualifiedSegments(userId: string, options: Array<OptimizelySegmentOption> = []): Promise<string[] | null> {
if (!this.enabled) {
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_NOT_ENABLED);
return null;
}

if (!this.segmentManager) {
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_FETCH_QUALIFIED_SEGMENTS_SEGMENTS_MANAGER_MISSING);
return null;
Expand All @@ -169,11 +149,6 @@ export abstract class OdpManager implements IOdpManager {
* @returns
*/
identifyUser(userId?: string, vuid?: string): void {
if (!this.enabled) {
this.logger.log(LogLevel.DEBUG, LOG_MESSAGES.ODP_IDENTIFY_FAILED_ODP_DISABLED);
return;
}

if (!this.odpConfig.isReady()) {
this.logger.log(LogLevel.DEBUG, LOG_MESSAGES.ODP_IDENTIFY_FAILED_ODP_NOT_INTEGRATED);
return;
Expand Down Expand Up @@ -203,24 +178,24 @@ export abstract class OdpManager implements IOdpManager {
mType = 'fullstack';
}

if (!this.enabled) {
throw new Error(ERROR_MESSAGES.ODP_NOT_ENABLED);
}

if (!this.odpConfig.isReady()) {
throw new Error(ERROR_MESSAGES.ODP_NOT_INTEGRATED);
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_NOT_INTEGRATED);
return;
}

if (invalidOdpDataFound(data)) {
throw new Error(ERROR_MESSAGES.ODP_INVALID_DATA);
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_INVALID_DATA);
return;
}

if (!this.eventManager) {
throw new Error(ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_EVENT_MANAGER_MISSING);
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_EVENT_MANAGER_MISSING);
return;
}

if (typeof action !== 'string' || action === '') {
throw new Error('ODP action is not valid (cannot be empty).');
this.logger.log(LogLevel.ERROR, 'ODP action is not valid (cannot be empty).');
return;
}

this.eventManager.sendEvent(new OdpEvent(mType, action, identifiers, data));
Expand Down
33 changes: 21 additions & 12 deletions lib/optimizely/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2020-2023, Optimizely, Inc. and contributors *
* Copyright 2020-2024, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -178,10 +178,7 @@ export default class Optimizely implements Client {

const eventProcessorStartedPromise = this.eventProcessor.start();

const dependentPromises: Array<Promise<any>> = [
projectConfigManagerReadyPromise,
eventProcessorStartedPromise,
];
const dependentPromises: Array<Promise<any>> = [projectConfigManagerReadyPromise, eventProcessorStartedPromise];

if (config.odpManager?.initPromise) {
dependentPromises.push(config.odpManager.initPromise);
Expand Down Expand Up @@ -778,7 +775,12 @@ export default class Optimizely implements Client {
* type, or null if the feature key is invalid or
* the variable key is invalid
*/
getFeatureVariable(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): FeatureVariableValue {
getFeatureVariable(
featureKey: string,
variableKey: string,
userId: string,
attributes?: UserAttributes
): FeatureVariableValue {
try {
if (!this.isValidInstance()) {
this.logger.log(LOG_LEVEL.ERROR, LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getFeatureVariable');
Expand Down Expand Up @@ -1684,7 +1686,12 @@ export default class Optimizely implements Client {
const projectConfig = this.projectConfigManager.getConfig();
if (this.odpManager != null && projectConfig != null) {
this.odpManager.updateSettings(
new OdpConfig(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.pixelUrlForOdp, projectConfig.allSegments)
new OdpConfig(
projectConfig.publicKeyForOdp,
projectConfig.hostForOdp,
projectConfig.pixelUrlForOdp,
projectConfig.allSegments
)
);
}
}
Expand Down Expand Up @@ -1742,9 +1749,12 @@ export default class Optimizely implements Client {
* @param {string} userId
*/
public identifyUser(userId: string): void {
if (this.odpManager && this.odpManager.enabled) {
this.odpManager.identifyUser(userId);
if (!this.odpManager) {
this.logger.error(ERROR_MESSAGES.ODP_IDENTIFY_USER_FAILED_ODP_MANAGER_MISSING);
return;
}

this.odpManager?.identifyUser(userId);
}

/**
Expand All @@ -1758,7 +1768,6 @@ export default class Optimizely implements Client {
options?: Array<OptimizelySegmentOption>
): Promise<string[] | null> {
if (!this.odpManager) {
this.logger.error(ERROR_MESSAGES.ODP_FETCH_QUALIFIED_SEGMENTS_FAILED_ODP_MANAGER_MISSING);
return null;
}

Expand All @@ -1769,14 +1778,14 @@ export default class Optimizely implements Client {
* @returns {string|undefined} Currently provisioned VUID from local ODP Manager or undefined if
* ODP Manager has not been instantiated yet for any reason.
*/
getVuid(): string | undefined {
public getVuid(): string | undefined {
if (!this.odpManager) {
this.logger?.error('Unable to get VUID - ODP Manager is not instantiated yet.');
return undefined;
}

if (!this.odpManager.isVuidEnabled()) {
this.logger.log(LOG_LEVEL.WARNING, 'getVuid() unavailable for this platform', MODULE_NAME);
this.logger.warn('getVuid() unavailable for this platform', MODULE_NAME);
return undefined;
}

Expand Down
18 changes: 6 additions & 12 deletions lib/plugins/odp_manager/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2023, Optimizely
* Copyright 2023-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,13 +56,6 @@ export class BrowserOdpManager extends OdpManager {

this.logger = logger || getLogger();

if (odpOptions?.disabled) {
this.initPromise = Promise.resolve();
this.enabled = false;
this.logger.log(LogLevel.INFO, LOG_MESSAGES.ODP_DISABLED);
return;
}

const browserClientEngine = JAVASCRIPT_CLIENT_ENGINE;
const browserClientVersion = BROWSER_CLIENT_VERSION;

Expand Down Expand Up @@ -125,7 +118,7 @@ export class BrowserOdpManager extends OdpManager {
this.eventManager!.start();

this.initPromise = this.initializeVuid(BrowserOdpManager.cache).catch(e => {
this.logger.log(this.enabled ? LogLevel.ERROR : LogLevel.DEBUG, e);
this.logger.log(LogLevel.ERROR, e);
});
}

Expand All @@ -147,8 +140,8 @@ export class BrowserOdpManager extends OdpManager {

try {
this.eventManager.registerVuid(vuid);
} catch (e) {
this.logger.log(this.enabled ? LogLevel.ERROR : LogLevel.DEBUG, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED);
} catch {
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_VUID_REGISTRATION_FAILED);
}
}

Expand Down Expand Up @@ -186,7 +179,8 @@ export class BrowserOdpManager extends OdpManager {
if (this.vuid) {
identifiersWithVuid.set(ODP_USER_KEY.VUID, this.vuid);
} else {
throw new Error(ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_VUID_MISSING);
this.logger.log(LogLevel.ERROR, ERROR_MESSAGES.ODP_SEND_EVENT_FAILED_VUID_MISSING);
return;
}
}

Expand Down
9 changes: 1 addition & 8 deletions lib/plugins/odp_manager/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2023, Optimizely
* Copyright 2023-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,13 +49,6 @@ export class NodeOdpManager extends OdpManager {

this.logger = logger || getLogger();

if (odpOptions?.disabled) {
this.initPromise = Promise.resolve();
this.enabled = false;
this.logger.log(LogLevel.INFO, LOG_MESSAGES.ODP_DISABLED);
return;
}

const nodeClientEngine = NODE_CLIENT_ENGINE;
const nodeClientVersion = NODE_CLIENT_VERSION;

Expand Down

0 comments on commit b1dc7a2

Please sign in to comment.