Skip to content

Commit

Permalink
feat(release) v0.10.6
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemon committed Sep 16, 2022
1 parent 618523f commit 43c8f19
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/react/src/ControllerRoutes.ts
Expand Up @@ -3,10 +3,10 @@ import type { RoutedResult, Router } from "@valkyr/router";
import type { Controller, JsonLike } from "./Controller";

export class ControllerRoutes<S extends JsonLike = {}, R extends Router = Router> {
#resolved?: (resolved: Router["resolved"], result: RoutedResult<typeof this.router>) => void;

constructor(readonly controller: Controller<S>, readonly router: R, readonly routes: Route[]) {}

#resolved?: (resolved: Router["resolved"], result: RoutedResult<typeof this.router>) => void;

resolved(handleResolved: (resolved: Router["resolved"], result: RoutedResult<typeof this.router>) => void): this {
this.#resolved = handleResolved;
return this;
Expand All @@ -33,7 +33,6 @@ export class ControllerRoutes<S extends JsonLike = {}, R extends Router = Router
async #preload() {
for (const { path } of this.routes) {
const isCurrentPath = this.router.match(path);
console.log(path, isCurrentPath);
if (isCurrentPath === true) {
const resolved = this.router.getResolvedRoute(this.router.location.pathname);
if (resolved !== undefined) {
Expand All @@ -49,13 +48,24 @@ export class ControllerRoutes<S extends JsonLike = {}, R extends Router = Router
}

async #setComponent(resolved: Router["resolved"]) {
const result = await this.router.getComponent(resolved);
if (result.component !== this.controller.state.routed?.component) {
this.controller.setState("routed", result as S[keyof S]);
const prev = this.controller.state.routed;
const next = await this.router.getComponent(resolved);
if (prev === undefined || hasChanged(prev, next) === true) {
this.controller.setState("routed", next as S[keyof S]);
}
}
}

function hasChanged(prev: RoutedResult<any>, next: RoutedResult<any>): boolean {
if (prev.component !== next.component) {
return true;
}
if (JSON.stringify(prev.props) !== JSON.stringify(next.props)) {
return true;
}
return false;
}

type Route = {
path: string;
};

0 comments on commit 43c8f19

Please sign in to comment.