Skip to content

Commit

Permalink
load ember_debug after ember and load ember inspector after boot (#2051)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx authored Jun 25, 2022
1 parent 87e8a4c commit aed8e59
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
21 changes: 21 additions & 0 deletions app/services/adapters/web-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ export default class WebExtension extends BasicAdapter {
function loadEmberDebug() {
let minimumVersion = config.emberVersionsSupported[0].replace(/\./g, '-');
let xhr;

function loadEmberDebugInWebpage() {
const waitForEmberLoad = new Promise((resolve) => {
if (window.Ember) return resolve();
if (window.requireModule && window.requireModule.has('ember')) {
return resolve();
}
window.addEventListener('Ember', resolve, { once: true });
});
waitForEmberLoad.then(() => {
'replace-with-ember-debug';
});
}
return new Promise((resolve) => {
if (!emberDebug) {
xhr = new XMLHttpRequest();
Expand All @@ -142,6 +155,14 @@ function loadEmberDebug() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
emberDebug = xhr.responseText;
// prepare for usage in replace, dollar signs are part of special replacement patterns...
emberDebug = emberDebug.replace(/\$/g, '$$$$');
emberDebug =
'(' +
loadEmberDebugInWebpage
.toString()
.replace("'replace-with-ember-debug';", emberDebug) +
')()';
resolve(emberDebug);
}
}
Expand Down
37 changes: 30 additions & 7 deletions ember_debug/vendor/startup-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
});

if (!window.EmberInspector._application) {
bootEmberInspector(instance);
setTimeout(() => bootEmberInspector(instance), 0);
}
}
});
Expand Down Expand Up @@ -131,7 +131,9 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};
};

// Newest Ember versions >= 1.10
window.addEventListener('Ember', triggerOnce, { once: true });

const later = () => setTimeout(triggerOnce, 0);
window.addEventListener('Ember', later, { once: true });
// Oldest Ember versions or if this was injected after Ember has loaded.
onReady(triggerOnce);
}
Expand Down Expand Up @@ -165,20 +167,41 @@ var EMBER_VERSIONS_SUPPORTED = {{EMBER_VERSIONS_SUPPORTED}};

sendApps(adapterInstance, apps);

function loadInstance(app) {
let instance = app.__deprecatedInstance__ || (app._applicationInstances && app._applicationInstances[0]);
if (instance) {
// App started
setupInstanceInitializer(app, callback);
callback(instance);
return true
}
}

var app;
for (var i = 0, l = apps.length; i < l; i++) {
app = apps[i];
// We check for the existance of an application instance because
// in Ember > 3 tests don't destroy the app when they're done but the app has no booted instances.
if (app._readinessDeferrals === 0) {
let instance = app.__deprecatedInstance__ || (app._applicationInstances && app._applicationInstances[0]);
if (instance) {
// App started
setupInstanceInitializer(app, callback);
callback(instance);
if (loadInstance(app)) {
break;
}
}

// app already run initializers, but no instance, use _bootPromise and didBecomeReady
if(app._bootPromise) {
app._bootPromise.then((app) => {
loadInstance(app);
});
}

app.reopen({
didBecomeReady() {
this._super.apply(this, arguments);
setTimeout(() => loadInstance(app), 0)
}
});

}
Ember.Application.initializer({
name: 'ember-inspector-booted',
Expand Down

0 comments on commit aed8e59

Please sign in to comment.