8000 feat(useEventSource): return `lastEventId` (#3984) · vueuse/vueuse@29bc690 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 29bc690

Browse files
authored
feat(useEventSource): return lastEventId (#3984)
1 parent b291c4c commit 29bc690

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/core/useEventSource/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface UseEventSourceReturn<Events extends string[]> {
8181
* Reference to the current EventSource instance.
8282
*/
8383
eventSource: Ref<EventSource | null>
84+
/**
85+
* The last event ID string, for server-sent events.
86+
* @see https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/lastEventId
87+
*/
88+
lastEventId: Ref<string | null>
8489
}
8590

8691
function resolveNestedOptions<T>(options: T | true): T {
@@ -109,6 +114,7 @@ export function useEventSource<Events extends string[]>(
109114
const eventSource = ref(null) as Ref<EventSource | null>
110115
const error = shallowRef(null) as Ref<Event | null>
111116
const urlRef = toRef(url)
117+
const lastEventId = shallowRef<string | null>(null)
112118

113119
let explicitlyClosed = false
114120
let retried = 0
@@ -169,6 +175,7 @@ export function useEventSource<Events extends string[]>(
169175
es.onmessage = (e: MessageEvent) => {
170176
event.value = null
171177
data.value = e.data
178+
lastEventId.value = e.lastEventId
172179
}
173180

174181
for (const event_name of events) {
@@ -201,5 +208,6 @@ export function useEventSource<Events extends string[]>(
201208
error,
202209
open,
203210
close,
211+
lastEventId,
204212
}
205213
}

0 commit comments

Comments
 (0)
0