Skip to content

Instantly share code, notes, and snippets.

@AriPal
Last active March 28, 2019 12:41
Show Gist options
  • Save AriPal/9f4148603fa060312fdb4978deda6ab0 to your computer and use it in GitHub Desktop.
Save AriPal/9f4148603fa060312fdb4978deda6ab0 to your computer and use it in GitHub Desktop.
import { from } from 'rxjs';
import { map, publishReplay, refCount } from 'rxjs/operators';
// Create observable that holds two values
const observable$ = from(['first', 'last']).pipe(
publishReplay(1),
refCount()
)
// First time subscribing, we get both values
observable$.subscribe(data => console.log(data));
// Second time subscribing, we get the latest value
observable$.subscribe(data => console.log(data));
observable$.subscribe(data => console.log(data));
observable$.subscribe(data => console.log(data));
// OUTPUT
// first
// last
// last
// last
// last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment