| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Definition the goog.debug.RelativeTimeProvider class. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.debug.RelativeTimeProvider'); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* A simple object to keep track of a timestamp considered the start of |
| 26 |
* something. The main use is for the logger system to maintain a start time |
| 27 |
* that is occasionally reset. For example, in Gmail, we reset this relative |
| 28 |
* time at the start of a user action so that timings are offset from the |
| 29 |
* beginning of the action. This class also provides a singleton as the default |
| 30 |
* behavior for most use cases is to share the same start time. |
| 31 |
* |
| 32 |
* @constructor |
| 33 |
* @final |
| 34 |
*/ |
| 35 |
goog.debug.RelativeTimeProvider = function() { |
| 36 |
/** |
| 37 |
* The start time. |
| 38 |
* @type {number} |
| 39 |
* @private |
| 40 |
*/ |
| 41 |
this.relativeTimeStart_ = goog.now(); |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Default instance. |
| 47 |
* @type {goog.debug.RelativeTimeProvider} |
| 48 |
* @private |
| 49 |
*/ |
| 50 |
goog.debug.RelativeTimeProvider.defaultInstance_ = null; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Sets the start time to the specified time. |
| 55 |
* @param {number} timeStamp The start time. |
| 56 |
*/ |
| 57 |
goog.debug.RelativeTimeProvider.prototype.set = function(timeStamp) { |
| 58 |
this.relativeTimeStart_ = timeStamp; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Resets the start time to now. |
| 64 |
*/ |
| 65 |
goog.debug.RelativeTimeProvider.prototype.reset = function() { |
| 66 |
this.set(goog.now()); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* @return {number} The start time. |
| 72 |
*/ |
| 73 |
goog.debug.RelativeTimeProvider.prototype.get = function() { |
| 74 |
return this.relativeTimeStart_; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* @return {goog.debug.RelativeTimeProvider} The default instance. |
| 80 |
*/ |
| 81 |
goog.debug.RelativeTimeProvider.getDefaultInstance = function() { |
| 82 |
if (!goog.debug.RelativeTimeProvider.defaultInstance_) { |
| 83 |
goog.debug.RelativeTimeProvider.defaultInstance_ = |
| 84 |
new goog.debug.RelativeTimeProvider(); |
| 85 |
} |
| 86 |
return goog.debug.RelativeTimeProvider.defaultInstance_; |
| 87 |
}; |