Skip to content

Commit 7ce2cda

Browse files
BrunoQuaresmaBobyMCbobs
authored andcommitted
Revert "fix: Optimistically update the UI when a workspace action is triggered (coder#4898)" (coder#4912)
This reverts commit 8f4ae5b.
1 parent 2228818 commit 7ce2cda

File tree

2 files changed

+18
-81
lines changed

2 files changed

+18
-81
lines changed

site/src/pages/WorkspacePage/WorkspacePage.test.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ afterAll(() => {
9292
})
9393

9494
describe("WorkspacePage", () => {
95+
it("requests a stop job when the user presses Stop", async () => {
96+
const stopWorkspaceMock = jest
97+
.spyOn(api, "stopWorkspace")
98+
.mockResolvedValueOnce(MockWorkspaceBuild)
99+
testButton(
100+
t("actionButton.stop", { ns: "workspacePage" }),
101+
stopWorkspaceMock,
102+
)
103+
})
104+
95105
it("requests a delete job when the user presses Delete and confirms", async () => {
96106
const user = userEvent.setup()
97107
const deleteWorkspaceMock = jest
@@ -130,23 +140,11 @@ describe("WorkspacePage", () => {
130140
const startWorkspaceMock = jest
131141
.spyOn(api, "startWorkspace")
132142
.mockImplementation(() => Promise.resolve(MockWorkspaceBuild))
133-
await testButton(
143+
testButton(
134144
t("actionButton.start", { ns: "workspacePage" }),
135145
startWorkspaceMock,
136146
)
137147
})
138-
139-
it("requests a stop job when the user presses Stop", async () => {
140-
const stopWorkspaceMock = jest
141-
.spyOn(api, "stopWorkspace")
142-
.mockResolvedValueOnce(MockWorkspaceBuild)
143-
144-
await testButton(
145-
t("actionButton.stop", { ns: "workspacePage" }),
146-
stopWorkspaceMock,
147-
)
148-
})
149-
150148
it("requests cancellation when the user presses Cancel", async () => {
151149
server.use(
152150
rest.get(

site/src/xServices/workspace/workspaceXService.ts

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,6 @@ const moreBuildsAvailable = (
4040
return event.data.latest_build.updated_at !== latestBuildInTimeline.updated_at
4141
}
4242

43-
const updateWorkspaceStatus = (
44-
status: TypesGen.WorkspaceStatus,
45-
workspace?: TypesGen.Workspace,
46-
) => {
47-
if (!workspace) {
48-
throw new Error("Workspace not defined")
49-
}
50-
51-
return {
52-
...workspace,
53-
latest_build: {
54-
...workspace.latest_build,
55-
status,
56-
},
57-
}
58-
}
59-
60-
const isUpdated = (newDateStr: string, oldDateStr: string): boolean => {
61-
return new Date(oldDateStr).getTime() - new Date(newDateStr).getTime() > 0
62-
}
63-
6443
const Language = {
6544
getTemplateWarning:
6645
"Error updating workspace: latest template could not be fetched.",
@@ -273,7 +252,6 @@ export const workspaceMachine = createMachine(
273252
on: {
274253
REFRESH_WORKSPACE: {
275254
actions: ["refreshWorkspace"],
276-
cond: "hasUpdates",
277255
},
278256
EVENT_SOURCE_ERROR: {
279257
target: "error",
@@ -347,7 +325,7 @@ export const workspaceMachine = createMachine(
347325
},
348326
},
349327
requestingStart: {
350-
entry: ["clearBuildError", "updateStatusToStarting"],
328+
entry: "clearBuildError",
351329
invoke: {
352330
src: "startWorkspace",
353331
id: "startWorkspace",
@@ -366,7 +344,7 @@ export const workspaceMachine = createMachine(
366344
},
367345
},
368346
requestingStop: {
369-
entry: ["clearBuildError", "updateStatusToStopping"],
347+
entry: "clearBuildError",
370348
invoke: {
371349
src: "stopWorkspace",
372350
id: "stopWorkspace",
@@ -385,7 +363,7 @@ export const workspaceMachine = createMachine(
385363
},
386364
},
387365
requestingDelete: {
388-
entry: ["clearBuildError", "updateStatusToDeleting"],
366+
entry: "clearBuildError",
389367
invoke: {
390368
src: "deleteWorkspace",
391369
id: "deleteWorkspace",
@@ -404,11 +382,7 @@ export const workspaceMachine = createMachine(
404382
},
405383
},
406384
requestingCancel: {
407-
entry: [
408-
"clearCancellationMessage",
409-
"clearCancellationError",
410-
"updateStatusToCanceling",
411-
],
385+
entry: ["clearCancellationMessage", "clearCancellationError"],
412386
invoke: {
413387
src: "cancelWorkspace",
414388
id: "cancelWorkspace",
@@ -456,7 +430,9 @@ export const workspaceMachine = createMachine(
456430
on: {
457431
REFRESH_TIMELINE: {
458432
target: "#workspaceState.ready.timeline.gettingBuilds",
459-
cond: "moreBuildsAvailable",
433+
cond: {
434+
type: "moreBuildsAvailable",
435+
},
460436
},
461437
},
462438
},
@@ -623,46 +599,9 @@ export const workspaceMachine = createMachine(
623599
}),
624600
{ to: "scheduleBannerMachine" },
625601
),
626-
// Optimistically updates. So when the user clicks on stop, we can show
627-
// the "stopping" state right away without having to wait 0.5s ~ 2s to
628-
// display the visual feedback to the user.
629-
updateStatusToStarting: assign({
630-
workspace: ({ workspace }) =>
631-
updateWorkspaceStatus("starting", workspace),
632-
}),
633-
updateStatusToStopping: assign({
634-
workspace: ({ workspace }) =>
635-
updateWorkspaceStatus("stopping", workspace),
636-
}),
637-
updateStatusToDeleting: assign({
638-
workspace: ({ workspace }) =>
639-
updateWorkspaceStatus("deleting", workspace),
640-
}),
641-
updateStatusToCanceling: assign({
642-
workspace: ({ workspace }) =>
643-
updateWorkspaceStatus("canceling", workspace),
644-
}),
645602
},
646603
guards: {
647604
moreBuildsAvailable,
648-
// We only want to update the workspace when there are changes to it to
649-
// avoid re-renderings and allow optimistically updates to improve the UI.
650-
// When updating the workspace every second, the optimistic updates that
651-
// were applied before get lost since it will be rewrite.
652-
hasUpdates: ({ workspace }, event: { data: TypesGen.Workspace }) => {
653-
if (!workspace) {
654-
throw new Error("Workspace not defined")
655-
}
656-
const isWorkspaceUpdated = isUpdated(
657-
event.data.updated_at,
658-
workspace.updated_at,
659-
)
660-
const isBuildUpdated = isUpdated(
661-
event.data.latest_build.updated_at,
662-
workspace.latest_build.updated_at,
663-
)
664-
return isWorkspaceUpdated || isBuildUpdated
665-
},
666605
},
667606
services: {
668607
getWorkspace: async (_, event) => {

0 commit comments

Comments
 (0)