-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Observable.from()
& accept conversions
#60
Conversation
Edit: just saw this was already pointed out above, sorry for the noise. |
The big blocker on the Web IDL level is accepting My suggestion is that in the spec, you define a conversion operation from a JS value to a Web IDL
and then the algorithm can proceed from there assuming observableArg is a Web IDL In the future we can add some sort of |
I suspect we'll want to use that the moment something wants to consume an |
Yep, that was my plan, I just wasn't sure if that (plus forcing Observable consumers to accept
The only thing I can think of is the last point I mentioned above, which is that we'd probably want to allow various things to be passed in as (and converted to) an Observable, while preventing code from returning i.e., async iterables in place of an expected Observable return value. At least that's what I thought you were saying on Matrix (and kind of how I interpreted the discussion in #44). |
I think you misunderstood me. Returning This is assuming we'd have many more APIs that want to consume |
Got it. I've clarified offline that @annevk was talking about whether or not we actually wanted to expose "async sequence" directly as a Web IDL type, and was not concerned about the automatic "async iterable => Observable" conversion incurred when a developer returns an async iterable from a function whose IDL callback return type is an Observable. I'll update the PR to reflect the option of making Observables its own WebIDL type that'd automatically perform the kind of conversion discussed above when going from ES->IDL. |
This PR should close #28 and #44, by providing a static
Observable.from()
method, as well as changingtakeUntil()
— the only web platform method proposed here that takes "an Observable" — to accept the same kinds of objects thatObservable.from()
does.I had some trouble initially writing this PR. For example: when used as an argument, WebIDL's
Promise<T>
type accepts native Promises as well as all thenables, I believe, which appears to be achieved by delegating to ECMAScript's laxness around thenables. As proposed in this repository, Observables are not part of the ECMAScript language, so they don't get special ES<->WebIDL conversion steps that we might be able to use to accept anything that:Observable
; orPromise<T>
; orSymbol.iterator
; orSymbol.asyncIterator
protocolGiven that, I was at least hoping we'd be able to supply some kind of nifty conversion algorithm to convert these kinds of types to native Observables, that WebIDL itself could hook into so that any web platform methods that want to accept an Observable can just accept the
Observable
type itself (and the conversion will be done automatically as part of the bindings). Unfortunately I don't think that's possible, nor does it seem possible to express an async iterable type in WebIDL either. For example, whatwg/webidl#874 seems to indicate that the best we can do is accept anany
and then do the conversion manually by calling ES'sGetIterator(object, async)
, as the Streams Standard does here: https://streams.spec.whatwg.org/#readable-stream-from-iterable.This isn't horrible, and it doesn't seem like a serious blocker or anything, but if Observables became widely used on the platform we'd be encouraging a bunch of
any
methods that have to delegate to some abstract conversion algorithm that our spec will add. We can always introduce syntax to WebIDL later to make it possible to express a type that is async iterable (something likeasync sequence<T>
perhaps); this would allow us to use methods that accept atypedef (Promise<T> or sequence<T> or async sequence<T>) ObservableInit
— except oh wait, Promises can't be used in unions! So I guess we're stuck with encouragingany
...