Skip to content

Commit 97b7462

Browse files
committed
Merge branch 'add-done-event'
2 parents c3fbe03 + 6b59efd commit 97b7462

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,24 @@ es.addEventListener("error", (event) => {
5151
}
5252
});
5353

54+
es.addEventListener("done", (event) => {
55+
console.log("Done SSE connection.");
56+
});
57+
5458
es.addEventListener("close", (event) => {
5559
console.log("Close SSE connection.");
5660
});
5761
```
5862

63+
### Done vs Close
64+
65+
`done` events will fire when server closes the connection.
66+
By default, the client will automatically reconnect when this happens.
67+
You can disable reconnections by setting the `pollingInterval` option to `0`.
68+
`close` events will fire when the connection is terminated by the client, using `.close()`.
69+
70+
### Headers and params
71+
5972
If you want to use Bearer token and/or topics, look at this example (TypeScript):
6073

6174
```typescript

index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close';
1+
export type BuiltInEventType = 'open' | 'message' | 'error' | 'done' | 'close';
22
export type EventType<E extends string = never> = E | BuiltInEventType;
33

44
export interface MessageEvent {
@@ -12,6 +12,10 @@ export interface OpenEvent {
1212
type: 'open';
1313
}
1414

15+
export interface DoneEvent {
16+
type: 'done';
17+
}
18+
1519
export interface CloseEvent {
1620
type: 'close';
1721
}
@@ -55,6 +59,7 @@ export interface EventSourceOptions {
5559
type BuiltInEventMap = {
5660
'message': MessageEvent,
5761
'open': OpenEvent,
62+
'done': DoneEvent,
5863
'close': CloseEvent,
5964
'error': ErrorEvent | TimeoutEvent | ExceptionEvent,
6065
};

src/EventSource.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class EventSource {
1818
open: [],
1919
message: [],
2020
error: [],
21+
done: [],
2122
close: [],
2223
};
2324

@@ -121,6 +122,7 @@ class EventSource {
121122
if (xhr.readyState === XMLHttpRequest.DONE) {
122123
this._logDebug('[EventSource][onreadystatechange][DONE] Operation done.');
123124
this._pollAgain(this.interval, false);
125+
this.dispatch('done', { type: 'done' });
124126
}
125127
} else if (xhr.status !== 0) {
126128
this.status = this.ERROR;

0 commit comments

Comments
 (0)