CacheNode.js 933 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Node, { addNodeClass } from './Node.js';
  2. import NodeCache from './NodeCache.js';
  3. import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';
  4. class CacheNode extends Node {
  5. constructor( node, cache = new NodeCache() ) {
  6. super();
  7. this.isCacheNode = true;
  8. this.node = node;
  9. this.cache = cache;
  10. }
  11. getNodeType( builder ) {
  12. return this.node.getNodeType( builder );
  13. }
  14. build( builder, ...params ) {
  15. const previousCache = builder.getCache();
  16. const cache = this.cache || builder.globalCache;
  17. builder.setCache( cache );
  18. const data = this.node.build( builder, ...params );
  19. builder.setCache( previousCache );
  20. return data;
  21. }
  22. }
  23. export default CacheNode;
  24. export const cache = nodeProxy( CacheNode );
  25. export const globalCache = ( node ) => cache( node, null );
  26. addNodeElement( 'cache', cache );
  27. addNodeElement( 'globalCache', globalCache );
  28. addNodeClass( 'CacheNode', CacheNode );