Releases: MatthewWid/better-sse
Releases · MatthewWid/better-sse
v0.6.0
Version 0.6.0
npm install better-sse@latest
This release adds a new iterate
method to Session
that allows you to send iterables as events and greatly improves the TypeScript experience by adding types to Session
and Channel
events as well as allowing you to type the Session
state
property.
Changes
New Session
iterate
method
You can now process iterables and send each yielded value to the client as an event.
Synchronous iterables:
const dataToWrite = [1, 2, 3];
function* syncIterator() {
for (let i = 0; i < dataToWrite.length; i++) {
yield dataToWrite[i];
}
}
// Iterate over each item of `dataToWrite` and send the item as an event with name `iteration`
await session.iterate<number>(syncIterator());
Asynchronous iterables:
const dataToWrite = [1, 2, 3];
async function* asyncIterator() {
for (let i = 0; i < dataToWrite.length; i++) {
yield dataToWrite[i];
}
}
// Iterate over each item of `dataToWrite` and send the resolved value as an event with name `iteration`
await session.iterate<number>(asyncIterator());
Event Typings
In-built events for the Session
and Channel
method are now typed, meaning you no longer have to define the type of the callback arguments:
// Before
channel.on("session-registered", (session: Session) => { ... });
// After
channel.on("session-registered", (session) => { ... });
Thanks to tiny-typed-emitter by binier for the implementation inspiration.
Session state
Property Typings
You can now add explicit types to the Session
state
property, whereas before all values would be unknown
, you can now add your own:
// Before
const session = await createSession(req, res);
session.state.id = 123;
session.state.id; // unknown
// After
const session = await createSession<{ id: number }>(req, res);
session.state.id = 123;
session.state.id; // number
Rename Session stream
Event Name Option
// Before
await session.stream(stream, {event: "waterfall"});
// After
await session.stream(stream, {eventName: "waterfall"});
Full Changelog
Added
- Added the
Session#iterate
method that allows processing iterables and sending yielded values to the client as events. - Added types for
Session
andChannel
event listener callback function arguments. - Added the ability to type
Session#state
using an optional generic argument forcreateSession
and theSession
constructor.
Changed
- Rename the
Session#stream
event
option toeventName
.
v0.5.0
Added
- Added broadcast channels that allow pushing events to multiple sessions at once.
- Added support for EventStream polyfills
event-source-polyfill
andeventsource-polyfill
. - Added the
Session#state
property to have a safe namespace for keeping information attached to the session.
Fixed
- Fixed TypeScript types for the
Session#lastId
not being read-only.
v0.4.0
- Added an automatic keep-alive mechanism that can be enabled or disable in the
Session
constructor options. - Added the
Session#isConnected
boolean property. - Fixed an issue where installing the package using
npm
would throw an error mandating it be installed withpnpm
.