Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / renderer / resources / async_waiter.js
blob08410d74c9f1b4241c04d792924c47766acb5439
1 // Copyright 2014 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.
5 define('async_waiter', [
6 'mojo/public/js/bindings/support',
7 ], function(supportModule) {
8 /**
9 * @module async_waiter
12 /**
13 * @callback module:async_waiter.AsyncWaiter.Callback
14 * @param {number} result The result of waiting.
17 /**
18 * A waiter that waits for a handle to be ready for either reading or writing.
19 * @param {MojoHandle} handle The handle to wait on.
20 * @param {int} signals The signals to wait for handle to be ready for.
21 * @param {module:async_waiter.AsyncWaiter.Callback} callback The callback to
22 * call when handle is ready.
23 * @constructor
24 * @alias module:async_waiter.AsyncWaiter
26 function AsyncWaiter(handle, signals, callback) {
27 /**
28 * The handle to wait on.
29 * @type {MojoHandle}
30 * @private
32 this.handle_ = handle;
34 /**
35 * The signals to wait for.
36 * @type {number}
37 * @private
39 this.signals_ = signals;
41 /**
42 * The callback to invoke when
43 * |[handle_]{@link module:async_waiter.AsyncWaiter#handle_}| is ready.
44 * @type {module:async_waiter.AsyncWaiter.Callback}
45 * @private
47 this.callback_ = callback;
48 this.id_ = null;
51 /**
52 * Start waiting for the handle to be ready.
53 * @throws Will throw if this is already waiting.
55 AsyncWaiter.prototype.start = function() {
56 if (this.id_)
57 throw new Error('Already started');
58 this.id_ = supportModule.asyncWait(
59 this.handle_, this.signals_, this.onHandleReady_.bind(this));
62 /**
63 * Stop waiting for the handle to be ready.
65 AsyncWaiter.prototype.stop = function() {
66 if (!this.id_)
67 return;
69 supportModule.cancelWait(this.id_);
70 this.id_ = null;
73 /**
74 * Returns whether this {@ linke AsyncWaiter} is waiting.
75 * @return {boolean} Whether this AsyncWaiter is waiting.
77 AsyncWaiter.prototype.isWaiting = function() {
78 return !!this.id_;
81 /**
82 * Invoked when |[handle_]{@link module:async_waiter.AsyncWaiter#handle_}| is
83 * ready.
84 * @param {number} result The result of the wait.
85 * @private
87 AsyncWaiter.prototype.onHandleReady_ = function(result) {
88 this.id_ = null;
89 this.callback_(result);
92 return {AsyncWaiter: AsyncWaiter};
93 });