Skip to content

Support for RxJS Observables and #await blocks, await and error blocks never render, only the then block #5206

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

Closed
AlexAegis opened this issue Jul 25, 2020 · 2 comments

Comments

@AlexAegis
Copy link

AlexAegis commented Jul 25, 2020

When using RxJS observables with the await block the first block and the error block never renders.
It renders: undefined -> 0 -> 1 -> 2

Using svelte-observable it works as expected
and renders: Loading... -> 0 -> 1 -> 2 -> Error

app.svelte

<script>
	import { interval, Subject } from 'rxjs';
	import { tap } from 'rxjs/operators';
	import { onMount } from 'svelte';
	import { observe } from 'svelte-observable';

	const subject = new Subject();
	const o1$ = subject.pipe(
		tap((i) => {
			if (i > 2) {
				throw new Error('O Error');
			}
		})
	);
	const o2$ = observe(o1$);

	const p1 = new Promise((res) => {
		setTimeout(() => {
			res('Promise 1');
		}, 500);
	});

	const p2 = new Promise((res, rej) => {
		setTimeout(() => {
			rej('Promise 2 error');
		}, 700);
	});

	onMount(() => {
		interval(500).subscribe((i) => {
			subject.next(i);
		})
	});
</script>

<h3>Observable, should emit thrice then error</h3>
{#await $o1$}
	O1 Loading...
{:then v}
	O1 Value: {v}
{:catch e}
	O1 Error: {e}
{/await}

<h3>Observable using svelte-observable, will emit thrice then error</h3>
{#await $o2$}
	O2 Loading...
{:then v}
	O2 Value: {v}
{:catch e}
	O2 Error: {e}
{/await}

<h3>Promise 1, will emit</h3>

{#await p1}
	P1 Loading...
{:then v}
	P1 Value: {v}
{:catch e}
	P1 Error: {e}
{/await}

<h3>Promise 2, will error out</h3>
{#await p2}
	P2 Loading...
{:then v}
	P2 Value: {v}
{:catch e}
	P2 Error: {e}
{/await}

Node 14
Chrome 84

	"dependencies": {
		"rxjs": "6.6.0",
		"svelte": "3.24.0",
		"svelte-observable": "0.4.0"
	},
	"devDependencies": {
		"rollup": "2.23.0",
	},
@dummdidumm
Copy link
Member

That you never see the catch block is "works as designed" I think. Technically you can await anything but only if it's a promise, you will get the catch behavior. Showing "loading" while undefined and then should work though.

@Conduitry
Copy link
Member

I believe {#await} wraps its argument in Promise.resolve() so that it also works with regular values, but yeah by design it doesn't work with whatever interface RxJS exposes. Only store autosubscription interfaces with that, and in a limited way. If there's something svelte-observable ought to be doing differently, you can open an issue in the project, as it's not an official library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants