Skip to content

Commit

Permalink
refactor: standardize Effect interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Mar 14, 2022
1 parent 307563c commit da2b782
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 190 deletions.
27 changes: 20 additions & 7 deletions benchmarks/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
Commit,
createElement,
DOMNode,
Mutation,
Effect,
Driver,
OLD_VNODE_FIELD,
useChildren,
VElement,
VEntity,
VNode,
EffectTypes,
} from 'packages/million';

const adjectives = [
Expand Down Expand Up @@ -92,11 +93,14 @@ const useNode = (drivers: Partial<Driver>[]) => {
newVNode?: VNode,
oldVNode?: VNode,
commit: Commit = (work: () => void) => work(),
effects: Mutation[] = [],
effects: Effect[] = [],
): ReturnType<Driver> => {
const finish = (element: DOMNode): ReturnType<Driver> => {
if (!oldVNode) {
effects.push(() => (element[OLD_VNODE_FIELD] = newVNode));
effects.push({
type: EffectTypes.SET_PROP,
flush: () => (element[OLD_VNODE_FIELD] = newVNode),
});
}

return {
Expand All @@ -108,15 +112,21 @@ const useNode = (drivers: Partial<Driver>[]) => {
};

if (newVNode === undefined || newVNode === null) {
effects.push(() => el.remove());
effects.push({
type: EffectTypes.REMOVE,
flush: () => el.remove(),
});
return finish(el);
} else {
let prevVNode: VNode | VEntity | undefined = oldVNode ?? el[OLD_VNODE_FIELD];
const hasString = typeof prevVNode === 'string' || typeof newVNode === 'string';

if (hasString && prevVNode !== newVNode) {
const newEl = createElement(<string>newVNode, false);
effects.push(() => el.replaceWith(newEl));
effects.push({
type: EffectTypes.REPLACE,
flush: () => el.replaceWith(newEl),
});

return finish(<DOMNode>newEl);
}
Expand All @@ -130,7 +140,10 @@ const useNode = (drivers: Partial<Driver>[]) => {
) {
if (oldVElement?.tag !== newVElement?.tag || el instanceof Text) {
const newEl = createElement(newVElement, false);
effects.push(() => el.replaceWith(newEl));
effects.push({
type: EffectTypes.REPLACE,
flush: () => el.replaceWith(newEl),
});
return finish(<DOMNode>newEl);
}

Expand Down Expand Up @@ -159,7 +172,7 @@ export const diff = useNode([useChildren()]);
export const patch = (el: DOMNode, newVNode: VNode, oldVNode?: VNode) => {
const data = diff(el, newVNode, oldVNode);
for (let i = 0; i < data.effects!.length; i++) {
data.effects![i]();
data.effects![i].flush();
}

return data.el;
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/appendManyRowsToLargeTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ const suite = Suite('append many rows to large table (appending 1,000 to a table
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/createManyRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ const suite = Suite('create many rows (creating 10,000 rows)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/createRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ const suite = Suite('create rows (creating 1,000 rows)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/partialUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ const suite = Suite('partial update (updating every 10th row for 1,000 rows)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/removeRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ const suite = Suite('remove row (removing one row)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }, i) => {
if (row === i) return;
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/suites/replaceAllRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ const suite = Suite('replace all rows (updating all 1,000 rows)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
7 changes: 4 additions & 3 deletions benchmarks/suites/selectRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ const suite = Suite('select row (highlighting a selected row)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }, i) => {
if (row === i)
element.innerHTML += `<tr style="background: red;"><td>${id}</td><td>${label}</td></tr>`;
else element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
if (row === i) html += `<tr style="background: red;"><td>${id}</td><td>${label}</td></tr>`;
else html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML = html;
},
});

Expand Down
6 changes: 4 additions & 2 deletions benchmarks/suites/swapRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
// @ts-nocheck

import { createElement, Delta } from 'packages/million';
import { createElement, Deltas } from 'packages/million';
import * as simple_virtual_dom from 'simple-virtual-dom';
import * as snabbdom from 'snabbdom';
import * as hundred from 'hundred';
Expand Down Expand Up @@ -70,9 +70,11 @@ const suite = Suite('swap rows (swap 2 rows for table with 1,000 rows)', {
},
innerHTML: () => {
const element = el();
let html = '';
data.forEach(({ id, label }) => {
element.innerHTML += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
html += `<tr><td>${String(id)}</td><td>${label}</td></tr>`;
});
element.innerHTML += html;
},
});

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@
"@picocss/pico": "^1.5.0",
"@types/benchmark": "^2.1.1",
"@types/canvas-confetti": "^1.4.2",
"@types/lodash": "^4.14.179",
"@types/lodash": "^4.14.180",
"@types/node": "^17.0.21",
"@types/virtual-dom": "^2.1.1",
"@typescript-eslint/eslint-plugin": "^5.14.0",
"@typescript-eslint/parser": "^5.14.0",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"@vitejs/plugin-legacy": "^1.7.1",
"@vitest/ui": "^0.2.8",
"benchmark": "^2.1.4",
"c8": "^7.11.0",
"canvas-confetti": "^1.5.1",
"chart.js": "^3.7.1",
"esbuild": "^0.14.26",
"esbuild": "^0.14.27",
"eslint": "^8.11.0",
"eslint-config-prettier": "^8.5.0",
"export-size": "^0.5.2",
Expand Down

0 comments on commit da2b782

Please sign in to comment.