1 /** 2 * Copyright (C) 2010-2014 KO GmbH <copyright@kogmbh.com> 3 * 4 * @licstart 5 * This file is part of WebODF. 6 * 7 * WebODF is free software: you can redistribute it and/or modify it 8 * under the terms of the GNU Affero General Public License (GNU AGPL) 9 * as published by the Free Software Foundation, either version 3 of 10 * the License, or (at your option) any later version. 11 * 12 * WebODF is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Affero General Public License for more details. 16 * 17 * You should have received a copy of the GNU Affero General Public License 18 * along with WebODF. If not, see <http://www.gnu.org/licenses/>. 19 * @licend 20 * 21 * @source: http://www.webodf.org/ 22 * @source: https://github.com/kogmbh/WebODF/ 23 */ 24 25 /*global core*/ 26 27 // TODO Apparently newer closure compilers do a better job with generics 28 29 /** 30 * Lazily loaded property. The value is loaded using the valueLoader and cached 31 * the first time it's requested. Subsequent requests will return the cached value. 32 * Calling reset will clear the cached value, causing the next value request 33 * to load a new value via the valueLoader. 34 * 35 * @constructor 36 * @template T 37 * @param {!function():Object} valueLoader Property value loader 38 */ 39 core.LazyProperty = function (valueLoader) { 40 "use strict"; 41 var cachedValue, 42 valueLoaded = false; 43 44 /** 45 * @return {T} 46 */ 47 this.value = function() { 48 if (!valueLoaded) { 49 cachedValue = valueLoader(); 50 valueLoaded = true; 51 } 52 return cachedValue; 53 }; 54 55 /** 56 * @return {undefined} 57 */ 58 this.reset = function() { 59 valueLoaded = false; 60 }; 61 }; 62