-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnabb-component.ts
61 lines (50 loc) · 1.44 KB
/
snabb-component.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
init as snabbdomInit,
toVNode,
VNode,
attributesModule,
classModule,
styleModule,
propsModule,
eventListenersModule,
} from 'snabbdom';
const snabbPatch = snabbdomInit([attributesModule, classModule, styleModule, eventListenersModule, propsModule]);
export type RenderCallBack = (vnode: VNode) => void;
export class Component<StateT> {
public state: StateT = {} as StateT;
public renderCallback: RenderCallBack = null;
private _renderQueued = false;
constructor() {
this.update();
}
update(stateUpdate?: Partial<StateT>) {
// simple update -> render -> callback loop
if (stateUpdate) {
this.state = Object.assign(this.state, stateUpdate);
}
this._queueRender();
}
_queueRender() {
if (!this._renderQueued) {
this._renderQueued = true;
requestAnimationFrame(() => {
if (this.renderCallback) {
this.renderCallback(this.render());
}
this._renderQueued = false;
});
}
}
render(): VNode {
return null;
}
}
export function render<StateT>(componentClass: new () => Component<StateT>, rootEl: HTMLElement): Component<StateT> {
let vnode = toVNode(document.createComment(``));
rootEl.appendChild(vnode.elm);
const componentInstance: Component<StateT> = new componentClass();
componentInstance.renderCallback = function renderCallback(newVNode: VNode) {
vnode = snabbPatch(vnode, newVNode);
};
return componentInstance;
}