Home | History | Annotate | Download | only in common
      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 /**
      6  * @constructor
      7  * @param {number} timeout
      8  */
      9 WebInspector.Throttler = function(timeout)
     10 {
     11     this._timeout = timeout;
     12     this._isRunningProcess = false;
     13     this._asSoonAsPossible = false;
     14     /** @type {?function(!WebInspector.Throttler.FinishCallback)} */
     15     this._process = null;
     16 }
     17 
     18 WebInspector.Throttler.prototype = {
     19     _processCompleted: function()
     20     {
     21         this._isRunningProcess = false;
     22         if (this._process)
     23             this._innerSchedule(false);
     24         this._processCompletedForTests();
     25     },
     26 
     27     _processCompletedForTests: function()
     28     {
     29         // For sniffing in tests.
     30     },
     31 
     32     _onTimeout: function()
     33     {
     34         delete this._processTimeout;
     35         this._asSoonAsPossible = false;
     36         this._isRunningProcess = true;
     37 
     38         // Process might issue synchronous calls to this throttler.
     39         var process = this._process;
     40         this._process = null;
     41         process(this._processCompleted.bind(this));
     42     },
     43 
     44     /**
     45      * @param {function(!WebInspector.Throttler.FinishCallback)} process
     46      * @param {boolean=} asSoonAsPossible
     47      */
     48     schedule: function(process, asSoonAsPossible)
     49     {
     50         // Deliberately skip previous process.
     51         this._process = process;
     52         var force = !!asSoonAsPossible && !this._asSoonAsPossible;
     53         this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible;
     54 
     55         this._innerSchedule(force);
     56     },
     57 
     58     /**
     59      * @param {boolean} force
     60      */
     61     _innerSchedule: function(force)
     62     {
     63         if (this._isRunningProcess)
     64             return;
     65         if (this._processTimeout && !force)
     66             return;
     67         if (this._processTimeout)
     68             this._clearTimeout(this._processTimeout);
     69 
     70         var timeout = this._asSoonAsPossible ? 0 : this._timeout;
     71         this._processTimeout = this._setTimeout(this._onTimeout.bind(this), timeout);
     72     },
     73 
     74     /**
     75      *  @param {number} timeoutId
     76      */
     77     _clearTimeout: function(timeoutId)
     78     {
     79         clearTimeout(timeoutId);
     80     },
     81 
     82     /**
     83      * @param {function()} operation
     84      * @param {number} timeout
     85      * @return {number}
     86      */
     87     _setTimeout: function(operation, timeout)
     88     {
     89         return setTimeout(operation, timeout);
     90     }
     91 }
     92 
     93 /** @typedef {function()} */
     94 WebInspector.Throttler.FinishCallback;
     95