mirror of
https://github.com/zulip/zulip.git
synced 2025-11-18 21:48:30 +00:00
Add CachedValue class to encapsulate the pattern of caching mostly static values
(imported from commit 14048847e77974cda3a6214f129492d8d373080d)
This commit is contained in:
@@ -289,6 +289,31 @@ exports.xhr_error_message = function (message, xhr) {
|
||||
return message;
|
||||
};
|
||||
|
||||
/* Represents a value that is expensive to compute and should be
|
||||
* computed on demand and then cached. The value can be forcefully
|
||||
* recalculated on the next call to get() by calling reset().
|
||||
*
|
||||
* You must supply a option to the constructor called compute_value
|
||||
* which should be a function that computes the uncached value.
|
||||
*/
|
||||
var unassigned_value_sentinel = {};
|
||||
exports.CachedValue = function (opts) {
|
||||
this._value = unassigned_value_sentinel;
|
||||
_.extend(this, opts);
|
||||
};
|
||||
|
||||
exports.CachedValue.prototype = {
|
||||
get: function CachedValue_get() {
|
||||
if (this._value === unassigned_value_sentinel) {
|
||||
this._value = this.compute_value();
|
||||
}
|
||||
return this._value;
|
||||
},
|
||||
|
||||
reset: function CachedValue_reset() {
|
||||
this._value = unassigned_value_sentinel;
|
||||
}
|
||||
};
|
||||
|
||||
return exports;
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user