Skip to content

Commit

Permalink
Merge 7ea624d into fe3208c
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Nov 27, 2022
2 parents fe3208c + 7ea624d commit 69704b0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/diffhtml/lib/util/memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Pool from './pool';
import { NodeCache } from './types';
import { EMPTY, NodeCache } from './types';

const { protect, unprotect, memory } = Pool;

Expand Down Expand Up @@ -56,6 +56,7 @@ export function gc() {
// Scrub a VTree of attributes and childNodes to avoid ever increasing RAM.
vTree.attributes = {};
vTree.childNodes.length = 0;
vTree.key = EMPTY.STR;

// Make the VTree available for future renders.
memory.free.add(vTree);
Expand Down
13 changes: 11 additions & 2 deletions packages/diffhtml/lib/util/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ const Pool = {
},

/**
*
* Moves a VTree from the "free" state to the "allocated" state. If the pool
* is empty, it creates a new object.
*
* @return {VTree}
*/
get() {
Expand All @@ -72,6 +76,9 @@ const Pool = {
},

/**
* Moves a VTree from "allocated" state to "protected" state. This means that
* the VTrees will persist between transactions.
*
* @param {VTree} vTree - Virtual Tree to protect
*/
protect(vTree) {
Expand All @@ -80,13 +87,15 @@ const Pool = {
},

/**
* Moves a VTree from "protected" state to "allocated" state. They will be
* brought back into "free" circulation during a GC.
*
* @param {VTree} vTree - Virtual Tree to unprotect and deallocate
*/
unprotect(vTree) {
if (protect.has(vTree) || allocate.has(vTree)) {
protect.delete(vTree);
allocate.delete(vTree);
free.add(vTree);
allocate.add(vTree);
}
},
};
Expand Down
45 changes: 45 additions & 0 deletions packages/diffhtml/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,51 @@ describe('Util', () => {
strictEqual(Pool.memory.protected.size, 0);
});

it('will only reset attributes once garbage collected', () => {
const expected = 'somestr';
const vTree = createTree('div', { someAttr: expected });

protectVTree(vTree);
strictEqual(vTree.attributes.someAttr, expected);

unprotectVTree(vTree);
strictEqual(vTree.attributes.someAttr, expected);

gc();

strictEqual(vTree.attributes.someAttr, undefined);
});

it('will only reset childNodes once garbage collected', () => {
const expected = 'somestr';
const vTree = createTree('div', null, createTree('span'));

protectVTree(vTree);
strictEqual(vTree.childNodes.length, 1);

unprotectVTree(vTree);
strictEqual(vTree.childNodes.length, 1);

gc();

strictEqual(vTree.childNodes.length, 0);
});

it('will only reset key once garbage collected', () => {
const expected = 'somestr';
const vTree = createTree('div', { key: expected });

protectVTree(vTree);
strictEqual(vTree.key, expected);

unprotectVTree(vTree);
strictEqual(vTree.key, expected);

gc();

strictEqual(vTree.key, '');
});

it('will garbage collect DOM Node associations', () => {
const domNode = document.createElement('div');
const vTree = createTree(domNode);
Expand Down

0 comments on commit 69704b0

Please sign in to comment.