Index: remoting/webapp/base/js/base.js |
diff --git a/remoting/webapp/base/js/base.js b/remoting/webapp/base/js/base.js |
index 11bd091acf0ca9d5397e91db878a399403450da9..abde63125c73e72b2d4370f1f1216a108df60b58 100644 |
--- a/remoting/webapp/base/js/base.js |
+++ b/remoting/webapp/base/js/base.js |
@@ -62,6 +62,25 @@ base.Disposable = function() {}; |
base.Disposable.prototype.dispose = function() {}; |
/** |
+ * @constructor |
+ * @param {...base.Disposable} var_args |
+ * @implements {base.Disposable} |
+ */ |
+base.Disposables = function(var_args) { |
+ /** |
+ * @type {Array.<base.Disposable>} |
+ * @private |
+ */ |
+ this.disposables_ = Array.prototype.slice.call(arguments, 0); |
+}; |
+ |
+base.Disposables.prototype.dispose = function() { |
+ for (var i = 0; i < this.disposables_.length; i++) { |
+ this.disposables_[i].dispose(); |
+ } |
+}; |
+ |
+/** |
* A utility function to invoke |obj|.dispose without a null check on |obj|. |
* @param {base.Disposable} obj |
*/ |
@@ -433,6 +452,69 @@ base.EventSource.prototype = { |
}; |
/** |
+ * An light weight object that helps managing the lifetime of an event |
Jamie
2015/02/02 23:46:19
s/An/A/
s/light weight/lightweight/
s/managing/man
|
+ * listener. |
+ * |
+ * For example, do the following if you want to automatically unhook events |
+ * when your object is disposed: |
+ * |
+ * var MyConstructor = function(domElement) { |
+ * this.eventHooks_ = new base.Disposables( |
+ * new base.EventHook(domElement, 'click', this.onClick_.bind(this)), |
+ * new base.EventHook(domElement, 'keydown', this.onClick_.bind(this)), |
+ * new base.ChromeEventHook(chrome.runtime.onMessage, |
+ * this.onMessage_.bind(this)) |
+ * ); |
+ * } |
+ * |
+ * MyConstructor.prototype.dispose = function () { |
Jamie
2015/02/02 23:46:20
No space before ()--even for comments :)
kelvinp
2015/02/03 01:42:01
Done.
|
+ * this.eventHooks_.dispose(); |
+ * this.eventHooks_ = null; |
+ * } |
+ * |
+ * @param {base.EventSource | HTMLElement} src |
+ * @param {string} eventName |
+ * @param {Function} listener |
kelvinp
2015/02/02 23:08:34
Function is the JavaScript official type name of f
|
+ * @param {boolean=} opt_capture |
+ * |
+ * @constructor |
+ * @implements {base.Disposable} |
+ */ |
+base.EventHook = function(src, eventName, listener, opt_capture) { |
+ this.src_ = src; |
+ this.eventName_ = eventName; |
+ this.listener_ = listener; |
+ this.capture_ = opt_capture; |
+ base.debug.assert( |
+ (typeof opt_capture === 'boolean') === (src instanceof HTMLElement), |
+ 'opt_capture is only valid for DOM Events.'); |
Jamie
2015/02/02 23:46:19
I think this would be better guarded against by se
|
+ src.addEventListener(eventName, listener, opt_capture); |
+}; |
+ |
+base.EventHook.prototype.dispose = function() { |
+ this.src_.removeEventListener(this.eventName_, this.listener_, this.capture_); |
+}; |
+ |
+/** |
+ * An event hook implementation for Chrome Events. |
+ * |
+ * @param {chrome.Event} src |
+ * @param {Function} listener |
+ * |
+ * @constructor |
+ * @implements {base.Disposable} |
+ */ |
+base.ChromeEventHook = function(src, listener) { |
+ this.src_ = src; |
+ this.listener_ = listener; |
+ src.addListener(listener); |
+}; |
+ |
+base.ChromeEventHook.prototype.dispose = function() { |
+ this.src_.removeListener(this.listener_); |
+}; |
+ |
+/** |
* Converts UTF-8 string to ArrayBuffer. |
* |
* @param {string} string |
@@ -444,7 +526,7 @@ base.encodeUtf8 = function(string) { |
for (var i = 0; i < utf8String.length; i++) |
result[i] = utf8String.charCodeAt(i); |
return result.buffer; |
-} |
+}; |
/** |
* Decodes UTF-8 string from ArrayBuffer. |
@@ -455,7 +537,7 @@ base.encodeUtf8 = function(string) { |
base.decodeUtf8 = function(buffer) { |
return decodeURIComponent( |
escape(String.fromCharCode.apply(null, new Uint8Array(buffer)))); |
-} |
+}; |
/** |
* Generate a nonce, to be used as an xsrf protection token. |
@@ -479,4 +561,4 @@ base.jsonParseSafe = function(jsonString) { |
} catch (err) { |
return undefined; |
} |
-} |
+}; |