gcanti / fp-ts-rxjs

fp-ts bindings for RxJS
https://gcanti.github.io/fp-ts-rxjs/
MIT License
188 stars 29 forks source link

Add fromOption, fromIO and fromTask #20

Closed mlegenhausen closed 4 years ago

gcanti commented 4 years ago

Thanks @mlegenhausen

anthonyjoeseph commented 3 years ago

@mlegenhausen Just out of curiosity, is there a reason fromIO uses asynchronous context?

For example, it could have been implemented like so:

export function fromIO<A>(io: IO<A>): Observable<A> {
  return defer(() => rxOf(io()))
}

Is there an advantage to using async here instead? Is it conceptually more correct?

mlegenhausen commented 3 years ago

Is there an advantage to using async here instead? Is it conceptually more correct?

Don't think so. I think I chose async cause it was more inline with the fromTask implementation. Without knowing the internals (and the rxjs documentation isn't there of any help) it could be that your implementation could save one event loop iteration when calling the IO but this is only a guess.

anthonyjoeseph commented 3 years ago

I think you're right. fromIO wasn't working for my game engine's spec b/c TestScheduler doesn't work in async contexts, but when I rolled my own fromSyncIO it worked just fine.

Could this be a desired PR? It does seem mildly more correct since you can raise into asynchronous context with observeOn(AsyncScheduler) but you can't go back to synchronous context from async.

It probably doesn't matter for most use cases though, especially since the function is so simple, and I certainly don't mind using my own fromSyncIO.

mlegenhausen commented 3 years ago

Could this be a desired PR?

Yes definitely.

anthonyjoeseph commented 3 years ago

PR