diff --git a/src/collaborative/session.ts b/src/collaborative/session.ts index 23be3c6716..358ed1c5a1 100644 --- a/src/collaborative/session.ts +++ b/src/collaborative/session.ts @@ -3,7 +3,7 @@ import { UuidGenerator } from "../helpers"; import { EventBus } from "../helpers/event_bus"; import { debounce, isDefined } from "../helpers/misc"; import { SelectiveHistory as RevisionLog } from "../history/selective_history"; -import { CoreCommand, HistoryChange, UID, WorkbookData } from "../types"; +import { CoreCommand, HistoryChange, Lazy, UID, WorkbookData } from "../types"; import { Client, ClientId, @@ -159,9 +159,9 @@ export class Session extends EventBus { /** * Notify the server that the user client left the collaborative session */ - leave(data: WorkbookData) { + leave(data: Lazy) { if (Object.keys(this.clients).length === 1 && this.processedRevisions.size) { - this.snapshot(data); + this.snapshot(data()); } delete this.clients[this.clientId]; this.transportService.leave(this.clientId); diff --git a/src/model.ts b/src/model.ts index 0f07438e57..f975ad1a25 100644 --- a/src/model.ts +++ b/src/model.ts @@ -3,7 +3,7 @@ import { LocalTransportService } from "./collaborative/local_transport_service"; import { Session } from "./collaborative/session"; import { DEFAULT_REVISION_ID } from "./constants"; import { EventBus } from "./helpers/event_bus"; -import { deepCopy, UuidGenerator } from "./helpers/index"; +import { deepCopy, lazy, UuidGenerator } from "./helpers/index"; import { buildRevisionLog } from "./history/factory"; import { createEmptyExcelWorkbookData, @@ -289,7 +289,7 @@ export class Model extends EventBus implements CommandDispatcher { } leaveSession() { - this.session.leave(this.exportData()); + this.session.leave(lazy(() => this.exportData())); } private setupUiPlugin(Plugin: UIPluginConstructor) { diff --git a/tests/collaborative/collaborative_session.test.ts b/tests/collaborative/collaborative_session.test.ts index 5b9a1dfaa6..6d58c99cc8 100644 --- a/tests/collaborative/collaborative_session.test.ts +++ b/tests/collaborative/collaborative_session.test.ts @@ -1,6 +1,7 @@ import { Model } from "../../src"; import { Session } from "../../src/collaborative/session"; import { DEBOUNCE_TIME, MESSAGE_VERSION } from "../../src/constants"; +import { lazy } from "../../src/helpers"; import { buildRevisionLog } from "../../src/history/factory"; import { Client, CommandResult, WorkbookData } from "../../src/types"; import { MockTransportService } from "../__mocks__/transport_service"; @@ -50,7 +51,7 @@ describe("Collaborative session", () => { test("local client leaves", () => { const spy = jest.spyOn(transport, "sendMessage"); - session.leave({} as WorkbookData); + session.leave(lazy({} as WorkbookData)); expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith({ type: "CLIENT_LEFT", @@ -71,7 +72,7 @@ describe("Collaborative session", () => { }); const spy = jest.spyOn(transport, "sendMessage"); const data = { sheets: [{}] } as WorkbookData; - session.leave(data); + session.leave(lazy(data)); expect(spy).toHaveBeenCalledWith({ type: "SNAPSHOT", version: MESSAGE_VERSION, @@ -123,7 +124,7 @@ describe("Collaborative session", () => { }); const spy = jest.spyOn(transport, "sendMessage"); const data = { sheets: [{}] } as WorkbookData; - session.leave(data); + session.leave(lazy(data)); expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith({ type: "CLIENT_LEFT", @@ -220,7 +221,7 @@ describe("Collaborative session", () => { test("Leave the session do not crash", () => { session.move({ sheetId: "sheetId", col: 1, row: 2 }); - session.leave({} as WorkbookData); + session.leave(lazy({} as WorkbookData)); jest.advanceTimersByTime(DEBOUNCE_TIME + 100); });