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

Fix route match no part activated but no deactivation #37

Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aster-js/app",
"version": "2.1.3",
"version": "2.1.4",
"description": "Aster core library part of Aster js library",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
9 changes: 8 additions & 1 deletion src/abstraction/iapplication-part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { IAppConfigureHandler, AppConfigureDelegate } from "./iapp-configure-han
import { AppServiceId } from "./app-service-id";
import { Route } from "../routing/route";

export const IApplicationPart = AppServiceId<IApplicationPart>( "IApplicationPart");
export const IApplicationPart = AppServiceId<IApplicationPart>("IApplicationPart");

/** Represents a part of an application based on a IoC Container and using it a registry for all its services. */
export interface IApplicationPart extends IIoCModule {

/**
* Gets a version number that give version information about activation of children.
* Each time a child is attempted to be activated, the version number increment.
* This version number gives information to the router to if the child still relevant.
*/
readonly childVersion: number;

/**
* Active child application part.
* The active child is the part that is currently activated and running.
Expand Down
10 changes: 7 additions & 3 deletions src/application-part/application-part.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbortToken, Delayed } from "@aster-js/async";
import { Constructor, DisposableHost, IDisposable } from "@aster-js/core";
import { IIoCContainerBuilder, IIoCModule, ILogger, ServiceProvider } from "@aster-js/ioc";
import { IIoCContainerBuilder, IIoCModule, ILogger, ServiceCollection, ServiceProvider } from "@aster-js/ioc";
import { AppConfigureDelegate, configure, IAppConfigureHandler, IApplicationPart, IApplicationPartBuilder } from "../abstraction";
import { Memoize } from "@aster-js/decorators";
import { ApplicationPartLifecycleHooks } from "./iapplication-part-lifecycle";
Expand All @@ -11,7 +11,8 @@ import { createApplicationPartModule } from "./default-application-part-services
export abstract class ApplicationPart extends DisposableHost implements IApplicationPart {
private readonly _module: IIoCModule;
private readonly _children: Map<string, IApplicationPart> = new Map();
private _current: [Route, IApplicationPart] | [] = [];
private _childVersion: number = 0;
private _current: [Route, IApplicationPart, number] | [] = [];

get name(): string { return this._module.name; }

Expand All @@ -21,6 +22,8 @@ export abstract class ApplicationPart extends DisposableHost implements IApplica

get running(): boolean { return this._module.running; }

get childVersion(): number { return this._current[2] ?? -1; }

get activeRoute(): Route | undefined { return this._current[0]; }

get activeChild(): IApplicationPart | undefined { return this._current[1]; }
Expand Down Expand Up @@ -108,6 +111,7 @@ export abstract class ApplicationPart extends DisposableHost implements IApplica
if (!name) throw new Error(`"name" parameter cannot be null or empty`);

if (this._current[0] === route && this._current[1]?.name === name) {
this._current[2] = ++this._childVersion;
this.logger.debug(`Part "{name}" is already activated`, name);
return;
}
Expand All @@ -117,7 +121,7 @@ export abstract class ApplicationPart extends DisposableHost implements IApplica
await this.desactivateCurrent();

await this.activatePart(part);
this._current = [route, part];
this._current = [route, part, ++this._childVersion];
}
else {
this.logger.error(null, `Cannot find any part named "{name}"`, name);
Expand Down
6 changes: 6 additions & 0 deletions src/routing/default-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ export class DefaultRouter implements IRouter {
const currentChild = this._application.activeChild;
if (currentChild) {
const router = currentChild.services.get(IRouter, true);
const childVersion = currentChild.childVersion;
if (router instanceof DefaultRouter && await router.handle(root, cursor, ctx)) {
this._logger.debug("Child router handled the remaining route {path}", cursor.toString());

// If no activation attempt, that mean current child is not relevant anymore
if(currentChild.childVersion === childVersion) {
this.deactivateChildren(currentChild);
}
}
else {
if (cursor.remaining !== 0) {
Expand Down
36 changes: 36 additions & 0 deletions tests/part-activation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,40 @@ describe("Part Activations", () => {
assert.equal(childService.deactivateCount, 1);
});

it("Should disable second level 2", async () => {

using app = await SinglePageApplication.start("LoadTest", x => {
x.addPart("/:part<moon>/*", x => {
x.addPart("~/:part<new|waxing|quarter|waning|full>", x => x.configure(x => x.addSingleton(CustomService)));
x.addAction("~/", _ => { });
});
x.addPart("/:part<sun>?sun", x => { });
});

// Initial state
const firstApp = app.activeChild!;
assert.isDefined(firstApp);
assert.equal(firstApp.name, "sun");

// Navigate to full moon
await app.navigate("/moon/full");

const lastApp = app.activeChild?.activeChild!;
assert.isDefined(lastApp);
assert.equal(lastApp.name, "full");

const childService = lastApp.services.get(ICustomService)!;
assert.isDefined(childService);
assert.equal(childService.setupCount, 1);
assert.equal(childService.activateCount, 1);
assert.equal(childService.deactivateCount, 0);

// Navigate to full moon but with no part activation
await app.navigate("/moon");

assert.equal(childService.setupCount, 1);
assert.equal(childService.activateCount, 1);
assert.equal(childService.deactivateCount, 1);
});

});
Loading