This repository was archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (52 loc) · 1.72 KB
/
index.js
File metadata and controls
60 lines (52 loc) · 1.72 KB
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
var flyd = require('flyd')
flyd.mergeAll = require('flyd/module/mergeall')
var snabbdom = require('snabbdom')
var patch = snabbdom.init([
require('snabbdom/modules/class').default,
require('snabbdom/modules/props').default,
require('snabbdom/modules/style').default,
require('snabbdom/modules/eventlisteners').default,
require('snabbdom/modules/attributes').default
])
// A component has a:
// view: snabbdom view function
// state: object of static data and flyd streams
// container: the DOM element we want to replace with our rendered snabbdom tree
function render (view, state, container) {
var state$ = flyd.mergeAll(getStreams(state))
var viewState = function () { return view(state) }
var view$ = flyd.map(viewState, state$)
var vtree$ = flyd.scan(patch, container, view$)
state$([]) // trigger an initial patch
var dom$ = flyd.map(function (vnode) { return vnode.elm }, vtree$)
return {state$: state$, vtree$: vtree$, dom$: dom$}
}
// Return all the streams within an object, including those nested further down
function getStreams (obj) {
var stack = [obj]
var streams = []
while(stack.length) {
var current = stack.pop()
for (var prop in current) {
var val = current[prop]
if (flyd.isStream(val)) {
streams.push(val)
} else if (isPlainObj(val)) {
stack.push(val)
}
}
}
return streams
}
// Utils
function isPlainObj (obj) {
if (typeof obj === 'object' && obj !== null) {
if (typeof Object.getPrototypeOf === 'function') {
var proto = Object.getPrototypeOf(obj)
return proto === Object.prototype || proto === null
}
return Object.prototype.toString.call(obj) == '[object Object]'
}
return false
}
module.exports = render