1
1
/**
2
- * Copyright 2017 Google Inc.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- import { fatal } from "../core/util/util" ;
18
- import { parseRepoInfo } from "../core/util/libs/parser" ;
19
- import { Path } from "../core/util/Path" ;
20
- import { PromiseImpl } from "../../utils/promise" ;
21
- import { Reference } from "./Reference" ;
22
- import { Repo } from "../core/Repo" ;
23
- import { RepoManager } from "../core/RepoManager" ;
24
- import { validateArgCount } from "../../utils/validation" ;
25
- import { FirebaseApp } from "../../app/firebase_app" ;
26
- import { validateUrl } from "../core/util/validation" ;
2
+ * Copyright 2017 Google Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { fatal } from '../core/util/util' ;
18
+ import { parseRepoInfo } from '../core/util/libs/parser' ;
19
+ import { Path } from '../core/util/Path' ;
20
+ import { PromiseImpl } from '../../utils/promise' ;
21
+ import { Reference } from './Reference' ;
22
+ import { Repo } from '../core/Repo' ;
23
+ import { RepoManager } from '../core/RepoManager' ;
24
+ import { validateArgCount } from '../../utils/validation' ;
25
+ import { validateUrl } from '../core/util/validation' ;
26
+ import { FirebaseApp , FirebaseService } from '../../app/firebase_app' ;
27
+ import { RepoInfo } from '../core/RepoInfo' ;
27
28
28
29
/**
29
30
* Class representing a firebase database.
30
- * @implements {firebase.Service }
31
+ * @implements {FirebaseService }
31
32
*/
32
- export class Database {
33
- repo_ : Repo ;
34
- root_ : Reference ;
35
- INTERNAL ;
36
-
37
- static ServerValue = {
33
+ export class Database implements FirebaseService {
34
+ INTERNAL : DatabaseInternals ;
35
+ private root_ : Reference ;
36
+
37
+ static readonly ServerValue = {
38
38
'TIMESTAMP' : {
39
- '.sv' : 'timestamp'
39
+ '.sv' : 'timestamp'
40
40
}
41
- }
41
+ } ;
42
42
43
43
/**
44
44
* The constructor should not be called by users of our public API.
45
- * @param {!Repo } repo
45
+ * @param {!Repo } repo_
46
46
*/
47
- constructor ( repo ) {
48
- if ( ! ( repo instanceof Repo ) ) {
49
- fatal ( " Don't call new Database() directly - please use firebase.database()." ) ;
47
+ constructor ( private repo_ : Repo ) {
48
+ if ( ! ( repo_ instanceof Repo ) ) {
49
+ fatal ( ' Don\ 't call new Database() directly - please use firebase.database().' ) ;
50
50
}
51
51
52
- /** @type {Repo } */
53
- this . repo_ = repo ;
54
-
55
- /** @type {Firebase } */
56
- this . root_ = new Reference ( repo , Path . Empty ) ;
52
+ /** @type {Reference } */
53
+ this . root_ = new Reference ( repo_ , Path . Empty ) ;
57
54
58
55
this . INTERNAL = new DatabaseInternals ( this ) ;
59
56
}
@@ -65,7 +62,7 @@ export class Database {
65
62
/**
66
63
* Returns a reference to the root or the path specified in opt_pathString.
67
64
* @param {string= } pathString
68
- * @return {!Firebase } Firebase reference.
65
+ * @return {!Reference } Firebase reference.
69
66
*/
70
67
ref ( pathString ?: string ) : Reference {
71
68
this . checkDeleted_ ( 'ref' ) ;
@@ -79,20 +76,21 @@ export class Database {
79
76
* We throw a exception if the url is not in the same domain as the
80
77
* current repo.
81
78
* @param {string } url
82
- * @return {!Firebase } Firebase reference.
79
+ * @return {!Reference } Firebase reference.
83
80
*/
84
- refFromURL ( url ) {
81
+ refFromURL ( url : string ) : Reference {
85
82
/** @const {string} */
86
- var apiName = 'database.refFromURL' ;
83
+ const apiName = 'database.refFromURL' ;
87
84
this . checkDeleted_ ( apiName ) ;
88
85
validateArgCount ( apiName , 1 , 1 , arguments . length ) ;
89
- var parsedURL = parseRepoInfo ( url ) ;
86
+ const parsedURL = parseRepoInfo ( url ) ;
90
87
validateUrl ( apiName , 1 , parsedURL ) ;
91
88
92
- var repoInfo = parsedURL . repoInfo ;
93
- if ( repoInfo . host !== this . repo_ . repoInfo_ . host ) {
94
- fatal ( apiName + ": Host name does not match the current database: " +
95
- "(found " + repoInfo . host + " but expected " + this . repo_ . repoInfo_ . host + ")" ) ;
89
+ const repoInfo = parsedURL . repoInfo ;
90
+ if ( repoInfo . host !== ( ( this . repo_ as any ) . repoInfo_ as RepoInfo ) . host ) {
91
+ fatal ( apiName + ': Host name does not match the current database: ' +
92
+ '(found ' + repoInfo . host + ' but expected ' +
93
+ ( ( this . repo_ as any ) . repoInfo_ as RepoInfo ) . host + ')' ) ;
96
94
}
97
95
98
96
return this . ref ( parsedURL . path . toString ( ) ) ;
@@ -101,9 +99,9 @@ export class Database {
101
99
/**
102
100
* @param {string } apiName
103
101
*/
104
- private checkDeleted_ ( apiName ) {
102
+ private checkDeleted_ ( apiName : string ) {
105
103
if ( this . repo_ === null ) {
106
- fatal ( " Cannot call " + apiName + " on a deleted database." ) ;
104
+ fatal ( ' Cannot call ' + apiName + ' on a deleted database.' ) ;
107
105
}
108
106
}
109
107
@@ -114,36 +112,28 @@ export class Database {
114
112
this . repo_ . interrupt ( ) ;
115
113
}
116
114
117
- goOnline ( ) {
115
+ goOnline ( ) {
118
116
validateArgCount ( 'database.goOnline' , 0 , 0 , arguments . length ) ;
119
117
this . checkDeleted_ ( 'goOnline' ) ;
120
118
this . repo_ . resume ( ) ;
121
119
}
122
- } ;
123
-
124
- Object . defineProperty ( Repo . prototype , 'database' , {
125
- get ( ) {
126
- return this . __database || ( this . __database = new Database ( this ) ) ;
127
- }
128
- } ) ;
120
+ }
129
121
130
- class DatabaseInternals {
131
- database
122
+ export class DatabaseInternals {
132
123
/** @param {!Database } database */
133
- constructor ( database ) {
134
- this . database = database ;
124
+ constructor ( public database : Database ) {
135
125
}
136
126
137
- /** @return {firebase. Promise<void> } */
138
- delete ( ) {
139
- this . database . checkDeleted_ ( 'delete' ) ;
140
- RepoManager . getInstance ( ) . deleteRepo ( /** @type { !Repo } */ ( this . database . repo_ ) ) ;
127
+ /** @return {Promise<void> } */
128
+ delete ( ) : Promise < void > {
129
+ ( this . database as any ) . checkDeleted_ ( 'delete' ) ;
130
+ RepoManager . getInstance ( ) . deleteRepo ( ( this . database as any ) . repo_ as Repo ) ;
141
131
142
- this . database . repo_ = null ;
143
- this . database . root_ = null ;
132
+ ( this . database as any ) . repo_ = null ;
133
+ ( this . database as any ) . root_ = null ;
144
134
this . database . INTERNAL = null ;
145
135
this . database = null ;
146
136
return PromiseImpl . resolve ( ) ;
147
137
}
148
- } ;
138
+ }
149
139
0 commit comments