ngOnInit() { // Exercise the flow where a state change results in a new action. this.search$.subscribe(keyword => { if (keyword != '') { this.actions.fetchResultDispatch(keyword.length) } }); // Exercise the flow where you set a member on change manually instead of // using async pipe. this.numChars$.subscribe(state => { this.numChars = state; }); }
constructor(public af: AngularFire, private zone: NgZone, private router : Router) { this.userData = this.af.auth.flatMap( authState => { // Overcome angularfire's zone smashing return zone.run((): Observable<any> => { if(authState) { this.updatableUser = af.database.object('/users/'+authState.uid); return this.updatableUser; } else { this.updatableUser = null; return Observable.of(null); } }); }).cache(1); // Detect missing user data and forward to quick-profile this.userData.subscribe( authState => { if(authState != null && !authState.name) { this.router.navigate(['/profile-short']); } }); // isAdmin should be an observable that sends trues of falses as the users gains or loses admin access // Need to combine two streams. take the stream of auth data, and use it to generate a stream of values // for the /admin/[userid] and then check to see if the user is an admin this.isAdmin = this.af.auth.switchMap( authState => { // Overcome angularfire's zone smashing return zone.run((): Observable<boolean> => { if(!authState) { return Observable.of(false); } else { return this.af.database.object('/admin/'+authState.uid) .catch((a, b) => { // This permission error means we aren't an admin return Observable.of(false) }); } }); }).map( adminObject => (adminObject && adminObject['$value'] === true) ).cache(1); this.isUser = this.af.auth.map( authState => !!authState).cache(1); this.uid = this.af.auth.switchMap( authState => { if(!authState) { return Observable.of(null); } else { return Observable.of(authState.uid); } }).cache(1); }
inc(){ this.store.dispatch({type: INCREMENT}); console.log('counter$', this.counter$); // object console.log('counter', this.counter$.destination._value.counter); // value console.log('store: ', this.store._value.counter); // same value this.counter$.subscribe(state => console.log('subscribe: ', state));// proper way }
return new Observable(observer => { this.txnDetail$.subscribe(dtl => { if(dtl.txnDtl.isLoadingSuccess){ observer.next(dtl); observer.complete(); } }) });
public getDbs() { this.dbConn.subscribe((v: DbConnModel) => { this.dbRepository.getDbs(v).subscribe((value: Response) => { this.store.dispatch(this.dbActions.addDbs(value.json())) this.proxyService.notifyDbs(value.json()); }); }); }
constructor(private appStore: AppStoreService, private utils: UtilsService) { this.items$ = this.appStore.items.items$; this.appStore.items.selectedItem$ .subscribe((res) => this.selectedItem = res); this.items$.subscribe(res => { console.log('items', res); }); }
ngOnInit(): void { this.pokemon$ = this.store.select<Pokemon>("pokemon", "selectedItem"); this.error$ = this.store.select<any>("pokemon", "error"); // TODO: fix template to use async this.sub = this.pokemon$.subscribe( (pokemon: Pokemon) => { this.pokemon = pokemon; }); const id = this.route.snapshot.params["id"]; this.store.dispatch(this.pokemonActions.getDetail(id)); }
export function nextFrom<T>(op: Observable<T>): T | undefined { let result: T | undefined = undefined; op.subscribe(res => result = res); tick(); return result; }
ngAfterViewInit() { this.lamps$.subscribe(lamps => { lamps.forEach(lamp => { const lampDirective = this.findLamp(lamp.id); if (lampDirective) { lampDirective.changeColor(lamp.color); } }); }); this.services$.subscribe(services => { services.forEach(service => { const serviceDirective = this.findService(service.id); if (serviceDirective) { serviceDirective.handleStatus(service.status, service.version); } }); }); }
ngOnInit(): void { const weatherURL = "https://publicdata-weather.firebaseio.com/"; let city = new Firebase(weatherURL).child(this.city); this.currently = observableFirebaseObject(city.child('currently')); this.currently.subscribe(res => { this.current = res; }); this.hourly = observableFirebaseArray(city.child('hourly/data').limitToLast(10)); }