v0.6.0
Version 0.6.0
npm install better-sse@latestThis 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; // numberRename 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#iteratemethod that allows processing iterables and sending yielded values to the client as events. - Added types for
SessionandChannelevent listener callback function arguments. - Added the ability to type
Session#stateusing an optional generic argument forcreateSessionand theSessionconstructor.
Changed
- Rename the
Session#streameventoption toeventName.