Skip to content

Commit 1f3d655

Browse files
authored
Update eventtarget.js
less branching for eventtarget changes
1 parent 5014ec3 commit 1f3d655

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

closure/goog/events/eventtarget.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ goog.events.EventTarget = function() {
8181
* @private {!Object}
8282
*/
8383
this.actualEventTarget_ = this;
84+
85+
/**
86+
* If true, when mixing in, avoid calling fireListeners on
87+
* the alternate target, call it on this instance
88+
* @private {boolean}
89+
*/
90+
this.alwaysFireLocal_ = false;
8491

8592
/**
8693
* Parent event target, used during event bubbling.
@@ -192,9 +199,10 @@ goog.events.EventTarget.prototype.dispatchEvent = function(e) {
192199
'infinite loop');
193200
}
194201
}
195-
196-
return goog.events.EventTarget.dispatchEventInternal_(
197-
this.actualEventTarget_, e, ancestorsTree);
202+
var actualTarget = this.actualEventTarget_;
203+
var eventTarget = this.alwaysFireLocal_ ? this : actualTarget;
204+
return goog.events.EventTarget.dispatchEventInternal_(
205+
actualTarget, eventTarget, e, ancestorsTree);
198206
};
199207

200208

@@ -316,9 +324,11 @@ goog.events.EventTarget.prototype.hasListener = function(
316324
* event. Mainly used for testing. For example, see
317325
* {@code goog.testing.events.mixinListenable}.
318326
* @param {!Object} target The target.
327+
* @param {boolean=} use an external target only (fireListeners is called on this EventTarget).
319328
*/
320-
goog.events.EventTarget.prototype.setTargetForTesting = function(target) {
329+
goog.events.EventTarget.prototype.setTargetForTesting = function(target, opt_external) {
321330
this.actualEventTarget_ = target;
331+
this.alwaysFireLocal_ = !!opt_external;
322332
};
323333

324334

@@ -337,7 +347,9 @@ goog.events.EventTarget.prototype.assertInitialized_ = function() {
337347
/**
338348
* Dispatches the given event on the ancestorsTree.
339349
*
340-
* @param {!Object} target The target to dispatch on.
350+
* @param {!Object} target The target for the event.
351+
* @param {!Object} eventTarget The target to dispatch on.
352+
* support for separating target and eventTarget permits proxy implementations
341353
* @param {goog.events.Event|Object|string} e The event object.
342354
* @param {Array<goog.events.Listenable>=} opt_ancestorsTree The ancestors
343355
* tree of the target, in reverse order from the closest ancestor
@@ -347,7 +359,7 @@ goog.events.EventTarget.prototype.assertInitialized_ = function() {
347359
* @private
348360
*/
349361
goog.events.EventTarget.dispatchEventInternal_ = function(
350-
target, e, opt_ancestorsTree) {
362+
target, eventTarget, e, opt_ancestorsTree, opt_dispatcher) {
351363
var type = e.type || /** @type {string} */ (e);
352364

353365
// If accepting a string or object, create a custom event object so that
@@ -375,7 +387,8 @@ goog.events.EventTarget.dispatchEventInternal_ = function(
375387

376388
// Executes capture and bubble listeners on the target.
377389
if (!e.propagationStopped_) {
378-
currentTarget = /** @type {?} */ (e.currentTarget = target);
390+
e.currentTarget = target;
391+
currentTarget /** @type {?} */ = eventTarget;
379392
rv = currentTarget.fireListeners(type, true, e) && rv;
380393
if (!e.propagationStopped_) {
381394
rv = currentTarget.fireListeners(type, false, e) && rv;

0 commit comments

Comments
 (0)