Skip to content

Commit

Permalink
Remove websocket functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight committed Sep 15, 2024
1 parent 4bf3a29 commit c62efc3
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 328 deletions.
27 changes: 2 additions & 25 deletions examples/test-app/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ export function Navbar({ connecting }: { connecting: boolean }): ReactNode {
const connectionInfo = useMemo(() => getConnectionInfo(), []);

const [inputClientUrl, setInputClientUrl] = useState(connectionInfo.url);
const [inputClientType, setInputClientType] = useState(connectionInfo.type);

const onSave = useCallback(
(e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
const newConnectionInfo = { url: inputClientUrl, type: inputClientType };
const newConnectionInfo = { url: inputClientUrl, type: "iframe" };
localStorage.setItem(
"clientConnectionInfo",
JSON.stringify(newConnectionInfo)
);
window.location.reload();
},
[inputClientUrl, inputClientType]
[inputClientUrl]
);

return (
Expand Down Expand Up @@ -72,28 +71,6 @@ export function Navbar({ connecting }: { connecting: boolean }): ReactNode {
value={inputClientUrl}
onChange={(e) => setInputClientUrl(e.target.value)}
/>
<div className="flex items-center gap-2">
<label className="flex items-center">
<input
type="radio"
value="iframe"
checked={inputClientType === "iframe"}
onChange={() => setInputClientType("iframe")}
className="radio radio-primary radio-xs mr-2"
/>
<span className="label-text">IFrame</span>
</label>
<label className="flex items-center">
<input
type="radio"
value="websocket"
checked={inputClientType === "websocket"}
onChange={() => setInputClientType("websocket")}
className="radio radio-primary radio-xs mr-2"
/>
<span className="label-text">WebSocket</span>
</label>
</div>
<button className="btn btn-sm btn-primary" onClick={onSave}>
Save
</button>
Expand Down
58 changes: 4 additions & 54 deletions examples/test-app/src/hooks/useParcnetClient.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
connect,
connectWebsocket,
ParcnetAPI,
Zapp
} from "@parcnet-js/app-connector";
import { connect, ParcnetAPI, Zapp } from "@parcnet-js/app-connector";
import {
createContext,
ReactNode,
Expand All @@ -15,7 +10,7 @@ import {

export type ClientConnectionInfo = {
url: string;
type: "iframe" | "websocket";
type: "iframe";
};

export enum ClientConnectionState {
Expand All @@ -31,24 +26,15 @@ export const ParcnetClientContext = createContext<ClientState>({
type ClientIframeState =
| {
state: ClientConnectionState.CONNECTING;
ref: React.RefObject<HTMLDivElement>;
ref: React.RefObject<HTMLDivElement> | null;
}
| {
state: ClientConnectionState.CONNECTED;
z: ParcnetAPI;
ref: React.RefObject<HTMLDivElement>;
};

type ClientWebsocketState =
| {
state: ClientConnectionState.CONNECTING;
}
| {
state: ClientConnectionState.CONNECTED;
z: ParcnetAPI;
};

type ClientState = ClientIframeState | ClientWebsocketState;
type ClientState = ClientIframeState;

export function ParcnetClientProvider({
zapp,
Expand All @@ -65,12 +51,6 @@ export function ParcnetClientProvider({
{children}
</ParcnetIframeProvider>
);
} else {
return (
<ParcnetWebsocketProvider zapp={zapp} url={connectionInfo.url}>
{children}
</ParcnetWebsocketProvider>
);
}
}

Expand Down Expand Up @@ -111,36 +91,6 @@ export function ParcnetIframeProvider({
);
}

function ParcnetWebsocketProvider({
zapp,
url,
children
}: {
zapp: Zapp;
url: string;
children: React.ReactNode;
}): ReactNode {
const [value, setValue] = useState<ClientWebsocketState>({
state: ClientConnectionState.CONNECTING
});

useEffect(() => {
void connectWebsocket(zapp, url).then((api) => {
setValue({
state: ClientConnectionState.CONNECTED,
z: api
});
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<ParcnetClientContext.Provider value={value}>
{children}
</ParcnetClientContext.Provider>
);
}

type UseParcnetClient =
| {
connected: true;
Expand Down
8 changes: 3 additions & 5 deletions examples/test-app/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export function cn(...classes: string[]): string {

export const DEFAULT_CONNECTION_INFO: ClientConnectionInfo = {
url: process.env.CLIENT_URL ?? "https://staging-rob.zupass.org",
type: (["iframe", "websocket"].includes(`${process.env.CLIENT_TYPE}`)
? process.env.CLIENT_TYPE
: "iframe") as "iframe" | "websocket"
type: "iframe"
};

export function getConnectionInfo(): ClientConnectionInfo {
Expand All @@ -20,8 +18,8 @@ export function getConnectionInfo(): ClientConnectionInfo {
storedConnectionInfo
) as ClientConnectionInfo;
if (
["iframe", "websocket"].includes(connectionInfo.type) &&
typeof connectionInfo.url === "string"
parsedConnectionInfo.type === "iframe" &&
typeof parsedConnectionInfo.url === "string"
) {
connectionInfo = parsedConnectionInfo;
}
Expand Down
57 changes: 0 additions & 57 deletions packages/app-connector/src/adapters/websocket.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/app-connector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./adapters/iframe.js";
export * from "./adapters/websocket.js";
export * from "./api_wrapper.js";
export * from "./rpc_client.js";
export * from "./utils.js";
Expand Down
7 changes: 2 additions & 5 deletions packages/app-connector/src/rpc_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,11 @@ export class ParcnetRPCConnector implements ParcnetRPC, ParcnetEvents {
args: v.InferInput<F["input"]>,
functionSchema: F
): Promise<v.InferOutput<F["output"]>> {
const result = this.#invoke(fn, v.parse(functionSchema.input, args));
const result = await this.#invoke(fn, v.parse(functionSchema.input, args));
// Ensure that the result is of the expected type
const parsedResult = v.safeParse(functionSchema.output, result);
if (parsedResult.success) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result = await parsedResult.output;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result;
return parsedResult.output;
} else {
throw new Error(
`Failed to parse result for ${fn}: ${parsedResult.issues.map((issue) => issue.message).join(", ")}`
Expand Down
10 changes: 6 additions & 4 deletions packages/app-connector/test/parcnet-connector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
import * as p from "@parcnet-js/podspec";
import { POD } from "@pcd/pod";
import crypto from "crypto";
import * as v from "valibot";
import { assert, describe, expect, it } from "vitest";
import { ZodError } from "zod";
import { postRPCMessage } from "../src/index.js";
import { ParcnetRPCConnector } from "../src/rpc_client.js";
import { connectedClient, mockDialog } from "./utils.js";
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("parcnet-client should work", function () {
const { chan, client } = await connectedClient();

chan.port1.onmessage = (event): void => {
const message = RPCMessageSchema.parse(event.data);
const message = v.parse(RPCMessageSchema, event.data);
assert(message.type === RPCMessageType.PARCNET_CLIENT_INVOKE);
assert(message.fn === "identity.getSemaphoreV3Commitment");
assert(message.args.length === 0);
Expand All @@ -76,7 +76,7 @@ describe("parcnet-client should work", function () {
const { chan, client } = await connectedClient();

chan.port1.onmessage = (event): void => {
const message = RPCMessageSchema.parse(event.data);
const message = v.parse(RPCMessageSchema, event.data);
assert(message.type === RPCMessageType.PARCNET_CLIENT_INVOKE);
assert(message.fn === "identity.getSemaphoreV3Commitment");
assert(message.args.length === 0);
Expand All @@ -91,7 +91,9 @@ describe("parcnet-client should work", function () {
result: "INCORRECT" // Not a BigInt
});

await expect(promise).rejects.toThrow(ZodError);
await expect(promise).rejects.toThrow(
`Failed to parse result for identity.getSemaphoreV3Commitment: Invalid type: Expected bigint but received "INCORRECT"`
);
});

it("should notify when a subscription is updated", async function () {
Expand Down
10 changes: 0 additions & 10 deletions packages/client-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
"types": "./src/connection/iframe.ts",
"import": "./dist/connection/iframe.js",
"require": "./dist/connection/iframe.cjs"
},
"./connection/websocket": {
"types": "./src/connection/websocket.ts",
"import": "./dist/connection/websocket.js",
"require": "./dist/connection/websocket.cjs"
}
},
"scripts": {
Expand Down Expand Up @@ -59,11 +54,6 @@
"types": "./dist/connection/iframe.d.ts",
"import": "./dist/connection/iframe.js",
"require": "./dist/connection/iframe.cjs"
},
"./connection/websocket": {
"types": "./dist/connection/websocket.d.ts",
"import": "./dist/connection/websocket.js",
"require": "./dist/connection/websocket.cjs"
}
}
}
Expand Down
Loading

0 comments on commit c62efc3

Please sign in to comment.