Skip to content

Commit 819d54d

Browse files
committed
refactor(database): Several classes and utility methods
1 parent 9ea07fb commit 819d54d

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

+3248
-3372
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: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
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-
zimport { validateUrl } from "../core/util/validation";
10+
zimport { 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;
18+
export class Database implements FirebaseService {
19+
/** @type {Reference} */
20+
private root_: Reference;
2021

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

2733
/**
2834
* The constructor should not be called by users of our public API.
29-
* @param {!Repo} repo
35+
* @param {!Repo} repo_
3036
*/
31-
constructor(repo) {
32-
if (!(repo instanceof Repo)) {
33-
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().');
3440
}
3541

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

4245
this.INTERNAL = new DatabaseInternals(this);
4346
}
@@ -49,7 +52,7 @@ export class Database {
4952
/**
5053
* Returns a reference to the root or the path specified in opt_pathString.
5154
* @param {string=} pathString
52-
* @return {!Firebase} Firebase reference.
55+
* @return {!Reference} Firebase reference.
5356
*/
5457
ref(pathString?: string): Reference {
5558
this.checkDeleted_('ref');
@@ -63,20 +66,21 @@ export class Database {
6366
* We throw a exception if the url is not in the same domain as the
6467
* current repo.
6568
* @param {string} url
66-
* @return {!Firebase} Firebase reference.
69+
* @return {!Reference} Firebase reference.
6770
*/
68-
refFromURL(url) {
71+
refFromURL(url: string): Reference {
6972
/** @const {string} */
70-
var apiName = 'database.refFromURL';
73+
const apiName = 'database.refFromURL';
7174
this.checkDeleted_(apiName);
7275
validateArgCount(apiName, 1, 1, arguments.length);
73-
var parsedURL = parseRepoInfo(url);
76+
const parsedURL = parseRepoInfo(url);
7477
validateUrl(apiName, 1, parsedURL);
7578

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

8286
return this.ref(parsedURL.path.toString());
@@ -85,9 +89,9 @@ export class Database {
8589
/**
8690
* @param {string} apiName
8791
*/
88-
private checkDeleted_(apiName) {
92+
private checkDeleted_(apiName: string) {
8993
if (this.repo_ === null) {
90-
fatal("Cannot call " + apiName + " on a deleted database.");
94+
fatal('Cannot call ' + apiName + ' on a deleted database.');
9195
}
9296
}
9397

@@ -98,36 +102,34 @@ export class Database {
98102
this.repo_.interrupt();
99103
}
100104

101-
goOnline () {
105+
goOnline() {
102106
validateArgCount('database.goOnline', 0, 0, arguments.length);
103107
this.checkDeleted_('goOnline');
104108
this.repo_.resume();
105109
}
106-
};
110+
}
107111

108112
Object.defineProperty(Repo.prototype, 'database', {
109-
get() {
113+
get(): Database {
110114
return this.__database || (this.__database = new Database(this));
111115
}
112116
});
113117

114-
class DatabaseInternals {
115-
database
118+
export class DatabaseInternals {
116119
/** @param {!Database} database */
117-
constructor(database) {
118-
this.database = database;
120+
constructor(public database: Database) {
119121
}
120122

121-
/** @return {firebase.Promise<void>} */
122-
delete() {
123-
this.database.checkDeleted_('delete');
124-
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ (this.database.repo_));
123+
/** @return {Promise<void>} */
124+
delete(): Promise<void> {
125+
(<any>this.database).checkDeleted_('delete');
126+
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ <Repo>((<any>this.database).repo_));
125127

126-
this.database.repo_ = null;
127-
this.database.root_ = null;
128+
(<any>this.database).repo_ = null;
129+
(<any>this.database).root_ = null;
128130
this.database.INTERNAL = null;
129131
this.database = null;
130132
return PromiseImpl.resolve();
131133
}
132-
};
134+
}
133135

src/database/api/Query.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class Query {
171171
* @param {?Object} context
172172
* @protected
173173
*/
174-
onValueEvent(callback: (a: DataSnapshot) => any, cancelCallback: ((a: Error) => any) | null, context: Object | null) {
174+
protected onValueEvent(callback: (a: DataSnapshot) => void, cancelCallback: ((a: Error) => void) | null, context: Object | null) {
175175
const container = new ValueEventRegistration(callback, cancelCallback || null, context || null);
176176
this.repo.addEventCallbackForQuery(this, container);
177177
}
@@ -180,6 +180,7 @@ export class Query {
180180
* @param {!Object.<string, !function(!DataSnapshot, ?string)>} callbacks
181181
* @param {?function(Error)} cancelCallback
182182
* @param {?Object} context
183+
* @protected
183184
*/
184185
onChildEvent(callbacks: { [k: string]: SnapshotCallback },
185186
cancelCallback: ((a: Error) => any) | null, context: Object | null) {
@@ -492,8 +493,8 @@ export class Query {
492493
* @return {{cancel: ?function(Error), context: ?Object}}
493494
* @private
494495
*/
495-
private static getCancelAndContextArgs_(fnName: string, cancelOrContext?: ((a: Error) => any) | Object,
496-
context?: Object): { cancel: ((a: Error) => any) | null, context: Object | null } {
496+
private static getCancelAndContextArgs_(fnName: string, cancelOrContext?: ((a: Error) => void) | Object,
497+
context?: Object): { cancel: ((a: Error) => void) | null, context: Object | null } {
497498
const ret = {cancel: null, context: null};
498499
if (cancelOrContext && context) {
499500
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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
*/
911
constructor(public committed, public snapshot) {}
1012
}

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
};

src/database/api/onDisconnect.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import {
1+
import {
22
validateArgCount,
33
validateCallback
4-
} from "../../utils/validation";
4+
} from '../../utils/validation';
55
import {
66
validateWritablePath,
77
validateFirebaseDataArg,
88
validatePriority,
99
validateFirebaseMergeDataArg,
10-
} from "../core/util/validation";
11-
import { warn } from "../core/util/util";
12-
import { Deferred } from "../../utils/promise";
10+
} from '../core/util/validation';
11+
import { warn } from '../core/util/util';
12+
import { Deferred } from '../../utils/promise';
1313
import { Repo } from '../core/Repo';
1414
import { Path } from '../core/util/Path';
1515

@@ -29,7 +29,7 @@ export class OnDisconnect {
2929
* @param {function(?Error)=} onComplete
3030
* @return {!firebase.Promise}
3131
*/
32-
cancel(onComplete?) {
32+
cancel(onComplete?: (a: Error | null) => void): Promise<void> {
3333
validateArgCount('OnDisconnect.cancel', 0, 1, arguments.length);
3434
validateCallback('OnDisconnect.cancel', 1, onComplete, true);
3535
const deferred = new Deferred();
@@ -41,7 +41,7 @@ export class OnDisconnect {
4141
* @param {function(?Error)=} onComplete
4242
* @return {!firebase.Promise}
4343
*/
44-
remove(onComplete?) {
44+
remove(onComplete?: (a: Error | null) => void): Promise<void> {
4545
validateArgCount('OnDisconnect.remove', 0, 1, arguments.length);
4646
validateWritablePath('OnDisconnect.remove', this.path_);
4747
validateCallback('OnDisconnect.remove', 1, onComplete, true);
@@ -55,7 +55,7 @@ export class OnDisconnect {
5555
* @param {function(?Error)=} onComplete
5656
* @return {!firebase.Promise}
5757
*/
58-
set(value, onComplete?) {
58+
set(value: any, onComplete?: (a: Error | null) => void): Promise<void> {
5959
validateArgCount('OnDisconnect.set', 1, 2, arguments.length);
6060
validateWritablePath('OnDisconnect.set', this.path_);
6161
validateFirebaseDataArg('OnDisconnect.set', 1, value, this.path_, false);
@@ -71,7 +71,7 @@ export class OnDisconnect {
7171
* @param {function(?Error)=} onComplete
7272
* @return {!firebase.Promise}
7373
*/
74-
setWithPriority(value, priority, onComplete?) {
74+
setWithPriority(value: any, priority: number | string | null, onComplete?: (a: Error | null) => void): Promise<void> {
7575
validateArgCount('OnDisconnect.setWithPriority', 2, 3, arguments.length);
7676
validateWritablePath('OnDisconnect.setWithPriority', this.path_);
7777
validateFirebaseDataArg('OnDisconnect.setWithPriority',
@@ -89,7 +89,7 @@ export class OnDisconnect {
8989
* @param {function(?Error)=} onComplete
9090
* @return {!firebase.Promise}
9191
*/
92-
update(objectToMerge, onComplete?) {
92+
update(objectToMerge: object, onComplete?: (a: Error | null) => void): Promise<void> {
9393
validateArgCount('OnDisconnect.update', 1, 2, arguments.length);
9494
validateWritablePath('OnDisconnect.update', this.path_);
9595
if (Array.isArray(objectToMerge)) {

0 commit comments

Comments
 (0)