-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement plugin data streaming using
EventSource
- Loading branch information
Showing
9 changed files
with
154 additions
and
31 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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
asyncio | ||
fastapi | ||
uvicorn[standard] | ||
sse_starlette | ||
uvicorn[standard] |
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,15 @@ | ||
class Flag: | ||
def __init__(self, default: bool = True) -> None: | ||
self._flag = default | ||
|
||
def set(self) -> None: | ||
self._flag = True | ||
|
||
def reset(self) -> None: | ||
self._flag = False | ||
|
||
def get(self) -> bool: | ||
return self._flag | ||
|
||
def __repr__(self) -> str: | ||
return f'Flag({self._flag})' |
32 changes: 20 additions & 12 deletions
32
src/frontend/src/lib/components/plugins/PluginsContainer.svelte
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,49 @@ | ||
type CallbackFunction = (data: any) => void; | ||
let pluginStatusStream = undefined; | ||
|
||
const createEventSource = ( | ||
url: string, | ||
defaultData: object | ||
): { | ||
subscribe: (listener: CallbackFunction) => () => boolean; | ||
fetch: () => object; | ||
close: () => void; | ||
} => { | ||
const eventSource = new EventSource(url); | ||
const listeners: Set<CallbackFunction> = new Set(); | ||
let cachedData: object = defaultData; | ||
|
||
eventSource.onmessage = (event: MessageEvent) => { | ||
cachedData = JSON.parse(event.data); | ||
listeners.forEach((listener) => listener(cachedData)); | ||
}; | ||
|
||
eventSource.onerror = () => { | ||
const errorData = defaultData; | ||
listeners.forEach((listener) => listener(errorData)); | ||
}; | ||
|
||
return { | ||
subscribe: (listener) => { | ||
listeners.add(listener); | ||
return () => listeners.delete(listener); | ||
}, | ||
close: () => { | ||
eventSource.close(); | ||
}, | ||
fetch: () => { | ||
return cachedData; | ||
}, | ||
}; | ||
}; | ||
|
||
export function initEventSources() { | ||
if (pluginStatusStream == undefined) { | ||
pluginStatusStream = createEventSource("/plugins/status", { | ||
ok: false, | ||
plugins: [], | ||
}); | ||
} | ||
} | ||
|
||
export { pluginStatusStream }; |
37 changes: 24 additions & 13 deletions
37
src/frontend/src/lib/components/shared/statusbar/Plugins.svelte
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