Skip to content

Commit

Permalink
fix(router-lite): current route subscription disposal (#1969)
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
Sayan751 committed May 17, 2024
1 parent 63ffdc9 commit ace4c65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
6 changes: 1 addition & 5 deletions packages/router-lite/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { ViewportCustomElement } from './resources/viewport';
import { LoadCustomAttribute } from './resources/load';
import { HrefCustomAttribute } from './resources/href';
import { IBaseHref, normalizePath } from './location-manager';
import { _disposeCurrentRouteSubscription } from './current-route';

export const RouterRegistration = IRouter as unknown as IRegistry;

Expand Down Expand Up @@ -71,10 +70,7 @@ function configure(container: IContainer, options?: IRouterConfigurationOptions)
AppTask.creating(IRouter, _ => { /* ensure a router instance before the app root is instantiated and there by ensuring all the necessary aliases. */ }),
AppTask.hydrated(IContainer, RouteContext.setRoot),
AppTask.activated(IRouter, router => router.start(true)),
AppTask.deactivated(IRouter, router => {
_disposeCurrentRouteSubscription();
router.stop();
}),
AppTask.deactivated(IRouter, router => router.stop()),
...DefaultComponents,
...DefaultResources,
);
Expand Down
14 changes: 6 additions & 8 deletions packages/router-lite/src/current-route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DI, IDisposable, Writable, emptyArray, resolve } from '@aurelia/kernel';
import { DI, Writable, emptyArray, resolve } from '@aurelia/kernel';
import { RESIDUE } from '@aurelia/route-recognizer';
import { batch } from '@aurelia/runtime';
import { IRouter } from './router';
Expand All @@ -7,12 +7,6 @@ import { IRouterEvents } from './router-events';
import type { Params, ViewportInstruction } from './instructions';
import type { RouteConfig } from './route';

let _currentRouteSubscription: IDisposable | null = null;
/** @internal */
export function _disposeCurrentRouteSubscription(): void {
_currentRouteSubscription?.dispose();
}

export const ICurrentRoute = /*@__PURE__*/ DI.createInterface<ICurrentRoute>('ICurrentRoute', x => x.singleton(CurrentRoute));
export interface ICurrentRoute extends CurrentRoute { }

Expand All @@ -26,7 +20,11 @@ export class CurrentRoute {
public constructor() {
const router = resolve(IRouter);
const options = router.options;
_currentRouteSubscription = resolve(IRouterEvents)
// In a realistic app, the lifetime of the CurrentRoute instance is the same as the app itself.
// Therefor the disposal of the subscription is avoided here.
// An alternative would be to introduce a new configuration option such that the router initializes this class in its constructor and also registers the class to container.
// Then the app task hooks of the router can be used directly to start/dispose the subscription.
resolve(IRouterEvents)
.subscribe('au:router:navigation-end', (event) => {
const vit = event.finalInstructions;
batch(() => {
Expand Down

0 comments on commit ace4c65

Please sign in to comment.