Skip to content

Commit

Permalink
feat(ng2.rx): Added RxJS Observables for transitions and states:
Browse files Browse the repository at this point in the history
- Transitions started
- Transitions successful
- Parameter values
- State registered
- State deregistered
  • Loading branch information
christopherthielen committed Aug 31, 2016
1 parent 999c42a commit 2a2f381
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ng2/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {Resolvable} from "../resolve/resolvable";
import {ngModuleResolvablesBuilder} from "./statebuilders/lazyLoadNgModuleResolvable";
import {flattenR} from "../common/common";
import {UIROUTER_STATES_TOKEN} from "./uiRouterNgModule";
import {UIRouterRx} from "./rx";

export const NG1_UIROUTER_TOKEN = new OpaqueToken("$uiRouter");

Expand Down Expand Up @@ -99,6 +100,7 @@ let uiRouterFactory = (injector: Injector) => {
// ----------------- Create router -----------------
// Create a new ng2 UIRouter and configure it for ng2
let router = new UIRouter();
new UIRouterRx(router);
let registry = router.stateRegistry;

// ----------------- Configure for ng2 -------------
Expand Down
52 changes: 52 additions & 0 deletions src/ng2/rx.ts
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 = [];
}
}

0 comments on commit 2a2f381

Please sign in to comment.