Search code examples
typescriptrxjsobservableurql

Convert ECMAScript Observable / Zen Observable to RXJS Observable


I have a spec-compliant ECMAScript Observable, specifically from the wonka library. I am trying to convert this type of observable to an rxjs 6 observable with no luck.

It seems this may have been possible with rxjs 5. I have tried this:

import { pipe, toObservable, fromArray } from 'wonka';
import { from } from 'rxjs';
...
from(
  pipe(
    fromArray([1, 2, 3]),
    toObservable
  )
);

I get this error in the browser:

ERROR TypeError: You provided an invalid object where a stream was expected. 
You can provide an Observable, Promise, Array, or Iterable.

and then this:

Argument of type 'observableT<any>' is not assignable to parameter
of type 'ObservableInput<any>'

in the Visual Code dialog.

I can convert it to a zen-observable by doing this:

npm i zen-observable
npm i --save-dev @types/zen-observable
import { from } from 'zen-observable';
...
getObservable(): Observable<any> {
  return from(
    pipe(
      fromArray([1, 2, 3]),
      toObservable
    ) as any) as any;
}

However, a zen-observable is not the same thing, and does not allow me to use all the rxjs methods etc...

How to I convert this to an rxjs observable?

Thanks J


Solution

  • Modifying backtick's answer, got it to work:

    return new Observable((observer: any) => {
      pipe(
        fromArray([1, 2, 3]),
        toObservable
      ).subscribe(observer);
    });