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' ;
10
12
11
13
/**
12
14
* Class representing a firebase database.
13
- * @implements {firebase.Service }
15
+ * @implements {FirebaseService }
14
16
*/
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 = {
23
27
'TIMESTAMP' : {
24
- '.sv' : 'timestamp'
28
+ '.sv' : 'timestamp'
25
29
}
26
- }
30
+ } ;
27
31
28
32
/**
29
33
* The constructor should not be called by users of our public API.
30
- * @param {!Repo } repo
34
+ * @param {!Repo } repo_
31
35
*/
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().' ) ;
35
39
}
36
40
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 ) ;
42
43
43
44
this . INTERNAL = new DatabaseInternals ( this ) ;
44
45
}
45
46
46
- app : null
47
-
48
47
/**
49
48
* Returns a reference to the root or the path specified in opt_pathString.
50
49
* @param {string= } opt_pathString
51
- * @return {!Firebase } Firebase reference.
50
+ * @return {!Reference } Firebase reference.
52
51
*/
53
- ref ( opt_pathString ) : Reference {
52
+ ref ( opt_pathString ?: string ) : Reference {
54
53
this . checkDeleted_ ( 'ref' ) ;
55
54
validateArgCount ( 'database.ref' , 0 , 1 , arguments . length ) ;
56
55
@@ -62,20 +61,21 @@ export class Database {
62
61
* We throw a exception if the url is not in the same domain as the
63
62
* current repo.
64
63
* @param {string } url
65
- * @return {!Firebase } Firebase reference.
64
+ * @return {!Reference } Firebase reference.
66
65
*/
67
- refFromURL ( url ) {
66
+ refFromURL ( url : string ) : Reference {
68
67
/** @const {string} */
69
- var apiName = 'database.refFromURL' ;
68
+ const apiName = 'database.refFromURL' ;
70
69
this . checkDeleted_ ( apiName ) ;
71
70
validateArgCount ( apiName , 1 , 1 , arguments . length ) ;
72
- var parsedURL = parseRepoInfo ( url ) ;
71
+ const parsedURL = parseRepoInfo ( url ) ;
73
72
validateUrl ( apiName , 1 , parsedURL ) ;
74
73
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 + ')' ) ;
79
79
}
80
80
81
81
return this . ref ( parsedURL . path . toString ( ) ) ;
@@ -85,9 +85,9 @@ export class Database {
85
85
* @param {string } apiName
86
86
* @private
87
87
*/
88
- checkDeleted_ ( apiName ) {
88
+ private checkDeleted_ ( apiName : string ) {
89
89
if ( this . repo_ === null ) {
90
- fatal ( " Cannot call " + apiName + " on a deleted database." ) ;
90
+ fatal ( ' Cannot call ' + apiName + ' on a deleted database.' ) ;
91
91
}
92
92
}
93
93
@@ -98,47 +98,45 @@ export class Database {
98
98
this . repo_ . interrupt ( ) ;
99
99
}
100
100
101
- goOnline ( ) {
101
+ goOnline ( ) {
102
102
validateArgCount ( 'database.goOnline' , 0 , 0 , arguments . length ) ;
103
103
this . checkDeleted_ ( 'goOnline' ) ;
104
104
this . repo_ . resume ( ) ;
105
105
}
106
- } ;
106
+ }
107
107
108
108
// Note: This is an un-minfied property of the Database only.
109
109
Object . defineProperty ( Database . prototype , 'app' , {
110
110
/**
111
111
* @this {!Database}
112
112
* @return {!firebase.app.App }
113
113
*/
114
- get ( ) {
114
+ get ( ) : FirebaseApp {
115
115
return this . repo_ . app ;
116
116
}
117
117
} ) ;
118
118
119
119
Object . defineProperty ( Repo . prototype , 'database' , {
120
- get ( ) {
120
+ get ( ) : Database {
121
121
return this . __database || ( this . __database = new Database ( this ) ) ;
122
122
}
123
123
} ) ;
124
124
125
- class DatabaseInternals {
126
- database
125
+ export class DatabaseInternals {
127
126
/** @param {!Database } database */
128
- constructor ( database ) {
129
- this . database = database ;
127
+ constructor ( public database : Database ) {
130
128
}
131
129
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_ ) ) ;
136
134
137
- this . database . repo_ = null ;
138
- this . database . root_ = null ;
135
+ ( < any > this . database ) . repo_ = null ;
136
+ ( < any > this . database ) . root_ = null ;
139
137
this . database . INTERNAL = null ;
140
138
this . database = null ;
141
139
return PromiseImpl . resolve ( ) ;
142
140
}
143
- } ;
141
+ }
144
142
0 commit comments