This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.js
150 lines (135 loc) · 3.76 KB
/
factory.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import isPlainObject from 'is-plain-object'
function deepMerge (target, source) {
if (!isPlainObject(target) || !isPlainObject(source)) {
return source
}
const destination = {}
for (const key in source) {
if (target[key]) {
destination[key] = deepMerge(target[key], source[key])
} else {
destination[key] = source[key]
}
}
for (const key in target) {
if (key in destination) {
continue
}
destination[key] = target[key]
}
return destination
}
function createObjectByPath ([head, ...tail], value) {
if (!head) {
return value
}
return {
[head]: createObjectByPath(tail, value)
}
}
function path ([head, ...tail], object) {
if (!head) {
return object
}
return path(tail, object[head])
}
export default (devTool, h, Component, createContext) => (initialSpecification = {}) => {
/* START DEVELOPMENT BUILD ONLY */
const onStateUpdate = devTool(() => that, () => update)
/* END DEVELOPMENT BUILD ONLY */
const State = createContext({})
let update
function wireSpecification (that, scope, specification) {
const actions = {}
for (const key in specification) {
const value = specification[key]
if (typeof value === 'function') {
actions[key] = (...parameters) => {
const state = path(['state', ...scope], that)
Promise.resolve(value(state, ...parameters))
.then((partial) => {
if (partial === undefined || partial === {}) {
return
}
update(createObjectByPath(scope, wireSpecification(that, scope, partial)))
})
}
}
}
return Object.assign({}, specification, actions)
}
const preAddedStates = []
let that
class StateProvider extends Component {
constructor (props) {
super(props)
that = this
update = (partial) => {
this.setState((state) => deepMerge(state, partial))
}
this.state = wireSpecification(this, [], initialSpecification)
this.value = this.value.bind(this)
}
componentWillMount () {
preAddedStates.forEach(({scope, specification}) => {
update(createObjectByPath(scope, wireSpecification(this, scope, specification)))
})
}
value () {
return this.state
}
render () {
/* START DEVELOPMENT BUILD ONLY */
onStateUpdate()
/* END DEVELOPMENT BUILD ONLY */
return h(State.Provider, {
value: this.state
}, this.props.children)
}
}
return {
StateProvider,
GlobalState: State.Consumer,
addState (name, specification) {
const scope = name.split('.')
if (update) {
update(createObjectByPath(scope, wireSpecification(that, scope, specification)))
} else {
preAddedStates.push({scope, specification})
}
let referenceCount = 0
let markForDeletion = false
class ChildState extends Component {
componentDidMount () {
referenceCount++
}
componentWillUnmount () {
referenceCount--
if (markForDeletion && referenceCount === 0) {
update(createObjectByPath(scope, undefined))
}
}
render () {
const {children, render} = this.props
const closure = typeof children === 'function'
? children
: children[0] || render
if (!closure) {
return
}
return h(State.Consumer, {}, (state) => closure(path(scope, state)))
}
}
Object.defineProperty(ChildState, 'value', {
get: () => path(scope, that.state)
})
Object.defineProperty(ChildState, 'delete', {
value () {
markForDeletion = true
},
writable: false
})
return ChildState
}
}
}