Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Last active December 11, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sawtaytoes/2c3ec1a5f1267c356468b5b3f2e9e40b to your computer and use it in GitHub Desktop.
Save Sawtaytoes/2c3ec1a5f1267c356468b5b3f2e9e40b to your computer and use it in GitHub Desktop.
An RxJS solution for a method utilizing an initial cache value and AJAX response on an interval to pass updated data to a subscriber.
fromCache('itemList')
.pipe(
switchMap(itemList => (
interval(3600000) // 1 hour
.pipe(
startWith(0),
switchMap(() => (
ajax({
url: 'https://api.example.com/data',
})
.pipe(
pluck('response'),
)
)),
startWith(itemList),
filter(Boolean),
distinctUntilChanged(),
)
)),
)
.subscribe(itemList$)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment