Skip to content

Commit f86c8e9

Browse files
committed
refactor(database): Several classes and utility methods
1 parent 7efa1b0 commit f86c8e9

Some content is hidden

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

62 files changed

+3224
-3364
lines changed

src/database/api/Database.ts

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,55 @@
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";
9-
import { validateUrl } from "../core/util/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';
9+
import { validateUrl } from '../core/util/validation';
10+
import { FirebaseApp, FirebaseService } from '../../app/firebase_app';
11+
import { RepoInfo } from '../core/RepoInfo';
1012

1113
/**
1214
* Class representing a firebase database.
13-
* @implements {firebase.Service}
15+
* @implements {FirebaseService}
1416
*/
15-
export class Database {
16-
/** @type {Repo} */
17-
repo_;
18-
/** @type {Firebase} */
19-
root_;
20-
INTERNAL;
21-
22-
static ServerValue = {
17+
export class Database implements FirebaseService {
18+
/** @type {Reference} */
19+
private root_: Reference;
20+
21+
/** @type {DatabaseInternals} */
22+
INTERNAL: DatabaseInternals;
23+
24+
app: FirebaseApp | null;
25+
26+
static readonly ServerValue = {
2327
'TIMESTAMP': {
24-
'.sv' : 'timestamp'
28+
'.sv': 'timestamp'
2529
}
26-
}
30+
};
2731

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

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

4344
this.INTERNAL = new DatabaseInternals(this);
4445
}
4546

46-
app: null
47-
4847
/**
4948
* Returns a reference to the root or the path specified in opt_pathString.
5049
* @param {string=} opt_pathString
51-
* @return {!Firebase} Firebase reference.
50+
* @return {!Reference} Firebase reference.
5251
*/
53-
ref(opt_pathString): Reference {
52+
ref(opt_pathString?: string): Reference {
5453
this.checkDeleted_('ref');
5554
validateArgCount('database.ref', 0, 1, arguments.length);
5655

@@ -62,20 +61,21 @@ export class Database {
6261
* We throw a exception if the url is not in the same domain as the
6362
* current repo.
6463
* @param {string} url
65-
* @return {!Firebase} Firebase reference.
64+
* @return {!Reference} Firebase reference.
6665
*/
67-
refFromURL(url) {
66+
refFromURL(url: string): Reference {
6867
/** @const {string} */
69-
var apiName = 'database.refFromURL';
68+
const apiName = 'database.refFromURL';
7069
this.checkDeleted_(apiName);
7170
validateArgCount(apiName, 1, 1, arguments.length);
72-
var parsedURL = parseRepoInfo(url);
71+
const parsedURL = parseRepoInfo(url);
7372
validateUrl(apiName, 1, parsedURL);
7473

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

8181
return this.ref(parsedURL.path.toString());
@@ -85,9 +85,9 @@ export class Database {
8585
* @param {string} apiName
8686
* @private
8787
*/
88-
checkDeleted_(apiName) {
88+
private checkDeleted_(apiName: string) {
8989
if (this.repo_ === null) {
90-
fatal("Cannot call " + apiName + " on a deleted database.");
90+
fatal('Cannot call ' + apiName + ' on a deleted database.');
9191
}
9292
}
9393

@@ -98,47 +98,45 @@ export class Database {
9898
this.repo_.interrupt();
9999
}
100100

101-
goOnline () {
101+
goOnline() {
102102
validateArgCount('database.goOnline', 0, 0, arguments.length);
103103
this.checkDeleted_('goOnline');
104104
this.repo_.resume();
105105
}
106-
};
106+
}
107107

108108
// Note: This is an un-minfied property of the Database only.
109109
Object.defineProperty(Database.prototype, 'app', {
110110
/**
111111
* @this {!Database}
112112
* @return {!firebase.app.App}
113113
*/
114-
get() {
114+
get(): FirebaseApp {
115115
return this.repo_.app;
116116
}
117117
});
118118

119119
Object.defineProperty(Repo.prototype, 'database', {
120-
get() {
120+
get(): Database {
121121
return this.__database || (this.__database = new Database(this));
122122
}
123123
});
124124

125-
class DatabaseInternals {
126-
database
125+
export class DatabaseInternals {
127126
/** @param {!Database} database */
128-
constructor(database) {
129-
this.database = database;
127+
constructor(public database: Database) {
130128
}
131129

132-
/** @return {firebase.Promise<void>} */
133-
delete() {
134-
this.database.checkDeleted_('delete');
135-
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ (this.database.repo_));
130+
/** @return {Promise<void>} */
131+
delete(): Promise<void> {
132+
(<any>this.database).checkDeleted_('delete');
133+
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ <Repo>((<any>this.database).repo_));
136134

137-
this.database.repo_ = null;
138-
this.database.root_ = null;
135+
(<any>this.database).repo_ = null;
136+
(<any>this.database).root_ = null;
139137
this.database.INTERNAL = null;
140138
this.database = null;
141139
return PromiseImpl.resolve();
142140
}
143-
};
141+
}
144142

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) => any) | null) {
4344
return ref.repo.interceptServerData_(callback);
4445
};

src/database/api/test_access.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ import { RepoInfo } from "../core/RepoInfo";
22
import { PersistentConnection } from "../core/PersistentConnection";
33
import { RepoManager } from "../core/RepoManager";
44
import { Connection } from "../realtime/Connection";
5+
import { Query } from './Query';
56

67
export const DataConnection = PersistentConnection;
78

89
/**
910
* @param {!string} pathString
1011
* @param {function(*)} onComplete
1112
*/
12-
(PersistentConnection.prototype as any).simpleListen = function(pathString, onComplete) {
13+
(PersistentConnection.prototype as any).simpleListen = function(pathString: string, onComplete: (a: any) => any) {
1314
this.sendRequest('q', {'p': pathString}, onComplete);
1415
};
1516

1617
/**
1718
* @param {*} data
1819
* @param {function(*)} onEcho
1920
*/
20-
(PersistentConnection.prototype as any).echo = function(data, onEcho) {
21+
(PersistentConnection.prototype as any).echo = function(data: any, onEcho: (a: any) => any) {
2122
this.sendRequest('echo', {'d': data}, onEcho);
2223
};
2324

@@ -28,8 +29,8 @@ export const RealTimeConnection = Connection;
2829
* @param {function(): string} newHash
2930
* @return {function()}
3031
*/
31-
export const hijackHash = function(newHash) {
32-
var oldPut = PersistentConnection.prototype.put;
32+
export const hijackHash = function(newHash: () => string) {
33+
const oldPut = PersistentConnection.prototype.put;
3334
PersistentConnection.prototype.put = function(pathString, data, opt_onComplete, opt_hash) {
3435
if (opt_hash !== undefined) {
3536
opt_hash = newHash();
@@ -42,31 +43,31 @@ export const hijackHash = function(newHash) {
4243
};
4344

4445
/**
45-
* @type {function(new:fb.core.RepoInfo, !string, boolean, !string, boolean): undefined}
46+
* @type {function(new:RepoInfo, !string, boolean, !string, boolean): undefined}
4647
*/
4748
export const ConnectionTarget = RepoInfo;
4849

4950
/**
50-
* @param {!fb.api.Query} query
51+
* @param {!Query} query
5152
* @return {!string}
5253
*/
53-
export const queryIdentifier = function(query) {
54+
export const queryIdentifier = function(query: Query) {
5455
return query.queryIdentifier();
5556
};
5657

5758
/**
58-
* @param {!fb.api.Query} firebaseRef
59+
* @param {!Query} firebaseRef
5960
* @return {!Object}
6061
*/
61-
export const listens = function(firebaseRef) {
62-
return firebaseRef.repo.persistentConnection_.listens_;
62+
export const listens = function(firebaseRef: Query) {
63+
return (firebaseRef.repo.persistentConnection_ as any).listens_;
6364
};
6465

6566
/**
6667
* Forces the RepoManager to create Repos that use ReadonlyRestClient instead of PersistentConnection.
6768
*
6869
* @param {boolean} forceRestClient
6970
*/
70-
export const forceRestClient = function(forceRestClient) {
71+
export const forceRestClient = function(forceRestClient: boolean) {
7172
RepoManager.getInstance().forceRestClient(forceRestClient);
7273
};

0 commit comments

Comments
 (0)