Index: chrome/browser/resources/local_ntp/local_ntp_util.js |
diff --git a/chrome/browser/resources/local_ntp/local_ntp_util.js b/chrome/browser/resources/local_ntp/local_ntp_util.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0da47dee5f7f69cd068a3092c10a9f78a0378bf2 |
--- /dev/null |
+++ b/chrome/browser/resources/local_ntp/local_ntp_util.js |
@@ -0,0 +1,43 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+ |
+/** |
+ * @fileoverview Utilities for local_ntp.js. |
+ */ |
+ |
+ |
+/** |
+ * 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! :)
|
+ * |
+ * @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()}
|
+ * @constructor |
+ */ |
+function Barrier(callback) { |
+ /** @private {Function} */ |
+ this.callback_ = callback; |
+ |
+ /** @private {number} */ |
+ this.count_ = 0; |
+} |
+ |
+ |
+/** |
+ * Increments count of the Barrier. |
+ */ |
+Barrier.prototype.add = function() { |
+ ++this.count_; |
+}; |
+ |
+ |
+/** |
+ * Decrements count of the Barrier, and executes callback on 1-to-0 transition. |
+ */ |
+Barrier.prototype.remove = function() { |
+ if (this.count_ === 0) // Guards against underflow. |
+ return; |
+ --this.count_; |
+ if (this.count_ === 0) |
+ this.callback_(); |
+}; |