Skip to content

Releases: MatthewWid/better-sse

v0.6.0

28 Oct 06:11
Compare
Choose a tag to compare

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());

See the API docs for more.

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 and Channel event listener callback function arguments.
  • Added the ability to type Session#state using an optional generic argument for createSession and the Session constructor.

Changed

v0.5.0

17 Jul 08:02
Compare
Choose a tag to compare

Added

Fixed

v0.4.0

09 Jul 01:32
Compare
Choose a tag to compare
  • 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 with pnpm.