Skip to content

Commit

Permalink
fix lazy routes debug (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Nov 27, 2023
1 parent d15c303 commit 30957f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
34 changes: 22 additions & 12 deletions ember_debug/route-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ import { later } from 'ember-debug/utils/ember/runloop';

const { hasOwnProperty } = Object.prototype;

export default class extends DebugPort {
export default class RouteDebug extends DebugPort {
_cachedRouteTree = null;
init() {
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);
}

Expand Down Expand Up @@ -67,18 +73,21 @@ export default class extends DebugPort {
}

get routeTree() {
const router = this.router;
const routerLib = router._routerMicrolib || router.router;
let routeNames = routerLib.recognizer.names;
let routeTree = {};
for (let routeName in routeNames) {
if (!hasOwnProperty.call(routeNames, routeName)) {
continue;
if (!this._cachedRouteTree) {
const router = this.router;
const routerLib = router._routerMicrolib || router.router;
let routeNames = routerLib.recognizer.names;
let routeTree = {};
for (let routeName in routeNames) {
if (!hasOwnProperty.call(routeNames, routeName)) {
continue;
}
let route = routeNames[routeName];
buildSubTree.call(this, routeTree, route);
}
let route = routeNames[routeName];
buildSubTree.call(this, routeTree, route);
this._cachedRouteTree = arrayizeChildren({ children: routeTree });
}
return arrayizeChildren({ children: routeTree });
return this._cachedRouteTree;
}

sendTree() {
Expand Down Expand Up @@ -143,6 +152,7 @@ export default class extends DebugPort {
*
* @param {*} routeTree
* @param {*} route
* @this {RouteDebug}
* @return {Void}
*/
function buildSubTree(routeTree, route) {
Expand Down Expand Up @@ -187,7 +197,7 @@ function buildSubTree(routeTree, route) {
// Skip when route is an unresolved promise
if (typeof routeHandler?.then === 'function') {
// ensure we rebuild the route tree when this route is resolved
routeHandler.then(() => this.notifyPropertyChange('routeTree'));
routeHandler.then(() => (this._cachedRouteTree = null));
controllerName = '(unresolved)';
controllerClassName = '(unresolved)';
templateName = '(unresolved)';
Expand Down
7 changes: 7 additions & 0 deletions tests/ember_debug/route-debug-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ module('Ember Debug - Route Tree', function (hooks) {
hooks.beforeEach(async function () {
this.owner.register('route:loading', Route);
this.owner.register('route:error', Route);
this.owner.register(
'route:posts',
class extends Route {
promise = Promise.resolve(Route);
then = this.promise.then.bind(this.promise);
}
);

EmberDebug.generalDebug.reopen({
emberCliConfig: null,
Expand Down

0 comments on commit 30957f2

Please sign in to comment.