wonka
wonka copied to clipboard
convert ECMAScript Observable to RXJS Observable
Is it possible to convert the observable to an rxjs observable?
This would be the desired behavior from an urql subscription:
const sub = pipe(
this.client.subscription(q),
toObservable
);
sub.subscribe((r: any) {
console.log(r);
});
This seems like a lot more complicated to write every time:
.subscribe({
next(r: any) {
console.log(r);
},
error(e: any) {
console.log('Error: ', e);
},
complete() { }
});
Also, in Angular, if I use the observable in the template, I get this error:
ERROR TypeError: Cannot read property 'bind' of undefined
So, I hacked it using this, although it seems like there should be an easier way without this hack:
test: BehaviorSubject<any> = new BehaviorSubject<any>([]);;
sub.subscribe({
next(r: any) {
_this.test.next(r);
console.log(r);
},
error(e: any) { },
complete() { }
});
Then I use test in my template...
Is this the only way?
I'm not sure if this has changed but you can convert any spec-compliant Observable to an RxJS Observable using from
.
The toObservable
operator outputs a spec-compliant ECMAScript Observable so converting it should be as simple as for any other spec-compliant observable.
Hmm, I am getting this:
import { pipe, toObservable } from 'wonka';
import { from } from 'rxjs';
...
from(
pipe(
this.client.subscription(q),
toObservable
)
);
ERROR TypeError: You provided an invalid object where a stream was expected.
You can provide an Observable, Promise, Array, or Iterable.
And I get this type error if I don't force an any
type:
Argument of type 'observableT<any>' is not assignable to parameter of type 'ObservableInput<any>'
Got it: zen-observable
, which is already needed for urql to work in Angular:
https://stackoverflow.com/questions/66309283/convert-ecmascript-observable-to-rxjs-observable/66380963#66380963