Skip to content

Commit 3398f89

Browse files
authored
feat(event processor): Send log event notification upon event dispatch when notification center available (#339)
Summary: With this change, the event processor can be constructed with a notification center. If a notification center is provided, it triggers a log event notification after the event is sent to the event dispatcher. Test plan: Added new unit test. Manually tested Issues: https://optimizely.atlassian.net/browse/OASIS-4976
1 parent 99f4f8a commit 3398f89

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

packages/event-processor/__tests__/v1EventProcessor.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '../src/eventDispatcher'
2424
import { EventProcessor } from '../src/eventProcessor'
2525
import { buildImpressionEventV1, makeBatchedEventV1 } from '../src/v1/buildEventV1'
26+
import { NotificationCenter, NOTIFICATION_TYPES } from '@optimizely/js-sdk-utils';
2627

2728
function createImpressionEvent() {
2829
return {
@@ -340,5 +341,32 @@ describe('LogTierV1EventProcessor', () => {
340341
// flushing should reset queue, at this point only has two events
341342
expect(dispatchStub).toHaveBeenCalledTimes(1)
342343
})
344+
345+
})
346+
347+
describe('when a notification center is provided', () => {
348+
it('should trigger a notification when the event dispatcher dispatches an event', () => {
349+
const dispatcher: EventDispatcher = {
350+
dispatchEvent: jest.fn()
351+
}
352+
353+
const notificationCenter: NotificationCenter = {
354+
sendNotifications: jest.fn()
355+
}
356+
357+
const processor = new LogTierV1EventProcessor({
358+
dispatcher,
359+
notificationCenter,
360+
maxQueueSize: 1,
361+
})
362+
processor.start()
363+
364+
const impressionEvent1 = createImpressionEvent()
365+
processor.process(impressionEvent1)
366+
367+
expect(notificationCenter.sendNotifications).toBeCalledTimes(1)
368+
const event = (dispatcher.dispatchEvent as jest.Mock).mock.calls[0][0]
369+
expect(notificationCenter.sendNotifications).toBeCalledWith(NOTIFICATION_TYPES.LOG_EVENT, event)
370+
})
343371
})
344372
})

packages/event-processor/package-lock.json

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/event-processor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"dependencies": {
4040
"@optimizely/js-sdk-logging": "^0.1.0",
41-
"@optimizely/js-sdk-utils": "^0.1.0"
41+
"@optimizely/js-sdk-utils": "^0.2.0"
4242
},
4343
"devDependencies": {
4444
"@types/jest": "^24.0.9",

packages/event-processor/src/eventProcessor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from './eventDispatcher'
2323
import { EventQueue, DefaultEventQueue, SingleEventQueue } from './eventQueue'
2424
import { getLogger } from '@optimizely/js-sdk-logging'
25+
import { NOTIFICATION_TYPES, NotificationCenter } from '@optimizely/js-sdk-utils'
2526

2627
const logger = getLogger('EventProcessor')
2728

@@ -37,15 +38,18 @@ const MIN_FLUSH_INTERVAL = 100
3738
export abstract class AbstractEventProcessor implements EventProcessor {
3839
protected dispatcher: EventDispatcher
3940
protected queue: EventQueue<ProcessableEvents>
41+
private notificationCenter?: NotificationCenter
4042

4143
constructor({
4244
dispatcher,
4345
flushInterval = 30000,
4446
maxQueueSize = 3000,
47+
notificationCenter,
4548
}: {
4649
dispatcher: EventDispatcher
4750
flushInterval?: number
4851
maxQueueSize?: number
52+
notificationCenter?: NotificationCenter
4953
}) {
5054
this.dispatcher = dispatcher
5155

@@ -61,6 +65,7 @@ export abstract class AbstractEventProcessor implements EventProcessor {
6165
sink: buffer => this.drainQueue(buffer),
6266
})
6367
}
68+
this.notificationCenter = notificationCenter
6469
}
6570

6671
drainQueue(buffer: ProcessableEvents[]): Promise<any> {
@@ -73,6 +78,13 @@ export abstract class AbstractEventProcessor implements EventProcessor {
7378
this.dispatcher.dispatchEvent(formattedEvent, () => {
7479
resolve()
7580
})
81+
82+
if (this.notificationCenter) {
83+
this.notificationCenter.sendNotifications(
84+
NOTIFICATION_TYPES.LOG_EVENT,
85+
formattedEvent
86+
)
87+
}
7688
})
7789
})
7890

packages/utils/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)