forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextUtils.js
More file actions
141 lines (116 loc) · 3.69 KB
/
ContextUtils.js
File metadata and controls
141 lines (116 loc) · 3.69 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
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
import React from 'react'
import PropTypes from 'prop-types'
// Works around issues with context updates failing to propagate.
// Caveat: the context value is expected to never change its identity.
// https://github.com/facebook/react/issues/2517
// https://github.com/reactjs/react-router/issues/470
const contextProviderShape = PropTypes.shape({
subscribe: PropTypes.func.isRequired,
eventIndex: PropTypes.number.isRequired
})
function makeContextName(name) {
return `@@contextSubscriber/${name}`
}
const prefixUnsafeLifecycleMethods = typeof React.forwardRef !== 'undefined'
export function ContextProvider(name) {
const contextName = makeContextName(name)
const listenersKey = `${contextName}/listeners`
const eventIndexKey = `${contextName}/eventIndex`
const subscribeKey = `${contextName}/subscribe`
const config = {
childContextTypes: {
[contextName]: contextProviderShape.isRequired
},
getChildContext() {
return {
[contextName]: {
eventIndex: this[eventIndexKey],
subscribe: this[subscribeKey]
}
}
},
// this method will be updated to UNSAFE_componentWillMount below for React versions >= 16.3
componentWillMount() {
this[listenersKey] = []
this[eventIndexKey] = 0
},
// this method will be updated to UNSAFE_componentWillReceiveProps below for React versions >= 16.3
componentWillReceiveProps() {
this[eventIndexKey]++
},
componentDidUpdate() {
this[listenersKey].forEach(listener =>
listener(this[eventIndexKey])
)
},
[subscribeKey](listener) {
// No need to immediately call listener here.
this[listenersKey].push(listener)
return () => {
this[listenersKey] = this[listenersKey].filter(item =>
item !== listener
)
}
}
}
if (prefixUnsafeLifecycleMethods) {
config.UNSAFE_componentWillMount = config.componentWillMount
config.UNSAFE_componentWillReceiveProps = config.componentWillReceiveProps
delete config.componentWillMount
delete config.componentWillReceiveProps
}
return config
}
export function ContextSubscriber(name) {
const contextName = makeContextName(name)
const lastRenderedEventIndexKey = `${contextName}/lastRenderedEventIndex`
const handleContextUpdateKey = `${contextName}/handleContextUpdate`
const unsubscribeKey = `${contextName}/unsubscribe`
const config = {
contextTypes: {
[contextName]: contextProviderShape
},
getInitialState() {
if (!this.context[contextName]) {
return {}
}
return {
[lastRenderedEventIndexKey]: this.context[contextName].eventIndex
}
},
componentDidMount() {
if (!this.context[contextName]) {
return
}
this[unsubscribeKey] = this.context[contextName].subscribe(
this[handleContextUpdateKey]
)
},
// this method will be updated to UNSAFE_componentWillReceiveProps below for React versions >= 16.3
componentWillReceiveProps() {
if (!this.context[contextName]) {
return
}
this.setState({
[lastRenderedEventIndexKey]: this.context[contextName].eventIndex
})
},
componentWillUnmount() {
if (!this[unsubscribeKey]) {
return
}
this[unsubscribeKey]()
this[unsubscribeKey] = null
},
[handleContextUpdateKey](eventIndex) {
if (eventIndex !== this.state[lastRenderedEventIndexKey]) {
this.setState({ [lastRenderedEventIndexKey]: eventIndex })
}
}
}
if (prefixUnsafeLifecycleMethods) {
config.UNSAFE_componentWillReceiveProps = config.componentWillReceiveProps
delete config.componentWillReceiveProps
}
return config
}