Skip to content

Fix transaction interference in v9 #5205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/database/src/core/view/EventRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ export class CallbackContext {
matches(other: CallbackContext): boolean {
return (
this.snapshotCallback === other.snapshotCallback ||
(this.snapshotCallback.userCallback ===
other.snapshotCallback.userCallback &&
(this.snapshotCallback.userCallback !== undefined &&
this.snapshotCallback.userCallback ===
other.snapshotCallback.userCallback &&
this.snapshotCallback.context === other.snapshotCallback.context)
);
}
Expand Down
37 changes: 36 additions & 1 deletion packages/database/test/exp/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

// eslint-disable-next-line import/no-extraneous-dependencies
import { initializeApp, deleteApp } from '@firebase/app-exp';
import { Deferred } from '@firebase/util';
import { expect } from 'chai';

import {
get,
getDatabase,
goOffline,
goOnline,
push,
ref,
refFromURL
refFromURL,
runTransaction
} from '../../exp/index';
import { onValue, set } from '../../src/exp/Reference_impl';
import { EventAccumulatorFactory } from '../helpers/EventAccumulator';
Expand Down Expand Up @@ -150,4 +153,36 @@ describe('Database@exp Tests', () => {
expect(() => ref(db)).to.throw('Cannot call ref on a deleted database.');
defaultApp = undefined;
});

it('Can listen to transaction changes', async () => {
// Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
let latestValue = 0;

let deferred = new Deferred<void>();

const database = getDatabase(defaultApp);
const counterRef = push(ref(database, 'counter'));

onValue(counterRef, snap => {
latestValue = snap.val();
deferred.resolve();
});

async function incrementViaTransaction() {
deferred = new Deferred<void>();
await runTransaction(counterRef, currentData => {
return currentData + 1;
});
// Wait for the snapshot listener to fire. They are not invoked inline
// for transactions.
await deferred.promise;
}

expect(latestValue).to.equal(0);

await incrementViaTransaction();
expect(latestValue).to.equal(1);
await incrementViaTransaction();
expect(latestValue).to.equal(2);
});
});
30 changes: 30 additions & 0 deletions packages/database/test/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1500,4 +1500,34 @@ describe('Transaction Tests', () => {
done();
});
});

it('Can listen to transaction changes', async () => {
// Repro for https://github.com/firebase/firebase-js-sdk/issues/5195
let latestValue = 0;

const ref = getRandomNode() as Reference;

let deferred = new Deferred<void>();
ref.on('value', snap => {
latestValue = snap.val() as number;
deferred.resolve();
});

async function incrementViaTransaction() {
deferred = new Deferred<void>();
await ref.transaction(currentData => {
return (currentData as number) + 1;
});
// Wait for the snapshot listener to fire. They are not invoked inline
// for transactions.
await deferred.promise;
}

expect(latestValue).to.equal(0);

await incrementViaTransaction();
expect(latestValue).to.equal(1);
await incrementViaTransaction();
expect(latestValue).to.equal(2);
});
});