-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng2.rx): Added RxJS Observables for transitions and states:
- Transitions started - Transitions successful - Parameter values - State registered - State deregistered
- Loading branch information
1 parent
999c42a
commit 2a2f381
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {Observable, ReplaySubject} from "rxjs/Rx"; | ||
import {Transition} from "../transition/transition"; | ||
import {UIRouter} from "../router"; | ||
import {StateDeclaration} from "../state/interface"; | ||
|
||
export interface StatesChangedEvent { | ||
currentStates: StateDeclaration[]; | ||
registered: StateDeclaration[]; | ||
deregistered: StateDeclaration[]; | ||
} | ||
|
||
declare module '../globals' { | ||
interface UIRouterGlobals { | ||
states$?: Observable<StatesChangedEvent>; | ||
start$?: Observable<Transition>; | ||
success$?: Observable<Transition>; | ||
params$?: Observable<{ [paramName: string]: any }>; | ||
} | ||
} | ||
|
||
/** Augments UIRouterGlobals with observables for transition starts, successful transitions, and state parameters */ | ||
export class UIRouterRx { | ||
private deregisterFns: Function[] = []; | ||
|
||
constructor(router: UIRouter) { | ||
let start$ = new ReplaySubject<Transition>(1); | ||
let success$ = <Observable<Transition>> start$.mergeMap((t: Transition) => t.promise.then(() => t)); | ||
let params$ = success$.map((transition: Transition) => transition.params()); | ||
|
||
let states$ = new ReplaySubject<StatesChangedEvent>(1); | ||
function onStatesChangedEvent(event: string, states: StateDeclaration[]) { | ||
let changeEvent = { | ||
currentStates: router.stateRegistry.get(), | ||
registered: [], | ||
deregistered: [] | ||
}; | ||
|
||
if (event) changeEvent[event] = states; | ||
states$.next(changeEvent); | ||
} | ||
|
||
this.deregisterFns.push(router.transitionService.onStart({}, transition => start$.next(transition))); | ||
this.deregisterFns.push(router.stateRegistry.onStatesChanged(onStatesChangedEvent)); | ||
onStatesChangedEvent(null, null); | ||
Object.assign(router.globals, {start$, success$, params$, states$}); | ||
} | ||
|
||
dispose() { | ||
this.deregisterFns.forEach(deregisterFn => deregisterFn()); | ||
this.deregisterFns = []; | ||
} | ||
} |