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 EventTracker is a simple class that manages the addition and | |
| 6 * removal of DOM event listeners. In particular, it keeps track of all | |
| 7 * listeners that have been added and makes it easy to remove some or all of | |
| 8 * them without requiring all the information again. This is particularly | |
| 9 * handy when the listener is a generated function such as a lambda or the | |
| 10 * result of calling Function.bind. The goal of this class is to make it | |
| 11 * easier to avoid memory leaks caused by DOM<->JS cycles - removing event | |
| 12 * listeners breakes the DOM->JS part of the cycle. | |
| 13 */ | |
| 14 | |
| 15 /** | |
| 16 * Create an EventTracker to track a set of events. | |
| 17 * EventTracker instances are typically tied 1:1 with other objects or | |
| 18 * DOM elements whose listeners should be removed when the object is disposed | |
| 19 * or the corresponding elements are removed from the DOM. | |
| 20 * @constructor | |
| 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 EventTracker.prototype = { | |
| 40 /** | |
| 41 * Add an event listener - replacement for Node.addEventListener. | |
| 42 * @this {EventTracker} | |
|
arv (Not doing code reviews)
2011/03/11 20:08:47
I would just remove this and ignore those warnings
Rick Byers
2011/03/15 21:47:54
Done.
| |
| 43 * @param {!Node} node The DOM node to add a listener to. | |
| 44 * @param {string} eventType The type of event to subscribe to. | |
| 45 * @param {Function} listener The listener to add. | |
| 46 * @param {boolean} capture Whether to invoke during the capture phase. | |
| 47 */ | |
| 48 add: function(node, eventType, listener, capture) { | |
| 49 var h = { | |
| 50 node: node, | |
| 51 eventType: eventType, | |
| 52 listener: listener, | |
| 53 capture: capture | |
| 54 }; | |
| 55 this.listeners_.push(h); | |
| 56 node.addEventListener(eventType, listener, capture); | |
| 57 }, | |
| 58 | |
| 59 /** | |
| 60 * Remove any specified event listeners added with this EventTracker. | |
| 61 * @this {EventTracker} | |
| 62 * @param {!Node} node The DOM node to remove a listener from. | |
| 63 * @param {string} eventType The type of event to remove. | |
| 64 */ | |
| 65 remove: function(node, eventType) { | |
| 66 this.listeners_ = this.listeners_.filter(function(h) { | |
| 67 if (h.node == node && h.eventType == eventType) { | |
| 68 EventTracker.removeEventListener_(h); | |
| 69 return false; | |
| 70 } | |
| 71 return true; | |
| 72 }); | |
| 73 }, | |
| 74 | |
| 75 /** | |
| 76 * Remove all event listeners added with this EventTracker. | |
| 77 * @this {EventTracker} | |
| 78 */ | |
| 79 removeAll: function() { | |
| 80 this.listeners_.forEach(EventTracker.removeEventListener_); | |
| 81 this.listeners_ = []; | |
| 82 } | |
| 83 }; | |
| 84 | |
| 85 /** | |
| 86 * Remove a single event listener given it's tracker entry. It's up to the | |
| 87 * caller to ensure the entry is removed from listeners_. | |
| 88 * @param {EventTracker.Entry} h The entry describing the listener to remove. | |
| 89 * @private | |
| 90 */ | |
| 91 EventTracker.removeEventListener_ = function(h) | |
| 92 { | |
| 93 h.node.removeEventListener(h.eventType, h.listener, h.capture); | |
| 94 }; | |
| 95 | |
| OLD | NEW |