Skip to content

Commit

Permalink
feat: warn when attempting to iterate syncronously
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Apr 27, 2024
1 parent 4661011 commit c639b09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ const noop = () => {
* Creates a new EventSource client. Used internally by the environment-specific entry points,
* and should not be used directly by consumers.
*
* @param options - Options for the client.
* @param optionsOrUrl - Options for the client, or an URL string.
* @param abstractions - Abstractions for the environments.
* @returns A new EventSource client instance
* @internal
*/
export function createEventSource(
options: EventSourceOptions,
optionsOrUrl: EventSourceOptions | string,
{getStream}: EnvAbstractions,
): EventSourceClient {
const options = typeof optionsOrUrl === 'string' ? {url: optionsOrUrl} : optionsOrUrl
const {onMessage, onConnect = noop, onDisconnect = noop, onScheduleReconnect = noop} = options
const {fetch, url, initialLastEventId} = validate(options)
const requestHeaders = {...options.headers} // Prevent post-creation mutations to headers
Expand All @@ -56,6 +57,11 @@ export function createEventSource(
return {
close,
connect,
[Symbol.iterator]: () => {
throw new Error(
'EventSource does not support synchronous iteration. Use `for await` instead.',
)
},
[Symbol.asyncIterator]: getEventIterator,
get lastEventId() {
return lastEventId
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export interface EventSourceClient {
/** Connect to the event source. Automatically called on creation - you only need to call this after manually calling `close()`, when server has sent an HTTP 204, or the server responded with a non-retryable error. */
connect(): void

/** Warns when attempting to iterate synchronously */
[Symbol.iterator](): never

/** Async iterator of messages received */
[Symbol.asyncIterator](): AsyncIterableIterator<EventSourceMessage>

Expand Down

0 comments on commit c639b09

Please sign in to comment.