OLD | NEW |
---|---|
(Empty) | |
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 }, | |
25 | |
26 _onTimeout: function() | |
27 { | |
28 delete this._processTimeout; | |
29 this._asSoonAsPossible = false; | |
30 this._isRunningProcess = true; | |
31 | |
32 // Process might issue synchronous calls to this throttler. | |
33 var process = this._process; | |
34 this._process = null; | |
35 process(this._processCompleted.bind(this)); | |
36 }, | |
37 | |
38 /** | |
39 * @param {function(!WebInspector.Throttler.FinishCallback)} process | |
40 * @param {boolean=} asSoonAsPossible | |
41 */ | |
42 schedule: function(process, asSoonAsPossible) | |
43 { | |
44 // Deliberately skip previous process. | |
45 this._process = process; | |
46 var force = !!asSoonAsPossible && !this._asSoonAsPossible; | |
47 this._asSoonAsPossible = this._asSoonAsPossible || !!asSoonAsPossible; | |
48 | |
49 this._innerSchedule(force); | |
50 }, | |
51 | |
52 /** | |
53 * @param {boolean} force | |
54 */ | |
55 _innerSchedule: function(force) | |
56 { | |
57 if (this._isRunningProcess) | |
58 return; | |
59 if (this._processTimeout && !force) | |
60 return; | |
61 if (this._processTimeout && force) | |
vsevik
2014/06/10 16:28:23
if (this._processTimeout) is enough
lushnikov
2014/06/10 16:44:59
Done.
| |
62 this._clearTimeout(this._processTimeout); | |
63 | |
64 var timeout = this._asSoonAsPossible ? 0 : this._timeout; | |
65 this._processTimeout = this._setTimeout(this._onTimeout.bind(this), time out); | |
66 }, | |
67 | |
68 /** | |
69 * @param {number} timeoutId | |
70 */ | |
71 _clearTimeout: function(timeoutId) | |
72 { | |
73 clearTimeout(timeoutId); | |
74 }, | |
75 | |
76 /** | |
77 * @param {function()} operation | |
78 * @param {number} timeout | |
79 * @return {number} | |
80 */ | |
81 _setTimeout: function(operation, timeout) | |
82 { | |
83 return setTimeout(operation, timeout); | |
84 } | |
85 } | |
86 | |
87 /** @typedef {function()} */ | |
88 WebInspector.Throttler.FinishCallback; | |
OLD | NEW |