Skip to content

Commit 2907ed5

Browse files
committed
[IMP] make a UuidGenerator singleton
Make a UuidGenerator singleton and use it instead of recreating a new instance every time it's needed. Task: 6058383
1 parent 2a6303d commit 2907ed5

29 files changed

Lines changed: 91 additions & 104 deletions

File tree

src/actions/insert_actions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { functionRegistry } from "../functions/function_registry";
2-
import { isDefined } from "../helpers";
2+
import { isDefined, UuidGenerator } from "../helpers";
33
import { handlePasteResult } from "../helpers/ui/paste_interactive";
44
import { _t } from "../translation";
55
import { ActionBuilder, ActionSpec } from "./action";
@@ -299,7 +299,7 @@ export const insertCheckbox: ActionSpec = {
299299
ranges,
300300
sheetId,
301301
rule: {
302-
id: env.model.uuidGenerator.smallUuid(),
302+
id: UuidGenerator.smallUuid(),
303303
criterion: {
304304
type: "isBoolean",
305305
values: [],
@@ -317,7 +317,7 @@ export const insertDropdown: ActionSpec = {
317317
const zones = env.model.getters.getSelectedZones();
318318
const sheetId = env.model.getters.getActiveSheetId();
319319
const ranges = zones.map((zone) => env.model.getters.getRangeDataFromZone(sheetId, zone));
320-
const ruleId = env.model.uuidGenerator.smallUuid();
320+
const ruleId = UuidGenerator.smallUuid();
321321
env.model.dispatch("ADD_DATA_VALIDATION_RULE", {
322322
ranges,
323323
sheetId,
@@ -351,7 +351,7 @@ export const insertSheet: ActionSpec = {
351351
execute: (env) => {
352352
const activeSheetId = env.model.getters.getActiveSheetId();
353353
const position = env.model.getters.getSheetIds().indexOf(activeSheetId) + 1;
354-
const sheetId = env.model.uuidGenerator.smallUuid();
354+
const sheetId = UuidGenerator.smallUuid();
355355
env.model.dispatch("CREATE_SHEET", {
356356
sheetId,
357357
position,

src/actions/menu_items_actions.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
largeMax,
2020
largeMin,
2121
numberToLetters,
22+
UuidGenerator,
2223
} from "../helpers/index";
2324
import { DEFAULT_TABLE_CONFIG } from "../helpers/table_presets";
2425
import { interactivePaste, interactivePasteFromOS } from "../helpers/ui/paste_interactive";
@@ -399,7 +400,7 @@ export const HIDE_ROWS_NAME = (env: SpreadsheetChildEnv) => {
399400

400401
export const CREATE_CHART = (env: SpreadsheetChildEnv) => {
401402
const getters = env.model.getters;
402-
const figureId = env.model.uuidGenerator.smallUuid();
403+
const figureId = UuidGenerator.smallUuid();
403404
const sheetId = getters.getActiveSheetId();
404405
let zones = getters.getSelectedZones();
405406

@@ -414,7 +415,7 @@ export const CREATE_CHART = (env: SpreadsheetChildEnv) => {
414415
const result = env.model.dispatch("CREATE_CHART", {
415416
sheetId,
416417
figureId,
417-
chartId: env.model.uuidGenerator.smallUuid(),
418+
chartId: UuidGenerator.smallUuid(),
418419
col,
419420
row,
420421
offset,
@@ -429,7 +430,7 @@ export const CREATE_CHART = (env: SpreadsheetChildEnv) => {
429430

430431
export const CREATE_CAROUSEL = (env: SpreadsheetChildEnv) => {
431432
const getters = env.model.getters;
432-
const figureId = env.model.uuidGenerator.smallUuid();
433+
const figureId = UuidGenerator.smallUuid();
433434
const sheetId = getters.getActiveSheetId();
434435

435436
const size = { width: DEFAULT_FIGURE_WIDTH, height: DEFAULT_FIGURE_HEIGHT };
@@ -455,8 +456,8 @@ export const CREATE_CAROUSEL = (env: SpreadsheetChildEnv) => {
455456
//------------------------------------------------------------------------------
456457

457458
export const CREATE_PIVOT = (env: SpreadsheetChildEnv) => {
458-
const pivotId = env.model.uuidGenerator.smallUuid();
459-
const newSheetId = env.model.uuidGenerator.smallUuid();
459+
const pivotId = UuidGenerator.smallUuid();
460+
const newSheetId = UuidGenerator.smallUuid();
460461
const result = env.model.dispatch("INSERT_NEW_PIVOT", { pivotId, newSheetId });
461462
if (result.isSuccessful) {
462463
env.openSidePanel("PivotSidePanel", { pivotId });
@@ -520,7 +521,7 @@ export const REINSERT_STATIC_PIVOT_CHILDREN = (env: SpreadsheetChildEnv) =>
520521
export const CREATE_IMAGE = async (env: SpreadsheetChildEnv) => {
521522
if (env.imageProvider) {
522523
const sheetId = env.model.getters.getActiveSheetId();
523-
const figureId = env.model.uuidGenerator.smallUuid();
524+
const figureId = UuidGenerator.smallUuid();
524525
const image = await env.imageProvider.requestImage();
525526
const size = getMaxFigureSize(env.model.getters, image.size);
526527
const { col, row, offset } = centerFigurePosition(env.model.getters, size);

src/actions/sheet_actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { buildSheetLink, markdownLink } from "../helpers";
1+
import { buildSheetLink, markdownLink, UuidGenerator } from "../helpers";
22
import { _t } from "../translation";
33
import { ActionSpec } from "./action";
44

@@ -39,7 +39,7 @@ export const duplicateSheet: ActionSpec = {
3939
execute: (env) => {
4040
const sheetIdFrom = env.model.getters.getActiveSheetId();
4141
const sheetNameFrom = env.model.getters.getSheetName(sheetIdFrom);
42-
const sheetIdTo = env.model.uuidGenerator.smallUuid();
42+
const sheetIdTo = UuidGenerator.smallUuid();
4343
const sheetNameTo = env.model.getters.getDuplicateSheetName(sheetNameFrom);
4444
env.model.dispatch("DUPLICATE_SHEET", {
4545
sheetId: sheetIdFrom,

src/clipboard_handlers/carousel_clipboard.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class CarouselClipboardHandler extends AbstractFigureClipboardHandler<Cli
5252
}
5353

5454
getPasteTarget(sheetId: UID): ClipboardPasteTarget {
55-
const newId = new UuidGenerator().smallUuid();
55+
const newId = UuidGenerator.smallUuid();
5656
return { zones: [], figureId: newId, sheetId };
5757
}
5858

@@ -84,14 +84,13 @@ export class CarouselClipboardHandler extends AbstractFigureClipboardHandler<Cli
8484
size: { height, width },
8585
});
8686

87-
const uuidGenerator = new UuidGenerator();
8887
const items = deepCopy(clippedContent.copiedCarousel.items);
8988
for (const item of items) {
9089
if (item.type !== "chart") {
9190
continue;
9291
}
9392
const chart = clippedContent.copiedCharts[item.chartId];
94-
const newId = uuidGenerator.smallUuid();
93+
const newId = UuidGenerator.smallUuid();
9594
const definition = chart.copyInSheetId(sheetId).getDefinition();
9695
this.dispatch("CREATE_CHART", { figureId, chartId: newId, sheetId, definition });
9796
item.chartId = newId;

src/clipboard_handlers/chart_clipboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ChartClipboardHandler extends AbstractFigureClipboardHandler<Clipbo
4646
content: ClipboardContent,
4747
options?: ClipboardOptions
4848
): ClipboardPasteTarget {
49-
const newId = new UuidGenerator().smallUuid();
49+
const newId = UuidGenerator.smallUuid();
5050
return { zones: [], figureId: newId, sheetId };
5151
}
5252

@@ -71,7 +71,7 @@ export class ChartClipboardHandler extends AbstractFigureClipboardHandler<Clipbo
7171
}
7272
this.dispatch("CREATE_CHART", {
7373
figureId,
74-
chartId: new UuidGenerator().smallUuid(),
74+
chartId: UuidGenerator.smallUuid(),
7575
sheetId,
7676
definition: copy.getDefinition(),
7777
col,

src/clipboard_handlers/conditional_format_clipboard.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class ConditionalFormatClipboardHandler extends AbstractCellClipboardHand
2525
ClipboardContent,
2626
Maybe<ClipboardConditionalFormat>
2727
> {
28-
private readonly uuidGenerator = new UuidGenerator();
2928
private queuedChanges: Record<UID, { toAdd: Zone[]; toRemove: Zone[]; cf: ConditionalFormat }[]> =
3029
{};
3130

@@ -169,6 +168,6 @@ export class ConditionalFormatClipboardHandler extends AbstractCellClipboardHand
169168
)?.cf;
170169
}
171170

172-
return targetCF || { ...originCF, id: this.uuidGenerator.smallUuid(), ranges: [] };
171+
return targetCF || { ...originCF, id: UuidGenerator.smallUuid(), ranges: [] };
173172
}
174173
}

src/clipboard_handlers/data_validation_clipboard.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class DataValidationClipboardHandler extends AbstractCellClipboardHandler
2525
ClipboardContent,
2626
Maybe<ClipboardDataValidationRule>
2727
> {
28-
private readonly uuidGenerator = new UuidGenerator();
2928
private queuedChanges: Record<
3029
UID,
3130
{ toAdd: Zone[]; toRemove: Zone[]; rule: DataValidationRule }[]
@@ -146,7 +145,7 @@ export class DataValidationClipboardHandler extends AbstractCellClipboardHandler
146145
return (
147146
targetRule || {
148147
...originRule,
149-
id: newId ? this.uuidGenerator.smallUuid() : originRule.id,
148+
id: newId ? UuidGenerator.smallUuid() : originRule.id,
150149
ranges: [],
151150
}
152151
);

src/clipboard_handlers/image_clipboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ImageClipboardHandler extends AbstractFigureClipboardHandler<Clipbo
4545
content: ClipboardContent,
4646
options?: ClipboardOptions
4747
): ClipboardPasteTarget {
48-
const newId = new UuidGenerator().smallUuid();
48+
const newId = UuidGenerator.smallUuid();
4949
return { sheetId, zones: [], figureId: newId };
5050
}
5151

src/collaborative/session.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export class Session extends EventBus<CollaborativeEvent> {
6565
| SnapshotCreatedMessage
6666
| undefined = undefined;
6767

68-
private uuidGenerator = new UuidGenerator();
6968
private lastLocalOperation: Revision | undefined;
7069
/**
7170
* Manages the collaboration between multiple users on the same spreadsheet.
@@ -102,7 +101,7 @@ export class Session extends EventBus<CollaborativeEvent> {
102101
return;
103102
}
104103
const revision = new Revision(
105-
this.uuidGenerator.uuidv4(),
104+
UuidGenerator.uuidv4(),
106105
this.clientId,
107106
commands,
108107
rootCommand,
@@ -132,7 +131,7 @@ export class Session extends EventBus<CollaborativeEvent> {
132131
type: "REVISION_UNDONE",
133132
version: MESSAGE_VERSION,
134133
serverRevisionId: this.serverRevisionId,
135-
nextRevisionId: this.uuidGenerator.uuidv4(),
134+
nextRevisionId: UuidGenerator.uuidv4(),
136135
undoneRevisionId: revisionId,
137136
});
138137
}
@@ -143,7 +142,7 @@ export class Session extends EventBus<CollaborativeEvent> {
143142
type: "REVISION_REDONE",
144143
version: MESSAGE_VERSION,
145144
serverRevisionId: this.serverRevisionId,
146-
nextRevisionId: this.uuidGenerator.uuidv4(),
145+
nextRevisionId: UuidGenerator.uuidv4(),
147146
redoneRevisionId: revisionId,
148147
});
149148
}
@@ -205,7 +204,7 @@ export class Session extends EventBus<CollaborativeEvent> {
205204
if (this.pendingMessages.length !== 0) {
206205
return;
207206
}
208-
const snapshotId = this.uuidGenerator.uuidv4();
207+
const snapshotId = UuidGenerator.uuidv4();
209208
await this.sendToTransport({
210209
type: "SNAPSHOT",
211210
nextRevisionId: snapshotId,

src/components/bottom_bar/bottom_bar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, onWillUpdateProps, useRef, useState } from "@odoo/owl";
2-
import { deepEquals } from "../../helpers";
2+
import { deepEquals, UuidGenerator } from "../../helpers";
33
import { MenuItemRegistry } from "../../registries/menu_items_registry";
44
import { _t } from "../../translation";
55
import { MenuMouseEvent, Pixel, Rect, UID } from "../../types";
@@ -72,7 +72,7 @@ export class BottomBar extends Component<Props, SpreadsheetChildEnv> {
7272
const activeSheetId = this.env.model.getters.getActiveSheetId();
7373
const position =
7474
this.env.model.getters.getSheetIds().findIndex((sheetId) => sheetId === activeSheetId) + 1;
75-
const sheetId = this.env.model.uuidGenerator.smallUuid();
75+
const sheetId = UuidGenerator.smallUuid();
7676
const name = this.env.model.getters.getNextSheetName(_t("Sheet"));
7777
this.env.model.dispatch("CREATE_SHEET", { sheetId, position, name });
7878
this.env.model.dispatch("ACTIVATE_SHEET", { sheetIdFrom: activeSheetId, sheetIdTo: sheetId });

0 commit comments

Comments
 (0)