-
Notifications
You must be signed in to change notification settings - Fork 83
Add Notification Registry for ODP Setting Updates #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1d43ab2
a577fa4
87aa569
3c0ff50
f4ebb4d
f0ad2ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright 2023, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { NotificationRegistry } from './notification_registry'; | ||
|
||
describe('Notification Registry', () => { | ||
it('Returns null notification center when SDK Key is null', () => { | ||
const notificationCenter = NotificationRegistry.getNotificationCenter(); | ||
expect(notificationCenter).to.be.undefined; | ||
}); | ||
|
||
it('Returns the same notification center when SDK Keys are the same and not null', () => { | ||
const sdkKey = 'testSDKKey'; | ||
const notificationCenterA = NotificationRegistry.getNotificationCenter(sdkKey); | ||
const notificationCenterB = NotificationRegistry.getNotificationCenter(sdkKey); | ||
expect(notificationCenterA).to.eql(notificationCenterB); | ||
}); | ||
|
||
it('Returns different notification centers when SDK Keys are not the same', () => { | ||
const sdkKeyA = 'testSDKKeyA'; | ||
const sdkKeyB = 'testSDKKeyB'; | ||
const notificationCenterA = NotificationRegistry.getNotificationCenter(sdkKeyA); | ||
const notificationCenterB = NotificationRegistry.getNotificationCenter(sdkKeyB); | ||
expect(notificationCenterA).to.not.eql(notificationCenterB); | ||
}); | ||
|
||
it('Removes old notification centers from the registry when removeNotificationCenter is called on the registry', () => { | ||
const sdkKey = 'testSDKKey'; | ||
const notificationCenterA = NotificationRegistry.getNotificationCenter(sdkKey); | ||
NotificationRegistry.removeNotificationCenter(sdkKey); | ||
|
||
const notificationCenterB = NotificationRegistry.getNotificationCenter(sdkKey); | ||
|
||
expect(notificationCenterA).to.not.eql(notificationCenterB); | ||
}); | ||
|
||
it('Does not throw an error when calling removeNotificationCenter with a null SDK Key', () => { | ||
const sdkKey = 'testSDKKey'; | ||
const notificationCenterA = NotificationRegistry.getNotificationCenter(sdkKey); | ||
NotificationRegistry.removeNotificationCenter(); | ||
|
||
const notificationCenterB = NotificationRegistry.getNotificationCenter(sdkKey); | ||
|
||
expect(notificationCenterA).to.eql(notificationCenterB); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright 2023, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { getLogger, LogHandler, LogLevel } from '../../modules/logging'; | ||
import { NotificationCenter, createNotificationCenter } from '../../core/notification_center'; | ||
|
||
/** | ||
* Internal notification center registry for managing multiple notification centers. | ||
*/ | ||
export class NotificationRegistry { | ||
private static _notificationCenters = new Map<string, NotificationCenter>(); | ||
|
||
constructor() {} | ||
|
||
/** | ||
* Retrieves an SDK Key's corresponding notification center in the registry if it exists, otherwise it creates one | ||
* @param sdkKey SDK Key to be used for the notification center tied to the ODP Manager | ||
* @param logger Logger to be used for the corresponding notification center | ||
* @returns {NotificationCenter | undefined} a notification center instance for ODP Manager if a valid SDK Key is provided, otherwise undefined | ||
*/ | ||
public static getNotificationCenter( | ||
sdkKey?: string, | ||
logger: LogHandler = getLogger() | ||
): NotificationCenter | undefined { | ||
if (!sdkKey) { | ||
logger.log(LogLevel.ERROR, 'No SDK key provided to getNotificationCenter.'); | ||
return undefined; | ||
} | ||
|
||
let notificationCenter; | ||
if (this._notificationCenters.has(sdkKey)) { | ||
notificationCenter = this._notificationCenters.get(sdkKey); | ||
} else { | ||
notificationCenter = createNotificationCenter({ | ||
logger, | ||
errorHandler: { handleError: () => {} }, | ||
}); | ||
this._notificationCenters.set(sdkKey, notificationCenter); | ||
} | ||
|
||
return notificationCenter; | ||
} | ||
|
||
public static removeNotificationCenter(sdkKey?: string): void { | ||
if (!sdkKey) { | ||
return; | ||
} | ||
|
||
const notificationCenter = this._notificationCenters.get(sdkKey); | ||
if (notificationCenter) { | ||
notificationCenter.clearAllNotificationListeners(); | ||
this._notificationCenters.delete(sdkKey); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/**************************************************************************** | ||
* Copyright 2020-2022, Optimizely, Inc. and contributors * | ||
* Copyright 2020-2023, 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. * | ||
|
@@ -17,6 +17,7 @@ import { LoggerFacade, ErrorHandler } from '../modules/logging'; | |
import { sprintf, objectValues } from '../utils/fns'; | ||
import { NotificationCenter } from '../core/notification_center'; | ||
import { EventProcessor } from '../../lib/modules/event_processor'; | ||
import { OdpManager } from './../core/odp/odp_manager'; | ||
|
||
import { | ||
UserAttributes, | ||
|
@@ -29,14 +30,16 @@ import { | |
FeatureVariable, | ||
OptimizelyOptions, | ||
OptimizelyDecideOption, | ||
OptimizelyDecision | ||
OptimizelyDecision, | ||
NotificationListener | ||
} from '../shared_types'; | ||
import { newErrorDecision } from '../optimizely_decision'; | ||
import OptimizelyUserContext from '../optimizely_user_context'; | ||
import { createProjectConfigManager, ProjectConfigManager } from '../core/project_config/project_config_manager'; | ||
import { createDecisionService, DecisionService, DecisionObj } from '../core/decision_service'; | ||
import { getImpressionEvent, getConversionEvent } from '../core/event_builder'; | ||
import { buildImpressionEvent, buildConversionEvent } from '../core/event_builder/event_helpers'; | ||
import { NotificationRegistry } from '../core/notification_center/notification_registry'; | ||
import fns from '../utils/fns' | ||
import { validate } from '../utils/attributes_validator'; | ||
import * as enums from '../utils/enums'; | ||
|
@@ -81,6 +84,7 @@ export default class Optimizely { | |
private decisionService: DecisionService; | ||
private eventProcessor: EventProcessor; | ||
private defaultDecideOptions: { [key: string]: boolean }; | ||
private odpManager?: OdpManager; | ||
public notificationCenter: NotificationCenter; | ||
|
||
constructor(config: OptimizelyOptions) { | ||
|
@@ -175,6 +179,27 @@ export default class Optimizely { | |
|
||
this.readyTimeouts = {}; | ||
this.nextReadyTimeoutId = 0; | ||
|
||
if (config.odpManager != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All this logic should come-up after the readyPromise? How config.sdkKey will be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving logic into Promise.all. Also, as discussed, this should catch for both cases where a new Optimizely instance is provided either an SDK Key or manually includes a datafile. |
||
this.odpManager = config.odpManager; | ||
this.odpManager.eventManager?.start(); | ||
if (this.projectConfigManager.getConfig() != null) { | ||
opti-jnguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.updateODPSettings(); | ||
} | ||
if (config.sdkKey != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have 2 sources of sdkKey, here and projectConfigManager. It looks like |
||
NotificationRegistry.getNotificationCenter(config.sdkKey, this.logger) | ||
?.addNotificationListener(enums.NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE, () => this.updateODPSettings()); | ||
} else { | ||
this.logger.log(LOG_LEVEL.ERROR, ERROR_MESSAGES.ODP_SDK_KEY_MISSING_NOTIFICATION_CENTER_FAILURE); | ||
} | ||
} | ||
} | ||
|
||
updateODPSettings(): void { | ||
const projectConfig = this.projectConfigManager.getConfig(); | ||
if (this.odpManager != null && projectConfig != null) { | ||
this.odpManager.updateSettings(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.allSegments); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -1315,6 +1340,10 @@ export default class Optimizely { | |
*/ | ||
close(): Promise<{ success: boolean; reason?: string }> { | ||
try { | ||
this.notificationCenter.clearAllNotificationListeners(); | ||
const sdkKey = this.projectConfigManager.getConfig()?.sdkKey; | ||
opti-jnguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (sdkKey) NotificationRegistry.removeNotificationCenter(sdkKey); | ||
|
||
const eventProcessorStoppedPromise = this.eventProcessor.stop(); | ||
if (this.disposeOnUpdate) { | ||
this.disposeOnUpdate(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.