Skip to content

Commit

Permalink
Merge pull request #17007 from emberjs/router-service
Browse files Browse the repository at this point in the history
Router.js Generics & Route Infos Prep Work
  • Loading branch information
chadhietala committed Oct 1, 2018
2 parents fe9b55e + 55bd846 commit 7165960
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 191 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"puppeteer": "^1.3.0",
"qunit": "^2.5.0",
"route-recognizer": "^0.3.4",
"router_js": "^4.0.3",
"router_js": "^5.0.3",
"rsvp": "^4.8.2",
"semver": "^5.5.0",
"serve-static": "^1.12.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ moduleFor(
get routerOptions() {
return {
location: 'none',

// This creates a handler function similar to what is in use by ember-engines
// internally. Specifically, it returns a promise when transitioning _into_
// the first engine route, but returns the synchronously available handler
// _after_ the engine has been resolved.
setupRouter() {
this._super(...arguments);
let syncHandler = this._routerMicrolib.getHandler;

let getRoute = this._routerMicrolib.getRoute;
this._enginePromises = Object.create(null);
this._resolvedEngines = Object.create(null);

this._routerMicrolib.getHandler = name => {
this._routerMicrolib.getRoute = name => {
let engineInfo = this._engineInfoByRoute[name];
if (!engineInfo) {
return syncHandler(name);
return getRoute(name);
}

let engineName = engineInfo.name;
if (this._resolvedEngines[engineName]) {
return syncHandler(name);
return getRoute(name);
}

let enginePromise = this._enginePromises[engineName];
Expand All @@ -50,7 +44,7 @@ moduleFor(
this._enginePromises[engineName] = enginePromise;
}

return enginePromise.then(() => syncHandler(name));
return enginePromise.then(() => getRoute(name));
};
},
};
Expand Down
10 changes: 5 additions & 5 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class RouterService extends Service {
let { routeName, models, queryParams } = extractRouteArgs(args);

let transition = this._router._doTransition(routeName, models, queryParams, true);
transition._keepDefaultQueryParamValues = true;
transition['_keepDefaultQueryParamValues'] = true;

return transition;
}
Expand Down Expand Up @@ -108,8 +108,8 @@ export default class RouterService extends Service {
@return {String} the string representing the generated URL
@public
*/
urlFor(/* routeName, ...models, options */) {
return this._router.generate(...arguments);
urlFor(routeName: string, ...args: any[]) {
return this._router.generate(routeName, ...args);
}

/**
Expand All @@ -129,7 +129,7 @@ export default class RouterService extends Service {
let { routeName, models, queryParams } = extractRouteArgs(args);
let routerMicrolib = this._router._routerMicrolib;

if (!routerMicrolib.isActiveIntent(routeName, models, null)) {
if (!routerMicrolib.isActiveIntent(routeName, models)) {
return false;
}
let hasQueryParams = Object.keys(queryParams).length > 0;
Expand All @@ -141,7 +141,7 @@ export default class RouterService extends Service {
queryParams,
true /* fromRouterService */
);
return shallowEqual(queryParams, routerMicrolib.state.queryParams);
return shallowEqual(queryParams, routerMicrolib.state!.queryParams);
}

return true;
Expand Down
66 changes: 34 additions & 32 deletions packages/@ember/-internals/routing/lib/system/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { once } from '@ember/runloop';
import { classify } from '@ember/string';
import { DEBUG } from '@glimmer/env';
import { TemplateFactory } from '@glimmer/opcode-compiler';
import { Transition, TransitionState } from 'router_js';
import HandlerInfo from 'router_js/dist/cjs/handler-info';
import { InternalRouteInfo, Route as IRoute, Transition, TransitionState } from 'router_js';
import {
calculateCacheKey,
normalizeControllerQueryParams,
Expand Down Expand Up @@ -65,7 +64,9 @@ export function hasDefaultSerialize(route: Route) {
@public
*/

class Route extends EmberObject {
class Route extends EmberObject implements IRoute {
routeName!: string;
context: {} = {};
serialize!: (model: {}, params: string[]) => object | undefined;

_router!: EmberRouter;
Expand Down Expand Up @@ -115,22 +116,22 @@ class Route extends EmberObject {
@method _stashNames
*/
_stashNames(handlerInfo: HandlerInfo, dynamicParent: HandlerInfo) {
_stashNames(routeInfo: InternalRouteInfo<this>, dynamicParent: InternalRouteInfo<this>) {
if (this._names) {
return;
}
let names = (this._names = handlerInfo['_names']);
let names = (this._names = routeInfo['_names']);

if (!names.length) {
handlerInfo = dynamicParent;
names = (handlerInfo && handlerInfo['_names']) || [];
routeInfo = dynamicParent;
names = (routeInfo && routeInfo['_names']) || [];
}

let qps = get(this, '_qp.qps');

let namePaths = new Array(names.length);
for (let a = 0; a < names.length; ++a) {
namePaths[a] = `${handlerInfo.name}.${names[a]}`;
namePaths[a] = `${routeInfo.name}.${names[a]}`;
}

for (let i = 0; i < qps.length; ++i) {
Expand Down Expand Up @@ -220,8 +221,8 @@ class Route extends EmberObject {
let state = transition ? transition.state : this._router._routerMicrolib.state;

let fullName = route.fullRouteName;
let params = assign({}, state.params[fullName]);
let queryParams = getQueryParamsFor(route, state);
let params = assign({}, state!.params[fullName]);
let queryParams = getQueryParamsFor(route, state!);

return Object.keys(queryParams).reduce((params, key) => {
assert(
Expand Down Expand Up @@ -331,7 +332,7 @@ class Route extends EmberObject {
@method _reset
@since 1.7.0
*/
_reset(isExiting: boolean, transition: Transition) {
_internalReset(isExiting: boolean, transition: Transition) {
let controller = this.controller;
controller._qpDelegate = get(this, '_qp.states.inactive');

Expand Down Expand Up @@ -786,7 +787,8 @@ class Route extends EmberObject {
@public
*/
intermediateTransitionTo(...args: any[]) {
this._router.intermediateTransitionTo(...prefixRouteNameArg(this, args));
let [name, ...preparedArgs] = prefixRouteNameArg(this, args);
this._router.intermediateTransitionTo(name, ...preparedArgs);
}

/**
Expand Down Expand Up @@ -896,7 +898,7 @@ class Route extends EmberObject {

if (transition) {
// Update the model dep values used to calculate cache keys.
stashParamNames(this._router, transition.state!.handlerInfos);
stashParamNames(this._router, transition.state!.routeInfos);

let cache = this._bucketCache;
let params = transition.params;
Expand Down Expand Up @@ -1149,7 +1151,7 @@ class Route extends EmberObject {
if (transition.resolveIndex < 1) {
return;
}
return transition.state!.handlerInfos[transition.resolveIndex - 1].context;
return transition.state!.routeInfos[transition.resolveIndex - 1].context;
}
}

Expand Down Expand Up @@ -1391,8 +1393,8 @@ class Route extends EmberObject {
// resolved parent contexts on the current transitionEvent.
if (transition !== undefined && transition !== null) {
let modelLookupName = (route && route.routeName) || name;
if (transition.resolvedModels.hasOwnProperty(modelLookupName)) {
return transition.resolvedModels[modelLookupName];
if (transition.resolvedModels.hasOwnProperty(modelLookupName!)) {
return transition.resolvedModels[modelLookupName!];
}
}

Expand Down Expand Up @@ -1659,13 +1661,13 @@ class Route extends EmberObject {

outletName = outletName || 'main';
this._disconnectOutlet(outletName, parentView);
let handlerInfos = this._router._routerMicrolib.currentHandlerInfos;
for (let i = 0; i < handlerInfos.length; i++) {
let routeInfos = this._router._routerMicrolib.currentRouteInfos!;
for (let i = 0; i < routeInfos.length; i++) {
// This non-local state munging is sadly necessary to maintain
// backward compatibility with our existing semantics, which allow
// any route to disconnectOutlet things originally rendered by any
// other route. This should all get cut in 2.0.
handlerInfos[i].handler._disconnectOutlet(outletName, parentView);
routeInfos[i].route!._disconnectOutlet(outletName, parentView);
}
}

Expand Down Expand Up @@ -1718,20 +1720,20 @@ Route.reopenClass({
});

function parentRoute(route: Route) {
let handlerInfo = handlerInfoFor(route, route._router._routerMicrolib.state.handlerInfos, -1);
return handlerInfo && handlerInfo.handler;
let routeInfo = routeInfoFor(route, route._router._routerMicrolib.state!.routeInfos, -1);
return routeInfo && routeInfo.route;
}

function handlerInfoFor(route: Route, handlerInfos: HandlerInfo[], offset = 0) {
if (!handlerInfos) {
function routeInfoFor(route: Route, routeInfos: InternalRouteInfo<Route>[], offset = 0) {
if (!routeInfos) {
return;
}

let current: any;
for (let i = 0; i < handlerInfos.length; i++) {
current = handlerInfos[i].handler;
for (let i = 0; i < routeInfos.length; i++) {
current = routeInfos[i].route;
if (current === route) {
return handlerInfos[i + offset];
return routeInfos[i + offset];
}
}

Expand Down Expand Up @@ -1836,19 +1838,19 @@ interface PartialRenderOptions {
model?: {};
}

function getFullQueryParams(router: EmberRouter, state: TransitionState) {
function getFullQueryParams(router: EmberRouter, state: TransitionState<Route>) {
if (state['fullQueryParams']) {
return state['fullQueryParams'];
}

state['fullQueryParams'] = {};
assign(state['fullQueryParams'], state.queryParams);

router._deserializeQueryParams(state.handlerInfos, state['fullQueryParams'] as QueryParam);
router._deserializeQueryParams(state.routeInfos, state['fullQueryParams'] as QueryParam);
return state['fullQueryParams'];
}

function getQueryParamsFor(route: Route, state: TransitionState) {
function getQueryParamsFor(route: Route, state: TransitionState<Route>) {
state['queryParamsFor'] = state['queryParamsFor'] || {};
let name = route.fullRouteName;

Expand Down Expand Up @@ -2426,13 +2428,13 @@ Route.reopen(ActionHandler, Evented, {
return;
}

let handlerInfos = transition.state!.handlerInfos;
let routeInfos = transition.state!.routeInfos;
let router = this._router;
let qpMeta = router._queryParamsFor(handlerInfos);
let qpMeta = router._queryParamsFor(routeInfos);
let changes = router._qpUpdates;
let replaceUrl;

stashParamNames(router, handlerInfos);
stashParamNames(router, routeInfos);

for (let i = 0; i < qpMeta.qps.length; ++i) {
let qp = qpMeta.qps[i];
Expand Down

0 comments on commit 7165960

Please sign in to comment.