Skip to content

Commit

Permalink
fix(ivy): Inject host UISref separately to account for behavior chang…
Browse files Browse the repository at this point in the history
…e in @ContentChildren

ContentChildren no longer returns the host element.
UISrefStatus needs all children UISref _AND_ the host UISref (if it exists).
see: angular/angular#8277 (comment)
  • Loading branch information
christopherthielen committed Oct 6, 2019
1 parent e0981cf commit ebd2e40
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/directives/uiSrefStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @ng2api @module directives */
/** */
import { Directive, Output, EventEmitter, ContentChildren, QueryList } from '@angular/core';
import { Directive, Output, EventEmitter, ContentChildren, QueryList, Host, Self, Optional } from '@angular/core';
import { UISref } from './uiSref';
import {
PathNode,
Expand All @@ -14,10 +14,12 @@ import {
UIRouterGlobals,
Param,
PathUtils,
identity,
uniqR,
} from '@uirouter/core';

import { Subscription, Observable, BehaviorSubject, of, from, combineLatest, concat } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import { switchMap, map, tap } from 'rxjs/operators';

/** @internalapi */
interface TransEvt {
Expand Down Expand Up @@ -206,8 +208,10 @@ export class UISrefStatus {
/** @internalapi */ private _srefChangesSub: Subscription;
/** @internalapi */ private _srefs$: BehaviorSubject<UISref[]>;
/** @internalapi */ private _globals: UIRouterGlobals;
constructor(_globals: UIRouterGlobals) {
/** @internalapi */ private _hostUiSref: UISref;
constructor(@Host() @Self() @Optional() _hostUiSref: UISref, _globals: UIRouterGlobals) {
this._globals = _globals;
this._hostUiSref = _hostUiSref;
this.status = Object.assign({}, inactiveStatus);
}

Expand All @@ -226,11 +230,15 @@ export class UISrefStatus {
})
);

// Watch the @ContentChildren UISref[] components and get their target states
const withHostSref = (childrenSrefs: UISref[]) =>
childrenSrefs
.concat(this._hostUiSref)
.filter(identity)
.reduce(uniqR, []);

// let srefs$: Observable<UISref[]> = of(this.srefs.toArray()).concat(this.srefs.changes);
this._srefs$ = new BehaviorSubject(this._srefs.toArray());
this._srefChangesSub = this._srefs.changes.subscribe(srefs => this._srefs$.next(srefs));
// Watch the @ContentChildren UISref[] components and get their target states
this._srefs$ = new BehaviorSubject(withHostSref(this._srefs.toArray()));
this._srefChangesSub = this._srefs.changes.subscribe(srefs => this._srefs$.next(withHostSref(srefs)));

const targetStates$: Observable<TargetState[]> = this._srefs$.pipe(
switchMap((srefs: UISref[]) => combineLatest<TargetState[]>(srefs.map(sref => sref.targetState$)))
Expand Down

0 comments on commit ebd2e40

Please sign in to comment.