Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** @fileoverview | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
@fileoverview EventTracker ...
Rick Byers
2011/03/11 02:44:33
Done.
| |
| 6 * EventTracker is a simple class that manages the addition and removal of | |
| 7 * DOM event listeners. In particular, it keeps track of all listeners that | |
| 8 * have been added and makes it easy to remove some or all of them without | |
| 9 * requiring all the information again. This is particularly handy when | |
| 10 * the listener is a generated function such as a lambda or the result of | |
| 11 * calling Function.bind. The goal of this class is to make it easier to | |
| 12 * avoid memory leaks caused by DOM<->JS cycles - removing event listeners | |
| 13 * breakes the DOM->JS part of the cycle. | |
| 14 */ | |
| 15 | |
| 16 /** @constructor | |
| 17 * Create an EventTracker to track a set of events. | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
/**
* Description
* @constructor
*/
Rick Byers
2011/03/11 02:44:33
Done.
| |
| 18 * EventTracker instances are typically tied 1:1 with other objects or | |
| 19 * DOM elements whose listeners should be removed when the object is disposed | |
| 20 * or the corresponding elements are removed from the DOM. | |
| 21 */ | |
| 22 function EventTracker() { | |
| 23 /** | |
| 24 * @type {Array.<EventTracker.Entry>} | |
| 25 * @private | |
| 26 */ | |
| 27 this.listeners_ = []; | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * The type of the internal tracking entry. | |
| 32 * @typedef {{node: !Node, | |
| 33 * eventType: string, | |
| 34 * listener: Function, | |
| 35 * capture: boolean}} | |
| 36 */ | |
| 37 EventTracker.Entry; | |
| 38 | |
| 39 /** | |
| 40 * Add an event listener - replacement for Node.addEventListener. | |
| 41 * @param {!Node} node The DOM node to add a listener to. | |
| 42 * @param {string} eventType The type of event to subscribe to. | |
| 43 * @param {Function} listener The listener to add. | |
| 44 * @param {boolean} capture Whether to invoke during the capture phase. | |
| 45 */ | |
| 46 EventTracker.prototype.add = function(node, eventType, listener, capture) | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
Use the following syntax instead:
EventTracker.pr
Rick Byers
2011/03/11 02:44:33
I like it, thanks! Done. Does it not matter that
arv (Not doing code reviews)
2011/03/11 20:08:46
No. constructor is not reliable anyway.
function
| |
| 47 { | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
{ before new line
Rick Byers
2011/03/11 02:44:33
Done.
| |
| 48 var h = {node: node, | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
var h = {
eventType: ...
...
};
Rick Byers
2011/03/11 02:44:33
Done.
| |
| 49 eventType: eventType, | |
| 50 listener: listener, | |
| 51 capture: capture}; | |
| 52 this.listeners_.push(h); | |
| 53 node.addEventListener(eventType, listener, capture); | |
| 54 }; | |
| 55 | |
| 56 /** | |
| 57 * Remove any specified event listeners added with this EventTracker. | |
| 58 * @param {!Node} node The DOM node to remove a listener from. | |
| 59 * @param {string} eventType The type of event to remove. | |
| 60 */ | |
| 61 EventTracker.prototype.remove = function(node, eventType) | |
| 62 { | |
| 63 this.listeners_ = this.listeners_.filter(function(h) { | |
| 64 if (h.node == node && h.eventType == eventType) { | |
| 65 EventTracker.removeEventListener_(h); | |
| 66 return false; | |
| 67 } | |
| 68 return true; | |
| 69 }); | |
| 70 }; | |
| 71 | |
| 72 /** | |
| 73 * Remove all event listeners added with this EventTracker. | |
| 74 */ | |
| 75 EventTracker.prototype.removeAll = function() | |
| 76 { | |
| 77 this.listeners_.forEach(EventTracker.removeEventListener_); | |
| 78 this.listeners_ = []; | |
| 79 }; | |
| 80 | |
| 81 /** | |
| 82 * Remove a single event listener given it's tracker entry. It's up to the | |
| 83 * caller to ensure the entry is removed from listeners_. | |
| 84 * @param {EventTracker.Entry} h The entry describing the listener to remove. | |
| 85 * @private | |
|
Rick Byers
2011/03/11 02:44:33
Note that this is a static method (doesn't access
arv (Not doing code reviews)
2011/03/11 20:08:46
Yes, looks perfect
| |
| 86 */ | |
| 87 EventTracker.removeEventListener_ = function(h) | |
| 88 { | |
| 89 h.node.removeEventListener(h.eventType, h.listener, h.capture); | |
| 90 }; | |
| 91 | |
| 92 | |
|
arv (Not doing code reviews)
2011/03/10 19:25:59
remove empty lines
Rick Byers
2011/03/11 02:44:33
Done.
| |
| 93 | |
| 94 | |
| 95 | |
| OLD | NEW |