Skip to content

Commit e86fd77

Browse files
committed
[transactions] Disallow start/finish in a listener
1 parent 3555b2a commit e86fd77

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/store.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,23 +1186,24 @@ export const createStore: typeof createStoreDecl = (): Store => {
11861186
const transaction = <Return>(
11871187
actions: () => Return,
11881188
doRollback?: DoRollback,
1189+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1190+
// @ts-ignore void return only occurs internally
11891191
): Return => {
1190-
if (transactions == -1) {
1191-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1192-
// @ts-ignore only occurs internally
1193-
return;
1192+
if (transactions != -1) {
1193+
startTransaction();
1194+
const result = actions();
1195+
finishTransaction(doRollback);
1196+
return result as Return;
11941197
}
1195-
startTransaction();
1196-
const result = actions();
1197-
finishTransaction(doRollback);
1198-
return result as Return;
11991198
};
12001199

12011200
const startTransaction = (): Store => {
12021201
if (transactions == 0) {
12031202
callListeners(startTransactionListeners, undefined, false, false);
12041203
}
1205-
transactions++;
1204+
if (transactions != -1) {
1205+
transactions++;
1206+
}
12061207
return store;
12071208
};
12081209

test/unit/store-advanced.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ describe('Transactions', () => {
721721
expectNoChanges(listener);
722722
});
723723

724-
test('Transaction in a listener ignored', () => {
724+
test('Transaction in a listener ignored 1', () => {
725725
listener.listenToCell('/t1/r1/c1', 't1', 'r1', 'c1');
726726
const listenerTransaction = jest.fn(() => {
727727
store.setTables({t1: {r1: {c1: 3}}});
@@ -735,6 +735,22 @@ describe('Transactions', () => {
735735
expect(listenerTransaction).not.toHaveBeenCalled();
736736
});
737737

738+
test('Transaction in a listener ignored 2', () => {
739+
listener.listenToCell('/t1/r1/c1', 't1', 'r1', 'c1');
740+
const listenerTransaction = jest.fn(() => {
741+
store.setTables({t1: {r1: {c1: 3}}});
742+
});
743+
store.addCellListener('t1', 'r1', 'c1', () => {
744+
store.startTransaction();
745+
listenerTransaction();
746+
store.finishTransaction();
747+
});
748+
store.setTables({t1: {r1: {c1: 2}}});
749+
expectChanges(listener, '/t1/r1/c1', {t1: {r1: {c1: 2}}});
750+
expectNoChanges(listener);
751+
expect(listenerTransaction).toHaveBeenCalledTimes(1);
752+
});
753+
738754
test('Adding a peer listener in a listener', () => {
739755
const listener = jest.fn(() => null);
740756
store.addCellListener('t1', 'r1', 'c1', () => {

0 commit comments

Comments
 (0)