Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REM] UUID: remove fast strategy #5030

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/helpers/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@
* */

export class UuidGenerator {
private isFastIdStrategy = false;

private fastIdStart = 0;

setIsFastStrategy(isFast: boolean) {
this.isFastIdStrategy = isFast;
}

uuidv4(): string {
if (this.isFastIdStrategy) {
this.fastIdStart++;
return String(this.fastIdStart);
//@ts-ignore
} else if (window.crypto && window.crypto.getRandomValues) {
//@ts-ignore
hokolomopo marked this conversation as resolved.
Show resolved Hide resolved
if (window.crypto && window.crypto.getRandomValues) {
//@ts-ignore
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
Expand Down
4 changes: 0 additions & 4 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ export class Model extends EventBus<any> implements CommandDispatcher {
isDashboard: () => this.config.mode === "dashboard",
} as Getters;

this.uuidGenerator.setIsFastStrategy(true);

// Initiate stream processor
this.selection = new SelectionStreamProcessorImpl(this.getters);

Expand Down Expand Up @@ -268,7 +266,6 @@ export class Model extends EventBus<any> implements CommandDispatcher {
this.handlers.push(plugin);
this.uiHandlers.push(plugin);
}
this.uuidGenerator.setIsFastStrategy(false);

// starting plugins
this.dispatch("START");
Expand Down Expand Up @@ -433,7 +430,6 @@ export class Model extends EventBus<any> implements CommandDispatcher {
range: this.range,
dispatch: this.dispatchFromCorePlugin,
canDispatch: this.canDispatch,
uuidGenerator: this.uuidGenerator,
custom: this.config.custom,
external: this.config.external,
};
Expand Down
12 changes: 7 additions & 5 deletions src/plugins/core/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {

import { CorePlugin } from "../core_plugin";

let nextTableId = 1;

interface TableState {
tables: Record<UID, Record<TableId, CoreTable | undefined>>;
}
Expand Down Expand Up @@ -126,7 +128,7 @@ export class TablePlugin extends CorePlugin<TableState> implements TableState {
const mergesInTarget = this.getters.getMergesInZone(cmd.sheetId, union.zone);
this.dispatch("REMOVE_MERGE", { sheetId: cmd.sheetId, target: mergesInTarget });

const id = this.uuidGenerator.uuidv4();
const id = `${nextTableId++}`;
const config = cmd.config || DEFAULT_TABLE_CONFIG;
const newTable =
cmd.tableType === "dynamic"
Expand Down Expand Up @@ -310,7 +312,7 @@ export class TablePlugin extends CorePlugin<TableState> implements TableState {
filters = [];
for (const i of range(zone.left, zone.right + 1)) {
const filterZone = { ...zone, left: i, right: i };
const uid = this.uuidGenerator.uuidv4();
const uid = `${nextTableId++}`;
filters.push(this.createFilterFromZone(uid, tableRange.sheetId, filterZone, config));
}
}
Expand Down Expand Up @@ -391,7 +393,7 @@ export class TablePlugin extends CorePlugin<TableState> implements TableState {
? table.filters.find((f) => f.col === i)
: undefined;
const filterZone = { ...tableZone, left: i, right: i };
const filterId = oldFilter?.id || this.uuidGenerator.uuidv4();
const filterId = oldFilter?.id || `${nextTableId++}`;
filters.push(this.createFilterFromZone(filterId, tableRange.sheetId, filterZone, config));
}
}
Expand Down Expand Up @@ -514,7 +516,7 @@ export class TablePlugin extends CorePlugin<TableState> implements TableState {
if (filters.length < zoneToDimension(tableZone).numberOfCols) {
for (let col = tableZone.left; col <= tableZone.right; col++) {
if (!filters.find((filter) => filter.col === col)) {
const uid = this.uuidGenerator.uuidv4();
const uid = `${nextTableId++}`;
const filterZone = { ...tableZone, left: col, right: col };
filters.push(this.createFilterFromZone(uid, sheetId, filterZone, table.config));
}
Expand All @@ -539,7 +541,7 @@ export class TablePlugin extends CorePlugin<TableState> implements TableState {
import(data: WorkbookData) {
for (const sheet of data.sheets) {
for (const tableData of sheet.tables || []) {
const uuid = this.uuidGenerator.uuidv4();
const uuid = `${nextTableId++}`;
const tableConfig = tableData.config || DEFAULT_TABLE_CONFIG;
const range = this.getters.getRangeFromSheetXC(sheet.id, tableData.range);
const tableType = tableData.type || "static";
Expand Down
13 changes: 1 addition & 12 deletions src/plugins/core_plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UuidGenerator } from "../helpers";
import { ModelConfig } from "../model";
import { StateObserver } from "../state_observer";
import {
Expand All @@ -19,7 +18,6 @@ export interface CorePluginConfig {
readonly range: RangeAdapter;
readonly dispatch: CoreCommandDispatcher["dispatch"];
readonly canDispatch: CoreCommandDispatcher["dispatch"];
readonly uuidGenerator: UuidGenerator;
readonly custom: ModelConfig["custom"];
readonly external: ModelConfig["external"];
}
Expand All @@ -40,20 +38,11 @@ export class CorePlugin<State = any>
implements RangeProvider
{
protected getters: CoreGetters;
protected uuidGenerator: UuidGenerator;

constructor({
getters,
stateObserver,
range,
dispatch,
canDispatch,
uuidGenerator,
}: CorePluginConfig) {
constructor({ getters, stateObserver, range, dispatch, canDispatch }: CorePluginConfig) {
super(stateObserver, dispatch, canDispatch);
range.addRangeProvider(this.adaptRanges.bind(this));
this.getters = getters;
this.uuidGenerator = uuidGenerator;
}

// ---------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions tests/__mocks__/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export class UuidGenerator {
private nextId = 1;

setIsFastStrategy(isFast: boolean) {}

uuidv4(): string {
return String(this.nextId++);
}
Expand Down