Skip to content

Commit

Permalink
React: Rename pausing of websocket to stopping (#28416)
Browse files Browse the repository at this point in the history
See parent PR, this one renames pause and resume to stop and restart (especially "restart" will better match `request_manager` and `local_state`).

GitOrigin-RevId: c61f3a52e1be9b572882b468bd83e2fc1122bf8d
  • Loading branch information
xixixao authored and Convex, Inc. committed Jul 30, 2024
1 parent 8394e21 commit bcdc4ec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
22 changes: 11 additions & 11 deletions npm-packages/convex/src/browser/sync/authentication_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export class AuthenticationManager {
private readonly syncState: LocalSyncState;
// Passed down by BaseClient, sends a message to the server
private readonly authenticate: (token: string) => void;
private readonly pauseSocket: () => Promise<void>;
private readonly resumeSocket: () => void;
private readonly stopSocket: () => Promise<void>;
private readonly restartSocket: () => void;
// Passed down by BaseClient, sends a message to the server
private readonly clearAuth: () => void;
private readonly verbose: boolean;
Expand All @@ -93,22 +93,22 @@ export class AuthenticationManager {
syncState: LocalSyncState,
{
authenticate,
pauseSocket: pause,
resumeSocket: resume,
stopSocket,
restartSocket,
clearAuth,
verbose,
}: {
authenticate: (token: string) => void;
pauseSocket: () => Promise<void>;
resumeSocket: () => void;
stopSocket: () => Promise<void>;
restartSocket: () => void;
clearAuth: () => void;
verbose: boolean;
},
) {
this.syncState = syncState;
this.authenticate = authenticate;
this.pauseSocket = pause;
this.resumeSocket = resume;
this.stopSocket = stopSocket;
this.restartSocket = restartSocket;
this.clearAuth = clearAuth;
this.verbose = verbose;
}
Expand Down Expand Up @@ -216,15 +216,15 @@ export class AuthenticationManager {
return;
}
this._logVerbose("attempting to reauthenticate");
await this.pauseSocket();
await this.stopSocket();
const token = await this.fetchTokenAndGuardAgainstRace(
this.authState.config.fetchToken,
{
forceRefreshToken: true,
},
);
if (token.isFromOutdatedConfig) {
await this.resumeSocket();
await this.restartSocket();
return;
}

Expand All @@ -245,7 +245,7 @@ export class AuthenticationManager {
}
this.setAndReportAuthFailed(this.authState.config.onAuthChange);
}
await this.resumeSocket();
await this.restartSocket();
}

// Force refetch the token and schedule another refetch
Expand Down
4 changes: 2 additions & 2 deletions npm-packages/convex/src/browser/sync/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ export class BaseConvexClient {
const message = this.state.setAuth(token);
this.webSocketManager.sendMessage(message);
},
pauseSocket: () => this.webSocketManager.pause(),
resumeSocket: () => this.webSocketManager.resume(),
stopSocket: () => this.webSocketManager.stop(),
restartSocket: () => this.webSocketManager.restart(),
clearAuth: () => {
this.clearAuth();
},
Expand Down
40 changes: 20 additions & 20 deletions npm-packages/convex/src/browser/sync/web_socket_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CLOSE_NOT_FOUND = 4040;
* - "connecting": We have created the WebSocket and are waiting for the
* `onOpen` callback.
* - "ready": We have an open WebSocket.
* - "paused": The WebSocket was closed and a new one can be created via `.resume()`.
* - "stopped": The WebSocket was closed and a new one can be created via `.restart()`.
* - "terminated": We have closed the WebSocket and will never create a new one.
*
*
Expand All @@ -39,10 +39,10 @@ const CLOSE_NOT_FOUND = 4040;
* terminate() -> terminated
* ready:
* close() -> disconnected
* pause() -> paused
* stop() -> stopped
* terminate() -> terminated
* paused:
* resume() -> connecting
* stopped:
* restart() -> connecting
* terminate() -> terminated
* terminalStates:
* terminated
Expand All @@ -60,14 +60,14 @@ const CLOSE_NOT_FOUND = 4040;
* │ terminate() │ │ │ │
* │ │ │ ▼ │ │
* │ ┌────────────────┐ └───────┌────────────────┐ │
* │ │ paused │───resume()───▶│ connecting │ │
* │ │ stopped │──restart()───▶│ connecting │ │
* │ └────────────────┘ └────────────────┘ │
* │ ▲ │ │
* │ │ onopen │
* │ │ │ │
* │ │ ▼ │
* terminate() │ ┌────────────────┐ │
* │ └────────pause()────────│ ready │──┘
* │ └────────stop()─────────│ ready │──┘
* │ └────────────────┘
* │ │
* │ │
Expand All @@ -78,7 +78,7 @@ type Socket =
| { state: "disconnected" }
| { state: "connecting"; ws: WebSocket }
| { state: "ready"; ws: WebSocket }
| { state: "paused" }
| { state: "stopped" }
| { state: "terminated" };

export type ReconnectMetadata = {
Expand Down Expand Up @@ -157,7 +157,7 @@ export class WebSocketManager {
}
if (
this.socket.state !== "disconnected" &&
this.socket.state !== "paused"
this.socket.state !== "stopped"
) {
throw new Error(
"Didn't start connection from disconnected state: " + this.socket.state,
Expand Down Expand Up @@ -295,7 +295,7 @@ export class WebSocketManager {
switch (this.socket.state) {
case "disconnected":
case "terminated":
case "paused":
case "stopped":
// Nothing to do if we don't have a WebSocket.
return;
case "connecting":
Expand Down Expand Up @@ -325,7 +325,7 @@ export class WebSocketManager {
switch (this.socket.state) {
case "disconnected":
case "terminated":
case "paused":
case "stopped":
// Nothing to do if we don't have a WebSocket.
return Promise.resolve();
case "connecting": {
Expand Down Expand Up @@ -371,7 +371,7 @@ export class WebSocketManager {
}
switch (this.socket.state) {
case "terminated":
case "paused":
case "stopped":
case "disconnected":
case "connecting":
case "ready": {
Expand All @@ -389,17 +389,17 @@ export class WebSocketManager {
}
}

pause(): Promise<void> {
stop(): Promise<void> {
switch (this.socket.state) {
case "terminated":
// If we're terminating we ignore pause
// If we're terminating we ignore stop
return Promise.resolve();
case "connecting":
case "paused":
case "stopped":
case "disconnected":
case "ready": {
const result = this.close();
this.socket = { state: "paused" };
this.socket = { state: "stopped" };
return result;
}
default: {
Expand All @@ -411,20 +411,20 @@ export class WebSocketManager {
}

/**
* Create a new WebSocket after a previous `pause()`, unless `terminate()` was
* Create a new WebSocket after a previous `stop()`, unless `terminate()` was
* called before.
*/
resume(): void {
restart(): void {
switch (this.socket.state) {
case "paused":
case "stopped":
break;
case "terminated":
// If we're terminating we ignore resume
// If we're terminating we ignore restart
return;
case "connecting":
case "ready":
case "disconnected":
throw new Error("`resume()` is only valid after `pause()`");
throw new Error("`restart()` is only valid after `stop()`");
default: {
// Enforce that the switch-case is exhaustive.
const _: never = this.socket;
Expand Down

0 comments on commit bcdc4ec

Please sign in to comment.