Skip to content

feat(event processor): Send log event notification upon event dispatch when notification center available #339

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
merged 12 commits into from
Aug 12, 2019
Merged
28 changes: 28 additions & 0 deletions packages/event-processor/__tests__/v1EventProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
})
})
16 changes: 13 additions & 3 deletions packages/event-processor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/event-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these changes are not supposed to be in this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did intend this. I'm updating to 0.2.0 so I can get access to NOTIFICATION_TYPES and NotificationCenter, new exports from js-sdk-utils. But did you want this update itself to be separated into its own PR?

},
"devDependencies": {
"@types/jest": "^24.0.9",
Expand Down
12 changes: 12 additions & 0 deletions packages/event-processor/src/eventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

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

constructor({
dispatcher,
flushInterval = 30000,
maxQueueSize = 3000,
notificationCenter,
}: {
dispatcher: EventDispatcher
flushInterval?: number
maxQueueSize?: number
notificationCenter?: NotificationCenter
}) {
this.dispatcher = dispatcher

Expand All @@ -61,6 +65,7 @@ export abstract class AbstractEventProcessor implements EventProcessor {
sink: buffer => this.drainQueue(buffer),
})
}
this.notificationCenter = notificationCenter
}

drainQueue(buffer: ProcessableEvents[]): Promise<any> {
Expand All @@ -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
)
}
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.