CachedSource.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. function CachedSource(source) {
  6. this._source = source;
  7. this._cachedSource = undefined;
  8. this._cachedSize = undefined;
  9. this._cachedMaps = {};
  10. }
  11. module.exports = CachedSource;
  12. CachedSource.prototype.source = function() {
  13. if(typeof this._cachedSource !== "undefined") return this._cachedSource;
  14. return this._cachedSource = this._source.source();
  15. };
  16. CachedSource.prototype.size = function() {
  17. if(typeof this._cachedSize !== "undefined") return this._cachedSize;
  18. if(typeof this._cachedSource !== "undefined")
  19. return this._cachedSize = this._cachedSource.length;
  20. return this._cachedSize = this._source.size();
  21. };
  22. CachedSource.prototype.sourceAndMap = function(options) {
  23. var key = JSON.stringify(options);
  24. if(typeof this._cachedSource !== "undefined" && key in this._cachedMaps)
  25. return { source: this._cachedSource, map: this._cachedMaps[key] };
  26. else if(typeof this._cachedSource !== "undefined") {
  27. return { source: this._cachedSource, map: this._cachedMaps[key] = this._source.map(options) };
  28. } else if(key in this._cachedMaps) {
  29. return { source: this._cachedSource = this._source.source(), map: this._cachedMaps[key] };
  30. }
  31. var result = this._source.sourceAndMap(options);
  32. this._cachedSource = result.source;
  33. this._cachedMaps[key] = result.map;
  34. return { source: this._cachedSource, map: this._cachedMaps[key] };
  35. };
  36. CachedSource.prototype.node = function(options) {
  37. return this._source.node(options);
  38. };
  39. CachedSource.prototype.listMap = function(options) {
  40. return this._source.listMap(options);
  41. }
  42. CachedSource.prototype.map = function(options) {
  43. if(!options) options = {};
  44. var key = JSON.stringify(options);
  45. if(key in this._cachedMaps)
  46. return this._cachedMaps[key];
  47. return this._cachedMaps[key] = this._source.map();
  48. };
  49. CachedSource.prototype.updateHash = function(hash) {
  50. this._source.updateHash(hash);
  51. };