Skip to content

Commit

Permalink
feat: notification wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Nov 9, 2020
1 parent 937ddd9 commit 8e759b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/containers/Inspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import TransportDropdown from "../components/TransportDropdown";
import useTransport, { ITransport, IWebTransport, TTransport } from "../hooks/useTransport";
import JSONRPCLogger, { JSONRPCLog } from "@open-rpc/logs-react";
import OptionsEditor from "./OptionsEditor";
import { JSONRPCMessage } from "@open-rpc/client-js/build/ClientInterface";

const defaultTransports: ITransport[] = [
{
Expand Down Expand Up @@ -200,7 +201,7 @@ const Inspector: React.FC<IProps> = (props) => {
if (selectedTransport !== undefined) {
setTransport(selectedTransport!);
const s: IWebTransport = selectedTransport as IWebTransport;
if (s.schema && s.schema?.examples) {
if (s.schema && typeof s.schema === "object") {
setTransportOptions(s.schema.examples[0]);
}
}
Expand Down Expand Up @@ -233,6 +234,24 @@ const Inspector: React.FC<IProps> = (props) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.url]);

useEffect(() => {
if (transport) {
transport.subscribe("notification", (notification: any) => {
const responseTimestamp = new Date();
const notificationObj: JSONRPCLog = {
type: "response",
notification: true,
method: notification.method,
timestamp: responseTimestamp,
payload: notification,
};
setLogs((prevLogs) => [...prevLogs, notificationObj]);
setTabLogs(tabIndex, [...(tabs[tabIndex].logs || []), notificationObj]);
});
}

}, [transport]);

const handlePlayButton = async () => {
let requestTimestamp = new Date();
if (transport) {
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ const useTransport: TUseTransport = (transports, url, defaultTransportType, tran
return;
}
const doSetTransport = async () => {
if (transport) {
transport.unsubscribe();
transport.close();
}
const localTransport = await getTransportFromType(url, transports, transportType, transportOptions);
return localTransport.connect().then(() => {
setTransportConnected(true);
Expand Down

0 comments on commit 8e759b1

Please sign in to comment.