diff --git a/packages/event-processor/__tests__/v1EventProcessor.spec.ts b/packages/event-processor/__tests__/v1EventProcessor.spec.ts index 5484e15e4..2f30dc01a 100644 --- a/packages/event-processor/__tests__/v1EventProcessor.spec.ts +++ b/packages/event-processor/__tests__/v1EventProcessor.spec.ts @@ -23,6 +23,7 @@ import { } from '../src/eventDispatcher' import { EventProcessor } from '../src/eventProcessor' import { buildImpressionEventV1, makeBatchedEventV1 } from '../src/v1/buildEventV1' +import { NotificationCenter, NOTIFICATION_TYPES } from '@optimizely/js-sdk-utils'; function createImpressionEvent() { return { @@ -340,5 +341,32 @@ describe('LogTierV1EventProcessor', () => { // flushing should reset queue, at this point only has two events expect(dispatchStub).toHaveBeenCalledTimes(1) }) + + }) + + describe('when a notification center is provided', () => { + it('should trigger a notification when the event dispatcher dispatches an event', () => { + const dispatcher: EventDispatcher = { + dispatchEvent: jest.fn() + } + + const notificationCenter: NotificationCenter = { + sendNotifications: jest.fn() + } + + const processor = new LogTierV1EventProcessor({ + dispatcher, + notificationCenter, + maxQueueSize: 1, + }) + processor.start() + + const impressionEvent1 = createImpressionEvent() + processor.process(impressionEvent1) + + expect(notificationCenter.sendNotifications).toBeCalledTimes(1) + const event = (dispatcher.dispatchEvent as jest.Mock).mock.calls[0][0] + expect(notificationCenter.sendNotifications).toBeCalledWith(NOTIFICATION_TYPES.LOG_EVENT, event) + }) }) }) diff --git a/packages/event-processor/package-lock.json b/packages/event-processor/package-lock.json index 3903cc5fd..c65822e61 100644 --- a/packages/event-processor/package-lock.json +++ b/packages/event-processor/package-lock.json @@ -38,12 +38,22 @@ "integrity": "sha512-Bs2zHvsdNIk2QSg05P6mKIlROHoBIRNStbrVwlePm603CucojKRPlFJG4rt7sFZQOo8xS8I7z1BmE4QI3/ZE9A==", "requires": { "@optimizely/js-sdk-utils": "^0.1.0" + }, + "dependencies": { + "@optimizely/js-sdk-utils": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@optimizely/js-sdk-utils/-/js-sdk-utils-0.1.0.tgz", + "integrity": "sha512-p7499GgVaX94YmkrwOiEtLgxgjXTPbUQsvETaAil5J7zg1TOA4Wl8ClalLSvCh+AKWkxGdkL4/uM/zfbxPSNNw==", + "requires": { + "uuid": "^3.3.2" + } + } } }, "@optimizely/js-sdk-utils": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@optimizely/js-sdk-utils/-/js-sdk-utils-0.1.0.tgz", - "integrity": "sha512-p7499GgVaX94YmkrwOiEtLgxgjXTPbUQsvETaAil5J7zg1TOA4Wl8ClalLSvCh+AKWkxGdkL4/uM/zfbxPSNNw==", + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@optimizely/js-sdk-utils/-/js-sdk-utils-0.2.0.tgz", + "integrity": "sha512-aHEccRVc5YjWAdIVtniKfUE3tuzHriIWZTS4sLEq/lXkNTITSL1jrBEJD91CVY5BahWu/aG/aOafrA7XGH3sDQ==", "requires": { "uuid": "^3.3.2" } diff --git a/packages/event-processor/package.json b/packages/event-processor/package.json index 40de23765..727bf6231 100644 --- a/packages/event-processor/package.json +++ b/packages/event-processor/package.json @@ -38,7 +38,7 @@ }, "dependencies": { "@optimizely/js-sdk-logging": "^0.1.0", - "@optimizely/js-sdk-utils": "^0.1.0" + "@optimizely/js-sdk-utils": "^0.2.0" }, "devDependencies": { "@types/jest": "^24.0.9", diff --git a/packages/event-processor/src/eventProcessor.ts b/packages/event-processor/src/eventProcessor.ts index 62f8b4597..35d1dd177 100644 --- a/packages/event-processor/src/eventProcessor.ts +++ b/packages/event-processor/src/eventProcessor.ts @@ -22,6 +22,7 @@ import { } from './eventDispatcher' import { EventQueue, DefaultEventQueue, SingleEventQueue } from './eventQueue' import { getLogger } from '@optimizely/js-sdk-logging' +import { NOTIFICATION_TYPES, NotificationCenter } from '@optimizely/js-sdk-utils' const logger = getLogger('EventProcessor') @@ -37,15 +38,18 @@ const MIN_FLUSH_INTERVAL = 100 export abstract class AbstractEventProcessor implements EventProcessor { protected dispatcher: EventDispatcher protected queue: EventQueue + private notificationCenter?: NotificationCenter constructor({ dispatcher, flushInterval = 30000, maxQueueSize = 3000, + notificationCenter, }: { dispatcher: EventDispatcher flushInterval?: number maxQueueSize?: number + notificationCenter?: NotificationCenter }) { this.dispatcher = dispatcher @@ -61,6 +65,7 @@ export abstract class AbstractEventProcessor implements EventProcessor { sink: buffer => this.drainQueue(buffer), }) } + this.notificationCenter = notificationCenter } drainQueue(buffer: ProcessableEvents[]): Promise { @@ -73,6 +78,13 @@ export abstract class AbstractEventProcessor implements EventProcessor { this.dispatcher.dispatchEvent(formattedEvent, () => { resolve() }) + + if (this.notificationCenter) { + this.notificationCenter.sendNotifications( + NOTIFICATION_TYPES.LOG_EVENT, + formattedEvent + ) + } }) }) diff --git a/packages/utils/package-lock.json b/packages/utils/package-lock.json index 3e5ad3ac6..5afb33344 100644 --- a/packages/utils/package-lock.json +++ b/packages/utils/package-lock.json @@ -1,6 +1,6 @@ { "name": "@optimizely/js-sdk-utils", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": {