Skip to content

Commit d0c40aa

Browse files
committed
refactor(database): Add 'noImplicitAny' support
Adds support for the 'noImplicitAny' Typescript compiler option to the database implementation.
1 parent d70ff08 commit d0c40aa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+351
-329
lines changed

src/database.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import * as INTERNAL from './database/api/internal';
2525
import * as TEST_ACCESS from './database/api/test_access';
2626
import { isNodeSdk } from "./utils/environment";
2727

28-
export function registerDatabase(instance) {
28+
export function registerDatabase(instance: FirebaseNamespace) {
2929
// Register the Database Service with the 'firebase' namespace.
3030
const namespace = instance.INTERNAL.registerService(
3131
'database',

src/database/api/Query.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
validateKey,
1414
} from '../core/util/validation';
1515
import { errorPrefix, validateArgCount, validateCallback, validateContextObject } from '../../utils/validation';
16-
import { ValueEventRegistration, ChildEventRegistration } from '../core/view/EventRegistration';
16+
import { ValueEventRegistration, ChildEventRegistration, EventRegistration } from '../core/view/EventRegistration';
1717
import { Deferred, attachDummyErrorHandler } from '../../utils/promise';
1818
import { Repo } from '../core/Repo';
1919
import { QueryParams } from '../core/view/QueryParams';
@@ -154,7 +154,7 @@ export class Query {
154154
if (eventType === 'value') {
155155
this.onValueEvent(callback, ret.cancel, ret.context);
156156
} else {
157-
const callbacks = {};
157+
const callbacks: { [k: string]: typeof callback } = {};
158158
callbacks[eventType] = callback;
159159
this.onChildEvent(callbacks, ret.cancel, ret.context);
160160
}
@@ -179,7 +179,7 @@ export class Query {
179179
* @protected
180180
*/
181181
protected onChildEvent(callbacks: { [k: string]: (a: DataSnapshot, b: string | null) => void },
182-
cancelCallback: ((a: Error) => void) | null, context: Object | null) {
182+
cancelCallback: ((a: Error) => void) | null, context: Object | null) {
183183
const container = new ChildEventRegistration(callbacks, cancelCallback, context);
184184
this.repo.addEventCallbackForQuery(this, container);
185185
}
@@ -195,10 +195,10 @@ export class Query {
195195
validateCallback('Query.off', 2, callback, true);
196196
validateContextObject('Query.off', 3, context, true);
197197

198-
let container = null;
199-
let callbacks = null;
198+
let container: EventRegistration | null = null;
199+
let callbacks: { [k: string]: typeof callback } | null = null;
200200
if (eventType === 'value') {
201-
const valueCallback = /** @type {function(!DataSnapshot)} */ (callback) || null;
201+
const valueCallback = callback || null;
202202
container = new ValueEventRegistration(valueCallback, null, context || null);
203203
} else if (eventType) {
204204
if (callback) {
@@ -219,7 +219,7 @@ export class Query {
219219
* @return {!firebase.Promise}
220220
*/
221221
once(eventType: string, userCallback: (a: DataSnapshot, b?: string) => void,
222-
cancelOrContext?, context?: Object) {
222+
cancelOrContext?: ((a: Error) => void) | Object, context?: Object): Promise<DataSnapshot> {
223223
validateArgCount('Query.once', 1, 4, arguments.length);
224224
validateEventType('Query.once', 1, eventType, false);
225225
validateCallback('Query.once', 2, userCallback, true);
@@ -234,7 +234,7 @@ export class Query {
234234
const deferred = new Deferred();
235235
attachDummyErrorHandler(deferred.promise);
236236

237-
const onceCallback = (snapshot) => {
237+
const onceCallback = (snapshot: DataSnapshot) => {
238238
// NOTE: Even though we unsubscribe, we may get called multiple times if a single action (e.g. set() with JSON)
239239
// triggers multiple events (e.g. child_added or child_changed).
240240
if (firstCall) {
@@ -489,10 +489,10 @@ export class Query {
489489
* @private
490490
*/
491491
private static getCancelAndContextArgs_(fnName: string, cancelOrContext?: ((a: Error) => void) | Object,
492-
context?: Object): { cancel: ((a: Error) => void) | null, context: Object | null } {
493-
const ret = {cancel: null, context: null};
492+
context?: Object): { cancel: ((a: Error) => void) | null, context: Object | null } {
493+
const ret: { cancel: ((a: Error) => void) | null, context: Object | null } = {cancel: null, context: null};
494494
if (cancelOrContext && context) {
495-
ret.cancel = /** @type {function(Error)} */ (cancelOrContext);
495+
ret.cancel = <(a: Error) => void>(cancelOrContext);
496496
validateCallback(fnName, 3, ret.cancel, true);
497497

498498
ret.context = context;

src/database/api/Reference.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ import { SyncPoint } from '../core/SyncPoint';
2424
import { Database } from './Database';
2525
import { DataSnapshot } from './DataSnapshot';
2626

27+
export interface ReferenceConstructor {
28+
new(repo: Repo, path: Path): Reference;
29+
}
30+
2731
export class Reference extends Query {
28-
public then;
29-
public catch;
32+
public then: (a?: any) => Promise<any>;
33+
public catch: (a?: Error) => Promise<any>;
3034

3135
/**
3236
* Call options:
@@ -126,7 +130,7 @@ export class Reference extends Query {
126130
validateWritablePath('Reference.update', this.path);
127131

128132
if (Array.isArray(objectToMerge)) {
129-
const newObjectToMerge = {};
133+
const newObjectToMerge: { [k: string]: any } = {};
130134
for (let i = 0; i < objectToMerge.length; ++i) {
131135
newObjectToMerge['' + i] = objectToMerge[i];
132136
}
@@ -206,7 +210,7 @@ export class Reference extends Query {
206210
attachDummyErrorHandler(deferred.promise);
207211
}
208212

209-
const promiseComplete = function (error, committed, snapshot) {
213+
const promiseComplete = function (error: Error, committed: boolean, snapshot: DataSnapshot) {
210214
if (error) {
211215
deferred.reject(error);
212216
} else {

src/database/api/internal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const isWebSocketsAvailable = function(): boolean {
2424
return WebSocketConnection['isAvailable']();
2525
};
2626

27-
export const setSecurityDebugCallback = function(ref: Reference, callback) {
27+
export const setSecurityDebugCallback = function(ref: Reference, callback: (a: Object) => void) {
2828
(ref.repo.persistentConnection_ as any).securityDebugCallback_ = callback;
2929
};
3030

src/database/api/onDisconnect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class OnDisconnect {
9393
validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
9494
validateWritablePath('OnDisconnect.update', this.path_);
9595
if (Array.isArray(objectToMerge)) {
96-
const newObjectToMerge = {};
96+
const newObjectToMerge: { [k: string]: any } = {};
9797
for (let i = 0; i < objectToMerge.length; ++i) {
9898
newObjectToMerge['' + i] = objectToMerge[i];
9999
}

src/database/core/CompoundWrite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class CompoundWrite {
5353
*/
5454
addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
5555
let newWrite = <CompoundWrite>this;
56-
forEach(updates, function(childKey, node) {
56+
forEach(updates, function(childKey: string, node: Node) {
5757
newWrite = newWrite.addWrite(path.child(childKey), node);
5858
});
5959
return newWrite;
@@ -108,7 +108,7 @@ export class CompoundWrite {
108108
* @return {!Array.<NamedNode>} A list of all complete children.
109109
*/
110110
getCompleteChildren(): Array<NamedNode> {
111-
const children = [];
111+
const children: NamedNode[] = [];
112112
let node = this.writeTree_.value;
113113
if (node != null) {
114114
// If it's a leaf node, it has no children; so nothing to do.

src/database/core/PersistentConnection.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ const SERVER_KILL_INTERRUPT_REASON = 'server_kill';
3030
const INVALID_AUTH_TOKEN_THRESHOLD = 3;
3131

3232
interface ListenSpec {
33-
onComplete(s: string, p?: any);
33+
onComplete(s: string, p?: any): void;
34+
3435
hashFn(): string;
36+
3537
query: Query;
3638
tag: number | null;
3739
}
@@ -87,7 +89,7 @@ export class PersistentConnection extends ServerActions {
8789
* sendRequest(Object),
8890
* close()
8991
* }} */
90-
private realtime_: { sendRequest(a: Object), close() } | null = null;
92+
private realtime_: { sendRequest(a: Object): void, close(): void } | null = null;
9193

9294
/** @private {string|null} */
9395
private authToken_: string | null = null;
@@ -123,7 +125,7 @@ export class PersistentConnection extends ServerActions {
123125
constructor(private repoInfo_: RepoInfo,
124126
private onDataUpdate_: (a: string, b: any, c: boolean, d: number | null) => void,
125127
private onConnectStatus_: (a: boolean) => void,
126-
private onServerInfoUpdate_,
128+
private onServerInfoUpdate_: (a: any) => void,
127129
private authTokenProvider_: AuthTokenProvider,
128130
private authOverride_?: Object | null) {
129131
super();
@@ -194,7 +196,7 @@ export class PersistentConnection extends ServerActions {
194196
const pathString = query.path.toString();
195197
const queryId = query.queryIdentifier();
196198
this.log_('Listen on ' + pathString + ' for ' + queryId);
197-
const req = {/*path*/ 'p': pathString};
199+
const req: { [k: string]: any } = {/*path*/ 'p': pathString};
198200

199201
const action = 'q';
200202

@@ -206,9 +208,9 @@ export class PersistentConnection extends ServerActions {
206208

207209
req[/*hash*/'h'] = listenSpec.hashFn();
208210

209-
this.sendRequest(action, req, (message: Object) => {
210-
const payload = message[/*data*/ 'd'];
211-
const status = message[/*status*/ 's'];
211+
this.sendRequest(action, req, (message: { [k: string]: any }) => {
212+
const payload: any = message[/*data*/ 'd'];
213+
const status: string = message[/*status*/ 's'];
212214

213215
// print warnings in any case...
214216
PersistentConnection.warnOnListenWarnings_(payload, query);
@@ -287,13 +289,13 @@ export class PersistentConnection extends ServerActions {
287289
if (this.connected_ && this.authToken_) {
288290
const token = this.authToken_;
289291
const authMethod = isValidFormat(token) ? 'auth' : 'gauth';
290-
const requestData = {'cred': token};
292+
const requestData: { [k: string]: any } = {'cred': token};
291293
if (this.authOverride_ === null) {
292294
requestData['noauth'] = true;
293295
} else if (typeof this.authOverride_ === 'object') {
294296
requestData['authvar'] = this.authOverride_;
295297
}
296-
this.sendRequest(authMethod, requestData, (res: Object) => {
298+
this.sendRequest(authMethod, requestData, (res: { [k: string]: any }) => {
297299
const status: string = res[/*status*/ 's'];
298300
const data: string = res[/*data*/ 'd'] || 'error';
299301

@@ -329,7 +331,7 @@ export class PersistentConnection extends ServerActions {
329331
private sendUnlisten_(pathString: string, queryId: string, queryObj: Object, tag: number | null) {
330332
this.log_('Unlisten on ' + pathString + ' for ' + queryId);
331333

332-
const req = {/*path*/ 'p': pathString};
334+
const req: { [k: string]: any } = {/*path*/ 'p': pathString};
333335
const action = 'n';
334336
// Only bother sending queryId if it's non-default.
335337
if (tag) {
@@ -391,7 +393,7 @@ export class PersistentConnection extends ServerActions {
391393
private sendOnDisconnect_(action: string, pathString: string, data: any, onComplete: (a: string, b: string) => void) {
392394
const request = {/*path*/ 'p': pathString, /*data*/ 'd': data};
393395
this.log_('onDisconnect ' + action, request);
394-
this.sendRequest(action, request, (response: Object) => {
396+
this.sendRequest(action, request, (response: { [k: string]: any }) => {
395397
if (onComplete) {
396398
setTimeout(function () {
397399
onComplete(response[/*status*/ 's'], response[/* data */'d']);
@@ -416,7 +418,7 @@ export class PersistentConnection extends ServerActions {
416418

417419
putInternal(action: string, pathString: string, data: any,
418420
onComplete: (a: string, b: string | null) => void, hash?: string) {
419-
const request = {/*path*/ 'p': pathString, /*data*/ 'd': data};
421+
const request: { [k: string]: any } = {/*path*/ 'p': pathString, /*data*/ 'd': data};
420422

421423
if (hash !== undefined)
422424
request[/*hash*/ 'h'] = hash;
@@ -444,7 +446,7 @@ export class PersistentConnection extends ServerActions {
444446
const onComplete = this.outstandingPuts_[index].onComplete;
445447
this.outstandingPuts_[index].queued = this.connected_;
446448

447-
this.sendRequest(action, request, (message: Object) => {
449+
this.sendRequest(action, request, (message: { [k: string]: any }) => {
448450
this.log_(action + ' response', message);
449451

450452
delete this.outstandingPuts_[index];
@@ -483,7 +485,7 @@ export class PersistentConnection extends ServerActions {
483485
* @param {*} message
484486
* @private
485487
*/
486-
private onDataMessage_(message: Object) {
488+
private onDataMessage_(message: { [k: string]: any }) {
487489
if ('r' in message) {
488490
// this is a response
489491
this.log_('from server: ' + stringify(message));
@@ -501,7 +503,7 @@ export class PersistentConnection extends ServerActions {
501503
}
502504
}
503505

504-
private onDataPush_(action: string, body: Object) {
506+
private onDataPush_(action: string, body: { [k: string]: any }) {
505507
this.log_('handleServerMessage', action, body);
506508
if (action === 'd')
507509
this.onDataUpdate_(body[/*path*/ 'p'], body[/*data*/ 'd'], /*isMerge*/false, body['t']);
@@ -629,7 +631,7 @@ export class PersistentConnection extends ServerActions {
629631
const self = this;
630632
const lastSessionId = this.lastSessionId;
631633
let canceled = false;
632-
let connection = null;
634+
let connection: Connection | null = null;
633635
const closeFn = function () {
634636
if (connection) {
635637
connection.close();
@@ -638,7 +640,7 @@ export class PersistentConnection extends ServerActions {
638640
onDisconnect();
639641
}
640642
};
641-
const sendRequestFn = function (msg) {
643+
const sendRequestFn = function (msg: Object) {
642644
assert(connection, 'sendRequest call when we\'re not connected not allowed.');
643645
connection.sendRequest(msg);
644646
};
@@ -761,7 +763,7 @@ export class PersistentConnection extends ServerActions {
761763
* @return {{queries:Array.<Query>, onComplete:function(string)}}
762764
* @private
763765
*/
764-
private removeListen_(pathString, queryId): ListenSpec {
766+
private removeListen_(pathString: string, queryId: string): ListenSpec {
765767
const normalizedPathString = new Path(pathString).toString(); // normalize path.
766768
let listen;
767769
if (this.listens_[normalizedPathString] !== undefined) {
@@ -798,7 +800,7 @@ export class PersistentConnection extends ServerActions {
798800
}
799801
}
800802

801-
private onSecurityDebugPacket_(body: Object) {
803+
private onSecurityDebugPacket_(body: { [k: string]: any }) {
802804
if (this.securityDebugCallback_) {
803805
this.securityDebugCallback_(body);
804806
} else {
@@ -836,7 +838,7 @@ export class PersistentConnection extends ServerActions {
836838
* @private
837839
*/
838840
private sendConnectStats_() {
839-
const stats = {};
841+
const stats: { [k: string]: number } = {};
840842

841843
let clientName = 'js';
842844
if (CONSTANTS.NODE_ADMIN) {

0 commit comments

Comments
 (0)