Skip to content

Commit

Permalink
Merge pull request #32 from RazzibShakya/add_mapUrl_function_defaulRo…
Browse files Browse the repository at this point in the history
…uter

add function setMapUrl to Router
  • Loading branch information
syaau authored Jan 27, 2022
2 parents 0611f06 + 6336471 commit 73d1a42
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Transition = {
confirm: () => void;
cancel: () => void;
route: Route;
}
};
export type ConfirmTransition = (transition: Transition) => void;
type UrlMapper = (url: string, cb: (url: string) => void) => Route | null;
/**
Expand All @@ -19,7 +19,7 @@ export class Router {
readonly get: EffectHandler<Route>['get'];
private readonly routeStack: Array<Route> = [];
private readonly stackSize: number;
private readonly mapUrl?: UrlMapper;
private mapUrl?: UrlMapper;
private parentRouter: Router | null;
private recentUrl: string | null = null;
private childUrl: string = '';
Expand All @@ -40,15 +40,20 @@ export class Router {

this.stackSize = stackSize;
this.mapUrl = mapUrl;

}

setMapUrl = (mapUrl: UrlMapper, initialUrl?: string) => {
if (this.mapUrl) throw new Error('Url mapper has already been declared');
this.mapUrl = mapUrl;
if (initialUrl) this.setUrl(initialUrl);
};

private getRecentUrl(): string | null {
if (this.recentUrl) return this.recentUrl;
if (this.parentRouter) return this.parentRouter.getRecentUrl();
return null;
}


registerChild = (childRouter: Router) => {
this.childListeners.push(childRouter);
childRouter.pushUrl(this.childUrl);
Expand All @@ -57,36 +62,38 @@ export class Router {
if (index >= 0) {
this.childListeners.splice(index, 1);
}
}
}
};
};

setChildUrl = (childUrl: string) => {
this.childListeners.forEach(c => {
c.pushUrl(childUrl);
})
this.childUrl=childUrl;
}
});
this.childUrl = childUrl;
};

getInitialRoute = (parentRouter: null | Router) => {
let k = this.get();
if (k) return k;
const url = parentRouter? parentRouter.childUrl :this.getRecentUrl();
const url = parentRouter ? parentRouter.childUrl : this.getRecentUrl();
if (url) {
k = this.mapUrl(url, this.setChildUrl);
this.effect.fire(k);
return k;
}
return null;
}
};

private update = (route: Route) => {
if (!route === null) {
console.warn('Trying to change to an invalid route. This doesn\'t have any effect, but might be a bug on your application');
console.warn(
"Trying to change to an invalid route. This doesn't have any effect, but might be a bug on your application",
);
}
this.effect.fire(route);
// Reset the recent url
this.recentUrl = null;
}
};
/**
* Change the route to the previous one based on the stack.
* @returns boolean The return value only indicates if the pop operation
Expand All @@ -100,7 +107,7 @@ export class Router {
const previousRoute = this.routeStack.pop();
await this.updateRoute(previousRoute, this.update);
return previousRoute;
}
};
/**
* Change the current route on this router, clearing the stack in process
* @param route
Expand All @@ -115,7 +122,7 @@ export class Router {
}
// Perform the transition
await this.updateRoute(route, this.update);
}
};
/**
* Add route to the router stack. The push will not work if a
* transition is already in progress, or one of the transition
Expand All @@ -135,7 +142,7 @@ export class Router {
// update the current route
this.update(route);
});
}
};
private async updateRoute(route: Route, op: (route: Route) => void): Promise<void> {
// Can't start a transition if a one is already in progress, can't do anything
if (this.currentTransition) throw new Error('Another route transition is in progress');
Expand Down Expand Up @@ -173,11 +180,13 @@ export class Router {
}
}
}
this.ConfirmTransitions.forEach(handler => handler({
route,
cancel: (err?: Error) => complete(null, err),
confirm: () => complete(handler),
}));
this.ConfirmTransitions.forEach(handler =>
handler({
route,
cancel: (err?: Error) => complete(null, err),
confirm: () => complete(handler),
}),
);
});
}
/**
Expand All @@ -187,18 +196,18 @@ export class Router {
*/
setUrl = async (url: string | null): Promise<Route> => {
return this.updateUrl(url, this.set);
}
};
/**
* Change the route based on the given url. Uses `push(route)` after mapping the url.
* @param url
* @returns boolean true if the mapping was successful otherwise false
*/
pushUrl = async (url: string | null): Promise<Route> => {
if ((url || '').startsWith('/') && this.parentRouter) {
this.parentRouter.pushUrl(url)
this.parentRouter.pushUrl(url);
}
return this.updateUrl(url, this.push);
}
};
private async updateUrl(url: string, op: (route: Route) => void): Promise<Route> {
let newRoute: Route;
if (this.mapUrl) {
Expand All @@ -219,7 +228,7 @@ export class Router {
this.ConfirmTransitions = this.ConfirmTransitions.filter(k => k !== effect);
// if the effect is part of the current transition, consider it as a
// confirmation
}
};
}
}
/**
Expand All @@ -228,4 +237,4 @@ export class Router {
* For all other use cases create your own router
*/
export const defaultRouter = new Router();
export const RouterContext = createContext<Router>(defaultRouter);
export const RouterContext = createContext<Router>(defaultRouter);

0 comments on commit 73d1a42

Please sign in to comment.