Skip to content

Commit 204841c

Browse files
authored
Revert "Drop support for websocket adapter" (#2762)
2 parents 38404d9 + b6a9a18 commit 204841c

4 files changed

Lines changed: 151 additions & 24 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import BasicAdapter from './basic.js';
2+
import { onReady } from '../utils/on-ready.js';
3+
import { run } from '../lib/ember/runloop.js';
4+
5+
export default class Websocket extends BasicAdapter {
6+
sendMessage(options = {}) {
7+
this.socket.emit('emberInspectorMessage', options);
8+
}
9+
10+
get socket() {
11+
return window.EMBER_INSPECTOR_CONFIG.remoteDebugSocket;
12+
}
13+
14+
_listen() {
15+
this.socket.on('emberInspectorMessage', (message) => {
16+
// We should generally not be run-wrapping here. Starting a runloop in
17+
// ember-debug will cause the inspected app to revalidate/rerender. We
18+
// are generally not intending to cause changes to the rendered output
19+
// of the app, so this is generally unnecessary, and in big apps this
20+
// could be quite slow. There is nothing special about the `view:*`
21+
// messages – I (GC) just happened to have reviewed all of them recently
22+
// and can be quite sure that they don't need the runloop. We should
23+
// audit the rest of them and see if we can remove the else branch. I
24+
// think we most likely can. In the limited cases (if any) where the
25+
// runloop is needed, the callback code should just do the wrapping
26+
// themselves.
27+
if (message.type.startsWith('view:')) {
28+
this._messageReceived(message);
29+
} else {
30+
run(() => {
31+
this._messageReceived(message);
32+
});
33+
}
34+
});
35+
}
36+
37+
_disconnect() {
38+
this.socket.removeAllListeners('emberInspectorMessage');
39+
}
40+
41+
connect() {
42+
return new Promise((resolve, reject) => {
43+
onReady(() => {
44+
if (this.isDestroyed) {
45+
reject();
46+
}
47+
const EMBER_INSPECTOR_CONFIG = window.EMBER_INSPECTOR_CONFIG;
48+
if (
49+
typeof EMBER_INSPECTOR_CONFIG === 'object' &&
50+
EMBER_INSPECTOR_CONFIG.remoteDebugSocket
51+
) {
52+
resolve();
53+
}
54+
});
55+
}).then(() => {
56+
this._listen();
57+
});
58+
}
59+
60+
willDestroy() {
61+
this._disconnect();
62+
}
63+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import hasEmber from './utils/has-ember.js';
2+
3+
await hasEmber();
4+
5+
// These dynamic imports are intentionally after the above await hasEmber() call.
6+
// We cannot move these to a regular import because we want to wait for Ember to
7+
// be available on page before we can initialise the module tree.
8+
const startInspector = await import('../lib/start-inspector.js');
9+
const adapter = await import('../adapters/websocket.js');
10+
11+
startInspector.default(adapter.default);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { run } from '@ember/runloop';
2+
import BasicAdapter from './basic';
3+
4+
export default class Websocket extends BasicAdapter {
5+
constructor() {
6+
super(...arguments);
7+
this._connect();
8+
}
9+
10+
get socket() {
11+
return window.EMBER_INSPECTOR_CONFIG.remoteDebugSocket;
12+
}
13+
14+
sendMessage(message) {
15+
this.socket.emit('emberInspectorMessage', message ?? {});
16+
}
17+
18+
_connect() {
19+
this.socket.on('emberInspectorMessage', (message) => {
20+
// eslint-disable-next-line ember/no-runloop
21+
run(() => {
22+
this._messageReceived(message);
23+
});
24+
});
25+
}
26+
27+
_disconnect() {
28+
this.socket.removeAllListeners('emberInspectorMessage');
29+
}
30+
31+
willDestroy() {
32+
this._disconnect();
33+
}
34+
}

packages/ember-inspector/ember-cli-build.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,23 @@ module.exports = function (defaults) {
101101
'dist',
102102
);
103103

104-
for (const dist of ['basic', 'chrome', 'firefox', 'bookmarklet']) {
105-
const entryPoint = concatFiles(
106-
new Funnel(emberDebug, {
107-
destDir: 'ember-debug',
108-
include: [`${dist}-debug.js`],
109-
}),
110-
{
111-
inputFiles: ['**/*.js'],
112-
outputFile: '/ember_debug.js',
113-
sourceMapConfig: { enabled: false },
114-
},
115-
);
104+
['basic', 'chrome', 'firefox', 'bookmarklet', 'websocket'].forEach(
105+
function (dist) {
106+
let entryPoint = concatFiles(
107+
new Funnel(emberDebug, {
108+
destDir: 'ember-debug',
109+
include: [`${dist}-debug.js`],
110+
}),
111+
{
112+
inputFiles: ['**/*.js'],
113+
outputFile: '/ember_debug.js',
114+
sourceMapConfig: { enabled: false },
115+
},
116+
);
116117

117-
emberDebugs[dist] = mergeTrees([emberDebug, entryPoint]);
118-
}
118+
emberDebugs[dist] = mergeTrees([emberDebug, entryPoint]);
119+
},
120+
);
119121

120122
let tree = app.toTree();
121123

@@ -241,6 +243,7 @@ module.exports = function (defaults) {
241243
chrome,
242244
firefox,
243245
bookmarklet,
246+
websocket: mergeTrees([tree, emberDebugs.websocket]),
244247
basic: mergeTrees([tree, emberDebugs.basic]),
245248
};
246249
Object.keys(dists).forEach(function (key) {
@@ -255,6 +258,20 @@ module.exports = function (defaults) {
255258
});
256259
});
257260

261+
// Add {{ remote-port }} to the head
262+
// so that the websocket addon can replace it.
263+
dists.websocket = replace(dists.websocket, {
264+
files: ['index.html'],
265+
patterns: [
266+
{
267+
match: /<head>/,
268+
replacement: '<head>\n{{ remote-port }}\n',
269+
},
270+
],
271+
});
272+
273+
let output;
274+
258275
if (env === 'test') {
259276
// `ember test` expects the index.html file to be in the
260277
// output directory.
@@ -268,16 +285,18 @@ module.exports = function (defaults) {
268285
},
269286
],
270287
});
271-
272-
return mergeTrees([dists.basic, dists.chrome]);
288+
output = mergeTrees([dists.basic, dists.chrome]);
289+
} else {
290+
dists.testing = mergeTrees([dists.basic, dists.chrome]);
291+
292+
output = mergeTrees([
293+
mv(dists.bookmarklet, 'bookmarklet'),
294+
mv(dists.firefox, 'firefox'),
295+
mv(dists.chrome, 'chrome'),
296+
mv(dists.websocket, 'websocket'),
297+
mv(dists.testing, 'testing'),
298+
]);
273299
}
274300

275-
dists.testing = mergeTrees([dists.basic, dists.chrome]);
276-
277-
return mergeTrees([
278-
mv(dists.bookmarklet, 'bookmarklet'),
279-
mv(dists.firefox, 'firefox'),
280-
mv(dists.chrome, 'chrome'),
281-
mv(dists.testing, 'testing'),
282-
]);
301+
return output;
283302
};

0 commit comments

Comments
 (0)