GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — :construction: optimizing renderers · nullstack/nullstack@3a4ad18

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • client
    • client.js
    • index.js
    • render.js
    • rerender.js
  • server
    • render.js
  • shared
    • generateTree.js
    • nodes.js
  • tests
    • .env
    • jest-puppeteer.config.js
    • src
      • TwoWayBindings.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎client/client.js‎

Copy file name to clipboardExpand all lines: client/client.js
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import router from './router'
77
const client = {}
88

99
client.initialized = false
10-
client.hydrated = false
1110
client.initializer = null
1211
client.instances = {}
1312
context.instances = client.instances
@@ -31,7 +30,7 @@ client.update = async function update() {
3130
client.initialized = false
3231
client.renewalQueue = []
3332
client.nextVirtualDom = await generateTree(client.initializer(), scope)
34-
rerender(client.selector)
33+
rerender()
3534
client.virtualDom = client.nextVirtualDom
3635
client.nextVirtualDom = null
3736
client.processLifecycleQueues()
@@ -42,7 +41,6 @@ client.update = async function update() {
4241
client.processLifecycleQueues = async function processLifecycleQueues() {
4342
if (!client.initialized) {
4443
client.initialized = true
45-
client.hydrated = true
4644
}
4745
let shouldUpdate = false
4846
while (client.initiationQueue.length) {
Collapse file

‎client/index.js‎

Copy file name to clipboardExpand all lines: client/index.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import page from './page';
1414
import params, { updateParams } from './params';
1515
import project from './project';
1616
import render from './render';
17-
import rerender from './rerender';
17+
import rerender, { hydrate } from './rerender';
1818
import router from './router';
1919
import settings from './settings';
2020
import worker from './worker';
@@ -63,12 +63,13 @@ export default class Nullstack {
6363
client.selector = body
6464
} else {
6565
client.virtualDom = await generateTree(client.initializer(), scope);
66+
hydrate(client.selector, client.virtualDom)
6667
context.environment = environment;
6768
scope.plugins = loadPlugins(scope);
6869
worker.online = navigator.onLine;
6970
typeof context.start === 'function' && await context.start(context);
7071
client.nextVirtualDom = await generateTree(client.initializer(), scope);
71-
rerender(client.selector);
72+
rerender();
7273
client.virtualDom = client.nextVirtualDom;
7374
client.nextVirtualDom = null;
7475
}
Collapse file

‎client/render.js‎

Copy file name to clipboardExpand all lines: client/render.js
+15-14Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@ import { anchorableElement } from './anchorableNode';
44
export default function render(node, options) {
55

66
if (isFalse(node) || node.type === 'head') {
7-
return document.createComment("");
7+
node.element = document.createComment("");
8+
return node.element
89
}
910

1011
if (isText(node)) {
11-
return document.createTextNode(node);
12+
node.element = document.createTextNode(node.text);
13+
return node.element
1214
}
1315

1416
const svg = (options && options.svg) || node.type === 'svg';
1517

16-
let element;
1718
if (svg) {
18-
element = document.createElementNS("http://www.w3.org/2000/svg", node.type);
19+
node.element = document.createElementNS("http://www.w3.org/2000/svg", node.type);
1920
} else {
20-
element = document.createElement(node.type);
21+
node.element = document.createElement(node.type);
2122
}
2223

2324
if (node.instance) {
24-
node.instance._self.element = element;
25+
node.instance._self.element = node.element;
2526
}
2627

2728
for (let name in node.attributes) {
2829
if (name === 'html') {
29-
element.innerHTML = node.attributes[name];
30-
anchorableElement(element);
30+
node.element.innerHTML = node.attributes[name];
31+
node.attributes['data-n'] === undefined && anchorableElement(node.element);
3132
} else if (name.startsWith('on')) {
3233
if (node.attributes[name] !== undefined) {
3334
const eventName = name.replace('on', '');
@@ -38,15 +39,15 @@ export default function render(node, options) {
3839
}
3940
node.attributes[name]({ ...node.attributes, event });
4041
};
41-
element.addEventListener(eventName, node[key]);
42+
node.element.addEventListener(eventName, node[key]);
4243
}
4344
} else {
4445
const type = typeof (node.attributes[name]);
4546
if (type !== 'object' && type !== 'function') {
4647
if (name != 'value' && node.attributes[name] === true) {
47-
element.setAttribute(name, '');
48+
node.element.setAttribute(name, '');
4849
} else if (name == 'value' || (node.attributes[name] !== false && node.attributes[name] !== null && node.attributes[name] !== undefined)) {
49-
element.setAttribute(name, node.attributes[name]);
50+
node.element.setAttribute(name, node.attributes[name]);
5051
}
5152
}
5253
}
@@ -55,14 +56,14 @@ export default function render(node, options) {
5556
if (!node.attributes.html) {
5657
for (let i = 0; i < node.children.length; i++) {
5758
const child = render(node.children[i], { svg });
58-
element.appendChild(child);
59+
node.element.appendChild(child);
5960
}
6061

6162
if (node.type == 'select') {
62-
element.value = node.attributes.value;
63+
node.element.value = node.attributes.value;
6364
}
6465
}
6566

66-
return element;
67+
return node.element;
6768

6869
}
Collapse file

‎client/rerender.js‎

Copy file name to clipboard
+133-72Lines changed: 133 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,125 @@
1-
import { isFalse, isText } from '../shared/nodes';
1+
import { isUndefined, isFalse, isText } from '../shared/nodes';
22
import { anchorableElement } from './anchorableNode';
33
import client from './client';
44
import render from './render';
55

6-
export default function rerender(selector, current, next) {
6+
function updateAttributes(selector, current, next) {
7+
const attributeNames = Object.keys({ ...current.attributes, ...next.attributes });
8+
for (const name of attributeNames) {
9+
if (name === 'html') {
10+
if (next.attributes[name] !== current.attributes[name]) {
11+
selector.innerHTML = next.attributes[name];
12+
}
13+
next.attributes['data-n'] === undefined && anchorableElement(selector);
14+
} else if (name === 'checked') {
15+
if (next.attributes[name] !== selector.value) {
16+
selector.checked = next.attributes[name];
17+
}
18+
} else if (name === 'value') {
19+
if (next.attributes[name] !== selector.value) {
20+
selector.value = next.attributes[name];
21+
}
22+
} else if (name.startsWith('on')) {
23+
const eventName = name.replace('on', '');
24+
const key = '_event.' + eventName;
25+
selector.removeEventListener(eventName, current[key]);
26+
if (next.attributes[name]) {
27+
next[key] = (event) => {
28+
if (next.attributes.default !== true) {
29+
event.preventDefault();
30+
}
31+
next.attributes[name]({ ...next.attributes, event });
32+
};
33+
selector.addEventListener(eventName, next[key]);
34+
}
35+
} else {
36+
const type = typeof (next.attributes[name]);
37+
if (type !== 'object' && type !== 'function') {
38+
if (current.attributes[name] !== undefined && next.attributes[name] === undefined) {
39+
selector.removeAttribute(name);
40+
} else if (current.attributes[name] !== next.attributes[name]) {
41+
if (name != 'value' && next.attributes[name] === false || next.attributes[name] === null || next.attributes[name] === undefined) {
42+
selector.removeAttribute(name);
43+
} else if (name != 'value' && next.attributes[name] === true) {
44+
selector.setAttribute(name, '');
45+
} else {
46+
selector.setAttribute(name, next.attributes[name]);
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
function _rerender(current, next) {
755

8-
current = current === undefined ? client.virtualDom : current;
9-
next = next === undefined ? client.nextVirtualDom : next;
56+
const selector = current.element
57+
next.element = current.element
1058

1159
if (next.instance) {
1260
next.instance._self.element = selector;
1361
}
1462

15-
if (!client.hydrated && selector) {
16-
for (const element of selector.childNodes) {
17-
if (element.tagName && element.tagName.toLowerCase() == 'textarea' && element.childNodes.length == 0) {
18-
element.appendChild(document.createTextNode(''));
19-
}
20-
if (element.COMMENT_NODE === 8 && element.textContent === '#') {
21-
selector.removeChild(element);
22-
}
23-
}
24-
}
25-
2663
if (isFalse(current) && isFalse(next)) {
2764
return;
2865
}
2966

3067
if ((isFalse(current) || isFalse(next)) && current != next) {
3168
const nextSelector = render(next);
32-
return selector.replaceWith(nextSelector);
69+
selector.replaceWith(nextSelector);
70+
if (current.type !== 'head' && next.type !== 'head') {
71+
return
72+
}
3373
}
3474

35-
if (current.type == 'head' && next.type == 'head') {
36-
return;
75+
if (current.type == 'head' ^ next.type == 'head') {
76+
const nextSelector = render(next);
77+
selector.replaceWith(nextSelector);
3778
}
3879

39-
if (current.type == 'head' || next.type == 'head') {
40-
const nextSelector = render(next);
41-
return selector.replaceWith(nextSelector);
80+
if (current.type !== 'head' && next.type === 'head') {
81+
const limit = next.children.length;
82+
for (let i = limit - 1; i > -1; i--) {
83+
const nextSelector = render(next.children[i]);
84+
document.querySelector('head').appendChild(nextSelector)
85+
}
86+
return
87+
}
88+
89+
if (current.type === 'head' && next.type !== 'head') {
90+
const limit = current.children.length;
91+
for (let i = limit - 1; i > -1; i--) {
92+
document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove()
93+
}
94+
return
95+
}
96+
97+
if (next.type === 'head') {
98+
const limit = Math.max(current.children.length, next.children.length);
99+
for (let i = limit - 1; i > -1; i--) {
100+
if (isUndefined(current.children[i]) && !isFalse(next.children[i])) {
101+
const nextSelector = render(next.children[i]);
102+
document.querySelector('head').appendChild(nextSelector)
103+
} else if (isUndefined(next.children[i]) && !isFalse(current.children[i])) {
104+
current.children[i].element.remove()
105+
} else if (!isFalse(current.children[i]) && !isFalse(next.children[i])) {
106+
if (current.children[i].type === next.children[i].type) {
107+
next.children[i].element = current.children[i].element
108+
const element = document.querySelector(`[data-n="${next.children[i].attributes['data-n']}"]`)
109+
updateAttributes(element, current.children[i], next.children[i])
110+
} else {
111+
document.querySelector(`[data-n="${current.children[i].attributes['data-n']}"]`).remove()
112+
const nextSelector = render(next.children[i]);
113+
document.querySelector('head').appendChild(nextSelector)
114+
}
115+
} else if (isFalse(current.children[i]) && !isFalse(next.children[i])) {
116+
const nextSelector = render(next.children[i]);
117+
document.querySelector('head').appendChild(nextSelector)
118+
} else {
119+
current.children[i].element.remove()
120+
}
121+
}
122+
return
42123
}
43124

44125
if (current.type !== next.type) {
@@ -48,73 +129,28 @@ export default function rerender(selector, current, next) {
48129

49130
if (isText(current) && isText(next)) {
50131
if (current != next) {
51-
selector.nodeValue = next;
132+
selector.nodeValue = next.text;
52133
}
53134
return;
54135
}
55136

56137
if (current.type === next.type) {
57-
58-
const attributeNames = Object.keys({ ...current.attributes, ...next.attributes });
59-
for (const name of attributeNames) {
60-
if (name === 'html') {
61-
if (next.attributes[name] !== current.attributes[name]) {
62-
selector.innerHTML = next.attributes[name];
63-
}
64-
anchorableElement(selector);
65-
} else if (name === 'checked') {
66-
if (next.attributes[name] !== selector.value) {
67-
selector.checked = next.attributes[name];
68-
}
69-
} else if (name === 'value') {
70-
if (next.attributes[name] !== selector.value) {
71-
selector.value = next.attributes[name];
72-
}
73-
} else if (name.startsWith('on')) {
74-
const eventName = name.replace('on', '');
75-
const key = '_event.' + eventName;
76-
selector.removeEventListener(eventName, current[key]);
77-
if (next.attributes[name]) {
78-
next[key] = (event) => {
79-
if (next.attributes.default !== true) {
80-
event.preventDefault();
81-
}
82-
next.attributes[name]({ ...next.attributes, event });
83-
};
84-
selector.addEventListener(eventName, next[key]);
85-
}
86-
} else {
87-
const type = typeof (next.attributes[name]);
88-
if (type !== 'object' && type !== 'function') {
89-
if (current.attributes[name] !== undefined && next.attributes[name] === undefined) {
90-
selector.removeAttribute(name);
91-
} else if (current.attributes[name] !== next.attributes[name]) {
92-
if (name != 'value' && next.attributes[name] === false || next.attributes[name] === null || next.attributes[name] === undefined) {
93-
selector.removeAttribute(name);
94-
} else if (name != 'value' && next.attributes[name] === true) {
95-
selector.setAttribute(name, '');
96-
} else {
97-
selector.setAttribute(name, next.attributes[name]);
98-
}
99-
}
100-
}
101-
}
102-
}
138+
updateAttributes(selector, current, next)
103139

104140
if (next.attributes.html) return;
105141

106142
const limit = Math.max(current.children.length, next.children.length);
107143
if (next.children.length > current.children.length) {
108144
for (let i = 0; i < current.children.length; i++) {
109-
rerender(selector.childNodes[i], current.children[i], next.children[i]);
145+
_rerender(current.children[i], next.children[i]);
110146
}
111147
for (let i = current.children.length; i < next.children.length; i++) {
112148
const nextSelector = render(next.children[i]);
113149
selector.appendChild(nextSelector);
114150
}
115151
} else if (current.children.length > next.children.length) {
116152
for (let i = 0; i < next.children.length; i++) {
117-
rerender(selector.childNodes[i], current.children[i], next.children[i]);
153+
_rerender(current.children[i], next.children[i]);
118154
}
119155
for (let i = current.children.length - 1; i >= next.children.length; i--) {
120156
selector.removeChild(selector.childNodes[i]);
@@ -129,12 +165,12 @@ export default function rerender(selector, current, next) {
129165
throw new Error('Virtual DOM does not match the DOM.')
130166
return;
131167
}
132-
rerender(selector.childNodes[i], current.children[i], next.children[i]);
168+
_rerender(current.children[i], next.children[i]);
133169
}
134170
}
135171

136172
if (next.type == 'textarea') {
137-
selector.value = next.children.join("");
173+
selector.value = next.children.reduce((t, c) => t + c.text, '');
138174
}
139175

140176
if (next.type == 'select') {
@@ -143,4 +179,29 @@ export default function rerender(selector, current, next) {
143179

144180
}
145181

182+
}
183+
184+
export default function rerender() {
185+
_rerender(client.virtualDom, client.nextVirtualDom)
186+
}
187+
188+
export function hydrate(selector, node) {
189+
if (node?.attributes?.['data-n'] !== undefined) {
190+
node.element = document.querySelector(`[data-n="${node.attributes['data-n']}"]`)
191+
return
192+
}
193+
node.element = selector
194+
for (const element of selector.childNodes) {
195+
if (element.tagName && element.tagName.toLowerCase() == 'textarea' && element.childNodes.length == 0) {
196+
element.appendChild(document.createTextNode(''));
197+
}
198+
if (element.COMMENT_NODE === 8 && element.textContent === '#') {
199+
selector.removeChild(element);
200+
}
201+
}
202+
if (!node.children) return
203+
const limit = node.children.length;
204+
for (let i = limit - 1; i > -1; i--) {
205+
hydrate(selector.childNodes[i], node.children[i])
206+
}
146207
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.