1 | import { updateComponent } from './lifecycle'
|
2 | import { filterProps } from './create-component'
|
3 | import nextTick from './next-tick'
|
4 |
|
5 | class Manager {
|
6 | map = {}
|
7 | observers = {}
|
8 |
|
9 | set (props = {}, compid, previd) {
|
10 | if (!compid) return
|
11 |
|
12 | if (previd) {
|
13 | this.delete(previd)
|
14 | }
|
15 |
|
16 | const { observers } = this
|
17 | if (!this.map[compid]) {
|
18 | Object.defineProperty(this.map, compid, {
|
19 | configurable: true,
|
20 | get () {
|
21 | return this[`__${compid}`]
|
22 | },
|
23 | set (props) {
|
24 | this[`__${compid}`] = props
|
25 |
|
26 | const component = observers[compid] && observers[compid].component
|
27 | const ComponentClass = observers[compid] && observers[compid].ComponentClass
|
28 | if (!component || !ComponentClass || !component.__isReady) return
|
29 |
|
30 | const nextProps = filterProps(ComponentClass.defaultProps, props, component.props)
|
31 | component.props = nextProps
|
32 | nextTick(() => {
|
33 | component._unsafeCallUpdate = true
|
34 | updateComponent(component)
|
35 | component._unsafeCallUpdate = false
|
36 | })
|
37 | }
|
38 | })
|
39 | }
|
40 | this.map[compid] = props
|
41 | }
|
42 |
|
43 | delete (compid) {
|
44 | delete this.map[compid]
|
45 | delete this.map[`__${compid}`]
|
46 | delete this.observers[compid]
|
47 | }
|
48 | }
|
49 |
|
50 | export default new Manager()
|