Skip to content

Commit f93c70b

Browse files
authored
improve route debug (#2536)
1 parent 5c3255c commit f93c70b

File tree

4 files changed

+59
-23
lines changed

4 files changed

+59
-23
lines changed

app/controllers/route-tree.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export default class RouteTreeController extends Controller {
2525
'searchValue'
2626
)
2727
get filtered() {
28+
if (!Array.isArray(this.model)) {
29+
return [];
30+
}
2831
return this.model.filter((routeItem) => {
2932
let currentRoute = this.currentRoute;
3033
let hideRoutes = this.get('options.hideRoutes');

app/routes/route-tree.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export default class RouteTreeRoute extends TabRoute {
2525
}
2626

2727
setTree(options) {
28+
if (options.error) {
29+
set(this, 'controller.model', options);
30+
return;
31+
}
2832
let routeArray = topSort(options.tree);
2933
set(this, 'controller.model', routeArray);
3034
}

app/templates/route-tree.hbs

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
{{/in-element}}
88
{{/if}}
99

10+
{{#if this.model.error}}
11+
<p>
12+
Routes could not be loaded.
13+
{{this.model.error}}
14+
</p>
15+
{{/if}}
16+
1017
<EmberTable as |t|>
1118
<t.head @columns={{schema-for "route-tree"}} @enableReorder={{false}} />
1219

@@ -36,4 +43,4 @@
3643
</r.cell>
3744
</b.row>
3845
</t.body>
39-
</EmberTable>
46+
</EmberTable>

ember_debug/route-debug.js

+44-22
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import classify from 'ember-debug/utils/classify';
55
import dasherize from 'ember-debug/utils/dasherize';
66

77
import Ember from 'ember-debug/utils/ember';
8-
import { later } from 'ember-debug/utils/ember/runloop';
8+
import { _backburner, later } from 'ember-debug/utils/ember/runloop';
9+
import bound from 'ember-debug/utils/bound-method';
910

1011
const { hasOwnProperty } = Object.prototype;
1112

@@ -15,32 +16,35 @@ export default class RouteDebug extends DebugPort {
1516
super.init();
1617
this.__currentURL = this.currentURL;
1718
this.__currentRouter = this.router;
18-
this.observer = setInterval(() => {
19-
if (this.__currentURL !== this.currentURL) {
20-
this.sendCurrentRoute();
21-
this.__currentURL = this.currentURL;
22-
}
23-
if (this.__currentRouter !== this.router) {
24-
this._cachedRouteTree = null;
25-
this.__currentRouter = this.router;
26-
}
27-
}, 150);
19+
_backburner.on('end', bound(this, this.checkForUpdate));
20+
}
21+
22+
checkForUpdate() {
23+
if (this.__currentURL !== this.currentURL) {
24+
this.sendCurrentRoute();
25+
this.__currentURL = this.currentURL;
26+
}
27+
if (this.__currentRouter !== this.router) {
28+
this._cachedRouteTree = null;
29+
this.__currentRouter = this.router;
30+
}
2831
}
2932

3033
willDestroy() {
31-
clearInterval(this.observer);
34+
_backburner.off('end', bound(this, this.checkForUpdate));
3235
super.willDestroy();
3336
}
3437

3538
get router() {
39+
if (
40+
this.namespace?.owner.isDestroyed ||
41+
this.namespace?.owner.isDestroying
42+
) {
43+
return null;
44+
}
3645
return this.namespace?.owner.lookup('router:main');
3746
}
3847

39-
get applicationController() {
40-
const container = this.namespace?.owner;
41-
return container.lookup('controller:application');
42-
}
43-
4448
get currentPath() {
4549
return this.namespace?.owner.router.currentPath;
4650
}
@@ -73,7 +77,13 @@ export default class RouteDebug extends DebugPort {
7377
}
7478

7579
get routeTree() {
76-
if (!this._cachedRouteTree) {
80+
if (
81+
this.namespace?.owner.isDestroyed ||
82+
this.namespace?.owner.isDestroying
83+
) {
84+
return null;
85+
}
86+
if (!this._cachedRouteTree && this.router) {
7787
const router = this.router;
7888
const routerLib = router._routerMicrolib || router.router;
7989
let routeNames = routerLib.recognizer.names;
@@ -91,8 +101,14 @@ export default class RouteDebug extends DebugPort {
91101
}
92102

93103
sendTree() {
94-
const routeTree = this.routeTree;
95-
this.sendMessage('routeTree', { tree: routeTree });
104+
let routeTree;
105+
let error;
106+
try {
107+
routeTree = this.routeTree;
108+
} catch (e) {
109+
error = e.message;
110+
}
111+
this.sendMessage('routeTree', { tree: routeTree, error });
96112
}
97113

98114
getClassName(name, type) {
@@ -113,7 +129,7 @@ export default class RouteDebug extends DebugPort {
113129
}
114130
if (className === fullName) {
115131
// full name returned as is - this resolver does not look for the module.
116-
className = className.replace(new RegExp(`^${type}\:`), '');
132+
className = className.replace(new RegExp(`^${type}:`), '');
117133
} else if (className) {
118134
// Module exists and found
119135
className = className.replace(
@@ -202,7 +218,13 @@ function buildSubTree(routeTree, route) {
202218
controllerClassName = '(unresolved)';
203219
templateName = '(unresolved)';
204220
} else {
205-
controllerName = routeHandler.controllerName || routeHandler.routeName;
221+
const get =
222+
routeHandler.get ||
223+
function (prop) {
224+
return this[prop];
225+
};
226+
controllerName =
227+
get.call(routeHandler, 'controllerName') || routeHandler.routeName;
206228
controllerFactory = owner.factoryFor
207229
? owner.factoryFor(`controller:${controllerName}`)
208230
: owner._lookupFactory(`controller:${controllerName}`);

0 commit comments

Comments
 (0)