diff --git a/static/js/util.js b/static/js/util.js index 0dc88abfd5..4b442a4d37 100644 --- a/static/js/util.js +++ b/static/js/util.js @@ -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; }());