CacheNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Node from './Node.js';
  2. import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js';
  3. /** @module CacheNode **/
  4. /**
  5. * This node can be used as a cache management component for another node.
  6. * Caching is in general used by default in {@link NodeBuilder} but this node
  7. * allows the usage of a shared parent cache during the build process.
  8. *
  9. * @augments Node
  10. */
  11. class CacheNode extends Node {
  12. static get type() {
  13. return 'CacheNode';
  14. }
  15. /**
  16. * Constructs a new cache node.
  17. *
  18. * @param {Node} node - The node that should be cached.
  19. * @param {Boolean} [parent=true] - Whether this node refers to a shared parent cache or not.
  20. */
  21. constructor( node, parent = true ) {
  22. super();
  23. /**
  24. * The node that should be cached.
  25. *
  26. * @type {Node}
  27. */
  28. this.node = node;
  29. /**
  30. * Whether this node refers to a shared parent cache or not.
  31. *
  32. * @type {Boolean}
  33. * @default true
  34. */
  35. this.parent = parent;
  36. /**
  37. * This flag can be used for type testing.
  38. *
  39. * @type {Boolean}
  40. * @readonly
  41. * @default true
  42. */
  43. this.isCacheNode = true;
  44. }
  45. getNodeType( builder ) {
  46. const previousCache = builder.getCache();
  47. const cache = builder.getCacheFromNode( this, this.parent );
  48. builder.setCache( cache );
  49. const nodeType = this.node.getNodeType( builder );
  50. builder.setCache( previousCache );
  51. return nodeType;
  52. }
  53. build( builder, ...params ) {
  54. const previousCache = builder.getCache();
  55. const cache = builder.getCacheFromNode( this, this.parent );
  56. builder.setCache( cache );
  57. const data = this.node.build( builder, ...params );
  58. builder.setCache( previousCache );
  59. return data;
  60. }
  61. }
  62. export default CacheNode;
  63. /**
  64. * TSL function for creating a cache node.
  65. *
  66. * @function
  67. * @param {Node} node - The node that should be cached.
  68. * @param {Boolean} parent - Whether this node refers to a shared parent cache or not.
  69. * @returns {CacheNode}
  70. */
  71. export const cache = ( node, parent ) => nodeObject( new CacheNode( nodeObject( node ), parent ) );
  72. addMethodChaining( 'cache', cache );
粤ICP备19079148号