From abb81f0ac8668f966c0f5e1801b05a27f82d2f17 Mon Sep 17 00:00:00 2001 From: "Mehdi Rachico (mera)" Date: Thu, 5 Dec 2024 13:59:51 +0000 Subject: [PATCH] [IMP] model: create snapshot when loading xlsx file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, when uploading an XLSX file, the code in the Odoo repository would create a spreadsheet document with data that is encoded in xml. Whenever the file is loaded, we parse the stringified xml to convert it to the o-spreadsheet json structure. The problem is that this conversion can be quite slow and costly when the XLSX file is large. This commit mitigates the issue by saving a snapshot once the XLSX file is loaded for the first time. closes odoo/o-spreadsheet#5446 Task: 4080514 X-original-commit: 4eb819fa8e314b5f10ab5d52a62bc82a33919671 Signed-off-by: Lucas Lefèvre (lul) Signed-off-by: Mehdi Rachico (mera) --- src/model.ts | 2 +- tests/model/model.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/model.ts b/src/model.ts index d785ed5822..a7b41c7635 100644 --- a/src/model.ts +++ b/src/model.ts @@ -284,7 +284,7 @@ export class Model extends EventBus implements CommandDispatcher { this.joinSession(); - if (config.snapshotRequested) { + if (config.snapshotRequested || (data["[Content_Types].xml"] && !this.getters.isReadonly())) { const startSnapshot = performance.now(); console.debug("Snapshot requested"); this.session.snapshot(this.exportData()); diff --git a/tests/model/model.test.ts b/tests/model/model.test.ts index aa42453a68..f72c410423 100644 --- a/tests/model/model.test.ts +++ b/tests/model/model.test.ts @@ -1,9 +1,12 @@ import { CommandResult, CorePlugin } from "../../src"; +import { MESSAGE_VERSION } from "../../src/constants"; import { toZone } from "../../src/helpers"; import { Model, ModelConfig } from "../../src/model"; import { corePluginRegistry, featurePluginRegistry } from "../../src/plugins/index"; import { UIPlugin } from "../../src/plugins/ui_plugin"; import { Command, CommandTypes, CoreCommand, DispatchResult, coreTypes } from "../../src/types"; +import { MockTransportService } from "../__mocks__/transport_service"; +import { getTextXlsxFiles } from "../__xlsx__/read_demo_xlsx"; import { setupCollaborativeEnv } from "../collaborative/collaborative_helpers"; import { copy, selectCell, setCellContent } from "../test_helpers/commands_helpers"; import { @@ -366,4 +369,33 @@ describe("Model", () => { ], }); }); + + test("it should snapshot when importing xlsx file", async () => { + const transport = new MockTransportService(); + const spy = jest.spyOn(transport, "sendMessage"); + const xlsxData = await getTextXlsxFiles(); + new Model(xlsxData, { + transportService: transport, + client: { id: "test", name: "Test" }, + }); + expect(spy).toHaveBeenCalledWith({ + type: "SNAPSHOT", + version: MESSAGE_VERSION, + nextRevisionId: expect.any(String), + serverRevisionId: "START_REVISION", + data: expect.any(Object), + }); + }); + + test("it should not snapshot when importing xlsx file in readonly mode", async () => { + const transport = new MockTransportService(); + const spy = jest.spyOn(transport, "sendMessage"); + const xlsxData = await getTextXlsxFiles(); + new Model(xlsxData, { + transportService: transport, + client: { id: "test", name: "Test" }, + mode: "readonly", + }); + expect(spy).not.toHaveBeenCalled(); + }); });