Skip to content

Commit da96d23

Browse files
committed
chore: swap API calls to OWWS
1 parent bb96c7f commit da96d23

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

site/src/api/api.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import userAgentParser from "ua-parser-js";
2525
import { delay } from "../utils/delay";
2626
import * as TypesGen from "./typesGenerated";
2727
import type { PostWorkspaceUsageRequest } from "./typesGenerated";
28+
import { OneWayWebSocket } from "utils/OneWayWebSocket";
2829

2930
const getMissingParameters = (
3031
oldBuildParameters: TypesGen.WorkspaceBuildParameter[],
@@ -101,26 +102,22 @@ const getMissingParameters = (
101102
};
102103

103104
/**
104-
*
105105
* @param agentId
106-
* @returns An EventSource that emits agent metadata event objects
107-
* (ServerSentEvent)
106+
* @returns A one-way WebSocket that receives agent metadata events
108107
*/
109-
export const watchAgentMetadata = (agentId: string): EventSource => {
110-
return new EventSource(
108+
export const watchAgentMetadata = (agentId: string): OneWayWebSocket => {
109+
return new OneWayWebSocket(
111110
`${location.protocol}//${location.host}/api/v2/workspaceagents/${agentId}/watch-metadata`,
112-
{ withCredentials: true },
113111
);
114112
};
115113

116114
/**
117-
* @returns {EventSource} An EventSource that emits workspace event objects
118-
* (ServerSentEvent)
115+
* @param workspaceId
116+
* @returns A one-way WebSocket that receives workspace events
119117
*/
120-
export const watchWorkspace = (workspaceId: string): EventSource => {
121-
return new EventSource(
118+
export const watchWorkspace = (workspaceId: string): OneWayWebSocket => {
119+
return new OneWayWebSocket(
122120
`${location.protocol}//${location.host}/api/v2/workspaces/${workspaceId}/watch`,
123-
{ withCredentials: true },
124121
);
125122
};
126123

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Should probably bring in MSW to test the communication
3+
*
4+
* Qualities to test:
5+
* 1. When an event (open, close, error, message) triggers, any registered
6+
* callbacks are called with a new event payload
7+
* 2. Lets user remove event listeners, as long as they provide a callback
8+
* 3. Is able to register multiple listeners for the same event type
9+
* 4. Lets user close a connection from the client side
10+
*
11+
* These aren't super high-priority right now, but if the definition ever
12+
* changes, it'd be good to have these as safety nets
13+
*/

0 commit comments

Comments
 (0)