Skip to content

Commit

Permalink
fix(router): should run resolvers for the same route concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
DzmitryShylovich committed Mar 26, 2017
1 parent 1616cae commit 78e5f16
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
7 changes: 3 additions & 4 deletions packages/router/src/utils/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {NgModuleFactory, ɵisObservable as isObservable, ɵisPromise as isPromis
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
import {of } from 'rxjs/observable/of';
import {concatAll} from 'rxjs/operator/concatAll';
import {every} from 'rxjs/operator/every';
import * as l from 'rxjs/operator/last';
import {last as _last} from 'rxjs/operator/last';
import {map} from 'rxjs/operator/map';
import {mergeAll} from 'rxjs/operator/mergeAll';
import {PRIMARY_OUTLET} from '../shared';
Expand Down Expand Up @@ -113,8 +112,8 @@ export function waitForMap<A, B>(
});

if (waitFor.length > 0) {
const concatted$ = concatAll.call(of (...waitFor));
const last$ = l.last.call(concatted$);
const merged$ = mergeAll.call(of (...waitFor));
const last$ = _last.call(merged$);
return map.call(last$, () => res);
}

Expand Down
56 changes: 53 additions & 3 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -844,24 +844,53 @@ describe('Integration', () => {
})));

describe('data', () => {
let log: string[];
class ResolveSix implements Resolve<number> {
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): number { return 6; }
}

beforeEach(() => {
log = [];
TestBed.configureTestingModule({
providers: [
{provide: 'resolveTwo', useValue: (a: any, b: any) => 2},
{provide: 'resolveFour', useValue: (a: any, b: any) => 4},
{provide: 'resolveSix', useClass: ResolveSix},
{provide: 'resolveError', useValue: (a: any, b: any) => Promise.reject('error')},
{provide: 'numberOfUrlSegments', useValue: (a: any, b: any) => a.url.length}
{provide: 'numberOfUrlSegments', useValue: (a: any, b: any) => a.url.length},
{
provide: 'resolver1',
useValue: () => {
log.push('resolver1');
return getPromise(15);
}
},
{
provide: 'resolver2',
useValue: () => {
log.push('resolver2');
return getPromise(10);
}
},
{
provide: 'resolver3',
useValue: () => {
log.push('resolver3');
return getPromise(5);
}
},
]
});
});

it('should provide resolved data',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
function getPromise(timeout: number): Promise<void> {
let resolve: () => void;
const promise = new Promise<void>(res => resolve = res);
setTimeout(() => resolve(), timeout);
return promise;
}

it('should provide resolved data', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmpWithTwoOutlets);

router.resetConfig([{
Expand Down Expand Up @@ -966,6 +995,27 @@ describe('Integration', () => {

expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 3});
})));

it('should run resolvers for the same route concurrently',
fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp);

router.resetConfig([{
path: 'a',
resolve: {
one: 'resolver1',
two: 'resolver2',
three: 'resolver3',
},
component: SimpleCmp
}]);

router.navigateByUrl('/a');
advance(fixture);
tick(15);

expect(log).toEqual(['resolver3', 'resolver2', 'resolver1']);
})));
});

describe('router links', () => {
Expand Down

0 comments on commit 78e5f16

Please sign in to comment.