Restore the previous workspace on app start (#1101)
* Restore previous workspace on app start * Guard workspace startup hydration * Extract last workspace selection store
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
createLastWorkspaceSelectionStore,
|
||||
type ActiveWorkspaceSelection,
|
||||
type LastWorkspaceSelectionStorage,
|
||||
} from "./last-workspace-selection";
|
||||
|
||||
class DelayedWorkspaceSelectionStorage implements LastWorkspaceSelectionStorage {
|
||||
private finishRead: (value: string | null) => void = () => {};
|
||||
private readonly pendingRead = new Promise<string | null>((resolve) => {
|
||||
this.finishRead = resolve;
|
||||
});
|
||||
private saved: string | null = null;
|
||||
|
||||
read(): Promise<string | null> {
|
||||
return this.pendingRead;
|
||||
}
|
||||
|
||||
async write(value: string): Promise<void> {
|
||||
this.saved = value;
|
||||
}
|
||||
|
||||
finishHydrationWith(selection: ActiveWorkspaceSelection | null) {
|
||||
this.finishRead(selection ? JSON.stringify(selection) : null);
|
||||
}
|
||||
|
||||
getSavedSelection(): ActiveWorkspaceSelection | null {
|
||||
return this.saved ? (JSON.parse(this.saved) as ActiveWorkspaceSelection) : null;
|
||||
}
|
||||
}
|
||||
|
||||
describe("last workspace selection", () => {
|
||||
it("hydrates the saved workspace selection", async () => {
|
||||
const storage = new DelayedWorkspaceSelectionStorage();
|
||||
const store = createLastWorkspaceSelectionStore(storage);
|
||||
const hydration = store.hydrate();
|
||||
|
||||
storage.finishHydrationWith({ serverId: "server-saved", workspaceId: "workspace-saved" });
|
||||
await hydration;
|
||||
|
||||
expect(store.getSelection()).toEqual({
|
||||
serverId: "server-saved",
|
||||
workspaceId: "workspace-saved",
|
||||
});
|
||||
expect(store.isHydrated()).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps a newer workspace selection when storage hydration finishes late", async () => {
|
||||
const storage = new DelayedWorkspaceSelectionStorage();
|
||||
const store = createLastWorkspaceSelectionStore(storage);
|
||||
const hydration = store.hydrate();
|
||||
|
||||
store.remember({ serverId: "server-new", workspaceId: "workspace-new" });
|
||||
storage.finishHydrationWith({ serverId: "server-old", workspaceId: "workspace-old" });
|
||||
await hydration;
|
||||
|
||||
expect(store.getSelection()).toEqual({
|
||||
serverId: "server-new",
|
||||
workspaceId: "workspace-new",
|
||||
});
|
||||
expect(storage.getSavedSelection()).toEqual({
|
||||
serverId: "server-new",
|
||||
workspaceId: "workspace-new",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user