-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
34 lines (31 loc) · 862 Bytes
/
app.ts
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
import { diff, patch, makeNode, VNode, EventHandler } from './mvd.js';
import { Incr, Reactor } from './inc.js';
export type App = (r: Reactor) => Incr<VNode>;
export function runApp(elt: Element, app: App): void {
const r = new Reactor();
const view = app(r);
let dom: ChildNode | null = null;
let vdom: VNode | null = null;
let renderWaiting = false;
view.observe().watch(_ => {
if(renderWaiting) {
return true;
}
renderWaiting = true;
window.requestAnimationFrame(() => {
renderWaiting = false;
const newVDom = view.get();
if(vdom === null || dom === null) {
vdom = newVDom
dom = makeNode(vdom);
} else {
const p = diff(vdom, newVDom);
vdom = newVDom;
dom = patch(dom, p);
}
elt.replaceWith(dom);
})
return true;
});
r.stabilize();
}