| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Behavior to be used by Polymer elements that want to | 6 * @fileoverview Behavior to be used by Polymer elements that want to |
| 7 * automatically remove WebUI listeners when detached. | 7 * automatically remove WebUI listeners when detached. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 /** @polymerBehavior */ | 10 /** @polymerBehavior */ |
| 11 var WebUIListenerBehavior = { | 11 var WebUIListenerBehavior = { |
| 12 properties: { | 12 properties: { |
| 13 /** | 13 /** |
| 14 * Holds WebUI listeners that need to be removed when this element is | 14 * Holds WebUI listeners that need to be removed when this element is |
| 15 * destroyed. | 15 * destroyed. |
| 16 * @private {!Array<!WebUIListener>} | 16 * @private {!Array<!WebUIListener>} |
| 17 */ | 17 */ |
| 18 webUIListeners_: { | 18 webUIListeners_: { |
| 19 type: Array, | 19 type: Array, |
| 20 value: function() { return []; }, | 20 value: function() { |
| 21 return []; |
| 22 }, |
| 21 }, | 23 }, |
| 22 }, | 24 }, |
| 23 | 25 |
| 24 /** | 26 /** |
| 25 * Adds a WebUI listener and registers it for automatic removal when this | 27 * Adds a WebUI listener and registers it for automatic removal when this |
| 26 * element is detached. | 28 * element is detached. |
| 27 * Note: Do not use this method if you intend to remove this listener | 29 * Note: Do not use this method if you intend to remove this listener |
| 28 * manually (use cr.addWebUIListener directly instead). | 30 * manually (use cr.addWebUIListener directly instead). |
| 29 * | 31 * |
| 30 * @param {string} eventName The event to listen to. | 32 * @param {string} eventName The event to listen to. |
| 31 * @param {!Function} callback The callback run when the event is fired. | 33 * @param {!Function} callback The callback run when the event is fired. |
| 32 */ | 34 */ |
| 33 addWebUIListener: function(eventName, callback) { | 35 addWebUIListener: function(eventName, callback) { |
| 34 this.webUIListeners_.push(cr.addWebUIListener(eventName, callback)); | 36 this.webUIListeners_.push(cr.addWebUIListener(eventName, callback)); |
| 35 }, | 37 }, |
| 36 | 38 |
| 37 /** @override */ | 39 /** @override */ |
| 38 detached: function() { | 40 detached: function() { |
| 39 while (this.webUIListeners_.length > 0) { | 41 while (this.webUIListeners_.length > 0) { |
| 40 cr.removeWebUIListener(this.webUIListeners_.pop()); | 42 cr.removeWebUIListener(this.webUIListeners_.pop()); |
| 41 } | 43 } |
| 42 }, | 44 }, |
| 43 }; | 45 }; |
| OLD | NEW |