-
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
Merged
opti-jnguyen
merged 6 commits into
jnguyen/odp-manager
from
jnguyen/fix-notification-center-issue
Feb 7, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d43ab2
Add Notification Registry for ODP Setting Updates
opti-jnguyen a577fa4
Fix: Refactor String Arrays to Sets
opti-jnguyen 87aa569
Fixes based on review
opti-jnguyen 3c0ff50
Patch ODP Manager to await project config
opti-jnguyen f4ebb4d
Minor Date/Import Updates
opti-jnguyen f0ad2ed
Patch sdkKey Usage
opti-jnguyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/optimizely-sdk/lib/core/notification_center/notification_registry.tests.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 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 | ||
* | ||
* https://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 { describe, it } from 'mocha'; | ||
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); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
packages/optimizely-sdk/lib/core/notification_center/notification_registry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.