-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
65 lines (61 loc) · 1.74 KB
/
Copy pathstore.ts
File metadata and controls
65 lines (61 loc) · 1.74 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
60
61
62
63
64
65
import { fromJS } from "immutable"
import * as React from "react"
const EF = (a, b) => ({})
let store = fromJS({})
let updateQueue: Function[] = []
export const init = (state = {}) => {
store = fromJS(state)
}
export const getState = () => store
export const dispatch = function dispatch(action) {
let res = action(store)
if (res !== store) {
store = res
updateQueue.map(f => f())
}
}
export const isSameObject = (obj1, obj2) => {
let keys1 = Object.keys(obj1)
let keys2 = Object.keys(obj2)
if (keys1.length !== keys2.length) {
return false
} else if (keys1.length + keys2.length === 0) {
return true
}
return !keys1.find(k => obj1[k] !== obj2[k])
}
export const connect = (mapProps = EF, mapDispatch?:Function) => (Component) => class extends React.Component {
props
tempProps
tempUpdate
forceUpdate
execProps() {
const { props } = this
const res1 = mapProps(getState, props)
let res2 = {}
if (mapDispatch) {
res2 = mapDispatch(dispatch, { ...props, ...res1})
}
return { ...props, ...res1, ...res2 }
}
constructor(props) {
super(props)
let t = this
t.execProps = t.execProps.bind(t)
t.tempProps = t.execProps()
t.tempUpdate = function () {
let newProps = t.execProps()
if (!isSameObject(t.tempProps, newProps)) {
t.tempProps = newProps
t.forceUpdate && t.forceUpdate()
}
}
updateQueue.push(t.tempUpdate)
}
componentWillUnmount() {
updateQueue.splice(updateQueue.indexOf(this.tempUpdate), 1)
}
render() {
return React.createElement(Component, this.tempProps)
}
}