Releases: razshare/sveltekit-sse
Releases · razshare/sveltekit-sse
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)
}
})
}
More consistent source values
Changes
- Previously, when using
source('/some-path')
you would always get a store with the first value initialized to blank.
That doesn't make much sense if you're connecting to a source that had already open and had already produced some data.
In that case, the returned store used to emit:- First an
undefined
value - And then the most recent value of the stream
This change makes it so that the returned store emits directly the most recent value of the stream if available.
For more details see #48
- First an
Fixing an issue with cache disabling
Changes
- Fixed a bug where if you disabled connection caching and tried to
source()
twice at the same time on the same path, the secondsource
would fail due to an internal aggressive semaphore.
So in this caseconst sse1 = source('/events', { cache: false }).select('message'); const sse2 = source('/events', { cache: false }).select('message');
sse2
would fail to connect silently.
Dropping Beacons & Simplifying Api
$\color{red}{\textsf{Breaking changes}}$
- Beacons are gone.
Instead, disconnected clients are now detected through a server side ping mechanism.
Unless you were manually configuring the beacon before - there's no need for any changes in userland, it should all just work.
You can read more here. - The old
events()
functions has been replaced with a simplerproduce()
function which doesn't take the request and a whole object anymore, but just a start function instead (and some options as a second parameter).
Something like thisexport async function POST() { return produce(conn => conn.emit('message', 'hello')) }
- You can now write your logic within the body of your POST function without any worries or setup.
Previously this was discouraged due to how beacons worked.
This is now completely fineexport function POST() { let counter = 0 return produce(conn => { while(counter < 3){ conn.emit("counter", `Counter is - ${counter}`) counter++ } }) }
Beacon variance & improved defaults
Changes
- The
source()
function now also accepts abeaconVariance
property.
This property will vary the actual beacon request timing in order to avoid the thundering-herd effect.
Defaults to 5 seconds.const connection = source(`/events`, { beacon: 15_000, beaconVariance: 4000 })
- The server side stream timeout is now 45 seconds by default (previously 7 seconds)
- The client side beacon now triggers every 30 seconds by default (previously 5 seconds)
- Fixed a memory leak which caused the internal timeouts map to keep growing.
The map entries are now removed as clients disconnect, as it should have been from the beginning.
Fixing reconnect issues
Changes
-
Fixing an issue where
source::close()
andEventListener::connect()
would not work as intended.
SpecificallyEventListener::connect()
would stop working after manually reconnecting through source::close()`.
See #43 -
Adding more playwright tests