Skip to content

Commit ea05bac

Browse files
committed
[ids] Re-use Ids if required
1 parent b7ed740 commit ea05bac

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/common/listeners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const getListenerFunctions = (
113113
extraArgsGetter: ExtraArgsGetter = () => [],
114114
): Id => {
115115
thing ??= getThing();
116-
const id = getId();
116+
const id = getId(1);
117117
mapSet(allListeners, id, [
118118
listener,
119119
idSetNode,

src/common/pool.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ import {test} from './other';
55

66
const INTEGER = /^\d+$/;
77

8-
export const getPoolFunctions = (): [() => Id, (id: Id) => void] => {
8+
export const getPoolFunctions = (): [
9+
(reuse: 0 | 1) => Id,
10+
(id: Id) => void,
11+
] => {
912
const pool: Ids = [];
1013
let nextId = 0;
1114
return [
12-
(): Id => arrayShift(pool) ?? EMPTY_STRING + nextId++,
15+
(reuse: 0 | 1): Id =>
16+
(reuse ? arrayShift(pool) : null) ?? EMPTY_STRING + nextId++,
1317
(id: Id): void => {
1418
if (test(INTEGER, id) && arrayLength(pool) < 1e3) {
1519
arrayPush(pool, id);

src/store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,13 @@ export const createStore: typeof createStoreDecl = (): Store => {
495495
}
496496
};
497497

498-
const getNewRowId = (tableId: Id): Id => {
498+
const getNewRowId = (tableId: Id, reuse: 0 | 1): Id => {
499499
const [getId] = mapEnsure(tablePoolFunctions, tableId, getPoolFunctions);
500-
const rowId = getId();
500+
const rowId = getId(reuse);
501501
if (!collHas(mapGet(tablesMap, tableId), rowId)) {
502502
return rowId;
503503
}
504-
return getNewRowId(tableId);
504+
return getNewRowId(tableId, reuse);
505505
};
506506

507507
const getOrCreateTable = (tableId: Id) =>
@@ -983,15 +983,15 @@ export const createStore: typeof createStoreDecl = (): Store => {
983983
rowId,
984984
);
985985

986-
const addRow = (tableId: Id, row: Row): Id | undefined =>
986+
const addRow = (tableId: Id, row: Row, reuseRowIds = true): Id | undefined =>
987987
transaction(() => {
988988
let rowId: Id | undefined = undefined;
989989
if (validateRow(tableId, rowId, row)) {
990990
tableId = id(tableId);
991991
setValidRow(
992992
tableId,
993993
getOrCreateTable(tableId),
994-
(rowId = getNewRowId(tableId)),
994+
(rowId = getNewRowId(tableId, reuseRowIds ? 1 : 0)),
995995
row,
996996
);
997997
}

0 commit comments

Comments
 (0)