-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: make {#await} blocks support async stores that can also emit errors, such as the observables from RxJS #10227
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
Comments
I'm not sure if adding this to |
Why not though? The Also, yes of course with the right wrapper and the <script>
import { createQuery } from '@tanstack/svelte-query';
const query = createQuery({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
});
</script>
{#if $query.isLoading}
<p>Loading...</p>
{:else if $query.isError}
<p>Error: {$query.error.message}</p>
{:else if $query.isSuccess}
{#each $query.data as todo}
<p>{todo.title}</p>
{/each}
{/if} Is just so much more ugly than this: <script>
import { createQuery } from '@tanstack/svelte-query';
const query = createQuery({
queryKey: ['todos'],
queryFn: () => fetchTodos(),
});
</script>
{#await query}
<p>Loading...</p>
{:then todos}
{#each todos as todo}
<p>{todo.title}</p>
{/each}
{:catch error}
<p>Error: {error.message}</p>
{/if} |
While I do like the idea of supporting this natively in function makePromiseLike<T>(observable: Observable<T>): Observable<T>&PromiseLike<T> {
obs.then = (complete, error) => firstValueFrom(obs).then(complete, error);
return obs;
} The above function makes any observable Alternatively, just wrap your observable like this: {#await firstValueFrom(name) }
Waiting for operator name
{:then _}
{ $name }
{/await} It's still not ideal but definitely workable in my opinion. I'm not sure if the example given by @dummdidumm is valid. Svelte observables are like RxJs an |
@SamMousa I actually did implement something like your I don't exactly remember how I cheated. I either created a But that was fragile, because it worked in some contexts (such as the In the end I settled on |
Uh oh!
There was an error while loading. Please reload this page.
Describe the problem
Context
With Svelte 5's adoption of signals, it's crucial to highlight the complementary role of observables. While signals efficiently handle simple and synchronous state, observables excel in managing complex, asynchronous states without creating spaghetti code.
Problem
Svelte’s current templating syntax offers native support for auto- subscribing and unsubscribing from both their built-in stores and RxJS's observables, but it doesn't offer native support for either the asynchronicity of RxJS's observables nor their error emissions. But Svelte does already offer the
{#await}
block for promises, which could also work perfectly for these observables.Describe the proposed solution
Enhance the
{#await}
block to support observables that are asynchronous and emit errors, bridging the gap between signals and observables for more complex state management.(If you'd like to see the code in
newTypeAheadStore()
you can look at this REPL)Explanation
The searchResults observable initially takes over half a second to emit its first value, displaying a
Loading...
message to the user during this period. Once it emits the first value, a<pre>
block is shown, updating continuously with new results.If an error occurs, particularly during a
fetch
operation, the{:catch}
block is displayed. Post-error, searchResults stops emitting values and requires re-initialization throughnewTypeAhead()
function. Upon re-initialization, the loading message displays again before showing updated results.This concept also applies to Svelte stores. While they may not emit errors, their initial
undefined
state can be interpreted as a loading phase. The loading block remains until the store's state changes fromundefined
, after which the{:then}
block is rendered, without reverting to the loading state, even if the store returns toundefined
.Slightly more advanced version (I'm okay if this doesn't get implemented)
RxJS's observables can also
complete
, after which they will no longer emit values. This is probably not relevant in most cases. But in some cases, we might want to give the user the option to re-subscribe to the observable to get a new stream of values. For that we could implement something like this:Importance
nice to have
The text was updated successfully, but these errors were encountered: