Skip to content

Commit

Permalink
Move session timeout handling into socket
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn committed Feb 23, 2024
1 parent eeb21e0 commit 9bd1aed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
16 changes: 16 additions & 0 deletions packages/protocol/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,22 @@ export async function sendMessage<M extends CommandMethods>(

console.warn("Message failed", method, { code, id, message, params }, data);

switch (code) {
case ProtocolError.UnknownSession:
case ProtocolError.SessionDestroyed: {
// Special type of "global" error; applies to more than just the specific message it is associated with
sessionDestroyedListener();
break;
}
}

let finalMessage = message;
if (process.env.NODE_ENV === "development") {
// Include details on the method and params in the error string so that we get more than
// _just_ "Internal Error" or similar
finalMessage = `${message} (request: ${method}, ${JSON.stringify(params)})`;
}

if (
!noCallerStackTrace &&
!noCallerStackTracesForErrorCodes.has(code) &&
Expand Down Expand Up @@ -361,3 +371,9 @@ if (typeof window === "object") {

(window as any).protocolClient = client;
}

let sessionDestroyedListener = () => {};

export function listenForSessionDestroyed(callback: () => void) {
sessionDestroyedListener = callback;
}
9 changes: 8 additions & 1 deletion src/ui/setup/dynamic/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import * as dbgClient from "devtools/client/debugger/src/client";
import debuggerReducers from "devtools/client/debugger/src/reducers";
import * as inspectorReducers from "devtools/client/inspector/reducers";
// eslint-disable-next-line no-restricted-imports
import { addEventListener, initSocket, client as protocolClient } from "protocol/socket";
import { addEventListener, initSocket } from "protocol/socket";
// eslint-disable-next-line no-restricted-imports
import { listenForSessionDestroyed, client as protocolClient } from "protocol/socket";
import { assert } from "protocol/utils";
import { buildIdCache, parseBuildIdComponents } from "replay-next/src/suspense/BuildIdCache";
import { networkRequestsCache } from "replay-next/src/suspense/NetworkRequestsCache";
Expand All @@ -18,6 +20,7 @@ import { ReplayClientInterface } from "shared/client/types";
import { CONSOLE_SETTINGS_DATABASE, POINTS_DATABASE } from "shared/user-data/IndexedDB/config";
import { IDBOptions } from "shared/user-data/IndexedDB/types";
import { UIStore, actions } from "ui/actions";
import { getDisconnectionError } from "ui/actions/session";
import { selectors } from "ui/reducers";
import app from "ui/reducers/app";
import network from "ui/reducers/network";
Expand Down Expand Up @@ -176,6 +179,10 @@ export default async function setupDevtools(store: AppStore, replayClient: Repla
}
}

listenForSessionDestroyed(() => {
store.dispatch(actions.setExpectedError(getDisconnectionError()));
});

addEventListener("Recording.uploadedData", (data: uploadedData) =>
store.dispatch(actions.onUploadedData(data))
);
Expand Down
20 changes: 0 additions & 20 deletions src/ui/setup/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// eslint-disable-next-line no-restricted-imports
import { client, sendMessage, triggerEvent } from "protocol/socket";
import { GraphQLService, userData } from "shared/user-data/GraphQL/UserData";
import { ProtocolError, isCommandError } from "shared/utils/error";
import { getRecordingId } from "shared/utils/recording";
import { UIStore } from "ui/actions";
import { setExpectedError } from "ui/actions/errors";
import { getDisconnectionError } from "ui/actions/session";
import { getProtocolError } from "ui/reducers/protocolMessages";

import { ReplaySession, getReplaySession } from "./prefs";

Expand All @@ -32,22 +28,6 @@ export async function setupAppHelper(store: UIStore) {
const recordingId = getRecordingId();
const replaySession = recordingId ? await getReplaySession(recordingId) : undefined;

window.addEventListener("unhandledrejection", function (event) {
const { reason } = event;

if (
reason &&
(isCommandError(reason, ProtocolError.UnknownSession) ||
isCommandError(reason, ProtocolError.SessionDestroyed))
) {
store.dispatch(setExpectedError(getDisconnectionError()));

return true;
}

return false;
});

window.app = {
store,
preferences: userData,
Expand Down

0 comments on commit 9bd1aed

Please sign in to comment.