-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.js
More file actions
39 lines (34 loc) · 1.13 KB
/
Core.js
File metadata and controls
39 lines (34 loc) · 1.13 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
//destructuring assignment
export default function html([first, ...strings], ...values){
//reduce là một phương thức của mảng (Array) cho phép bạn chuyển đổi một mảng thành một giá trị duy nhất thông qua việc áp dụng một hàm callback lên từng phần tử của mảng.
return values.reduce(
(acc, cur) => acc.concat(cur, strings.shift()),
[first]
)
.filter(x => x && x !== true || x === 0)
.join('')
}
export function createStore(reducer){
let state = reducer()
const roots = new Map()
function render(){
for(const [root, component] of roots){
const output = component()
root.innerHTML = output
}
}
return {
attach(component, root){
roots.set(root, component)
render()
},
connect(selector = state => state){
return component => (props, ...args) =>
component(Object.assign({},props,selector(state), ...args))
},
dispatch(action, ...args){
state = reducer(state, action, args)
render()
}
}
}