Skip to content

Commit

Permalink
[PERF] model: export lazily when leaving
Browse files Browse the repository at this point in the history
Since 7bcff34, the model data is exported every time
the spreadsheet is left.
Exporting the data can be slow though and it's actually not
needed every time.

With this commit, the data is exported lazily, only when
needed.

closes #4852

Task: 4119536
X-original-commit: 9bc489b
Signed-off-by: Pierre Rousseau (pro) <[email protected]>
  • Loading branch information
LucasLefevre committed Aug 19, 2024
1 parent 22625e8 commit f9d6c38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/collaborative/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -165,9 +165,9 @@ export class Session extends EventBus<CollaborativeEvent> {
/**
* Notify the server that the user client left the collaborative session
*/
leave(data: WorkbookData) {
leave(data: Lazy<WorkbookData>) {
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);
Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -303,7 +303,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
}

leaveSession() {
this.session.leave(this.exportData());
this.session.leave(lazy(() => this.exportData()));
}

private setupUiPlugin(Plugin: UIPluginConstructor) {
Expand Down
9 changes: 5 additions & 4 deletions tests/collaborative/collaborative_session.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit f9d6c38

Please sign in to comment.