Home | History | Annotate | Download | only in resources
      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.
      4 
      5 define('async_waiter', [
      6     'mojo/public/js/bindings/support',
      7 ], function(supportModule) {
      8   /**
      9    * @module async_waiter
     10    */
     11 
     12   /**
     13    * @callback module:async_waiter.AsyncWaiter.Callback
     14    * @param {number} result The result of waiting.
     15    */
     16 
     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 {number} 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
     25    */
     26   function AsyncWaiter(handle, signals, callback) {
     27     /**
     28      * The handle to wait on.
     29      * @type {!MojoHandle}
     30      * @private
     31      */
     32     this.handle_ = handle;
     33 
     34     /**
     35      * The signals to wait for.
     36      * @type {number}
     37      * @private
     38      */
     39     this.signals_ = signals;
     40 
     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
     46      */
     47     this.callback_ = callback;
     48     this.id_ = null;
     49   }
     50 
     51   /**
     52    * Start waiting for the handle to be ready.
     53    * @throws Will throw if this is already waiting.
     54    */
     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));
     60   };
     61 
     62   /**
     63    * Stop waiting for the handle to be ready.
     64    */
     65   AsyncWaiter.prototype.stop = function() {
     66     if (!this.id_)
     67       return;
     68 
     69     supportModule.cancelWait(this.id_);
     70     this.id_ = null;
     71   };
     72 
     73   /**
     74    * Returns whether this {@link AsyncWaiter} is waiting.
     75    * @return {boolean} Whether this AsyncWaiter is waiting.
     76    */
     77   AsyncWaiter.prototype.isWaiting = function() {
     78     return !!this.id_;
     79   };
     80 
     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
     86    */
     87   AsyncWaiter.prototype.onHandleReady_ = function(result) {
     88     this.id_ = null;
     89     this.callback_(result);
     90   };
     91 
     92   return {AsyncWaiter: AsyncWaiter};
     93 });
     94