Skip to content

Commit f970f9c

Browse files
author
Greg Soltis
authored
Finish type migration and get rid of AnyDuringMigration (#1424)
Remove AnyDuringMigration
1 parent 35e3cb5 commit f970f9c

File tree

5 files changed

+29
-36
lines changed

5 files changed

+29
-36
lines changed

packages/firestore/src/local/simple_db.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import { assert } from '../util/assert';
1818
import { Code, FirestoreError } from '../util/error';
1919
import { debug } from '../util/log';
20-
import { AnyDuringMigration } from '../util/misc';
2120
import { AnyJs } from '../util/misc';
2221
import { Deferred } from '../util/promise';
2322
import { SCHEMA_VERSION } from './indexeddb_schema';
@@ -173,15 +172,18 @@ export class SimpleDb {
173172
.catch(error => {
174173
// Abort the transaction if there was an error.
175174
transaction.abort(error);
175+
// We cannot actually recover, and calling `abort()` will cause the transaction's
176+
// completion promise to be rejected. This in turn means that we won't use
177+
// `transactionFnResult` below. We return a rejection here so that we don't add the
178+
// possibility of returning `void` to the type of `transactionFnResult`.
179+
return PersistencePromise.reject<T>(error);
176180
})
177181
.toPromise();
178182

179183
// Wait for the transaction to complete (i.e. IndexedDb's onsuccess event to
180184
// fire), but still return the original transactionFnResult back to the
181185
// caller.
182-
return transaction.completionPromise.then(
183-
() => transactionFnResult
184-
) as AnyDuringMigration;
186+
return transaction.completionPromise.then(() => transactionFnResult);
185187
}
186188

187189
close(): void {

packages/firestore/src/util/misc.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export type AnyJs = null | undefined | boolean | number | string | object;
3232
*/
3333
export type Unknown = null | undefined | {} | void;
3434

35-
// TODO(b/66916745): AnyDuringMigration was used to suppress type check failures
36-
// that were found during the upgrade to TypeScript 2.4. They need to be audited
37-
// and fixed.
38-
// tslint:disable-next-line:no-any
39-
export type AnyDuringMigration = any;
40-
4135
// tslint:disable-next-line:class-as-namespace
4236
export class AutoId {
4337
static newId(): string {

packages/firestore/src/util/promise.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { AnyDuringMigration } from './misc';
18-
1917
export interface Resolver<R> {
2018
(value?: R | Promise<R>): void;
2119
}
@@ -42,20 +40,17 @@ export class Deferred<R> {
4240
}
4341

4442
/**
45-
* Takes an array of values and sequences them using the promise (or value)
46-
* returned by the supplied callback. The callback for each item is called
47-
* after the promise is resolved for the previous item.
48-
* The function returns a promise which is resolved after the promise for
49-
* the last item is resolved.
43+
* Takes an array of values and a function from a value to a Promise. The function is run on each
44+
* value sequentially, waiting for the previous promise to resolve before starting the next one.
45+
* The returned promise resolves once the function has been run on all values.
5046
*/
51-
export function sequence<T, R>(
47+
export function sequence<T>(
5248
values: T[],
53-
fn: (value: T, result?: R) => R | Promise<R>,
54-
initialValue?: R
55-
): Promise<R> {
56-
let result = Promise.resolve(initialValue);
57-
values.forEach(value => {
58-
result = result.then(lastResult => fn(value, lastResult));
59-
});
60-
return result as AnyDuringMigration;
49+
fn: (value: T) => Promise<void>
50+
): Promise<void> {
51+
let p = Promise.resolve();
52+
for (const value of values) {
53+
p = p.then(() => fn(value));
54+
}
55+
return p;
6156
}

packages/firestore/test/unit/local/test_mutation_queue.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { DocumentKeySet } from '../../../src/model/collections';
2323
import { DocumentKey } from '../../../src/model/document_key';
2424
import { Mutation } from '../../../src/model/mutation';
2525
import { MutationBatch } from '../../../src/model/mutation_batch';
26-
import { AnyDuringMigration } from '../../../src/util/misc';
2726

2827
/**
2928
* A wrapper around a MutationQueue that automatically creates a
@@ -64,9 +63,15 @@ export class TestMutationQueue {
6463
'getLastStreamToken',
6564
'readonly',
6665
txn => {
67-
return this.queue.getLastStreamToken(txn);
66+
return this.queue.getLastStreamToken(txn).next(token => {
67+
if (typeof token === 'string') {
68+
return token;
69+
} else {
70+
throw new Error('Test mutation queue cannot handle Uint8Arrays');
71+
}
72+
});
6873
}
69-
) as AnyDuringMigration;
74+
);
7075
}
7176

7277
setLastStreamToken(streamToken: string): Promise<void> {

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ import {
8383
import { assert, fail } from '../../../src/util/assert';
8484
import { AsyncQueue, TimerId } from '../../../src/util/async_queue';
8585
import { FirestoreError } from '../../../src/util/error';
86-
import {
87-
AnyDuringMigration,
88-
AnyJs,
89-
primitiveComparator
90-
} from '../../../src/util/misc';
86+
import { AnyJs, primitiveComparator } from '../../../src/util/misc';
9187
import * as obj from '../../../src/util/obj';
9288
import { ObjectMap } from '../../../src/util/obj_map';
9389
import { Deferred, sequence } from '../../../src/util/promise';
@@ -160,8 +156,9 @@ class MockConnection implements Connection {
160156
}
161157

162158
waitForWriteRequest(): Promise<api.WriteRequest> {
163-
if (this.earlyWrites.length > 0) {
164-
return Promise.resolve(this.earlyWrites.shift()) as AnyDuringMigration;
159+
const earlyWrite = this.earlyWrites.shift();
160+
if (earlyWrite) {
161+
return Promise.resolve(earlyWrite);
165162
}
166163
const barrier = new Deferred<WriteRequest>();
167164
this.writeSendBarriers.push(barrier);

0 commit comments

Comments
 (0)