diff --git a/docs/api.md b/docs/api.md index 70e4bf6..03d3771 100644 --- a/docs/api.md +++ b/docs/api.md @@ -19,7 +19,7 @@ A Session represents an open connection between the server and the client. It emits the `connected` event after it has connected and flushed all headers to the client, and the `disconnected` event after client connection has been closed. -#### `new Session(req: IncomingMessage, res: ServerResponse, [options] = {})` +#### `new Session(req: IncomingMessage, res: ServerResponse[, options = {}])` `req` is an instance of [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage). @@ -47,6 +47,12 @@ This is initialized to the last event ID given by the user (in the `Last-Event-I Indicates whether the session and connection is open or not. +#### `Session#state`: `{}` + +Custom state for this session. + +Use this object to safely store information related to the session and user. + #### `Session#dispatch`: `() => this` Flush the buffered data to the client by writing an additional newline. @@ -140,7 +146,7 @@ Fires the `session-deregistered` event with the session as its first argument. If the session was disconnected the channel will also fire the `session-disconnected` event with the disconnected session as its first argument beforehand. -#### `Channel#broadcast`: `(eventName: string, data: any) => this` +#### `Channel#broadcast`: `(eventName: string, data: any[, options = {}]) => this` Broadcasts an event with the given name and data to every active session subscribed to the channel. @@ -148,6 +154,10 @@ Under the hood this calls the [`push`](#session%23push%3A-(event%3A-string%2C-da Fires the `broadcast` event with the given event name and data in their respective order. +|`options.`|Type|Default|Description| +|-|-|-|-| +|`filter`|`(session: Session) => unknown`||Filter sessions that should receive the event.

Called with each session and should return a truthy value to allow the event to be sent, otherwise return a falsy value to prevent the session from receiving the event.| + ### `createChannel`: `(...args: ConstructorParameters) => Channel` Creates and returns an instance of a [Channel](#channel). diff --git a/src/Channel.ts b/src/Channel.ts index 1114abf..ee164ba 100644 --- a/src/Channel.ts +++ b/src/Channel.ts @@ -5,7 +5,7 @@ interface BroadcastOptions { /** * Filter sessions that should receive the event. * - * Will be called with each session and should return a truthy value to allow the event to be sent, otherwise return a falsy value to prevent the session from receiving the event. + * Called with each session and should return a truthy value to allow the event to be sent, otherwise return a falsy value to prevent the session from receiving the event. */ filter?: (session: Session) => unknown; }