Skip to content

Commit

Permalink
fix(Networking): Fixes an issue where reconnecting doesn't restart ne…
Browse files Browse the repository at this point in the history
…tRequests. Closes #79
  • Loading branch information
alexanderson1993 committed Nov 4, 2021
1 parent 1640856 commit ed42a80
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
30 changes: 28 additions & 2 deletions client/src/context/useNetRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect} from "react";
import {useCallback, useEffect, useReducer} from "react";
import {
AllRequestNames,
AllRequestParams,
Expand Down Expand Up @@ -56,12 +56,38 @@ export function useNetRequest<
const requestId = stableValueHash({requestName, params});
const data = useSnapshot(netRequestProxy);
const {socket} = useThorium();
const [ready, resetReady] = useReducer(() => ({}), {});
if (!socket) throw new Promise(() => {});

if (!data[requestId] && data[requestId] !== null) {
const setUpRequest = useCallback(() => {
if (!netRequestPromises[requestId]) {
socket.send("netRequest", {requestName, params, requestId});
}
}, [params, requestId, requestName, socket]);

const takeDownRequest = useCallback(() => {
socket.send("netRequestEnd", {requestId});
delete netRequestPromises[requestId];
}, [requestId, socket]);

useEffect(() => {
const handleReady = () => {
resetReady();
delete data[requestId];
};
socket.on("ready", handleReady);
return () => {
socket.off("ready", handleReady);
};
}, [socket, requestId, data]);

useEffect(() => {
setUpRequest();
return () => takeDownRequest();
}, [setUpRequest, takeDownRequest, ready]);

if (!data[requestId] && data[requestId] !== null) {
setUpRequest();
throw new Promise(res => {
netRequestPromises[requestId] = res;
});
Expand Down
24 changes: 24 additions & 0 deletions client/src/hooks/useInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {useEffect, useRef} from "react";

export default function useInterval(
callback: () => void,
delay: number = 1000
) {
const savedCallback = useRef<() => void>(callback);

// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}
8 changes: 8 additions & 0 deletions server/src/classes/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class ServerClient extends BaseClient {
pubsub.unsubscribe(netRequestList[requestId]?.subscriptionId);
}
});

// Set up the whole netSend process for calling input functions
socket.socket.on("message", async data => {
try {
Expand Down Expand Up @@ -234,6 +235,13 @@ export class ServerClient extends BaseClient {
);
}
});

// Send a message to the client indicating that the connection is open
socket.socket.send(
JSON.stringify({
type: "ready",
})
);
}
get cards() {
// TODO Aug 28, 2021 Populate this list with the dynamic list of cards assigned to the client.
Expand Down
1 change: 1 addition & 0 deletions server/src/classes/Plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class BasePlugin extends FSDataStore {
description: string;
_coverImage: string;
get coverImage() {
if (!this._coverImage) return "";
// Allow images from the internet
if (this._coverImage.startsWith("http")) return this._coverImage;
// Allow absolute paths
Expand Down

0 comments on commit ed42a80

Please sign in to comment.