-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement PoC Polyfill
- Loading branch information
Showing
9 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
changelog/unreleased/enhancement-sse-get-notifications-instantly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Enhancement: Add SSE to get notifications instantly | ||
|
||
We've added SSE to the notifications which allows us to be notified | ||
about new notifications instantly and from the server without polling | ||
every few seconds. | ||
|
||
https://github.com/owncloud/web/pull/9451 | ||
https://github.com/owncloud/web/issues/9434 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useServerSentEvents' |
64 changes: 64 additions & 0 deletions
64
packages/web-pkg/src/composables/sse/useServerSentEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-source' | ||
import { ref, unref, watch } from 'vue' | ||
import { v4 as uuidV4 } from 'uuid' | ||
import { useGettext } from 'vue3-gettext' | ||
import { configurationManager, useAccessToken, useStore } from 'web-pkg/src' | ||
|
||
export interface ServerSentEventsOptions { | ||
url: string | ||
onOpen?: (response: Response) => void | ||
onMessage?: (msg: EventSourceMessage) => void | ||
} | ||
|
||
export const useServerSentEvents = (options: ServerSentEventsOptions) => { | ||
const store = useStore() | ||
const language = useGettext() | ||
const accessToken = useAccessToken({ store }) | ||
const ctrl = ref(new AbortController()) | ||
const retryCounter = ref(0) | ||
|
||
watch( | ||
() => language.current, | ||
() => { | ||
unref(ctrl).abort() | ||
} | ||
) | ||
const setupServerSentEvents = () => { | ||
if (unref(retryCounter) >= 5) { | ||
unref(ctrl).abort() | ||
throw new Error('Too many retries') | ||
} | ||
const setupSSE = async () => { | ||
retryCounter.value++ | ||
try { | ||
ctrl.value = new AbortController() | ||
await fetchEventSource(new URL(options.url, configurationManager.serverUrl).href, { | ||
signal: unref(ctrl).signal, | ||
headers: { | ||
Authorization: `Bearer ${accessToken.value}`, | ||
'Accept-Language': unref(language).current, | ||
'X-Request-ID': uuidV4() | ||
}, | ||
async onopen(response) { | ||
if (response.status === 401) { | ||
unref(ctrl).abort() | ||
return | ||
} | ||
retryCounter.value = 0 | ||
await options.onOpen?.(response) | ||
}, | ||
onmessage(msg) { | ||
options.onMessage?.(msg) | ||
} | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
setupSSE().then(() => { | ||
setupServerSentEvents() | ||
}) | ||
} | ||
|
||
return setupServerSentEvents | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.