Skip to content

Commit ca18646

Browse files
committed
Fix pulling and workspace data update
1 parent b4bc684 commit ca18646

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const WorkspacesPage: FC = () => {
1717
const query = filter !== null ? filter : workspaceFilterQuery.me
1818

1919
send({
20-
type: "SET_FILTER",
20+
type: "GET_WORKSPACES",
2121
query,
2222
})
2323
}, [searchParams, send])

site/src/xServices/workspaces/workspacesXService.ts

Lines changed: 88 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ interface WorkspaceItemContext {
1616
updatedTemplate?: TypesGen.Template
1717
}
1818

19-
type WorkspaceItemEvent = {
20-
type: "UPDATE_VERSION"
21-
}
19+
type WorkspaceItemEvent =
20+
| {
21+
type: "UPDATE_VERSION"
22+
}
23+
| {
24+
type: "UPDATE_DATA"
25+
data: TypesGen.Workspace
26+
}
2227

2328
export const workspaceItemMachine = createMachine(
2429
{
@@ -40,6 +45,7 @@ export const workspaceItemMachine = createMachine(
4045
},
4146
tsTypes: {} as import("./workspacesXService.typegen").Typegen0,
4247
type: "parallel",
48+
4349
states: {
4450
updateVersion: {
4551
initial: "idle",
@@ -53,6 +59,9 @@ export const workspaceItemMachine = createMachine(
5359
// don't need to display an extra spinner.
5460
actions: ["assignQueuedStatus", "displayUpdatingVersionMessage"],
5561
},
62+
UPDATE_DATA: {
63+
actions: "assignUpdatedData",
64+
},
5665
},
5766
},
5867
gettingUpdatedTemplate: {
@@ -64,7 +73,7 @@ export const workspaceItemMachine = createMachine(
6473
target: "restartingWorkspace",
6574
},
6675
onError: {
67-
target: "error",
76+
target: "idle",
6877
actions: "displayUpdateVersionError",
6978
},
7079
},
@@ -78,7 +87,7 @@ export const workspaceItemMachine = createMachine(
7887
target: "waitingToBeUpdated",
7988
},
8089
onError: {
81-
target: "error",
90+
target: "idle",
8291
actions: "displayUpdateVersionError",
8392
},
8493
},
@@ -99,18 +108,12 @@ export const workspaceItemMachine = createMachine(
99108
actions: ["assignUpdatedData"],
100109
},
101110
{
102-
target: "success",
111+
target: "idle",
103112
actions: ["assignUpdatedData", "displayUpdatedSuccessMessage"],
104113
},
105114
],
106115
},
107116
},
108-
error: {
109-
type: "final",
110-
},
111-
success: {
112-
type: "final",
113-
},
114117
},
115118
},
116119
},
@@ -187,7 +190,7 @@ interface WorkspacesContext {
187190
getWorkspacesError?: Error | unknown
188191
}
189192

190-
type WorkspacesEvent = { type: "SET_FILTER"; query: string } | { type: "UPDATE_VERSION"; workspaceId: string }
193+
type WorkspacesEvent = { type: "GET_WORKSPACES"; query: string } | { type: "UPDATE_VERSION"; workspaceId: string }
191194

192195
export const workspacesMachine = createMachine(
193196
{
@@ -201,42 +204,54 @@ export const workspacesMachine = createMachine(
201204
}
202205
},
203206
},
204-
id: "workspaceState",
205-
initial: "ready",
206-
states: {
207-
ready: {
208-
on: {
209-
SET_FILTER: "extractingFilter",
210-
UPDATE_VERSION: {
211-
actions: "triggerUpdateVersion",
212-
},
213-
},
207+
id: "workspacesState",
208+
on: {
209+
GET_WORKSPACES: {
210+
actions: "assignFilter",
211+
target: "gettingWorkspaces",
214212
},
215-
extractingFilter: {
216-
entry: "assignFilter",
217-
always: {
218-
target: "gettingWorkspaces",
219-
},
213+
UPDATE_VERSION: {
214+
actions: "triggerUpdateVersion",
215+
},
216+
},
217+
initial: "idle",
218+
states: {
219+
idle: {
220+
tags: ["loading"],
220221
},
221222
gettingWorkspaces: {
222223
entry: "clearGetWorkspacesError",
223224
invoke: {
224225
src: "getWorkspaces",
225226
id: "getWorkspaces",
226-
onDone: {
227-
target: "ready",
228-
actions: ["assignWorkspaceRefs", "clearGetWorkspacesError"],
229-
},
227+
onDone: [
228+
{
229+
target: "waitToRefreshWorkspaces",
230+
actions: ["assignWorkspaceRefs"],
231+
cond: "isEmpty",
232+
},
233+
{
234+
target: "waitToRefreshWorkspaces",
235+
actions: ["updateWorkspaceRefs"],
236+
},
237+
],
230238
onError: {
231-
target: "ready",
232-
actions: ["assignGetWorkspacesError", "clearWorkspaces"],
239+
target: "waitToRefreshWorkspaces",
240+
actions: ["assignGetWorkspacesError"],
233241
},
234242
},
235-
tags: "loading",
243+
},
244+
waitToRefreshWorkspaces: {
245+
after: {
246+
5000: "gettingWorkspaces",
247+
},
236248
},
237249
},
238250
},
239251
{
252+
guards: {
253+
isEmpty: (context) => !context.workspaceRefs,
254+
},
240255
actions: {
241256
assignWorkspaceRefs: assign({
242257
workspaceRefs: (_, event) =>
@@ -251,7 +266,6 @@ export const workspacesMachine = createMachine(
251266
getWorkspacesError: (_, event) => event.data,
252267
}),
253268
clearGetWorkspacesError: (context) => assign({ ...context, getWorkspacesError: undefined }),
254-
clearWorkspaces: (context) => assign({ ...context, workspaces: undefined }),
255269
triggerUpdateVersion: (context, event) => {
256270
const workspaceRef = context.workspaceRefs?.find((ref) => ref.id === event.workspaceId)
257271

@@ -261,6 +275,44 @@ export const workspacesMachine = createMachine(
261275

262276
workspaceRef.send("UPDATE_VERSION")
263277
},
278+
updateWorkspaceRefs: assign({
279+
workspaceRefs: (context, event) => {
280+
let workspaceRefs = context.workspaceRefs
281+
282+
if (!workspaceRefs) {
283+
throw new Error("No workspaces loaded.")
284+
}
285+
286+
// Update the existent workspaces or create the new ones
287+
for (const data of event.data) {
288+
const ref = workspaceRefs.find((ref) => ref.id === data.id)
289+
290+
if (!ref) {
291+
workspaceRefs.push(spawn(workspaceItemMachine.withContext({ data }), data.id))
292+
} else {
293+
ref.send({ type: "UPDATE_DATA", data })
294+
}
295+
}
296+
297+
// Remove workspaces that were deleted
298+
for (const ref of workspaceRefs) {
299+
const refData = event.data.find((workspaceData) => workspaceData.id === ref.id)
300+
301+
// If there is no refData, it is because the workspace was deleted
302+
if (!refData) {
303+
// Stop the actor before remove it from the array
304+
if (ref.stop) {
305+
ref.stop()
306+
}
307+
308+
// Remove ref from the array
309+
workspaceRefs = workspaceRefs.filter((oldRef) => oldRef.id === ref.id)
310+
}
311+
}
312+
313+
return workspaceRefs
314+
},
315+
}),
264316
},
265317
services: {
266318
getWorkspaces: (context) => API.getWorkspaces(workspaceQueryToFilter(context.filter)),

0 commit comments

Comments
 (0)