-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
131 lines (110 loc) · 3.32 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function () {
var truesilver = {
component: component,
connect: Connector(),
pure: pure
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = truesilver
} else {
window.truesilver = truesilver
}
function Connector (context ) {
var refs = []
context = typeof context === 'undefined' ? {} : context
connect.replaceContext = function (pContext) {
context = pContext
}
Object.defineProperty(connect, 'context', {
get: function () {
return context
},
set: function (props) {
return Object.assign(context, props)
}
})
connect.withContext = Connector
return connect
function connect (mapStateToProps, mapDispatchToProps, mergeProps) {
if (typeof mapStateToProps === 'object' && mapStateToProps.hasOwnProperty('view')) {
refs = [context]
return connectedComponent(mapStateToProps)
}
if (typeof mapStateToProps === 'function') {
refs.push(function () {
return mapStateToProps(context.getState())
})
} else if (typeof mapStateToProps != null) {
refs.push(function () {
return context.getState()
})
}
if (typeof mapDispatchToProps === 'object') {
refs.push(Object.keys(mapDispatchToProps).reduce(function (out, key) {
out[key] = function (val) {
return function () {
return context.dispatch(mapDispatchToProps[key](val))
}
}
return out
}, {}))
} else if (typeof mapDispatchToProps === 'function') {
refs.push(mapDispatchToProps(context.dispatch))
}
return connectedComponent
function connectedComponent (component) {
return Object.assign({}, component, {
oninit (vnode) {
this.streams = []
addConnectedProps(vnode.attrs)
if (component.oninit) {
component.oninit.call(vnode.state, vnode)
}
},
onbeforeupdate (vnode, old) {
addConnectedProps(vnode.attrs)
if (component.onbeforeupdate)
return component.onbeforeupdate.call(vnode.state, vnode, old)
return true
},
onbeforeremove (vnode) {
addConnectedProps(vnode.attrs)
if (component.onbeforeremove)
return component.onbeforeremove.call(vnode.state, vnode)
return null
},
onremove (vnode) {
if (Array.isArray(this.streams))
this.streams.forEach(s => s.end(true))
if (component.onremove)
component.onremove.call(vnode.state, vnode)
},
})
}
function addConnectedProps (props) {
var toAdd = refs.map(function (f) {
if (typeof f === 'function') return f()
return f
})
return Object.assign.apply(null, [props].concat(toAdd))
}
}
}
function component (initializor) {
return {
oninit: function (vnode) {
this.view = initializor(vnode)
},
view: function (vnode) {
return this.view(vnode)
}
}
}
function pure (view) {
return {
view: function (vnode) {
return view(Object.assign({}, vnode.attrs, { children: vnode.children }))
}
}
}
})();