Skip to content

Commit

Permalink
Errors: Call removeEventListener before addEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
cvazac authored and nicjansma committed Apr 4, 2018
1 parent 1e79cd4 commit 2d2cfd0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugins/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,12 @@
return;
}

var rEL;
if (functionName === "addEventListener") {
// grab the native
rEL = that.removeEventListener;
}

that[functionName] = function() {
try {
var args = Array.prototype.slice.call(arguments);
Expand All @@ -1336,6 +1342,14 @@
// if the callback is already tracked, we won't call addEventListener
return;
}
if (rEL) {
// Remove the listener before adding it back in.
// This takes care of the (pathological) case where code is relying on the native
// de-dupping that the browser provides and BOOMR instruments `addEventListener` between
// their redundant calls to `addEventListener`.
// We detach with the native because there's no point in calling our wrapped version.
rEL.apply(targetObj, arguments);
}
}

return origFn.apply(targetObj, args);
Expand Down
27 changes: 27 additions & 0 deletions tests/page-templates/14-errors/41-addEventListener-dedupping.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= header %>
<%= boomerangDelayedSnippet %>
<script src="./41-addEventListener-dedupping.js" type="text/javascript"></script>
<script>
function handler() {
BOOMR.appendVar("foo", "bar");
}
function attach() {
window.addEventListener("foo", handler);
}

BOOMR_test.init({
testAfterOnBeacon: 1,
Errors: {
enabled: true,
monitorEvents: true,
sendAfterOnload: true
},
onBoomerangLoaded: function() {
attach();
window.dispatchEvent(new Event("foo"));
}
});

attach();
</script>
<%= footer %>
10 changes: 10 additions & 0 deletions tests/page-templates/14-errors/41-addEventListener-dedupping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*eslint-env mocha*/

describe("e2e/14-errors/41-addEventListener-dedupping", function() {
var tf = BOOMR.plugins.TestFramework;
it("Should have only fired the foo handler once", function(done) {
var b = tf.lastBeacon();
assert.equal(b.foo, "bar");
done();
});
});

0 comments on commit 2d2cfd0

Please sign in to comment.