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 /** | |
7 * @fileoverview Utilities for local_ntp.js. | |
8 */ | |
9 | |
10 | |
11 /** | |
12 * A counter with a callback that gets executed on the 1-to-0 transition. | |
James Hawkins
2014/07/28 17:43:35
Please add a test for this.
huangs
2014/07/29 19:43:45
Maybe in a different CL? LocalNTPJavascriptTest i
Mathieu
2014/07/29 21:36:08
Assigned the crbug to you. Thanks! :)
| |
13 * | |
14 * @param {Function} callback The callback to be executed. | |
Jered
2014/07/28 18:32:51
nit: !Function or function() here and below
huangs
2014/07/29 19:43:46
Done, using {function()}
| |
15 * @constructor | |
16 */ | |
17 function Barrier(callback) { | |
18 /** @private {Function} */ | |
19 this.callback_ = callback; | |
20 | |
21 /** @private {number} */ | |
22 this.count_ = 0; | |
23 } | |
24 | |
25 | |
26 /** | |
27 * Increments count of the Barrier. | |
28 */ | |
29 Barrier.prototype.add = function() { | |
30 ++this.count_; | |
31 }; | |
32 | |
33 | |
34 /** | |
35 * Decrements count of the Barrier, and executes callback on 1-to-0 transition. | |
36 */ | |
37 Barrier.prototype.remove = function() { | |
38 if (this.count_ === 0) // Guards against underflow. | |
39 return; | |
40 --this.count_; | |
41 if (this.count_ === 0) | |
42 this.callback_(); | |
43 }; | |
OLD | NEW |