Skip to content

Commit 1e626ee

Browse files
Merge branch 'mrschmidt/listener' into mrschmidt/applyremoteevent
2 parents e6b4329 + 351bebb commit 1e626ee

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

packages/firestore/src/local/memory_persistence.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ export class MemoryPersistence implements Persistence {
180180
this.referenceDelegate.onTransactionStarted();
181181
return transactionOperation(txn)
182182
.next(result => {
183-
txn.raiseOnCommittedEvent();
184183
return this.referenceDelegate
185184
.onTransactionCommitted(txn)
186185
.next(() => result);
187186
})
188-
.toPromise();
187+
.toPromise()
188+
.then(result => {
189+
txn.raiseOnCommittedEvent();
190+
return result;
191+
});
189192
}
190193

191194
mutationQueuesContainKey(

packages/firestore/src/local/persistence.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ import { ClientId } from './shared_client_state';
3737
* on persistence.
3838
*/
3939
export abstract class PersistenceTransaction {
40-
private readonly onCommittedListeners: Array<() => {}> = [];
40+
private readonly onCommittedListeners: Array<() => void> = [];
4141

4242
abstract readonly currentSequenceNumber: ListenSequenceNumber;
4343

44-
addOnCommittedListener(listener: () => {}): void {
44+
addOnCommittedListener(listener: () => void): void {
4545
this.onCommittedListeners.push(listener);
4646
}
4747

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as persistenceHelpers from './persistence_test_helpers';
19+
import { expect } from 'chai';
20+
import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence';
21+
import { Persistence } from '../../../src/local/persistence';
22+
import { PersistencePromise } from '../../../src/local/persistence_promise';
23+
24+
let persistence: Persistence;
25+
26+
describe('MemoryTransaction', () => {
27+
beforeEach(() => {
28+
return persistenceHelpers.testMemoryEagerPersistence().then(p => {
29+
persistence = p;
30+
});
31+
});
32+
33+
genericTransactionTests();
34+
});
35+
36+
describe('IndexedDbTransaction', () => {
37+
if (!IndexedDbPersistence.isAvailable()) {
38+
console.warn('No IndexedDB. Skipping IndexedDbTransaction tests.');
39+
return;
40+
}
41+
42+
beforeEach(() => {
43+
return persistenceHelpers.testIndexedDbPersistence().then(p => {
44+
persistence = p;
45+
});
46+
});
47+
48+
afterEach(() => persistence.shutdown());
49+
50+
genericTransactionTests();
51+
});
52+
53+
function genericTransactionTests(): void {
54+
it('invokes onCommittedListener when transaction succeeds', async () => {
55+
let onCommitted = false;
56+
await persistence.runTransaction('onCommitted', 'readonly', txn => {
57+
txn.addOnCommittedListener(() => {
58+
onCommitted = true;
59+
});
60+
61+
expect(onCommitted).to.be.false;
62+
return PersistencePromise.resolve();
63+
});
64+
65+
expect(onCommitted).to.be.true;
66+
});
67+
68+
it('does not invoke onCommittedListener when transaction fails', async () => {
69+
let onCommitted = false;
70+
await persistence
71+
.runTransaction('onCommitted', 'readonly', txn => {
72+
txn.addOnCommittedListener(() => {
73+
onCommitted = true;
74+
});
75+
76+
return PersistencePromise.reject(new Error('Aborted'));
77+
})
78+
.catch(() => {});
79+
80+
expect(onCommitted).to.be.false;
81+
});
82+
}

0 commit comments

Comments
 (0)