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 /** @private {boolean} */ |
| 25 this.isCancelled_ = false; |
| 26 } |
| 27 |
| 28 |
| 29 /** |
| 30 * Increments count of the Barrier. |
| 31 */ |
| 32 Barrier.prototype.add = function() { |
| 33 ++this.count_; |
| 34 }; |
| 35 |
| 36 |
| 37 /** |
| 38 * Decrements count of the Barrier, and executes callback on 1-to-0 transition. |
| 39 */ |
| 40 Barrier.prototype.remove = function() { |
| 41 if (this.count_ === 0) // Guards against underflow. |
| 42 return; |
| 43 --this.count_; |
| 44 if (!this.isCancelled_ && this.count_ === 0) |
| 45 this.callback_(); |
| 46 }; |
| 47 |
| 48 |
| 49 /** |
| 50 * Deactivates the Barrier; the callback will never be executed. |
| 51 */ |
| 52 Barrier.prototype.cancel = function() { |
| 53 this.isCancelled_ = true; |
| 54 }; |
OLD | NEW |