Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Allow dispatching the same event multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Oct 10, 2013
1 parent 5bb221b commit abfa4bf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/wrappers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,12 @@
},
dispatchEvent: function(event) {
var target = getTargetToListenAt(this);
return target.dispatchEvent_(unwrap(event));
var nativeEvent = unwrap(event);
// Allow dispatching the same event again. This is safe because if user
// code calls this during an existing dispatch of the same event the
// native dispatchEvent throws (that is required by the spec).
handledEventsTable.set(nativeEvent, false);
return target.dispatchEvent_(nativeEvent);
}
};

Expand All @@ -654,7 +659,6 @@
forwardMethodsToWrapper(constructors, methodNames);
}


var originalElementFromPoint = document.elementFromPoint;

function elementFromPoint(self, document, x, y) {
Expand Down
39 changes: 39 additions & 0 deletions test/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -1277,4 +1277,43 @@ test('retarget order (multiple shadow roots)', function() {
text.dispatchEvent(new Event('x'));
});

test('dispatch same event object twice', function() {
var e = new Event('x', {bubbles: true});
var doc = wrap(document);

var count = 0;
function handler(e) {
count++;
};

doc.addEventListener('x', handler);

document.dispatchEvent(e);
assert.equal(count, 1);
document.dispatchEvent(e);
assert.equal(count, 2);
document.dispatchEvent(e);
assert.equal(count, 3);

doc.removeEventListener('x', handler);
});

test('Ensure nested dispatch is not allowed', function() {
var e = new Event('x', {bubbles: true});
var doc = wrap(document);

var count = 0;

doc.addEventListener('x', function f(e) {
count++;
assert.throws(function() {
doc.dispatchEvent(e);
});
});

doc.dispatchEvent(e);

assert.equal(count, 1);
});

});

0 comments on commit abfa4bf

Please sign in to comment.