Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit c85008d

Browse files
committed
refactor highlighter setup
1 parent 4c17157 commit c85008d

File tree

4 files changed

+45
-52
lines changed

4 files changed

+45
-52
lines changed

frontend/Highlighter/setup.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*/
11+
'use strict';
12+
13+
var Highlighter = require('./Highlighter');
14+
15+
import type * as Agent from '../../agent/Agent';
16+
17+
module.exports = function setup(agent: Agent) {
18+
var hl = new Highlighter(window, node => {
19+
agent.selectFromDOMNode(node);
20+
});
21+
agent.on('highlight', data => hl.highlight(data.node, data.name));
22+
agent.on('highlightMany', nodes => hl.highlightMany(nodes));
23+
agent.on('hideHighlight', () => hl.hideHighlight());
24+
agent.on('startInspecting', () => hl.startInspecting());
25+
agent.on('stopInspecting', () => hl.stopInspecting());
26+
agent.on('shutdown', () => {
27+
hl.remove();
28+
});
29+
}
30+

shells/chrome/src/backend.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
var Agent = require('../../../agent/Agent');
1414
var Bridge = require('../../../agent/Bridge');
15-
var Highlighter = require('../../../frontend/Highlighter/Highlighter');
15+
var setupHighlighter = require('../../../frontend/Highlighter/setup');
1616
var setupRNStyle = require('../../../plugins/ReactNativeStyle/setupBackend');
1717

1818
var inject = require('../../../agent/inject');
@@ -71,31 +71,15 @@ function setup(hook) {
7171
setupRNStyle(bridge, agent, hook.resolveRNStyle);
7272
}
7373

74-
var hl;
7574
agent.on('shutdown', () => {
7675
hook.emit('shutdown');
7776
listeners.forEach(fn => {
7877
window.removeEventListener('message', fn);
7978
});
8079
listeners = [];
81-
if (hl) {
82-
hl.remove();
83-
}
8480
});
8581

8682
if (!isReactNative) {
87-
hl = new Highlighter(window, node => {
88-
agent.selectFromDOMNode(node);
89-
});
90-
// $FlowFixMe flow thinks hl might be undefined
91-
agent.on('highlight', data => hl.highlight(data.node, data.name));
92-
// $FlowFixMe flow thinks hl might be undefined
93-
agent.on('highlightMany', nodes => hl.highlightMany(nodes));
94-
// $FlowFixMe flow thinks hl might be undefined
95-
agent.on('hideHighlight', () => hl.hideHighlight());
96-
// $FlowFixMe flow thinks hl might be undefined
97-
agent.on('startInspecting', () => hl.startInspecting());
98-
// $FlowFixMe flow thinks hl might be undefined
99-
agent.on('stopInspecting', () => hl.stopInspecting());
83+
setupHighlighter(agent);
10084
}
10185
}

shells/firefox/src/backend.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
var Agent = require('../../../agent/Agent');
1414
var Bridge = require('../../../agent/Bridge');
15-
var Highlighter = require('../../../frontend/Highlighter/Highlighter');
15+
var setupHighlighter = require('../../../frontend/Highlighter/setup');
1616

1717
var inject = require('../../../agent/inject');
1818

@@ -55,39 +55,27 @@ function setup() {
5555

5656
var bridge = new Bridge();
5757
bridge.attach(wall);
58-
var backend = new Agent(window);
59-
backend.addBridge(bridge);
60-
var hl;
58+
var agent = new Agent(window);
59+
agent.addBridge(bridge);
6160

6261
var _connectTimeout = setTimeout(function () {
63-
console.error('react-devtools backend got no connection');
62+
console.error('react-devtools agent got no connection');
6463
}, 1000);
6564

66-
backend.once('connected', () => {
67-
inject(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, backend, /* lookForOldReact= */true);
65+
agent.once('connected', () => {
66+
inject(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, agent, /* lookForOldReact= */true);
6867
clearTimeout(_connectTimeout);
6968
});
7069

71-
backend.on('shutdown', () => {
70+
agent.on('shutdown', () => {
7271
listeners.forEach(fn => {
7372
window.removeEventListener('message', fn);
7473
});
7574
listeners = [];
76-
if (hl) {
77-
hl.remove();
78-
}
7975
});
8076

8177
if (window.document && window.document.createElement) {
82-
hl = new Highlighter(window, node => {
83-
backend.selectFromDOMNode(node);
84-
});
85-
// $FlowFixMe flow things hl might be undefined
86-
backend.on('highlight', data => hl.highlight(data.node, data.name));
87-
// $FlowFixMe flow things hl might be undefined
88-
backend.on('highlightMany', nodes => hl.highlightMany(nodes));
89-
// $FlowFixMe flow things hl might be undefined
90-
backend.on('hideHighlight', () => hl.hideHighlight());
78+
setupHighlighter(agent);
9179
}
9280
}
9381

shells/plain/backend.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
var Agent = require('../../agent/Agent');
1414
var Bridge = require('../../agent/Bridge');
15-
var Highlighter = require('../../frontend/Highlighter/Highlighter');
15+
var setupHighlighter = require('../../frontend/Highlighter/setup');
1616

1717
var inject = require('../../agent/inject');
1818

@@ -27,18 +27,9 @@ var wall = {
2727

2828
var bridge = new Bridge();
2929
bridge.attach(wall);
30-
var backend = new Agent(window);
31-
backend.addBridge(bridge);
30+
var agent = new Agent(window);
31+
agent.addBridge(bridge);
3232

33-
inject(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, backend);
33+
inject(window.__REACT_DEVTOOLS_GLOBAL_HOOK__, agent);
3434

35-
var hl = new Highlighter(window, node => {
36-
backend.selectFromDOMNode(node);
37-
});
38-
hl.injectButton();
39-
backend.on('highlight', data => hl.highlight(data.node, data.name));
40-
backend.on('highlightMany', nodes => hl.highlightMany(nodes));
41-
backend.on('hideHighlight', () => hl.hideHighlight());
42-
43-
backend.on('startInspecting', () => hl.startInspecting());
44-
backend.on('stopInspecting', () => hl.stopInspecting());
35+
setupHighlighter(agent);

0 commit comments

Comments
 (0)