Skip to content

Commit 3ee5bdf

Browse files
merlinnotjshcrowthe
authored andcommitted
Remove return Promise.resolve() statements from the codebase (#422)
* Remove `return Promise.resolve()` statements from the codebase. * Fix compilation errors. * Fix more compiler errors. * Use `tslib` module and `importHelpers` Typescript compiler option. It reduces the size of TypeScript packages (namely `firebase-database`, `firebase-firestore`, `firebase-messaging` and the combined `firebase`) be reusing TypeScript helper methods (e.g. `__awaiter`) which were included in every single file relying on features not available in ES5 (emitted during transpilation process). * [AUTOMATED]: Prettier Code Styling * Regenerate top-level yarn.lock * Add `tslib` as a dependency to app, polyfill, storage, template and util packages. Since we set `importHelpers` compiler option to `true` in the base config, we need to add `tslib` to all packages. `*-types` packages are omitted since they do not contain any executable code. * Revert unnecessary change in comment formatting. * Revert moving comments out of empty else block to make the context more clear. Addresses @mikelehen review #422 (review)
1 parent 3aec5ac commit 3ee5bdf

31 files changed

+114
-201
lines changed

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compileOnSave": false,
33
"compilerOptions": {
44
"declaration": true,
5+
"importHelpers": true,
56
"lib": [
67
"es5",
78
"es2015",

integration/messaging/test/utils/test-server.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class MessagingTestServer {
4242
return `http://localhost:${PORT_NUMBER}`;
4343
}
4444

45-
start() {
45+
async start() {
4646
if (this._server) {
47-
return Promise.resolve();
47+
return;
4848
}
4949

5050
return new Promise((resolve, reject) => {
@@ -56,15 +56,11 @@ class MessagingTestServer {
5656

5757
// Sometimes the server doesn't trigger the callback due to
5858
// currently open sockets. So call `closethis._server
59-
stop() {
60-
if (!this._server) {
61-
return Promise.resolve();
59+
async stop() {
60+
if (this._server) {
61+
this._server.close();
62+
this._server = null;
6263
}
63-
64-
this._server.close();
65-
this._server = null;
66-
67-
return Promise.resolve();
6864
}
6965
}
7066
module.exports = new MessagingTestServer();

packages/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"license": "Apache-2.0",
1717
"dependencies": {
1818
"@firebase/app-types": "0.1.1",
19-
"@firebase/util": "0.1.9"
19+
"@firebase/util": "0.1.9",
20+
"tslib": "^1.9.0"
2021
},
2122
"devDependencies": {
2223
"@types/chai": "^4.0.4",

packages/database/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"dependencies": {
2222
"@firebase/database-types": "0.1.1",
2323
"@firebase/util": "0.1.9",
24-
"faye-websocket": "0.11.1"
24+
"faye-websocket": "0.11.1",
25+
"tslib": "^1.9.0"
2526
},
2627
"devDependencies": {
2728
"@types/chai": "^4.0.4",

packages/database/src/api/Database.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,13 @@ export class DatabaseInternals {
132132
constructor(public database: Database) {}
133133

134134
/** @return {Promise<void>} */
135-
delete(): Promise<void> {
135+
async delete(): Promise<void> {
136136
(this.database as any).checkDeleted_('delete');
137137
RepoManager.getInstance().deleteRepo((this.database as any).repo_ as Repo);
138138

139139
(this.database as any).repo_ = null;
140140
(this.database as any).root_ = null;
141141
this.database.INTERNAL = null;
142142
this.database = null;
143-
return Promise.resolve();
144143
}
145144
}

packages/firestore/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"dependencies": {
2525
"@firebase/firestore-types": "0.2.1",
2626
"@firebase/webchannel-wrapper": "0.2.6",
27-
"grpc": "^1.7.1"
27+
"grpc": "^1.7.1",
28+
"tslib": "^1.9.0"
2829
},
2930
"peerDependencies": {
3031
"@firebase/app": "^0.1.0",

packages/firestore/src/api/database.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,9 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
383383
}
384384

385385
INTERNAL = {
386-
delete: (): Promise<void> => {
386+
delete: async (): Promise<void> => {
387387
if (this._firestoreClient) {
388388
return this._firestoreClient.shutdown();
389-
} else {
390-
return Promise.resolve();
391389
}
392390
}
393391
};
@@ -685,13 +683,11 @@ export class WriteBatch implements firestore.WriteBatch {
685683
return this;
686684
}
687685

688-
commit(): Promise<void> {
686+
async commit(): Promise<void> {
689687
this.verifyNotCommitted();
690688
this._committed = true;
691689
if (this._mutations.length > 0) {
692690
return this._firestore.ensureClientConfigured().write(this._mutations);
693-
} else {
694-
return Promise.resolve();
695691
}
696692
}
697693

packages/firestore/src/core/event_manager.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class EventManager {
8787
}
8888
}
8989

90-
unlisten(listener: QueryListener): Promise<void> {
90+
async unlisten(listener: QueryListener): Promise<void> {
9191
const query = listener.query;
9292
let lastListen = false;
9393

@@ -103,8 +103,6 @@ export class EventManager {
103103
if (lastListen) {
104104
this.queries.delete(query);
105105
return this.syncEngine.unlisten(query);
106-
} else {
107-
return Promise.resolve();
108106
}
109107
}
110108

packages/firestore/src/core/firestore_client.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,7 @@ export class FirestoreClient {
371371
): Promise<T> {
372372
// We have to wait for the async queue to be sure syncEngine is initialized.
373373
return this.asyncQueue
374-
.enqueue(() => {
375-
return Promise.resolve();
376-
})
377-
.then(() => {
378-
return this.syncEngine.runTransaction(updateFunction);
379-
});
374+
.enqueue(async () => {})
375+
.then(() => this.syncEngine.runTransaction(updateFunction));
380376
}
381377
}

packages/firestore/src/local/memory_persistence.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,16 @@ export class MemoryPersistence implements Persistence {
4747

4848
private started = false;
4949

50-
start(): Promise<void> {
50+
async start(): Promise<void> {
51+
// No durable state to read on startup.
5152
assert(!this.started, 'MemoryPersistence double-started!');
5253
this.started = true;
53-
// No durable state to read on startup.
54-
return Promise.resolve();
5554
}
5655

57-
shutdown(): Promise<void> {
56+
async shutdown(): Promise<void> {
5857
// No durable state to ensure is closed on shutdown.
5958
assert(this.started, 'MemoryPersistence shutdown without start!');
6059
this.started = false;
61-
return Promise.resolve();
6260
}
6361

6462
getMutationQueue(user: User): MutationQueue {

packages/firestore/src/remote/persistent_stream.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,12 @@ export abstract class PersistentStream<
288288
}
289289

290290
/** Called by the idle timer when the stream should close due to inactivity. */
291-
private handleIdleCloseTimer(): Promise<void> {
291+
private async handleIdleCloseTimer(): Promise<void> {
292292
if (this.isOpen()) {
293293
// When timing out an idle stream there's no reason to force the stream into backoff when
294294
// it restarts so set the stream state to Initial instead of Error.
295295
return this.close(PersistentStreamState.Initial);
296296
}
297-
return Promise.resolve();
298297
}
299298

300299
/** Marks the stream as active again. */
@@ -319,7 +318,7 @@ export abstract class PersistentStream<
319318
* @param finalState the intended state of the stream after closing.
320319
* @param error the error the connection was closed with.
321320
*/
322-
private close(
321+
private async close(
323322
finalState: PersistentStreamState,
324323
error?: FirestoreError
325324
): Promise<void> {
@@ -361,8 +360,6 @@ export abstract class PersistentStream<
361360
// could trigger undesirable recovery logic, etc.).
362361
if (finalState !== PersistentStreamState.Stopped) {
363362
return listener.onClose(error);
364-
} else {
365-
return Promise.resolve();
366363
}
367364
}
368365

@@ -403,16 +400,14 @@ export abstract class PersistentStream<
403400
this.startStream(token);
404401
},
405402
(error: Error) => {
406-
this.queue.enqueue(() => {
403+
this.queue.enqueue(async () => {
407404
if (this.state !== PersistentStreamState.Stopped) {
408405
// Stream can be stopped while waiting for authorization.
409406
const rpcError = new FirestoreError(
410407
Code.UNKNOWN,
411408
'Fetching auth token failed: ' + error.message
412409
);
413410
return this.handleStreamClose(rpcError);
414-
} else {
415-
return Promise.resolve();
416411
}
417412
});
418413
}
@@ -436,12 +431,10 @@ export abstract class PersistentStream<
436431
stream: Stream<SendType, ReceiveType>,
437432
fn: () => Promise<void>
438433
) => {
439-
this.queue.enqueue(() => {
434+
this.queue.enqueue(async () => {
440435
// Only raise events if the stream instance has not changed
441436
if (this.stream === stream) {
442437
return fn();
443-
} else {
444-
return Promise.resolve();
445438
}
446439
});
447440
};
@@ -480,16 +473,15 @@ export abstract class PersistentStream<
480473
);
481474
this.state = PersistentStreamState.Backoff;
482475

483-
this.backoff.backoffAndRun(() => {
476+
this.backoff.backoffAndRun(async () => {
484477
if (this.state === PersistentStreamState.Stopped) {
485478
// Stream can be stopped while waiting for backoff to complete.
486-
return Promise.resolve();
479+
return;
487480
}
488481

489482
this.state = PersistentStreamState.Initial;
490483
this.start(listener);
491484
assert(this.isStarted(), 'PersistentStream should have started');
492-
return Promise.resolve();
493485
});
494486
}
495487

0 commit comments

Comments
 (0)