All files / lib/util memory.js

100% Statements 56/56
100% Branches 9/9
100% Functions 3/3
100% Lines 56/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 572x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1605x 1605x 1605x 565x 725x 725x 565x 1605x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1161x 399x 514x 514x 399x 1161x 1161x 1161x 1161x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 695x 7938x 7938x 7938x 695x 695x  
import Pool from './pool';
import { NodeCache, VTree } from './types';
 
const { protect, unprotect, memory } = Pool;
 
/**
 * Ensures that vTree is not recycled during a render cycle.
 *
 * @param {VTree} vTree
 * @return {void}
 */
export function protectVTree(vTree) {
  protect(vTree);
 
  if (vTree.childNodes.length) {
    for (let i = 0; i < vTree.childNodes.length; i++) {
      protectVTree(vTree.childNodes[i]);
    }
  }
}
 
/**
 * Recycles a VTree by unprotecting itself, removing its DOM Node reference, and
 * recursively unprotecting all nested children. Resets the VTree's attributes
 * and childNode properties afterwards, as these can contribute to unwanted
 * increases in the heap.
 *
 * @param {VTree} vTree
 * @return {void}
 */
export function unprotectVTree(vTree) {
  if (vTree.childNodes.length) {
    for (let i = 0; i < vTree.childNodes.length; i++) {
      unprotectVTree(vTree.childNodes[i]);
    }
  }
 
  NodeCache.delete(vTree);
  unprotect(vTree);
}
 
/**
 * Collects any unused VTree's and puts them back into the free Set. This is
 * primarily used by tests, but could also be useful for specific niche cases
 * as a way to ease memory/CPU pressure when lots of temporary trees are
 * created but never used.
 *
 * @return {void}
 */
export function gc() {
  memory.allocated.forEach(vTree => {
    memory.free.add(vTree);
    memory.allocated.delete(vTree);
    NodeCache.delete(vTree);
  });
}