Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve route debug #2536

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/controllers/route-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export default class RouteTreeController extends Controller {
'searchValue'
)
get filtered() {
if (!Array.isArray(this.model)) {
return [];
}
return this.model.filter((routeItem) => {
let currentRoute = this.currentRoute;
let hideRoutes = this.get('options.hideRoutes');
Expand Down
4 changes: 4 additions & 0 deletions app/routes/route-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default class RouteTreeRoute extends TabRoute {
}

setTree(options) {
if (options.error) {
set(this, 'controller.model', options);
return;
}
let routeArray = topSort(options.tree);
set(this, 'controller.model', routeArray);
}
Expand Down
9 changes: 8 additions & 1 deletion app/templates/route-tree.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
{{/in-element}}
{{/if}}

{{#if this.model.error}}
<p>
Routes could not be loaded.
{{this.model.error}}
</p>
{{/if}}

<EmberTable as |t|>
<t.head @columns={{schema-for "route-tree"}} @enableReorder={{false}} />

Expand Down Expand Up @@ -36,4 +43,4 @@
</r.cell>
</b.row>
</t.body>
</EmberTable>
</EmberTable>
66 changes: 44 additions & 22 deletions ember_debug/route-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import classify from 'ember-debug/utils/classify';
import dasherize from 'ember-debug/utils/dasherize';

import Ember from 'ember-debug/utils/ember';
import { later } from 'ember-debug/utils/ember/runloop';
import { _backburner, later } from 'ember-debug/utils/ember/runloop';
import bound from 'ember-debug/utils/bound-method';

const { hasOwnProperty } = Object.prototype;

Expand All @@ -15,32 +16,35 @@ export default class RouteDebug extends DebugPort {
super.init();
this.__currentURL = this.currentURL;
this.__currentRouter = this.router;
this.observer = setInterval(() => {
if (this.__currentURL !== this.currentURL) {
this.sendCurrentRoute();
this.__currentURL = this.currentURL;
}
if (this.__currentRouter !== this.router) {
this._cachedRouteTree = null;
this.__currentRouter = this.router;
}
}, 150);
_backburner.on('end', bound(this, this.checkForUpdate));
}

checkForUpdate() {
if (this.__currentURL !== this.currentURL) {
this.sendCurrentRoute();
this.__currentURL = this.currentURL;
}
if (this.__currentRouter !== this.router) {
this._cachedRouteTree = null;
this.__currentRouter = this.router;
}
}

willDestroy() {
clearInterval(this.observer);
_backburner.off('end', bound(this, this.checkForUpdate));
super.willDestroy();
}

get router() {
if (
this.namespace?.owner.isDestroyed ||
this.namespace?.owner.isDestroying
) {
return null;
}
return this.namespace?.owner.lookup('router:main');
}

get applicationController() {
const container = this.namespace?.owner;
return container.lookup('controller:application');
}

get currentPath() {
return this.namespace?.owner.router.currentPath;
}
Expand Down Expand Up @@ -73,7 +77,13 @@ export default class RouteDebug extends DebugPort {
}

get routeTree() {
if (!this._cachedRouteTree) {
if (
this.namespace?.owner.isDestroyed ||
this.namespace?.owner.isDestroying
) {
return null;
}
if (!this._cachedRouteTree && this.router) {
const router = this.router;
const routerLib = router._routerMicrolib || router.router;
let routeNames = routerLib.recognizer.names;
Expand All @@ -91,8 +101,14 @@ export default class RouteDebug extends DebugPort {
}

sendTree() {
const routeTree = this.routeTree;
this.sendMessage('routeTree', { tree: routeTree });
let routeTree;
let error;
try {
routeTree = this.routeTree;
} catch (e) {
error = e.message;
}
this.sendMessage('routeTree', { tree: routeTree, error });
}

getClassName(name, type) {
Expand All @@ -113,7 +129,7 @@ export default class RouteDebug extends DebugPort {
}
if (className === fullName) {
// full name returned as is - this resolver does not look for the module.
className = className.replace(new RegExp(`^${type}\:`), '');
className = className.replace(new RegExp(`^${type}:`), '');
} else if (className) {
// Module exists and found
className = className.replace(
Expand Down Expand Up @@ -202,7 +218,13 @@ function buildSubTree(routeTree, route) {
controllerClassName = '(unresolved)';
templateName = '(unresolved)';
} else {
controllerName = routeHandler.controllerName || routeHandler.routeName;
const get =
routeHandler.get ||
function (prop) {
return this[prop];
};
controllerName =
get.call(routeHandler, 'controllerName') || routeHandler.routeName;
controllerFactory = owner.factoryFor
? owner.factoryFor(`controller:${controllerName}`)
: owner._lookupFactory(`controller:${controllerName}`);
Expand Down