Created
November 7, 2018 18:15
-
-
Save jerryorta-dev/a1208512ec18f278ca5b589ea93f63b9 to your computer and use it in GitHub Desktop.
Demo Poller
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 { IParamsObject, ISearchParams } from '@sp-model/globalSearch.model'; | |
import { Observable } from 'rxjs/Observable'; | |
import { timer } from 'rxjs/observable/timer'; | |
import { merge } from 'rxjs/observable/merge'; | |
import { distinctUntilKeyChanged } from 'rxjs/operators/distinctUntilKeyChanged'; | |
import { filter } from 'rxjs/operators/filter'; | |
import { map } from 'rxjs/operators/map'; | |
import { mapTo } from 'rxjs/operators/mapTo'; | |
import { switchMap } from 'rxjs/operators/switchMap'; | |
import { takeUntil } from 'rxjs/operators/takeUntil'; | |
export function somePoller( obs: Observable<any>, | |
stopPollObservables: Observable<any>[] ): Observable<any> { | |
return obs.pipe( | |
// allow poll only if searchTerm as value | |
filter(( routeParams: IParamsObject ) => | |
routeParams.params.searchTerm && | |
!!routeParams.params.searchTerm.length), | |
// take only when params.searchTerm change value | |
distinctUntilKeyChanged('params', ( x: ISearchParams, y: ISearchParams ): boolean => { | |
return x.searchTerm === y.searchTerm; | |
}), | |
switchMap(( routeParams: IParamsObject ): Observable<any> => { | |
// Poll starting immediately, and then every 5 min interval | |
return timer(0, 300000) | |
.pipe( | |
// pass routeParams in stream instead of interval number | |
map(() => routeParams), | |
// stop interval when navigating away from deviceManager page | |
takeUntil( | |
merge(...stopPollObservables) | |
.pipe(mapTo(() => 'STOP POLL')), // fyi only | |
), | |
); | |
}), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment