-
Notifications
You must be signed in to change notification settings - Fork 84
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 1 commit
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.null; | ||
| }); | ||
|
|
||
| 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,54 @@ | ||
| /** | ||
| * 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 } 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() {} | ||
|
|
||
| public static getNotificationCenter(sdkKey?: string, logger?: LogHandler): NotificationCenter | null { | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!sdkKey) return null; | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let notificationCenter; | ||
| if (this._notificationCenters.has(sdkKey)) { | ||
| notificationCenter = this._notificationCenters.get(sdkKey) || null; | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| notificationCenter = createNotificationCenter({ | ||
| logger: logger || getLogger(), | ||
| errorHandler: { handleError: () => {} }, | ||
| }); | ||
| this._notificationCenters.set(sdkKey, notificationCenter); | ||
| } | ||
|
|
||
| return notificationCenter; | ||
| } | ||
|
|
||
| public static removeNotificationCenter(sdkKey?: string): void { | ||
| if (!sdkKey) return; | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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,26 @@ export default class Optimizely { | |
|
|
||
| this.readyTimeouts = {}; | ||
| this.nextReadyTimeoutId = 0; | ||
|
|
||
| if (config.odpManager != null) { | ||
|
||
| this.odpManager = config.odpManager; | ||
| this.odpManager.eventManager?.start(); | ||
| if (this.projectConfigManager.getConfig() != null) { | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.updateODPSettings(); | ||
| } | ||
| const sdkKey = this.projectConfigManager.getConfig()?.sdkKey; | ||
|
||
| if (sdkKey != null) { | ||
opti-jnguyen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| NotificationRegistry.getNotificationCenter(sdkKey, this.logger) | ||
| ?.addNotificationListener(enums.NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE, () => this.updateODPSettings()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| updateODPSettings(): void { | ||
| const projectConfig = this.projectConfigManager.getConfig(); | ||
| if (this.odpManager != null && projectConfig != null) { | ||
| this.odpManager.updateSettings(projectConfig.publicKeyForOdp, projectConfig.hostForOdp, projectConfig.allSegments); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1315,6 +1339,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.