MCPcopy
hub / github.com/facebook/react / initialize

Function initialize

packages/react-devtools-core/src/standalone.js:220–278  ·  view source on GitHub ↗
(socket: WebSocket)

Source from the content-addressed store, hash-verified

218}
219
220function initialize(socket: WebSocket) {
221 const listeners = [];
222 socket.onmessage = event => {
223 let data;
224 try {
225 if (typeof event.data === 'string') {
226 data = JSON.parse(event.data);
227
228 if (__DEBUG__) {
229 debug('WebSocket.onmessage', data);
230 }
231 } else {
232 throw Error();
233 }
234 } catch (e) {
235 log.error('Failed to parse JSON', event.data);
236 return;
237 }
238 listeners.forEach(fn => {
239 try {
240 fn(data);
241 } catch (error) {
242 log.error('Error calling listener', data);
243 throw error;
244 }
245 });
246 };
247
248 bridge = new Bridge({
249 listen(fn) {
250 listeners.push(fn);
251 return () => {
252 const index = listeners.indexOf(fn);
253 if (index >= 0) {
254 listeners.splice(index, 1);
255 }
256 };
257 },
258 send(event: string, payload: any, transferable?: Array<any>) {
259 if (socket.readyState === socket.OPEN) {
260 socket.send(JSON.stringify({event, payload}));
261 }
262 },
263 });
264 ((bridge: any): FrontendBridge).addListener('shutdown', () => {
265 socket.close();
266 });
267
268 // $FlowFixMe[incompatible-call] found when upgrading Flow
269 store = new Store(bridge, {
270 checkBridgeProtocolCompatibility: true,
271 supportsTraceUpdates: true,
272 supportsClickToInspect: true,
273 });
274
275 log('Connected');
276 statusListener('DevTools initialized.', 'devtools-connected');
277 reload();

Callers 2

connectToSocketFunction · 0.70
startServerFunction · 0.70

Calls 9

statusListenerFunction · 0.85
reloadFunction · 0.85
debugFunction · 0.70
logFunction · 0.70
errorMethod · 0.65
forEachMethod · 0.65
addListenerMethod · 0.65
closeMethod · 0.65
fnFunction · 0.50

Tested by

no test coverage detected