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

Passing generics to SourceSelect #57

Closed
jonashoen opened this issue Oct 1, 2024 · 2 comments
Closed

Passing generics to SourceSelect #57

jonashoen opened this issue Oct 1, 2024 · 2 comments
Labels
seen I've seen and read this issue and I'll try find some time soon to work on it.

Comments

@jonashoen
Copy link

First of all thanks for creating this library! Its the first time I'm having fun working with SSE.

I tried to use the json function but noticed that the typing is kinda odd to me.

When creating a listener with source you can pass in a generic which then isn't passed down to the select function:

* @property {SourceSelect} select Select an event from the stream.

Because of this the type of the readable store returned by the json function is always any.

But in my opinion it would also be more useful to specify the generic when calling select instead of source because different events can emit different kinds of data.

What are your thoughts on that? Am I missing something?

Thanks!

@razshare razshare added the seen I've seen and read this issue and I'll try find some time soon to work on it. label Oct 1, 2024
@razshare
Copy link
Owner

razshare commented Oct 4, 2024

Hello @jonashoen , thank you for the issue!

Yes, I agree with you, source is not a good place to specify a generic.

But I also don't think source::select is a good place, because that returns a Readable<string> by default.

The source::select::json and source::select::transform functions should each specify the generic type.

I've just released a new version with these changes v0.13.8

In short, you can now do this

<!-- +page.svelte -->
<script lang="ts">
  import { source } from 'sveltekit-sse'

  type User = {
    username: string
  }

  const user = 
    source('/my-event')
      .select('user')
        .json<User>()
</script>

{#if $user}
  <h3>{$user.username}</h3>
{/if}

Let me know if this solves your issue.

@jonashoen
Copy link
Author

Hey @razshare, yeah this is great!

Thanks for the change :)

@razshare razshare closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
seen I've seen and read this issue and I'll try find some time soon to work on it.
Projects
None yet
Development

No branches or pull requests

2 participants