Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

Fetch value of custom observable field #331

Closed arosendorff closed 5 years ago

arosendorff commented 5 years ago

Say I have a custom observable field like so:

@lazy isNoProblem = this.noProblemEntries
    .observeCount()
    .pipe(
      map$(count => count > 0),
      distinctUntilChanged(),
    );

I know that withObservables() will correctly observe and return my custom field, but is there a way to directly return the current value of this custom observable, i.e. like how you can call .fetch() on a regular query to get its response?

rkrajewski commented 5 years ago

@arosendorff how about this?

import { first } from 'rxjs/operators'

const isNoProblem = await obj.isNoProblem.pipe(first()).toPromise()
arosendorff commented 5 years ago

@rkrajewski Fantastic, that works perfectly! I'm still a bit novice with RXJS, but it's pretty powerful! Does this emit a value when you call it because any WatermelonDB source is a BehaviorSubject?

rkrajewski commented 5 years ago

Does this emit a value when you call it because any WatermelonDB source is a BehaviorSubject?

This should work for any kind of Observable, but it may await for emitted value, if it hasn't been emitted yet, and BehaviorSubject has always its initial value. Besides it's not true, that any WatermelonDB source is a BehaviorSubject.