-
Notifications
You must be signed in to change notification settings - Fork 2
Listen for jumpToRule message from nev #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
| import { createContext, MutableRefObject, useContext } from "react"; | ||
| import { NemoWorker } from "../nemoWorker/NemoWorker"; | ||
|
|
||
| export const NemoWorkerContext = createContext<MutableRefObject< | ||
| NemoWorker | undefined | ||
| > | null>(null); | ||
|
|
||
| export function useNemoWorkerRef() { | ||
| return useContext(NemoWorkerContext); | ||
| } |
This file contains hidden or 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,17 @@ | ||
| import { useRef } from "react"; | ||
| import { NemoWorker } from "../nemoWorker/NemoWorker"; | ||
| import { NemoWorkerContext } from "./NemoWorkerContext"; | ||
|
|
||
| interface NemoWorkerProviderProps { | ||
| children: React.ReactNode; | ||
| } | ||
|
|
||
| export function NemoWorkerProvider({ children }: NemoWorkerProviderProps) { | ||
| const workerRef = useRef<NemoWorker | undefined>(undefined); | ||
|
|
||
| return ( | ||
| <NemoWorkerContext.Provider value={workerRef}> | ||
| {children} | ||
| </NemoWorkerContext.Provider> | ||
| ); | ||
| } |
This file contains hidden or 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,97 @@ | ||
| import { useContext, useEffect } from "react"; | ||
| import { NemoSessionIdContext } from "./nemoSessionIdContext"; | ||
|
|
||
| let singletonNevChannel: BroadcastChannel | undefined = undefined; | ||
|
|
||
| function initChannel() { | ||
| if (!singletonNevChannel) { | ||
| singletonNevChannel = new BroadcastChannel("NemoVisualization"); | ||
| } | ||
| } | ||
|
|
||
| export type NevBroadcastChannelHandler<RequestPayload, ResponsePayload> = ( | ||
| payload: RequestPayload, | ||
| ) => Promise<ResponsePayload>; | ||
| type CloseCallback = () => void; | ||
|
|
||
| function addNevListener<RequestPayload, ResponsePayload>( | ||
| nemoSessionId: string, | ||
| eventName: string, | ||
| handler: NevBroadcastChannelHandler<RequestPayload, ResponsePayload>, | ||
| ): CloseCallback { | ||
| // create connection to channel if none exist yet | ||
| // NOTE: We never close the connection since there is no sensible time to do so. | ||
| // During normal operations, once reasoning has been performed, there will always be listeners on the channel. | ||
| initChannel(); | ||
| const bc = singletonNevChannel!; | ||
|
|
||
| const onMessage = async (event: MessageEvent) => { | ||
| // Don't listen on own messages (which might be filtered by default by the channel, I did not check this...). | ||
| if (!!event.data.error || !!event.data.responseType) { | ||
| return; | ||
| } | ||
|
|
||
| // Ignore messages that are not meant for this nemo session. | ||
| if (event.data.nemoId !== nemoSessionId) { | ||
| return; | ||
| } | ||
|
|
||
| const id = event.data.id; | ||
|
|
||
| if (!event.data.queryType || !event.data.payload) { | ||
| bc.postMessage({ | ||
| nemoId: nemoSessionId, | ||
| id, | ||
| error: "Expected an object with queryType and payload.", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const { queryType, payload } = event.data; // data should consist of queryType and payload | ||
|
|
||
| // ignore everything but the target eventName | ||
| if (queryType !== eventName) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const response = await handler(payload); | ||
| // if the handler has a proper response, then we send the response | ||
| // (some handler might just perform an action and don't respond) | ||
| if (response !== undefined) { | ||
| bc.postMessage({ | ||
| nemoId: nemoSessionId, | ||
| id, | ||
| responseType: eventName, | ||
| payload: response, | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| console.error(error); | ||
| bc.postMessage({ | ||
| nemoId: nemoSessionId, | ||
| id, | ||
| error, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| bc.addEventListener("message", onMessage); | ||
|
|
||
| return () => { | ||
| bc.removeEventListener("message", onMessage); | ||
| }; | ||
| } | ||
|
|
||
| export function useNevBroadcastChannelListener<RequestPayload, ResponsePayload>( | ||
| eventName: string, | ||
| handler: NevBroadcastChannelHandler<RequestPayload, ResponsePayload>, | ||
| ) { | ||
| const nemoSessionId = useContext(NemoSessionIdContext); | ||
|
|
||
| useEffect(() => { | ||
| const removeListener = addNevListener(nemoSessionId, eventName, handler); | ||
|
|
||
| return () => removeListener(); | ||
| }, [nemoSessionId, eventName, handler]); | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really something we want to do? I'd find that pretty intrusive, when something like a flash message would suffice for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, well, this happens while the Nev tab is in foreground, doesn't it? Still, perhaps better to just prefix the existing title instead of overriding it completely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we were trying to find reliable ways to indicate to a user that they should switch the tab and to highlight which tab is even the relevant one. I'm also not quite happy with what we have but I also do not have a good solution in mind 😅
Prefixing instead of completely changing the title sounds better though. I'll change that :)