-
-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathmount-redraw.js
49 lines (40 loc) · 1.03 KB
/
mount-redraw.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
38
39
40
41
42
43
44
45
46
47
48
49
"use strict"
var Vnode = require("../render/vnode")
module.exports = function(render, schedule, console) {
var subscriptions = []
var pending = false
var offset = -1
function sync() {
for (offset = 0; offset < subscriptions.length; offset += 2) {
try { render(subscriptions[offset], Vnode(subscriptions[offset + 1]), redraw) }
catch (e) { console.error(e) }
}
offset = -1
}
function redraw() {
if (!pending) {
pending = true
schedule(function() {
pending = false
sync()
})
}
}
redraw.sync = sync
function mount(root, component) {
if (component != null && component.view == null && typeof component !== "function") {
throw new TypeError("m.mount expects a component, not a vnode.")
}
var index = subscriptions.indexOf(root)
if (index >= 0) {
subscriptions.splice(index, 2)
if (index <= offset) offset -= 2
render(root, [])
}
if (component != null) {
subscriptions.push(root, component)
render(root, Vnode(component), redraw)
}
}
return {mount: mount, redraw: redraw}
}