Skip to content

Commit e3c5880

Browse files
authored
Add lastEventId and url in events with defined type (#2)
* Add lastEventId in events with defined type * Add lastEventId and url in CustomEvent interface
1 parent de90c9d commit e3c5880

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export interface ErrorEvent {
3333
export interface CustomEvent<E extends string> {
3434
type: E;
3535
data: string | null;
36+
lastEventId: string | null;
37+
url: string;
3638
}
3739

3840
export interface ExceptionEvent {

src/EventSource.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,15 @@ class EventSource {
183183
this.lastEventId = null;
184184
} else if (line === '') {
185185
if (data.length > 0) {
186-
if (this.eventType !== undefined) {
187-
const event = {
188-
type: this.eventType,
189-
data: data.join('\n'),
190-
};
191-
192-
this.dispatch(this.eventType, event);
193-
} else {
194-
const event = {
195-
type: 'message',
196-
data: data.join('\n'),
197-
url: this.url,
198-
lastEventId: this.lastEventId,
199-
};
200-
201-
this.dispatch('message', event);
202-
}
186+
const eventType = this.eventType || 'message'
187+
const event = {
188+
type: eventType,
189+
data: data.join("\n"),
190+
url: this.url,
191+
lastEventId: this.lastEventId,
192+
};
193+
194+
this.dispatch(eventType, event);
203195

204196
data = [];
205197
this.eventType = undefined;

0 commit comments

Comments
 (0)