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
) {
13 * @callback module:async_waiter.AsyncWaiter.Callback
14 * @param {number} result The result of waiting.
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.
24 * @alias module:async_waiter.AsyncWaiter
26 function AsyncWaiter(handle
, signals
, callback
) {
28 * The handle to wait on.
32 this.handle_
= handle
;
35 * The signals to wait for.
39 this.signals_
= signals
;
42 * The callback to invoke when
43 * |[handle_]{@link module:async_waiter.AsyncWaiter#handle_}| is ready.
44 * @type {module:async_waiter.AsyncWaiter.Callback}
47 this.callback_
= callback
;
52 * Start waiting for the handle to be ready.
53 * @throws Will throw if this is already waiting.
55 AsyncWaiter
.prototype.start = function() {
57 throw new Error('Already started');
58 this.id_
= supportModule
.asyncWait(
59 this.handle_
, this.signals_
, this.onHandleReady_
.bind(this));
63 * Stop waiting for the handle to be ready.
65 AsyncWaiter
.prototype.stop = function() {
69 supportModule
.cancelWait(this.id_
);
74 * Returns whether this {@ linke AsyncWaiter} is waiting.
75 * @return {boolean} Whether this AsyncWaiter is waiting.
77 AsyncWaiter
.prototype.isWaiting = function() {
82 * Invoked when |[handle_]{@link module:async_waiter.AsyncWaiter#handle_}| is
84 * @param {number} result The result of the wait.
87 AsyncWaiter
.prototype.onHandleReady_ = function(result
) {
89 this.callback_(result
);
92 return {AsyncWaiter
: AsyncWaiter
};