forked from facebook/react
/
context.js
202 lines (171 loc) · 5.71 KB
/
context.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
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from 'react';
import {unstable_batchedUpdates as batchedUpdates} from 'react-dom';
import {createResource} from 'react-devtools-shared/src/devtools/cache';
import {
BridgeContext,
StoreContext,
} from 'react-devtools-shared/src/devtools/views/context';
import {TreeStateContext} from '../TreeContext';
import type {StyleAndLayout as StyleAndLayoutBackend} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';
import type {StyleAndLayout as StyleAndLayoutFrontend} from './types';
import type {Element} from 'react-devtools-shared/src/devtools/views/Components/types';
import type {
Resource,
Thenable,
} from 'react-devtools-shared/src/devtools/cache';
export type GetStyleAndLayout = (id: number) => StyleAndLayoutFrontend | null;
type Context = {|
getStyleAndLayout: GetStyleAndLayout,
|};
const NativeStyleContext = createContext<Context>(((null: any): Context));
NativeStyleContext.displayName = 'NativeStyleContext';
type ResolveFn = (styleAndLayout: StyleAndLayoutFrontend) => void;
type InProgressRequest = {|
promise: Thenable<StyleAndLayoutFrontend>,
resolveFn: ResolveFn,
|};
const inProgressRequests: WeakMap<Element, InProgressRequest> = new WeakMap();
const resource: Resource<
Element,
Element,
StyleAndLayoutFrontend,
> = createResource(
(element: Element) => {
let request = inProgressRequests.get(element);
if (request != null) {
return request.promise;
}
let resolveFn = ((null: any): ResolveFn);
const promise = new Promise(resolve => {
resolveFn = resolve;
});
inProgressRequests.set(element, {promise, resolveFn});
return promise;
},
(element: Element) => element,
{useWeakMap: true},
);
type Props = {|
children: React$Node,
|};
function NativeStyleContextController({children}: Props) {
const bridge = useContext(BridgeContext);
const store = useContext(StoreContext);
const getStyleAndLayout = useCallback<GetStyleAndLayout>(
(id: number) => {
const element = store.getElementByID(id);
if (element !== null) {
return resource.read(element);
} else {
return null;
}
},
[store],
);
// It's very important that this context consumes selectedElementID and not NativeStyleID.
// Otherwise the effect that sends the "inspect" message across the bridge-
// would itself be blocked by the same render that suspends (waiting for the data).
const {selectedElementID} = useContext(TreeStateContext);
const [
currentStyleAndLayout,
setCurrentStyleAndLayout,
] = useState<StyleAndLayoutFrontend | null>(null);
// This effect handler invalidates the suspense cache and schedules rendering updates with React.
useEffect(() => {
const onStyleAndLayout = ({id, layout, style}: StyleAndLayoutBackend) => {
let element = store.getElementByID(id);
if (element !== null) {
const styleAndLayout: StyleAndLayoutFrontend = {
layout,
style,
};
const request = inProgressRequests.get(element);
if (request != null) {
inProgressRequests.delete(element);
batchedUpdates(() => {
request.resolveFn(styleAndLayout);
setCurrentStyleAndLayout(styleAndLayout);
});
} else {
resource.write(element, styleAndLayout);
// Schedule update with React if the currently-selected element has been invalidated.
if (id === selectedElementID) {
setCurrentStyleAndLayout(styleAndLayout);
}
}
}
};
bridge.addListener('NativeStyleEditor_styleAndLayout', onStyleAndLayout);
return () =>
bridge.removeListener(
'NativeStyleEditor_styleAndLayout',
onStyleAndLayout,
);
}, [bridge, currentStyleAndLayout, selectedElementID, store]);
// This effect handler polls for updates on the currently selected element.
useEffect(() => {
if (selectedElementID === null) {
return () => {};
}
const rendererID = store.getRendererIDForElement(selectedElementID);
let timeoutID: TimeoutID | null = null;
const sendRequest = () => {
timeoutID = null;
if (rendererID !== null) {
bridge.send('NativeStyleEditor_measure', {
id: selectedElementID,
rendererID,
});
}
};
// Send the initial measurement request.
// We'll poll for an update in the response handler below.
sendRequest();
const onStyleAndLayout = ({id}: StyleAndLayoutBackend) => {
// If this is the element we requested, wait a little bit and then ask for another update.
if (id === selectedElementID) {
if (timeoutID !== null) {
clearTimeout(timeoutID);
}
timeoutID = setTimeout(sendRequest, 1000);
}
};
bridge.addListener('NativeStyleEditor_styleAndLayout', onStyleAndLayout);
return () => {
bridge.removeListener(
'NativeStyleEditor_styleAndLayout',
onStyleAndLayout,
);
if (timeoutID !== null) {
clearTimeout(timeoutID);
}
};
}, [bridge, selectedElementID, store]);
const value = useMemo(
() => ({getStyleAndLayout}),
// NativeStyle is used to invalidate the cache and schedule an update with React.
[currentStyleAndLayout, getStyleAndLayout],
);
return (
<NativeStyleContext.Provider value={value}>
{children}
</NativeStyleContext.Provider>
);
}
export {NativeStyleContext, NativeStyleContextController};