Skip to content
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

tc39 proposal compatible fromObservable for better ecosystem interop. #2754

Closed
jessekelly881 opened this issue May 15, 2024 · 3 comments
Closed
Labels
enhancement New feature or request

Comments

@jessekelly881
Copy link
Contributor

jessekelly881 commented May 15, 2024

What is the problem this feature would solve?

It would be nice to add a Stream.fromObservable fn to better interop. w/ rxjs and other libs which use Observables.

https://github.com/tc39/proposal-observable

What is the feature you are proposing to solve the problem?

import { range, Observable } from "rxjs";
import { Effect, Stream } from "effect";

/**
 * Creates a `Stream` from an `Observable` instance.
 * @since 3.2.0
 * @see https://tc39.es/proposal-observable/
 */
export const fromObservable = <T>(observable: Observable<T>) =>
	Stream.async((emit) => {
		const subscription = observable.subscribe((value) =>
			emit.single(value)
		);
		return Effect.sync(() => subscription.unsubscribe());
	});

fromObservable(range(0, 10)).pipe(
	Stream.runForEach((s) => Effect.log(s)),
	Effect.runPromise
);

Observable type could be simplified to a compatible ObservableLike.

interface ObservableLike<T> {
	subscribe: (_: (_: T) => any) => { unsubscribe: () => void };
}
@jessekelly881 jessekelly881 added the enhancement New feature or request label May 15, 2024
@gcanti
Copy link
Contributor

gcanti commented May 15, 2024

I think we should wait until it reaches stage 3 and is officially supported by TypeScript.

@jessekelly881
Copy link
Contributor Author

jessekelly881 commented May 15, 2024

The spec might never make it into typescript but there are still quite a few libraries that adhere to the proposed type: rxjs, xstate, redux, facebook's relay being a few of them. A simplified interface ObservableLike { subscribe: (_: (_: T) => void) => { unsubscribe: () => void }; } should be enough to be compatible w/ them w/o having to use the full type.

https://github.com/search?q=proposal-observable&type=code

@mikearnaldi
Copy link
Member

The spec might never make it into typescript but there are still quite a few libraries that adhere to the proposed type: rxjs, xstate, redux, facebook's relay being a few of them. A simplified interface ObservableLike { subscribe: (_: (_: T) => void) => { unsubscribe: () => void }; } should be enough to be compatible w/ them w/o having to use the full type.

https://github.com/search?q=proposal-observable&type=code

Unless we introduce an Observable module I would avoid using the name, there is also the risk that the tc39 spec ends up in a different direction. I'd wait and see

@jessekelly881 jessekelly881 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants