-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial frontend SSE implementation (WIP)
- Current assignment state will be send on connect - Changes will not yet be send to event source
- Loading branch information
1 parent
310329f
commit d9ab32c
Showing
6 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
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,34 @@ | ||
import { ServerResponse } from "http"; | ||
import ActiveEnvironmentTracker from "../trackers/ActiveEnvironmentTracker"; | ||
|
||
export default class TrackerSSEHandler { | ||
private static clients: Map<string, ServerResponse> = new Map(); | ||
|
||
public static clientAlreadyConnected(username: string): boolean { | ||
return this.clients.has(username); | ||
} | ||
|
||
public static addClient(client: ServerResponse, username: string): void { | ||
this.forceCloseStaleConnection(username); | ||
this.clients.set(username, client); | ||
} | ||
|
||
public static removeClient(username: string): void { | ||
this.clients.delete(username); | ||
} | ||
|
||
public static sendInitialActivityData(client: ServerResponse): void { | ||
const data = JSON.stringify( | ||
Object.fromEntries(ActiveEnvironmentTracker.getActivityMap()), | ||
); | ||
|
||
client.write(`data: ${data}\n\n`); | ||
} | ||
|
||
public static forceCloseStaleConnection(username: string): void { | ||
if (this.clientAlreadyConnected(username)) { | ||
const client = this.clients.get(username); | ||
client?.end(); | ||
} | ||
} | ||
} |
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,71 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { fetchEventSource } from "@microsoft/fetch-event-source"; | ||
import { APIBasePath } from "../api/Request"; | ||
import { useAuthStore } from "../stores/authStore"; | ||
|
||
const ActiveEnvironmentTracker = (): JSX.Element => { | ||
const abortControllerRef = useRef(new AbortController()); | ||
|
||
useEffect(() => { | ||
const abortController = new AbortController(); | ||
abortControllerRef.current = abortController; | ||
|
||
const initSSE = async () => { | ||
try { | ||
console.log("initSSE"); | ||
await fetchEventSource(`${APIBasePath}/admin/environments`, { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "text/event-stream", | ||
Authorization: useAuthStore.getState().token, | ||
}, | ||
onmessage(ev) { | ||
console.log("SSE message"); | ||
console.log(ev.data); | ||
}, | ||
onclose() { | ||
console.log("SSE closed"); | ||
stopResponseSSE(); | ||
}, | ||
async onopen(response) { | ||
console.log(response); | ||
if (response.ok) { | ||
console.log("SSE open"); | ||
} else { | ||
console.log("SSE failed to open"); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
onerror(ev) { | ||
console.error(ev); | ||
throw new Error("SSE error"); | ||
}, | ||
signal: abortController.signal, | ||
}); | ||
} catch (error) { | ||
console.log("Error initializing SSE:", error); | ||
} | ||
}; | ||
|
||
initSSE().catch((error) => { | ||
console.log(error); | ||
}); | ||
|
||
return () => { | ||
console.log("Cleanup"); | ||
stopResponseSSE(); | ||
}; | ||
}, []); | ||
|
||
function stopResponseSSE() { | ||
if (abortControllerRef.current) { | ||
abortControllerRef.current.abort(); | ||
console.log("SSE connection aborted"); | ||
} | ||
abortControllerRef.current = new AbortController(); | ||
} | ||
|
||
return <p>Hello</p>; | ||
}; | ||
|
||
export default ActiveEnvironmentTracker; |
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.