Skip to content

Commit 25b80e1

Browse files
committed
[indexes] Fix multi-slice index with sorting bug
Fixes #217
1 parent ed041eb commit 25b80e1

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/indexes/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import type {
99
createIndexes as createIndexesDecl,
1010
} from '../@types/indexes/index.d.ts';
1111
import type {GetCell, Store} from '../@types/store/index.d.ts';
12-
import {arrayIsSorted, arrayMap, arraySort} from '../common/array.ts';
12+
import {
13+
arrayForEach,
14+
arrayIsSorted,
15+
arrayMap,
16+
arraySort,
17+
} from '../common/array.ts';
1318
import {
1419
collDel,
1520
collForEach,
@@ -99,7 +104,7 @@ export const createIndexes = getCreateFunction((store: Store): Indexes => {
99104
change: () => void,
100105
changedSliceIds: IdMap<[Id | Ids | undefined, Id | Ids | undefined]>,
101106
changedSortKeys: IdMap<SortKey>,
102-
sliceIds?: IdMap<Id | Ids>,
107+
sliceIdOrIdsByRowId?: IdMap<Id | Ids>,
103108
sortKeys?: IdMap<SortKey>,
104109
force?: boolean,
105110
) => {
@@ -150,8 +155,13 @@ export const createIndexes = getCreateFunction((store: Store): Indexes => {
150155
mapForEach(index, (sliceId) => setAdd(unsortedSlices, sliceId));
151156
} else {
152157
mapForEach(changedSortKeys, (rowId) =>
153-
ifNotUndefined(mapGet(sliceIds, rowId), (sliceId) =>
154-
setAdd(unsortedSlices, sliceId),
158+
ifNotUndefined(
159+
mapGet(sliceIdOrIdsByRowId, rowId),
160+
(sliceIdOrIds) =>
161+
arrayForEach(
162+
isArray(sliceIdOrIds) ? sliceIdOrIds : [sliceIdOrIds],
163+
(sliceId) => setAdd(unsortedSlices, sliceId),
164+
),
155165
),
156166
);
157167
}

test/unit/core/other/indexes.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,26 @@ describe('Miscellaneous', () => {
17711771
expect(store.getValues()).toEqual({});
17721772
});
17731773

1774+
test('Sorted multi-slice index does not error', () => {
1775+
const store = createStore();
1776+
const indexes = createIndexes(store);
1777+
1778+
indexes.setIndexDefinition(
1779+
'i1',
1780+
't1',
1781+
(getCell) => (getCell('c1') + '').split(''),
1782+
(getCell) => (getCell('c1') + '').split('')[0],
1783+
);
1784+
1785+
store.setCell('t1', 'r1', 'c1', 'abc');
1786+
store.setCell('t1', 'r2', 'c1', 'abc');
1787+
store.setCell('t1', 'r1', 'c1', 'ab');
1788+
expect(store.getTables()).toEqual({t1: {r1: {c1: 'ab'}, r2: {c1: 'abc'}}});
1789+
expect(getIndexesObject(indexes)).toEqual({
1790+
i1: {a: ['r1', 'r2'], b: ['r1', 'r2'], c: ['r2']},
1791+
});
1792+
});
1793+
17741794
test('bad sort key', () => {
17751795
indexes.setIndexDefinition('i1', 't1', 'c2', 'c0');
17761796
setCells();

0 commit comments

Comments
 (0)