-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
37 lines (36 loc) · 1.3 KB
/
main.js
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
var virtualize = require('vdom-virtualize');
var diff = require('virtual-dom/diff');
var patch =require('virtual-dom/patch');
var h = require('virtual-dom/h');
var createElement = require('virtual-dom/create-element');
window.onload = function () {
var evtSource = new EventSource("/events");
var div = document.getElementById("content");
var lastVnode = h('div');
var root = createElement(lastVnode);
div.appendChild(root);
var i = 0;
evtSource.onmessage = function(e) {
i ++;
var vnode = virtualize.fromHTML("<div>" + e.data.replace(/\\n/g, "\n") + "</div>");
var domNode = createElement(vnode);
var hidden = createElement(
h('div', {
style: {
visibility: "hidden",
position: "absolute",
top: "0"
}
})
);
document.body.appendChild(hidden);
hidden.appendChild(domNode);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, hidden, function() {
var newTree = virtualize(hidden.firstChild);
var patches = diff(lastVnode, newTree);
root = patch(root, patches);
document.getElementsByTagName('html')[0].scrollTop = root.scrollHeight;
lastVnode = newTree;
}]);
}
}