Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / common / Throttler.js
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
53         // Run the first scheduled task instantly.
54         var hasScheduledTasks = !!this._processTimeout || this._isRunningProcess;
55         asSoonAsPossible = !!asSoonAsPossible || !hasScheduledTasks;
56
57         var forceTimerUpdate = asSoonAsPossible && !this._asSoonAsPossible;
58         this._asSoonAsPossible = this._asSoonAsPossible || asSoonAsPossible;
59
60         this._innerSchedule(forceTimerUpdate);
61     },
62
63     /**
64      * @param {boolean} forceTimerUpdate
65      */
66     _innerSchedule: function(forceTimerUpdate)
67     {
68         if (this._isRunningProcess)
69             return;
70         if (this._processTimeout && !forceTimerUpdate)
71             return;
72         if (this._processTimeout)
73             this._clearTimeout(this._processTimeout);
74
75         var timeout = this._asSoonAsPossible ? 0 : this._timeout;
76         this._processTimeout = this._setTimeout(this._onTimeout.bind(this), timeout);
77     },
78
79     /**
80      *  @param {number} timeoutId
81      */
82     _clearTimeout: function(timeoutId)
83     {
84         clearTimeout(timeoutId);
85     },
86
87     /**
88      * @param {function()} operation
89      * @param {number} timeout
90      * @return {number}
91      */
92     _setTimeout: function(operation, timeout)
93     {
94         return setTimeout(operation, timeout);
95     }
96 }
97
98 /** @typedef {function()} */
99 WebInspector.Throttler.FinishCallback;