Derived Reactive Snapshot State with Slice OStore

Ole Ersoy
Ole Ersoy
Jan 14 · 1 min read
Image by skeeze from Pixabay

Scenario

We have a Observable<boolean> that notifies when an option has been selected. It’s defined like this:

const OPTION_ONE_SELECTED = 'OPTION_ONE_SELECTED'const os:OStore = new OStore()os.post(OPTION_ONE_SELECTED, true)const optionOneSelected$:Observable<boolean> = os.observe(OPTION_ONE_SELECTED)

We want a snapshot (A one time synchronous value derived from the optionOneSelected$) of whether option one has been selected.

Approach

function isOptionOneSelected() {
return optionOneSelected$.pipe(take(1)).toPromise()
}

Now we can check whether option one has been selected like this:

async function checkIfOptionOneIsSelected() {
console.log("Is option one selected: ",
await isOptionOneSelected())
}

Demo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade