Releases: razshare/sveltekit-sse
Releases · razshare/sveltekit-sse
Fixing type inference in .json() for JSDoc users
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, andT
defaults tonull
in its type definition, but JS Doc's default behavior (and Typescript's) is to infer missing template types asany
, thus a type mismatch occurs.Generic template in
.json()
now defaults toany
instead ofnull
.
.json() errors logs are now opt in
Changes
- function
.json()
no longer logs errors by default, you now have to opt in to log these errors.
For exampleconnection.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
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
Changes
- The initial value of
select().json()
now fallback to the or predicate on both the server and the client.
In short, instead of thisyou can now also do something like 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>
<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
Changes
- Previously
emit
would internally encode data as uri andsource
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
Changes
- Fixing an issue where when the client disconnects the
produce::stop
function would not trigger untilproduce::start
finished.
Which is problematic, because the order should be reversed: functionproduce::stop
should run first, thenproduce::start
, otherwise infinite emitters would never stop.
Fixing more type hints
Changes
source
andsource::select
no longer accept genericsT
, insteadsource::select::json
andsource::select::transform
will each accept separate generics.
Meaning you will now type your values like so withsource::select::json
And with<!-- +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}
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
Changes
source::select::transform
is now correctly typed as returningReadable<T>
Exposing more types & renaming some
$\color{red}{\textsf{Breaking changes}}$
- type
ProduceOptions
renamed toProducePayload
. - type
EmitterOfManyEvents
renamed toEmitter
.
Changes
- Exposing all types except internal ones.
Allowing custom http method names
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)
}
})
}