Implement a new method to add multiple rows simultaneously without rerenders in-between the row additions

This commit is contained in:
Oliver-Akins 2025-05-04 21:35:22 -06:00
parent bb4c24329a
commit 8b488f488b
2 changed files with 28 additions and 4 deletions

View file

@ -62,10 +62,14 @@ export class Database {
}; };
// MARK: Row Ops // MARK: Row Ops
static createRow(table, userID, row) { static createRow(table, userID, row, opts) {
throw new Error(`createRow() must be implemented`); throw new Error(`createRow() must be implemented`);
}; };
static createRows(table, userID, rows, opts) {
throw new Error(`createRows() must be implemented`);
};
static getRows(tableID, userIDs, privacy = `none`) { static getRows(tableID, userIDs, privacy = `none`) {
throw new Error(`getRows() must be implemented`); throw new Error(`getRows() must be implemented`);
}; };

View file

@ -1,5 +1,6 @@
import { Database } from "./Database.mjs"; import { Database } from "./Database.mjs";
import { filterPrivateRows } from "../filterPrivateRows.mjs"; import { filterPrivateRows } from "../privacy.mjs";
import { Logger } from "../Logger.mjs";
const { randomID, mergeObject } = foundry.utils; const { randomID, mergeObject } = foundry.utils;
@ -89,17 +90,36 @@ export class MemoryDatabase extends Database {
return this.#tables[tableID]; return this.#tables[tableID];
}; };
static createRow(table, userID, row) { static createRow(table, userID, row, { rerender = true } = {}) {
if (!this.#tables[table]) { return }; if (!this.#tables[table]) { return };
this.#rows[userID] ??= {}; this.#rows[userID] ??= {};
this.#rows[userID][table] ??= []; this.#rows[userID][table] ??= [];
// data format assertions // data format assertions
row._id ||= randomID(); row._id ||= randomID();
row.timestamp = new Date().toISOString();
Logger.debug(`Adding row:`, row);
this.#rows[userID][table].push(row); this.#rows[userID][table].push(row);
if (rerender) {
this.render({ userUpdated: userID }); this.render({ userUpdated: userID });
}; };
};
static createRows(table, userID, rows, { rerender = true } = {}) {
if (!this.#tables[table]) { return };
this.#rows[userID] ??= {};
this.#rows[userID][table] ??= [];
// data format assertions
for (const row of rows) {
this.createRow( table, userID, row, { rerender: false } );
};
if (rerender) {
this.render({ userUpdated: userID });
};
};
static getRows(tableID, userIDs, privacy = `none`) { static getRows(tableID, userIDs, privacy = `none`) {
if (userIDs.length === 0) { if (userIDs.length === 0) {