Skip to content

Commit

Permalink
fixed async adding listeners after event firing
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstantineYurevich committed Feb 4, 2016
1 parent 01a98e1 commit 4b17896
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 28 deletions.
78 changes: 58 additions & 20 deletions dist/dd-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5651,6 +5651,10 @@ var _debug = require('debug');

var _debug2 = _interopRequireDefault(_debug);

var _noop = require('./functions/noop.js');

var _noop2 = _interopRequireDefault(_noop);

var _deleteProperty = require('./functions/deleteProperty.js');

var _deleteProperty2 = _interopRequireDefault(_deleteProperty);
Expand Down Expand Up @@ -5691,6 +5695,7 @@ var _previousDigitalData = {};
var _digitalData = {};
var _checkForChangesIntervalId = undefined;
var _autoEvents = undefined;
var _isInitialized = false;

var _callbackOnComplete = function _callbackOnComplete(error) {
if (error) {
Expand Down Expand Up @@ -5741,6 +5746,8 @@ var EventManager = (function () {
_checkForChangesIntervalId = setInterval(function () {
_this.checkForChanges();
}, 100);

_isInitialized = true;
};

EventManager.prototype.setAutoEvents = function setAutoEvents(autoEvents) {
Expand All @@ -5760,7 +5767,11 @@ var EventManager = (function () {
}
};

EventManager.prototype.addCallback = function addCallback(callbackInfo) {
EventManager.prototype.addCallback = function addCallback(callbackInfo, processPastEvents) {
if (processPastEvents !== false) {
processPastEvents = true;
}

if (!Array.isArray(callbackInfo) || callbackInfo.length < 2) {
return;
}
Expand All @@ -5770,7 +5781,7 @@ var EventManager = (function () {
return;
}
var asyncHandler = _async2['default'].asyncify(callbackInfo[2]);
this.on(callbackInfo[1], asyncHandler);
this.on(callbackInfo[1], asyncHandler, processPastEvents);
}if (callbackInfo[0] === 'off') {
// TODO
}
Expand Down Expand Up @@ -5841,7 +5852,6 @@ var EventManager = (function () {
}

var eventCopy = (0, _componentClone2['default'])(event);
(0, _deleteProperty2['default'])(eventCopy, 'updateDigitalData');
(0, _deleteProperty2['default'])(eventCopy, 'callback');
if (eventCopy.enrichEventData !== false) {
eventCopy = _this2.enrichEventWithData(eventCopy);
Expand All @@ -5858,7 +5868,7 @@ var EventManager = (function () {
event.hasFired = true;
};

EventManager.prototype.on = function on(eventInfo, handler) {
EventManager.prototype.on = function on(eventInfo, handler, processPastEvents) {
var _eventInfo$split = eventInfo.split(':');

var type = _eventInfo$split[0];
Expand All @@ -5875,9 +5885,12 @@ var EventManager = (function () {
handler: handler
});
}
if (_isInitialized && type === 'event' && processPastEvents) {
this.applyCallbackForPastEvents(handler);
}
};

EventManager.prototype.fireUnfiredEvents = function fireUnfiredEvents() {
EventManager.prototype.applyCallbackForPastEvents = function applyCallbackForPastEvents(handler) {
var events = _digitalData.events;
var event = undefined;
for (var _iterator3 = events, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
Expand All @@ -5890,22 +5903,46 @@ var EventManager = (function () {
event = _i3.value;
}

if (!event.hasFired) {
this.fireEvent(event);
if (event.hasFired) {
var eventCopy = (0, _componentClone2['default'])(event);
(0, _deleteProperty2['default'])(eventCopy, 'callback');
if (eventCopy.enrichEventData !== false) {
eventCopy = this.enrichEventWithData(eventCopy);
}
handler(eventCopy, _noop2['default']);
}
}
};

EventManager.prototype.addEarlyCallbacks = function addEarlyCallbacks() {
var callbackInfo = undefined;
for (var _iterator4 = _ddListener, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
EventManager.prototype.fireUnfiredEvents = function fireUnfiredEvents() {
var events = _digitalData.events;
var event = undefined;
for (var _iterator4 = events, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
callbackInfo = _iterator4[_i4++];
event = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
callbackInfo = _i4.value;
event = _i4.value;
}

if (!event.hasFired) {
this.fireEvent(event);
}
}
};

EventManager.prototype.addEarlyCallbacks = function addEarlyCallbacks() {
var callbackInfo = undefined;
for (var _iterator5 = _ddListener, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
if (_isArray5) {
if (_i5 >= _iterator5.length) break;
callbackInfo = _iterator5[_i5++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
callbackInfo = _i5.value;
}

this.addCallback(callbackInfo);
Expand All @@ -5915,16 +5952,16 @@ var EventManager = (function () {
EventManager.prototype.enrichEventWithData = function enrichEventWithData(event) {
var enrichableVars = ['product', 'transaction', 'campaign', 'user', 'page'];

for (var _iterator5 = enrichableVars, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
for (var _iterator6 = enrichableVars, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
var _ref;

if (_isArray5) {
if (_i5 >= _iterator5.length) break;
_ref = _iterator5[_i5++];
if (_isArray6) {
if (_i6 >= _iterator6.length) break;
_ref = _iterator6[_i6++];
} else {
_i5 = _iterator5.next();
if (_i5.done) break;
_ref = _i5.value;
_i6 = _iterator6.next();
if (_i6.done) break;
_ref = _i6.value;
}

var enrichableVar = _ref;
Expand All @@ -5951,7 +5988,7 @@ var EventManager = (function () {

exports['default'] = EventManager;

},{"./DDHelper.js":55,"./EventDataEnricher.js":57,"./functions/after.js":63,"./functions/deleteProperty.js":64,"./functions/jsonIsEqual.js":70,"./functions/size.js":76,"async":1,"component-clone":4,"debug":44}],59:[function(require,module,exports){
},{"./DDHelper.js":55,"./EventDataEnricher.js":57,"./functions/after.js":63,"./functions/deleteProperty.js":64,"./functions/jsonIsEqual.js":70,"./functions/noop.js":74,"./functions/size.js":76,"async":1,"component-clone":4,"debug":44}],59:[function(require,module,exports){
'use strict';

function _typeof(obj) { return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }
Expand Down Expand Up @@ -6487,6 +6524,7 @@ var ddManager = {
*
* {
* autoEvents: true,
* domain: 'example.com',
* sessionLength: 3600,
* integrations: {
* 'Google Tag Manager': {
Expand Down
6 changes: 3 additions & 3 deletions dist/dd-manager.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "digital-data-manager",
"description": "The hassle-free way to integrate Digital Data Layer on your website.",
"author": "Driveback LLC <[email protected]>",
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"main": "dist/dd-manager.js",
"directories": {
Expand Down
33 changes: 29 additions & 4 deletions src/EventManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import clone from 'component-clone';
import async from 'async';
import debug from 'debug';
import noop from './functions/noop.js';
import deleteProperty from './functions/deleteProperty.js';
import size from './functions/size.js';
import after from './functions/after.js';
Expand All @@ -14,6 +15,7 @@ let _previousDigitalData = {};
let _digitalData = {};
let _checkForChangesIntervalId;
let _autoEvents;
let _isInitialized = false;

const _callbackOnComplete = (error) => {
if (error) {
Expand Down Expand Up @@ -61,6 +63,8 @@ class EventManager {
_checkForChangesIntervalId = setInterval(() => {
this.checkForChanges();
}, 100);

_isInitialized = true;
}

setAutoEvents(autoEvents) {
Expand All @@ -80,7 +84,11 @@ class EventManager {
}
}

addCallback(callbackInfo) {
addCallback(callbackInfo, processPastEvents) {
if (processPastEvents !== false) {
processPastEvents = true;
}

if (!Array.isArray(callbackInfo) || callbackInfo.length < 2) {
return;
}
Expand All @@ -90,7 +98,7 @@ class EventManager {
return;
}
const asyncHandler = async.asyncify(callbackInfo[2]);
this.on(callbackInfo[1], asyncHandler);
this.on(callbackInfo[1], asyncHandler, processPastEvents);
} if (callbackInfo[0] === 'off') {
// TODO
}
Expand Down Expand Up @@ -140,7 +148,6 @@ class EventManager {

for (eventCallback of _callbacks.event) {
let eventCopy = clone(event);
deleteProperty(eventCopy, 'updateDigitalData');
deleteProperty(eventCopy, 'callback');
if (eventCopy.enrichEventData !== false) {
eventCopy = this.enrichEventWithData(eventCopy);
Expand All @@ -156,7 +163,7 @@ class EventManager {
event.hasFired = true;
}

on(eventInfo, handler) {
on(eventInfo, handler, processPastEvents) {
const [type, key] = eventInfo.split(':');
_callbacks[type] = _callbacks[type] || [];
if (key) {
Expand All @@ -169,6 +176,24 @@ class EventManager {
handler,
});
}
if (_isInitialized && type === 'event' && processPastEvents) {
this.applyCallbackForPastEvents(handler);
}
}

applyCallbackForPastEvents(handler) {
const events = _digitalData.events;
let event;
for (event of events) {
if (event.hasFired) {
let eventCopy = clone(event);
deleteProperty(eventCopy, 'callback');
if (eventCopy.enrichEventData !== false) {
eventCopy = this.enrichEventWithData(eventCopy);
}
handler(eventCopy, noop);
}
}
}

fireUnfiredEvents() {
Expand Down
1 change: 1 addition & 0 deletions src/ddManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const ddManager = {
*
* {
* autoEvents: true,
* domain: 'example.com',
* sessionLength: 3600,
* integrations: {
* 'Google Tag Manager': {
Expand Down
18 changes: 18 additions & 0 deletions test/EventManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ describe('EventManager', () => {
});
});

it('should process past events event if listener was added later', (done) => {
_eventManager.initialize();

window.digitalData.events.push({
name: 'Clicked Product',
category: 'Ecommerce',
product: {
id: '123',
name: 'Test Product'
}
});

window.ddListener.push(['on', 'event', (e) => {
assert.ok(true);
done();
}]);
});

});

describe(': listening for digitalData changes', () => {
Expand Down

0 comments on commit 4b17896

Please sign in to comment.