Created
July 30, 2016 19:40
-
-
Save adharris/f71a599c1da838e5b3e3de59eae47416 to your computer and use it in GitHub Desktop.
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
import {Injectable} from '@angular/core'; | |
import {Transition, TransitionService, HookMatchCriteria, RawParams,TransitionHookFn} from 'ui-router-ng2'; | |
import {Observable, ReplaySubject} from 'rxjs'; | |
@Injectable() | |
export class ParamStream { | |
constructor(private transitionService: TransitionService) {} | |
createStream<T>(critera: HookMatchCriteria): Observable<T> { | |
let unsub: Function; | |
let add = (handler: TransitionHookFn) => { | |
unsub = this.transitionService.onSuccess(critera, handler); | |
} | |
let remove = (h) => { | |
console.log('removing', critera); | |
unsub(); | |
}; | |
let selector = (t: Transition) => t.params() as T; | |
return Observable.fromEventPattern(add, remove, selector); | |
} | |
createSubject(transition: Transition) { | |
let critera = {to: transition.$to().name} | |
let params = transition.params(); | |
let subject = new ReplaySubject<RawParams>(1); | |
subject.next(params) | |
return subject.merge(this.createStream(critera)); | |
} | |
static makeResolve() { | |
return { | |
provide: 'params', | |
useFactory: (t: Transition, stream: ParamStream) => stream.createSubject(t), | |
deps: [Transition, ParamStream], | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment