Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/resources/touch_ntp/eventtracker.js

Issue 6661024: Use a specialized new tab page in TOUCH_UI builds (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Changes for CR feedback from Arv Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 'use strict';
arv (Not doing code reviews) 2011/03/15 23:53:43 Please do not use 'use strict' at the top level. I
Rick Byers 2011/03/16 22:06:02 Oh, because we concatenate all the files together
arv (Not doing code reviews) 2011/03/16 22:55:05 Yup
15
16 /**
17 * Create an EventTracker to track a set of events.
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 * @constructor
22 */
23 function EventTracker() {
24 /**
25 * @type {Array.<EventTracker.Entry>}
26 * @private
27 */
28 this.listeners_ = [];
29 }
30
31 /**
32 * The type of the internal tracking entry.
33 * @typedef {{node: !Node,
34 * eventType: string,
35 * listener: Function,
36 * capture: boolean}}
37 */
38 EventTracker.Entry;
39
40 EventTracker.prototype = {
41 /**
42 * Add an event listener - replacement for Node.addEventListener.
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 * @param {!Node} node The DOM node to remove a listener from.
62 * @param {string} eventType The type of event to remove.
63 */
64 remove: function(node, eventType) {
65 this.listeners_ = this.listeners_.filter(function(h) {
66 if (h.node == node && h.eventType == eventType) {
67 EventTracker.removeEventListener_(h);
68 return false;
69 }
70 return true;
71 });
72 },
73
74 /**
75 * Remove all event listeners added with this EventTracker.
76 */
77 removeAll: function() {
78 this.listeners_.forEach(EventTracker.removeEventListener_);
79 this.listeners_ = [];
80 }
81 };
82
83 /**
84 * Remove a single event listener given it's tracker entry. It's up to the
85 * caller to ensure the entry is removed from listeners_.
86 * @param {EventTracker.Entry} h The entry describing the listener to remove.
87 * @private
88 */
89 EventTracker.removeEventListener_ = function(h)
90 {
91 h.node.removeEventListener(h.eventType, h.listener, h.capture);
92 };
93
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698