Skip to content

Commit be4c087

Browse files
Fix transaction interference in v9
1 parent 02586c9 commit be4c087

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

packages/database/src/core/view/EventRegistration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ export class CallbackContext {
7171
matches(other: CallbackContext): boolean {
7272
return (
7373
this.snapshotCallback === other.snapshotCallback ||
74-
(this.snapshotCallback.userCallback ===
75-
other.snapshotCallback.userCallback &&
74+
(this.snapshotCallback.userCallback !== undefined &&
75+
this.snapshotCallback.userCallback ===
76+
other.snapshotCallback.userCallback &&
77+
this.snapshotCallback.userCallback !== undefined &&
7678
this.snapshotCallback.context === other.snapshotCallback.context)
7779
);
7880
}

packages/database/test/exp/integration.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ import {
2424
getDatabase,
2525
goOffline,
2626
goOnline,
27+
push,
2728
ref,
28-
refFromURL
29+
refFromURL,
30+
runTransaction
2931
} from '../../exp/index';
3032
import { onValue, set } from '../../src/exp/Reference_impl';
3133
import { EventAccumulatorFactory } from '../helpers/EventAccumulator';
3234
import { DATABASE_ADDRESS, DATABASE_URL } from '../helpers/util';
35+
import { Deferred } from '@firebase/util';
3336

3437
export function createTestApp() {
3538
return initializeApp({ databaseURL: DATABASE_URL });
@@ -150,4 +153,39 @@ describe('Database@exp Tests', () => {
150153
expect(() => ref(db)).to.throw('Cannot call ref on a deleted database.');
151154
defaultApp = undefined;
152155
});
156+
157+
it('Can listen to transaction changes', async () => {
158+
// Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
159+
let latestValue = 0;
160+
161+
let deferred = new Deferred<void>();
162+
163+
const database = getDatabase(defaultApp);
164+
const counterRef = push(ref(database, 'counter'));
165+
166+
onValue(counterRef, snap => {
167+
latestValue = snap.val();
168+
deferred.resolve();
169+
});
170+
171+
async function incrementViaTransaction() {
172+
deferred = new Deferred<void>();
173+
await runTransaction(counterRef, currentData => {
174+
console.log(
175+
'old value ' + currentData + ' new value ' + (currentData + 1)
176+
);
177+
return currentData + 1;
178+
});
179+
// Wait for the snapshot listener to fire. They are not invoked inline
180+
// for transactions.
181+
await deferred.promise;
182+
}
183+
184+
expect(latestValue).to.equal(0);
185+
186+
await incrementViaTransaction();
187+
expect(latestValue).to.equal(1);
188+
await incrementViaTransaction();
189+
expect(latestValue).to.equal(2);
190+
});
153191
});

packages/database/test/transaction.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,4 +1500,35 @@ describe('Transaction Tests', () => {
15001500
done();
15011501
});
15021502
});
1503+
1504+
it('Can listen to transaction changes', async () => {
1505+
// Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
1506+
let latestValue = 0;
1507+
1508+
const ref = getRandomNode() as Reference;
1509+
1510+
let deferred = new Deferred<void>();
1511+
ref.on('value', snap => {
1512+
latestValue = snap.val() as number;
1513+
console.log('received event ' + latestValue);
1514+
deferred.resolve();
1515+
});
1516+
1517+
async function incrementViaTransaction() {
1518+
deferred = new Deferred<void>();
1519+
await ref.transaction(currentData => {
1520+
return (currentData as number) + 1;
1521+
});
1522+
// Wait for the snapshot listener to fire. They are not invoked inline
1523+
// for transactions.
1524+
await deferred.promise;
1525+
}
1526+
1527+
expect(latestValue).to.equal(0);
1528+
1529+
await incrementViaTransaction();
1530+
expect(latestValue).to.equal(1);
1531+
await incrementViaTransaction();
1532+
expect(latestValue).to.equal(2);
1533+
});
15031534
});

0 commit comments

Comments
 (0)