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

Add recognize and recognizeAndLoad #261

Merged
merged 2 commits into from
Oct 3, 2018
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
41 changes: 27 additions & 14 deletions lib/router/route-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,20 @@ export interface RouteInfo {
): RouteInfo | undefined;
}

export interface RouteInfoWithAttributes extends RouteInfo {
attributes: Dict<unknown>;
}

let ROUTE_INFOS = new WeakMap<InternalRouteInfo<Route>, RouteInfo>();

export function toReadOnlyRouteInfo(
routeInfos: InternalRouteInfo<Route>[],
queryParams: Dict<unknown> = {}
queryParams: Dict<unknown> = {},
includeAttributes = false
) {
return routeInfos.map((info, i) => {
let { name, params, paramNames } = info;
let publicRouteInfo = new class implements RouteInfo {
let { name, params, paramNames, context } = info;
let routeInfo: RouteInfo = {
find(
predicate: (this: any, routeInfo: RouteInfo, i?: number, arr?: RouteInfo[]) => boolean,
thisArg: any
Expand All @@ -77,15 +82,15 @@ export function toReadOnlyRouteInfo(
}

return undefined;
}
},

get name() {
return name;
}
},

get paramNames() {
return paramNames;
}
},

get parent() {
let parent = routeInfos[i - 1];
Expand All @@ -95,7 +100,7 @@ export function toReadOnlyRouteInfo(
}

return ROUTE_INFOS.get(parent)!;
}
},

get child() {
let child = routeInfos[i + 1];
Expand All @@ -105,25 +110,33 @@ export function toReadOnlyRouteInfo(
}

return ROUTE_INFOS.get(child)!;
}
},

get localName() {
let parts = this.name.split('.');
return parts[parts.length - 1];
}
},

get params() {
return params;
}
},

get queryParams() {
return queryParams;
}
}();
},
};

if (includeAttributes) {
routeInfo = Object.assign(routeInfo, {
get attributes() {
return context;
},
});
}

ROUTE_INFOS.set(info, publicRouteInfo);
ROUTE_INFOS.set(info, routeInfo);

return publicRouteInfo;
return routeInfo;
});
}

Expand Down
48 changes: 46 additions & 2 deletions lib/router/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import RouteRecognizer, { MatchCallback, Params } from 'route-recognizer';
import { Promise } from 'rsvp';
import { Dict, Maybe } from './core';
import InternalRouteInfo, { Route, toReadOnlyRouteInfo } from './route-info';
import { Dict, Maybe, Option } from './core';
import InternalRouteInfo, {
Route,
RouteInfo,
RouteInfoWithAttributes,
toReadOnlyRouteInfo,
} from './route-info';
import InternalTransition, {
logAbort,
OpaqueTransition,
Expand Down Expand Up @@ -150,6 +155,45 @@ export default abstract class Router<T extends Route> {
}
}

recognize(url: string): Option<RouteInfo> {
let intent = new URLTransitionIntent<T>(this, url);
let newState = this.generateNewState(intent);

if (newState === null) {
return newState;
}

let readonlyInfos = toReadOnlyRouteInfo(newState.routeInfos, newState.queryParams);
return readonlyInfos[readonlyInfos.length - 1];
}

recognizeAndLoad(url: string): Promise<RouteInfoWithAttributes> {
let intent = new URLTransitionIntent<T>(this, url);
let newState = this.generateNewState(intent);

if (newState === null) {
return Promise.reject(`URL ${url} was not recognized`);
}

let newTransition: OpaqueTransition = new InternalTransition(this, intent, newState, undefined);
return newTransition.then(() => {
let routeInfosWithAttributes = toReadOnlyRouteInfo(
newState!.routeInfos,
newTransition.queryParams,
true
) as RouteInfoWithAttributes[];
return routeInfosWithAttributes[routeInfosWithAttributes.length - 1];
});
}

private generateNewState(intent: TransitionIntent<T>): Option<TransitionState<T>> {
try {
return intent.applyToState(this.state!, false);
} catch (e) {
return null;
}
}

private getTransitionByIntent(
intent: TransitionIntent<T>,
isIntermediate: boolean
Expand Down
Loading