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 // Use an anonymous function to enable strict mode just for this file (which |
| 16 // will be concatenated with other files when embedded in Chrome) |
| 17 var EventTracker = (function() { |
| 18 'use strict'; |
| 19 |
| 20 /** |
| 21 * Create an EventTracker to track a set of events. |
| 22 * EventTracker instances are typically tied 1:1 with other objects or |
| 23 * DOM elements whose listeners should be removed when the object is disposed |
| 24 * or the corresponding elements are removed from the DOM. |
| 25 * @constructor |
| 26 */ |
| 27 function EventTracker() { |
| 28 /** |
| 29 * @type {Array.<EventTracker.Entry>} |
| 30 * @private |
| 31 */ |
| 32 this.listeners_ = []; |
| 33 } |
| 34 |
| 35 /** |
| 36 * The type of the internal tracking entry. |
| 37 * @typedef {{node: !Node, |
| 38 * eventType: string, |
| 39 * listener: Function, |
| 40 * capture: boolean}} |
| 41 */ |
| 42 EventTracker.Entry; |
| 43 |
| 44 EventTracker.prototype = { |
| 45 /** |
| 46 * Add an event listener - replacement for Node.addEventListener. |
| 47 * @param {!Node} node The DOM node to add a listener to. |
| 48 * @param {string} eventType The type of event to subscribe to. |
| 49 * @param {Function} listener The listener to add. |
| 50 * @param {boolean} capture Whether to invoke during the capture phase. |
| 51 */ |
| 52 add: function(node, eventType, listener, capture) { |
| 53 var h = { |
| 54 node: node, |
| 55 eventType: eventType, |
| 56 listener: listener, |
| 57 capture: capture |
| 58 }; |
| 59 this.listeners_.push(h); |
| 60 node.addEventListener(eventType, listener, capture); |
| 61 }, |
| 62 |
| 63 /** |
| 64 * Remove any specified event listeners added with this EventTracker. |
| 65 * @param {!Node} node The DOM node to remove a listener from. |
| 66 * @param {string} eventType The type of event to remove. |
| 67 */ |
| 68 remove: function(node, eventType) { |
| 69 this.listeners_ = this.listeners_.filter(function(h) { |
| 70 if (h.node == node && h.eventType == eventType) { |
| 71 EventTracker.removeEventListener_(h); |
| 72 return false; |
| 73 } |
| 74 return true; |
| 75 }); |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Remove all event listeners added with this EventTracker. |
| 80 */ |
| 81 removeAll: function() { |
| 82 this.listeners_.forEach(EventTracker.removeEventListener_); |
| 83 this.listeners_ = []; |
| 84 } |
| 85 }; |
| 86 |
| 87 /** |
| 88 * Remove a single event listener given it's tracker entry. It's up to the |
| 89 * caller to ensure the entry is removed from listeners_. |
| 90 * @param {EventTracker.Entry} h The entry describing the listener to remove. |
| 91 * @private |
| 92 */ |
| 93 EventTracker.removeEventListener_ = function(h) { |
| 94 h.node.removeEventListener(h.eventType, h.listener, h.capture); |
| 95 }; |
| 96 |
| 97 return EventTracker; |
| 98 })(); |
| 99 |
OLD | NEW |