Skip to content

Revert "fix: Optimistically update the UI when a workspace action is triggered" #4912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ afterAll(() => {
})

describe("WorkspacePage", () => {
it("requests a stop job when the user presses Stop", async () => {
const stopWorkspaceMock = jest
.spyOn(api, "stopWorkspace")
.mockResolvedValueOnce(MockWorkspaceBuild)
testButton(
t("actionButton.stop", { ns: "workspacePage" }),
stopWorkspaceMock,
)
})

it("requests a delete job when the user presses Delete and confirms", async () => {
const user = userEvent.setup()
const deleteWorkspaceMock = jest
Expand Down Expand Up @@ -130,23 +140,11 @@ describe("WorkspacePage", () => {
const startWorkspaceMock = jest
.spyOn(api, "startWorkspace")
.mockImplementation(() => Promise.resolve(MockWorkspaceBuild))
await testButton(
testButton(
t("actionButton.start", { ns: "workspacePage" }),
startWorkspaceMock,
)
})

it("requests a stop job when the user presses Stop", async () => {
const stopWorkspaceMock = jest
.spyOn(api, "stopWorkspace")
.mockResolvedValueOnce(MockWorkspaceBuild)

await testButton(
t("actionButton.stop", { ns: "workspacePage" }),
stopWorkspaceMock,
)
})

it("requests cancellation when the user presses Cancel", async () => {
server.use(
rest.get(
Expand Down
75 changes: 7 additions & 68 deletions site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,6 @@ const moreBuildsAvailable = (
return event.data.latest_build.updated_at !== latestBuildInTimeline.updated_at
}

const updateWorkspaceStatus = (
status: TypesGen.WorkspaceStatus,
workspace?: TypesGen.Workspace,
) => {
if (!workspace) {
throw new Error("Workspace not defined")
}

return {
...workspace,
latest_build: {
...workspace.latest_build,
status,
},
}
}

const isUpdated = (newDateStr: string, oldDateStr: string): boolean => {
return new Date(oldDateStr).getTime() - new Date(newDateStr).getTime() > 0
}

const Language = {
getTemplateWarning:
"Error updating workspace: latest template could not be fetched.",
Expand Down Expand Up @@ -273,7 +252,6 @@ export const workspaceMachine = createMachine(
on: {
REFRESH_WORKSPACE: {
actions: ["refreshWorkspace"],
cond: "hasUpdates",
},
EVENT_SOURCE_ERROR: {
target: "error",
Expand Down Expand Up @@ -347,7 +325,7 @@ export const workspaceMachine = createMachine(
},
},
requestingStart: {
entry: ["clearBuildError", "updateStatusToStarting"],
entry: "clearBuildError",
invoke: {
src: "startWorkspace",
id: "startWorkspace",
Expand All @@ -366,7 +344,7 @@ export const workspaceMachine = createMachine(
},
},
requestingStop: {
entry: ["clearBuildError", "updateStatusToStopping"],
entry: "clearBuildError",
invoke: {
src: "stopWorkspace",
id: "stopWorkspace",
Expand All @@ -385,7 +363,7 @@ export const workspaceMachine = createMachine(
},
},
requestingDelete: {
entry: ["clearBuildError", "updateStatusToDeleting"],
entry: "clearBuildError",
invoke: {
src: "deleteWorkspace",
id: "deleteWorkspace",
Expand All @@ -404,11 +382,7 @@ export const workspaceMachine = createMachine(
},
},
requestingCancel: {
entry: [
"clearCancellationMessage",
"clearCancellationError",
"updateStatusToCanceling",
],
entry: ["clearCancellationMessage", "clearCancellationError"],
invoke: {
src: "cancelWorkspace",
id: "cancelWorkspace",
Expand Down Expand Up @@ -456,7 +430,9 @@ export const workspaceMachine = createMachine(
on: {
REFRESH_TIMELINE: {
target: "#workspaceState.ready.timeline.gettingBuilds",
cond: "moreBuildsAvailable",
cond: {
type: "moreBuildsAvailable",
},
},
},
},
Expand Down Expand Up @@ -623,46 +599,9 @@ export const workspaceMachine = createMachine(
}),
{ to: "scheduleBannerMachine" },
),
// Optimistically updates. So when the user clicks on stop, we can show
// the "stopping" state right away without having to wait 0.5s ~ 2s to
// display the visual feedback to the user.
updateStatusToStarting: assign({
workspace: ({ workspace }) =>
updateWorkspaceStatus("starting", workspace),
}),
updateStatusToStopping: assign({
workspace: ({ workspace }) =>
updateWorkspaceStatus("stopping", workspace),
}),
updateStatusToDeleting: assign({
workspace: ({ workspace }) =>
updateWorkspaceStatus("deleting", workspace),
}),
updateStatusToCanceling: assign({
workspace: ({ workspace }) =>
updateWorkspaceStatus("canceling", workspace),
}),
},
guards: {
moreBuildsAvailable,
// We only want to update the workspace when there are changes to it to
// avoid re-renderings and allow optimistically updates to improve the UI.
// When updating the workspace every second, the optimistic updates that
// were applied before get lost since it will be rewrite.
hasUpdates: ({ workspace }, event: { data: TypesGen.Workspace }) => {
if (!workspace) {
throw new Error("Workspace not defined")
}
const isWorkspaceUpdated = isUpdated(
event.data.updated_at,
workspace.updated_at,
)
const isBuildUpdated = isUpdated(
event.data.latest_build.updated_at,
workspace.latest_build.updated_at,
)
return isWorkspaceUpdated || isBuildUpdated
},
},
services: {
getWorkspace: async (_, event) => {
Expand Down