Skip to content

Dont use pendingEventsDispatcher with user defined eventDispatcher #289

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions packages/optimizely-sdk/lib/index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,26 @@ module.exports = {
config.skipJSONValidation = true;
}

var wrappedEventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
eventDispatcher: config.eventDispatcher || defaultEventDispatcher,
});
if (!hasRetriedEvents) {
wrappedEventDispatcher.sendPendingEvents();
hasRetriedEvents = true;
var eventDispatcher;
// prettier-ignore
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
// only wrap the event dispatcher with pending events retry if the user didnt override
eventDispatcher = new eventProcessor.LocalStoragePendingEventsDispatcher({
eventDispatcher: defaultEventDispatcher,
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that we we no longer allow a custom eventDispatcher to be used with LocalStoragePendingEventsDispatcher, does passing eventDispatcher to LocalStoragePendingEventsDispatcher still make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, you mean the idea of having an event dispatcher that wraps another event dispatcher.

Probably isn't the most ideal design, but I dont hate it. If we just made this the default event dispatcher, without wrapping it, then we'd be replacing the existing defaultEventDispatcher implementation. That could be a bit strange for people using the eventDispatcher in a custom manner.

When we made the decision to only wrap the defaultEventDispatcher, I did feel a bit safer about this pattern then replacement.

WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense to me. Initially, I thought this wrapper pattern was weird given that it only ever wraps one thing, but it is safe, and I like that pending-events-related logic is isolated.

});

if (!hasRetriedEvents) {
eventDispatcher.sendPendingEvents();
hasRetriedEvents = true;
}
} else {
eventDispatcher = config.eventDispatcher;
}

config = fns.assignIn({
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
}, config, {
eventDispatcher: wrappedEventDispatcher,
eventDispatcher: eventDispatcher,
// always get the OptimizelyLogger facade from logging
logger: logger,
errorHandler: logging.getErrorHandler(),
Expand Down
33 changes: 30 additions & 3 deletions packages/optimizely-sdk/lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('javascript-sdk', function() {
requests.push(req);
};

sinon.spy(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
sinon.stub(LocalStoragePendingEventsDispatcher.prototype, 'sendPendingEvents');
});

afterEach(function() {
Expand All @@ -73,11 +73,39 @@ describe('javascript-sdk', function() {
xhr.restore();
});

describe('when an eventDispatcher is not passed in', function() {
it('should wrap the default eventDispatcher and invoke sendPendingEvents', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});

sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
});
});

describe('when an eventDispatcher is passed in', function() {
it('should NOT wrap the default eventDispatcher and invoke sendPendingEvents', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
optlyInstance.onReady().catch(function() {});

sinon.assert.notCalled(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
});
});

it('should invoke resendPendingEvents at most once', function() {
var optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
// Invalid datafile causes onReady Promise rejection - catch this error
Expand All @@ -88,7 +116,6 @@ describe('javascript-sdk', function() {
optlyInstance = optimizelyFactory.createInstance({
datafile: {},
errorHandler: fakeErrorHandler,
eventDispatcher: fakeEventDispatcher,
logger: silentLogger,
});
optlyInstance.onReady().catch(function() {});
Expand Down