Skip to content

Commit

Permalink
[IMP] *: use deepCopy instead of JSON.parse(JSON.stringify)
Browse files Browse the repository at this point in the history
closes #2340

Task: 3272878
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
hokolomopo authored and rrahir committed Apr 19, 2023
1 parent 997ccc6 commit 300596b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/migrations/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
FORBIDDEN_IN_EXCEL_REGEX,
FORMULA_REF_IDENTIFIER,
} from "../constants";
import { getItemId, toXC, toZone, UuidGenerator } from "../helpers/index";
import { deepCopy, getItemId, toXC, toZone, UuidGenerator } from "../helpers/index";
import { StateUpdateMessage } from "../types/collaborative/transport_service";
import {
CoreCommand,
Expand Down Expand Up @@ -46,7 +46,7 @@ export function load(data?: any, verboseImport?: boolean): WorkbookData {
}
}
}
data = JSON.parse(JSON.stringify(data));
data = deepCopy(data);

// apply migrations, if needed
if ("version" in data) {
Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
}
}
data.revisionId = this.session.getRevisionId() || DEFAULT_REVISION_ID;
data = JSON.parse(JSON.stringify(data));
data = deepCopy(data);
return data;
}

Expand Down Expand Up @@ -533,7 +533,7 @@ export class Model extends EventBus<any> implements CommandDispatcher {
handler.exportForExcel(data);
}
}
data = JSON.parse(JSON.stringify(data));
data = deepCopy(data);

return getXLSX(data);
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/core/sheet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FORBIDDEN_IN_EXCEL_REGEX } from "../../constants";
import {
createDefaultRows,
deepCopy,
getUnquotedSheetName,
groupConsecutive,
isDefined,
Expand Down Expand Up @@ -686,7 +687,7 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
private duplicateSheet(fromId: UID, toId: UID) {
const sheet = this.getSheet(fromId);
const toName = this.getDuplicateSheetName(sheet.name);
const newSheet: Sheet = JSON.parse(JSON.stringify(sheet));
const newSheet: Sheet = deepCopy(sheet);
newSheet.id = toId;
newSheet.name = toName;
for (let col = 0; col <= newSheet.numberOfCols; col++) {
Expand Down
5 changes: 3 additions & 2 deletions tests/__mocks__/transport_service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DEFAULT_REVISION_ID } from "../../src/constants";
import { deepCopy } from "../../src/helpers";
import { UID, WorkbookData } from "../../src/types";
import {
CollaborationMessage,
Expand All @@ -18,7 +19,7 @@ export class MockTransportService implements TransportService<CollaborationMessa
}

sendMessage(message: CollaborationMessage) {
const msg: CollaborationMessage = JSON.parse(JSON.stringify(message));
const msg: CollaborationMessage = deepCopy(message);
switch (msg.type) {
case "REMOTE_REVISION":
case "REVISION_UNDONE":
Expand Down Expand Up @@ -62,7 +63,7 @@ export class MockTransportService implements TransportService<CollaborationMessa

notifyListeners(message: CollaborationMessage) {
for (const { callback } of this.listeners) {
callback(JSON.parse(JSON.stringify(message)));
callback(deepCopy(message));
}
}

Expand Down

0 comments on commit 300596b

Please sign in to comment.