Skip to content

Releases: razshare/sveltekit-sse

Fixing type inference in .json() for JSDoc users

16 Mar 08:58
Compare
Choose a tag to compare

Changes

  • Previously this would not work in JS Doc

    connection.select('stats').json(function or({error}) {
      console.error("Could not parse stats!", error)
      return { cpu_usage: '0.00%', memory: '0.00 MB', time: 0 }
    })

    Because .json() is generic, and T defaults to null in its type definition, but JS Doc's default behavior (and Typescript's) is to infer missing template types as any, thus a type mismatch occurs.

    Generic template in .json() now defaults to any instead of null.

.json() errors logs are now opt in

16 Mar 08:33
Compare
Choose a tag to compare

Changes

  • function .json() no longer logs errors by default, you now have to opt in to log these errors.
    For example
    connection.select('stats').json(function or({error}) {
      console.error("Could not parse stats!", error)
      return { cpu_usage: '0.00%', memory: '0.00 MB', time: 0 }
    })

Improving type hints for select::json

22 Feb 02:52
Compare
Choose a tag to compare

Changes

  • select::json type hinting now falls back to a nullable empty object as a type and infers the actual type from either the generic type or the result from the predicate.

Initial value of .json now falls back to predicate

22 Feb 02:26
Compare
Choose a tag to compare

Changes

  • The initial value of select().json() now fallback to the or predicate on both the server and the client.
    In short, instead of this
    <script>
      import { source } from 'sveltekit-sse';
    
      const connection = source('/events');
      const stats = connection.select('message').json();
    </script>
    
    <main>
      <h2>Admin dashboard page</h2>
    
      {#if $stats}
        <span>Cpu usage - {$stats.cpu_usage}</span><br />
        <span>Memory - {$stats.memory}</span><br />
        <span>Time - {$stats.time}</span><br />
      {/if}
    </main>
    you can now also do something like this
    <script>
      import { source } from 'sveltekit-sse';
    
      const connection = source('/events');
      const stats = connection.select('message').json(function or() {
        return { cpu_usage: '0.00%', memory: '0.00 MB', time: 0 };
      });
    </script>
    
    <main>
      <h2>Admin dashboard page</h2>
    
      <span>Cpu usage - {$stats.cpu_usage}</span><br />
      <span>Memory - {$stats.memory}</span><br />
      <span>Time - {$stats.time}</span><br />
    </main>

Dropping internal uri encoding/decoding

07 Jan 17:10
Compare
Choose a tag to compare

Changes

  • Previously emit would internally encode data as uri and source would decode it.
    This behavior is not needed, so it's being removed, instead data will be sent raw from now on.
    Related to #64

Fixing cleanup order of execution

06 Oct 08:51
Compare
Choose a tag to compare

Changes

  • Fixing an issue where when the client disconnects the produce::stop function would not trigger until produce::start finished.
    Which is problematic, because the order should be reversed: function produce::stop should run first, then produce::start, otherwise infinite emitters would never stop.

Fixing more type hints

04 Oct 17:06
Compare
Choose a tag to compare

Changes

  • source and source::select no longer accept generics T, instead source::select::json and source::select::transform will each accept separate generics.
    Meaning you will now type your values like so with source::select::json
    <!-- +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}
    And with source::select::transform
    <!-- +page.svelte -->
    <script lang="ts">
      import { source } from 'sveltekit-sse'
    
      type User = {
        username: string
      }
    
      // For the sake of brevity
      // let's suppose all the event emits 
      // is the raw name of the user
      function transformer(raw:string):User{
        return {
          username: raw
        }
      }
    
      const user = 
        source('/my-event')
          .select('user')
            .transform<User>(transformer)
    </script>
    
    {#if $user}
      <h3>{$user.username}</h3>
    {/if}

Fixing type hint for transform

24 Sep 16:37
Compare
Choose a tag to compare

Changes

  • source::select::transform is now correctly typed as returning Readable<T>

Exposing more types & renaming some

28 Jul 11:31
Compare
Choose a tag to compare

$\color{red}{\textsf{Breaking changes}}$

  • type ProduceOptions renamed to ProducePayload.
  • type EmitterOfManyEvents renamed to Emitter.

Changes

  • Exposing all types except internal ones.

Allowing custom http method names

27 Jul 10:26
Compare
Choose a tag to compare

Changes

  • It is now possible to set the http method of your source()
<!-- +page.svelte -->
<script>
  import { source } from 'sveltekit-sse'
  const connection = source('/issue-55/events', {
    options: {
      method: 'GET',  // The default is POST.
    },
  })
  const message = connection.select('message')
</script>

<h3>{$message}</h3>
import { produce } from 'sveltekit-sse'
import { delay } from '$lib/delay.js'

export async function GET() { // Then you can use GET here.
  return produce(async function start({ emit }) {
    while (true) {
      const { error } = emit('message', `${Date.now()}`)
      if (error) {
        return
      }
      await delay(1000)
    }
  })
}