UI for Zipcoin Blue

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Source = require("./Source");
  7. class CachedSource extends Source {
  8. constructor(source) {
  9. super();
  10. this._source = source;
  11. this._cachedSource = undefined;
  12. this._cachedSize = undefined;
  13. this._cachedMaps = {};
  14. if(source.node) this.node = function(options) {
  15. return this._source.node(options);
  16. };
  17. if(source.listMap) this.listMap = function(options) {
  18. return this._source.listMap(options);
  19. };
  20. }
  21. source() {
  22. if(typeof this._cachedSource !== "undefined") return this._cachedSource;
  23. return this._cachedSource = this._source.source();
  24. }
  25. size() {
  26. if(typeof this._cachedSize !== "undefined") return this._cachedSize;
  27. if(typeof this._cachedSource !== "undefined")
  28. return this._cachedSize = this._cachedSource.length;
  29. return this._cachedSize = this._source.size();
  30. }
  31. sourceAndMap(options) {
  32. const key = JSON.stringify(options);
  33. if(typeof this._cachedSource !== "undefined" && key in this._cachedMaps)
  34. return {
  35. source: this._cachedSource,
  36. map: this._cachedMaps[key]
  37. };
  38. else if(typeof this._cachedSource !== "undefined") {
  39. return {
  40. source: this._cachedSource,
  41. map: this._cachedMaps[key] = this._source.map(options)
  42. };
  43. } else if(key in this._cachedMaps) {
  44. return {
  45. source: this._cachedSource = this._source.source(),
  46. map: this._cachedMaps[key]
  47. };
  48. }
  49. const result = this._source.sourceAndMap(options);
  50. this._cachedSource = result.source;
  51. this._cachedMaps[key] = result.map;
  52. return {
  53. source: this._cachedSource,
  54. map: this._cachedMaps[key]
  55. };
  56. }
  57. map(options) {
  58. if(!options) options = {};
  59. const key = JSON.stringify(options);
  60. if(key in this._cachedMaps)
  61. return this._cachedMaps[key];
  62. return this._cachedMaps[key] = this._source.map();
  63. }
  64. updateHash(hash) {
  65. this._source.updateHash(hash);
  66. }
  67. }
  68. module.exports = CachedSource;