Skip to content

Commit 0c6e9da

Browse files
committed
refactor(database): Several classes and utility methods
1 parent 71e4440 commit 0c6e9da

Some content is hidden

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

69 files changed

+3304
-3445
lines changed

src/database/api/DataSnapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class DataSnapshot {
112112
* @return {boolean} True if forEach was canceled by action returning true for
113113
* one of the child nodes.
114114
*/
115-
forEach(action: (d: DataSnapshot) => any): boolean {
115+
forEach(action: (d: DataSnapshot) => void): boolean {
116116
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
117117
validateCallback('DataSnapshot.forEach', 1, action, false);
118118

src/database/api/Database.ts

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,56 @@
1-
import { fatal } from "../core/util/util";
2-
import { parseRepoInfo } from "../core/util/libs/parser";
3-
import { Path } from "../core/util/Path";
4-
import { PromiseImpl } from "../../utils/promise";
5-
import { Reference } from "./Reference";
6-
import { Repo } from "../core/Repo";
7-
import { RepoManager } from "../core/RepoManager";
8-
import { validateArgCount } from "../../utils/validation";
1+
import { fatal } from '../core/util/util';
2+
import { parseRepoInfo } from '../core/util/libs/parser';
3+
import { Path } from '../core/util/Path';
4+
import { PromiseImpl } from '../../utils/promise';
5+
import { Reference } from './Reference';
6+
import { Repo } from '../core/Repo';
7+
import { RepoManager } from '../core/RepoManager';
8+
import { validateArgCount } from '../../utils/validation';
99
import { FirebaseApp } from "../../app/firebase_app";
10-
import { validateUrl } from "../core/util/validation";
10+
import { validateUrl } from '../core/util/validation';
11+
import { FirebaseApp, FirebaseService } from '../../app/firebase_app';
12+
import { RepoInfo } from '../core/RepoInfo';
1113

1214
/**
1315
* Class representing a firebase database.
14-
* @implements {firebase.Service}
16+
* @implements {FirebaseService}
1517
*/
16-
export class Database {
17-
repo_: Repo;
18-
root_: Reference;
19-
INTERNAL;
20-
private __database: Database;
18+
export class Database implements FirebaseService {
19+
/** @type {Reference} */
20+
private root_: Reference;
2121

22-
static ServerValue = {
22+
/** @type {DatabaseInternals} */
23+
INTERNAL: DatabaseInternals;
24+
25+
app: FirebaseApp | null;
26+
27+
static readonly ServerValue = {
2328
'TIMESTAMP': {
24-
'.sv' : 'timestamp'
29+
'.sv': 'timestamp'
2530
}
26-
}
31+
};
2732

2833
/**
2934
* The constructor should not be called by users of our public API.
30-
* @param {!Repo} repo
35+
* @param {!Repo} repo_
3136
*/
32-
constructor(repo) {
33-
if (!(repo instanceof Repo)) {
34-
fatal("Don't call new Database() directly - please use firebase.database().");
37+
constructor(private repo_: Repo) {
38+
if (!(repo_ instanceof Repo)) {
39+
fatal('Don\'t call new Database() directly - please use firebase.database().');
3540
}
3641

37-
/** @type {Repo} */
38-
this.repo_ = repo;
39-
40-
/** @type {Firebase} */
41-
this.root_ = new Reference(repo, Path.Empty);
42+
/** @type {Reference} */
43+
this.root_ = new Reference(repo_, Path.Empty);
4244

4345
this.INTERNAL = new DatabaseInternals(this);
4446
}
4547

46-
get app(): FirebaseApp {
47-
return this.repo_.app;
48-
}
49-
50-
get database(): Database {
51-
return this.__database || (this.__database = new Database(this));
52-
}
53-
5448
/**
5549
* Returns a reference to the root or the path specified in opt_pathString.
5650
* @param {string=} pathString
57-
* @return {!Firebase} Firebase reference.
51+
* @return {!Reference} Firebase reference.
5852
*/
59-
ref(pathString?): Reference {
53+
ref(pathString?: string): Reference {
6054
this.checkDeleted_('ref');
6155
validateArgCount('database.ref', 0, 1, arguments.length);
6256

@@ -68,20 +62,21 @@ export class Database {
6862
* We throw a exception if the url is not in the same domain as the
6963
* current repo.
7064
* @param {string} url
71-
* @return {!Firebase} Firebase reference.
65+
* @return {!Reference} Firebase reference.
7266
*/
73-
refFromURL(url) {
67+
refFromURL(url: string): Reference {
7468
/** @const {string} */
75-
var apiName = 'database.refFromURL';
69+
const apiName = 'database.refFromURL';
7670
this.checkDeleted_(apiName);
7771
validateArgCount(apiName, 1, 1, arguments.length);
78-
var parsedURL = parseRepoInfo(url);
72+
const parsedURL = parseRepoInfo(url);
7973
validateUrl(apiName, 1, parsedURL);
8074

81-
var repoInfo = parsedURL.repoInfo;
82-
if (repoInfo.host !== this.repo_.repoInfo_.host) {
83-
fatal(apiName + ": Host name does not match the current database: " +
84-
"(found " + repoInfo.host + " but expected " + this.repo_.repoInfo_.host + ")");
75+
const repoInfo = parsedURL.repoInfo;
76+
if (repoInfo.host !== (<RepoInfo>(<any>this.repo_).repoInfo_).host) {
77+
fatal(apiName + ': Host name does not match the current database: ' +
78+
'(found ' + repoInfo.host + ' but expected ' +
79+
(<RepoInfo>(<any>this.repo_).repoInfo_).host + ')');
8580
}
8681

8782
return this.ref(parsedURL.path.toString());
@@ -90,9 +85,9 @@ export class Database {
9085
/**
9186
* @param {string} apiName
9287
*/
93-
private checkDeleted_(apiName) {
88+
private checkDeleted_(apiName: string) {
9489
if (this.repo_ === null) {
95-
fatal("Cannot call " + apiName + " on a deleted database.");
90+
fatal('Cannot call ' + apiName + ' on a deleted database.');
9691
}
9792
}
9893

@@ -103,30 +98,29 @@ export class Database {
10398
this.repo_.interrupt();
10499
}
105100

106-
goOnline () {
101+
goOnline() {
107102
validateArgCount('database.goOnline', 0, 0, arguments.length);
108103
this.checkDeleted_('goOnline');
109104
this.repo_.resume();
110105
}
111-
};
106+
}
112107

113108
class DatabaseInternals {
114109
database
115110
/** @param {!Database} database */
116-
constructor(database) {
117-
this.database = database;
111+
constructor(public database: Database) {
118112
}
119113

120-
/** @return {firebase.Promise<void>} */
121-
delete() {
122-
this.database.checkDeleted_('delete');
123-
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ (this.database.repo_));
114+
/** @return {Promise<void>} */
115+
delete(): Promise<void> {
116+
(<any>this.database).checkDeleted_('delete');
117+
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ <Repo>((<any>this.database).repo_));
124118

125-
this.database.repo_ = null;
126-
this.database.root_ = null;
119+
(<any>this.database).repo_ = null;
120+
(<any>this.database).root_ = null;
127121
this.database.INTERNAL = null;
128122
this.database = null;
129123
return PromiseImpl.resolve();
130124
}
131-
};
125+
}
132126

src/database/api/Query.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export class Query {
143143
* @param {Object=} context
144144
* @return {!function(DataSnapshot, string=)}
145145
*/
146-
on(eventType: string, callback: (a: DataSnapshot, b?: string) => any,
147-
cancelCallbackOrContext?: ((a: Error) => any) | Object, context?: Object): (a: DataSnapshot, b?: string) => any {
146+
on(eventType: string, callback: (a: DataSnapshot, b?: string) => void,
147+
cancelCallbackOrContext?: ((a: Error) => void) | Object, context?: Object): (a: DataSnapshot, b?: string) => void {
148148
validateArgCount('Query.on', 2, 4, arguments.length);
149149
validateEventType('Query.on', 1, eventType, false);
150150
validateCallback('Query.on', 2, callback, false);
@@ -167,7 +167,7 @@ export class Query {
167167
* @param {?Object} context
168168
* @protected
169169
*/
170-
onValueEvent(callback: (a: DataSnapshot) => any, cancelCallback: ((a: Error) => any) | null, context: Object | null) {
170+
protected onValueEvent(callback: (a: DataSnapshot) => void, cancelCallback: ((a: Error) => void) | null, context: Object | null) {
171171
const container = new ValueEventRegistration(callback, cancelCallback || null, context || null);
172172
this.repo.addEventCallbackForQuery(this, container);
173173
}
@@ -176,9 +176,10 @@ export class Query {
176176
* @param {!Object.<string, !function(!DataSnapshot, ?string)>} callbacks
177177
* @param {?function(Error)} cancelCallback
178178
* @param {?Object} context
179+
* @protected
179180
*/
180-
onChildEvent(callbacks: { [k: string]: (a: DataSnapshot, b: string | null) => any },
181-
cancelCallback: ((a: Error) => any) | null, context: Object | null) {
181+
protected onChildEvent(callbacks: { [k: string]: (a: DataSnapshot, b: string | null) => void },
182+
cancelCallback: ((a: Error) => void) | null, context: Object | null) {
182183
const container = new ChildEventRegistration(callbacks, cancelCallback, context);
183184
this.repo.addEventCallbackForQuery(this, container);
184185
}
@@ -188,7 +189,7 @@ export class Query {
188189
* @param {(function(!DataSnapshot, ?string=))=} callback
189190
* @param {Object=} context
190191
*/
191-
off(eventType?: string, callback?: (a: DataSnapshot, b?: string | null) => any, context?: Object) {
192+
off(eventType?: string, callback?: (a: DataSnapshot, b?: string | null) => void, context?: Object) {
192193
validateArgCount('Query.off', 0, 3, arguments.length);
193194
validateEventType('Query.off', 1, eventType, true);
194195
validateCallback('Query.off', 2, callback, true);
@@ -217,7 +218,7 @@ export class Query {
217218
* @param context
218219
* @return {!firebase.Promise}
219220
*/
220-
once(eventType: string, userCallback: (a: DataSnapshot, b?: string) => any,
221+
once(eventType: string, userCallback: (a: DataSnapshot, b?: string) => void,
221222
cancelOrContext?, context?: Object) {
222223
validateArgCount('Query.once', 1, 4, arguments.length);
223224
validateEventType('Query.once', 1, eventType, false);
@@ -487,8 +488,8 @@ export class Query {
487488
* @return {{cancel: ?function(Error), context: ?Object}}
488489
* @private
489490
*/
490-
private static getCancelAndContextArgs_(fnName: string, cancelOrContext?: ((a: Error) => any) | Object,
491-
context?: Object): { cancel: ((a: Error) => any) | null, context: Object | null } {
491+
private static getCancelAndContextArgs_(fnName: string, cancelOrContext?: ((a: Error) => void) | Object,
492+
context?: Object): { cancel: ((a: Error) => void) | null, context: Object | null } {
492493
const ret = {cancel: null, context: null};
493494
if (cancelOrContext && context) {
494495
ret.cancel = /** @type {function(Error)} */ (cancelOrContext);

src/database/api/Reference.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Reference extends Query {
105105
* @param {function(?Error)=} onComplete
106106
* @return {!Promise}
107107
*/
108-
set(newVal: any, onComplete?: (a: Error | null) => any): Promise<any> {
108+
set(newVal: any, onComplete?: (a: Error | null) => void): Promise<any> {
109109
validateArgCount('Reference.set', 1, 2, arguments.length);
110110
validateWritablePath('Reference.set', this.path);
111111
validateFirebaseDataArg('Reference.set', 1, newVal, this.path, false);
@@ -121,7 +121,7 @@ export class Reference extends Query {
121121
* @param {function(?Error)=} onComplete
122122
* @return {!Promise}
123123
*/
124-
update(objectToMerge: Object, onComplete?: (a: Error | null) => any): Promise<any> {
124+
update(objectToMerge: Object, onComplete?: (a: Error | null) => void): Promise<any> {
125125
validateArgCount('Reference.update', 1, 2, arguments.length);
126126
validateWritablePath('Reference.update', this.path);
127127

@@ -151,7 +151,7 @@ export class Reference extends Query {
151151
* @return {!Promise}
152152
*/
153153
setWithPriority(newVal: any, newPriority: string | number | null,
154-
onComplete?: (a: Error | null) => any): Promise<any> {
154+
onComplete?: (a: Error | null) => void): Promise<any> {
155155
validateArgCount('Reference.setWithPriority', 2, 3, arguments.length);
156156
validateWritablePath('Reference.setWithPriority', this.path);
157157
validateFirebaseDataArg('Reference.setWithPriority', 1, newVal, this.path, false);
@@ -170,7 +170,7 @@ export class Reference extends Query {
170170
* @param {function(?Error)=} onComplete
171171
* @return {!Promise}
172172
*/
173-
remove(onComplete?: (a: Error | null) => any): Promise<any> {
173+
remove(onComplete?: (a: Error | null) => void): Promise<any> {
174174
validateArgCount('Reference.remove', 0, 1, arguments.length);
175175
validateWritablePath('Reference.remove', this.path);
176176
validateCallback('Reference.remove', 1, onComplete, true);
@@ -185,7 +185,7 @@ export class Reference extends Query {
185185
* @return {!Promise}
186186
*/
187187
transaction(transactionUpdate: (a: any) => any,
188-
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => any,
188+
onComplete?: (a: Error | null, b: boolean, c: DataSnapshot | null) => void,
189189
applyLocally?: boolean): Promise<any> {
190190
validateArgCount('Reference.transaction', 1, 3, arguments.length);
191191
validateWritablePath('Reference.transaction', this.path);
@@ -226,7 +226,7 @@ export class Reference extends Query {
226226
* @param {function(?Error)=} onComplete
227227
* @return {!Promise}
228228
*/
229-
setPriority(priority: string | number | null, onComplete?: (a: Error | null) => any): Promise<any> {
229+
setPriority(priority: string | number | null, onComplete?: (a: Error | null) => void): Promise<any> {
230230
validateArgCount('Reference.setPriority', 1, 2, arguments.length);
231231
validateWritablePath('Reference.setPriority', this.path);
232232
validatePriority('Reference.setPriority', 1, priority, false);
@@ -242,7 +242,7 @@ export class Reference extends Query {
242242
* @param {function(?Error)=} onComplete
243243
* @return {!Reference}
244244
*/
245-
push(value?: any, onComplete?: (a: Error | null) => any): Reference {
245+
push(value?: any, onComplete?: (a: Error | null) => void): Reference {
246246
validateArgCount('Reference.push', 0, 2, arguments.length);
247247
validateWritablePath('Reference.push', this.path);
248248
validateFirebaseDataArg('Reference.push', 1, value, this.path, true);

src/database/api/TransactionResult.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
import { DataSnapshot } from './DataSnapshot';
2+
13
export class TransactionResult {
24
/**
35
* A type for the resolve value of Firebase.transaction.
46
* @constructor
57
* @dict
68
* @param {boolean} committed
7-
* @param {fb.api.DataSnapshot} snapshot
9+
* @param {DataSnapshot} snapshot
810
*/
9-
constructor(committed, snapshot) {
10-
/**
11-
* @type {boolean}
12-
*/
13-
this['committed'] = committed;
14-
/**
15-
* @type {fb.api.DataSnapshot}
16-
*/
17-
this['snapshot'] = snapshot;
11+
constructor(public committed: boolean, public snapshot: DataSnapshot) {
1812
}
1913
}

src/database/api/internal.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { WebSocketConnection } from "../realtime/WebSocketConnection";
22
import { BrowserPollConnection } from "../realtime/BrowserPollConnection";
3+
import { Reference } from './Reference';
34

45
/**
56
* INTERNAL methods for internal-use only (tests, etc.).
@@ -19,26 +20,26 @@ export const forceWebSockets = function() {
1920
};
2021

2122
/* Used by App Manager */
22-
export const isWebSocketsAvailable = function() {
23+
export const isWebSocketsAvailable = function(): boolean {
2324
return WebSocketConnection['isAvailable']();
2425
};
2526

26-
export const setSecurityDebugCallback = function(ref, callback) {
27-
ref.repo.persistentConnection_.securityDebugCallback_ = callback;
27+
export const setSecurityDebugCallback = function(ref: Reference, callback) {
28+
(ref.repo.persistentConnection_ as any).securityDebugCallback_ = callback;
2829
};
2930

30-
export const stats = function(ref, showDelta) {
31+
export const stats = function(ref: Reference, showDelta?: boolean) {
3132
ref.repo.stats(showDelta);
3233
};
3334

34-
export const statsIncrementCounter = function(ref, metric) {
35+
export const statsIncrementCounter = function(ref: Reference, metric: string) {
3536
ref.repo.statsIncrementCounter(metric);
3637
};
3738

38-
export const dataUpdateCount = function(ref) {
39+
export const dataUpdateCount = function(ref: Reference): number {
3940
return ref.repo.dataUpdateCount;
4041
};
4142

42-
export const interceptServerData = function(ref, callback) {
43+
export const interceptServerData = function(ref: Reference, callback: ((a: string, b: any) => void) | null) {
4344
return ref.repo.interceptServerData_(callback);
4445
};

0 commit comments

Comments
 (0)