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. |
| 13 * |
| 14 * @param {function()} callback The callback to be executed. |
| 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 |