This repository was archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (157 loc) · 5.15 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React from 'react';
const providerSort = (a, b) =>
a[0] < b[0] ?
-1 :
1;
const createMultiContext = (defaults = Object.create(null)) => {
const contexts = Object.create(null);
for (const [ key, value ] of Object.entries(defaults)) {
contexts[key] = React.createContext(value);
}
return class MultiContext extends React.PureComponent {
static with(...contextKeys) {
const reduceContextToProps = (props, contextValue, index) => {
props[contextKeys[index]] = contextValue;
return props;
};
return (Component) =>
class WithMultiContext extends React.PureComponent {
contextConsumer = (...contextValues) => {
const props = contextValues.reduce(
reduceContextToProps,
Object.create(null)
);
return (
<Component
{...props}
{...this.props}
/>
);
}
render() {
return (
<MultiContext
children={this.contextConsumer}
get={contextKeys}
/>
);
}
};
}
createContext(key) {
if (!Object.prototype.hasOwnProperty.call(contexts, key)) {
contexts[key] =
typeof this.props.default === 'object' &&
this.props.default !== null &&
Object.prototype.hasOwnProperty.call(this.props.default, key) ?
React.createContext(this.props.default[key]) :
React.createContext();
}
return contexts[key];
}
// Recursively generate <Context.Consumer>, with each consumer being a child of the last.
getConsumer(children, index, values) {
// If we have all the consumers, we can spread the values to the child render prop.
if (this.props.get.length === index) {
return children(...values);
}
// Check if this context exists.
const contextKey = this.props.get[index];
if (!Object.prototype.hasOwnProperty.call(contexts, contextKey)) {
throw new Error('Context `' + contextKey + '` does not exist.');
}
// Create this context's consumer.
const Context = contexts[contextKey];
return (
<Context.Consumer
children={
(value) =>
this.getConsumer(
children,
index + 1,
values.concat([ value ])
)
}
/>
);
}
getProvider(children, providers, index) {
// If we have all the consumers, we can spread the values to the child render prop.
if (providers.length === index) {
return children;
}
// Create the context (if it doesn't exist).
const Context = this.createContext(providers[index][0]);
// Create this context's provider.
return (
<Context.Provider
children={this.getProvider(children, providers, index + 1)}
value={providers[index][1]}
/>
);
}
get hasConsumers() {
return (
Array.isArray(this.props.get) &&
this.props.get.length > 0
);
}
get hasProviders() {
return (
typeof this.props.set === 'object' &&
this.props.set !== null
);
}
renderConsumers(children) {
// If there are no consumers to get, just return the children.
if (!this.hasConsumers) {
return children;
}
// If there are consumers to get, get them one at a time.
if (typeof children !== 'function') {
throw new Error('Consuming context requires a function child.');
}
return this.getConsumer(children, 0, []);
}
renderProviders(children) {
// If there are no providers to set, just return the children.
if (!this.hasProviders) {
return children;
}
// If there are providers to set,
const providers = Object.entries(this.props.set);
if (
typeof this.props.default === 'object' &&
this.props.default !== null
) {
const propDefaults = Object.entries(this.props.default);
const propDefaultsLength = propDefaults.length;
for (let x = 0; x < propDefaultsLength; x++) {
// Don't duplicate a provider if it is already set.
if (!Object.prototype.hasOwnProperty.call(this.props.set, propDefaults[x][0])) {
providers.push(propDefaults[x]);
}
}
}
if (providers.length === 0) {
return children;
}
// Sort them to insure same hierarchy and prevent unnecessary re-rendering (if hierarchy were to change).
providers.sort(providerSort);
// If there are consumers, return a function render prop that sets each provider one at a time.
if (this.hasConsumers) {
return (...values) => this.getProvider(children(...values), providers, 0);
}
// If there are no consumers, set each provider one at a time.
return this.getProvider(children, providers, 0);
}
render() {
return this.renderConsumers(
this.renderProviders(
this.props.children
)
);
}
};
};
export default createMultiContext;