Skip to content

Instantly share code, notes, and snippets.

@adharris
Created July 30, 2016 19:40
Show Gist options
  • Save adharris/f71a599c1da838e5b3e3de59eae47416 to your computer and use it in GitHub Desktop.
Save adharris/f71a599c1da838e5b3e3de59eae47416 to your computer and use it in GitHub Desktop.
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