@@ -33,8 +33,11 @@ import { FirebaseDatabase } from '@firebase/database-types';
33
33
* @implements {FirebaseService}
34
34
*/
35
35
export class Database implements FirebaseService {
36
- INTERNAL : DatabaseInternals ;
37
- private root_ : Reference ;
36
+ /** Track if the instance has been used (root or repo accessed) */
37
+ private instanceStarted_ : boolean = false ;
38
+
39
+ /** Backing state for root_ */
40
+ private rootInternal_ ?: Reference ;
38
41
39
42
static readonly ServerValue = {
40
43
TIMESTAMP : {
@@ -51,25 +54,70 @@ export class Database implements FirebaseService {
51
54
52
55
/**
53
56
* The constructor should not be called by users of our public API.
54
- * @param {!Repo } repo_
57
+ * @param {!Repo } repoInternal_
55
58
*/
56
- constructor ( private repo_ : Repo ) {
57
- if ( ! ( repo_ instanceof Repo ) ) {
59
+ constructor ( private repoInternal_ : Repo ) {
60
+ if ( ! ( repoInternal_ instanceof Repo ) ) {
58
61
fatal (
59
62
"Don't call new Database() directly - please use firebase.database()."
60
63
) ;
61
64
}
65
+ }
62
66
63
- /** @type {Reference } */
64
- this . root_ = new Reference ( repo_ , Path . Empty ) ;
67
+ INTERNAL = {
68
+ delete : async ( ) => {
69
+ this . checkDeleted_ ( 'delete' ) ;
70
+ RepoManager . getInstance ( ) . deleteRepo ( this . repo_ ) ;
71
+ this . repoInternal_ = null ;
72
+ this . rootInternal_ = null ;
73
+ }
74
+ } ;
65
75
66
- this . INTERNAL = new DatabaseInternals ( this ) ;
76
+ private get repo_ ( ) : Repo {
77
+ if ( ! this . instanceStarted_ ) {
78
+ this . repoInternal_ . start ( ) ;
79
+ this . instanceStarted_ = true ;
80
+ }
81
+ return this . repoInternal_ ;
82
+ }
83
+
84
+ get root_ ( ) : Reference {
85
+ if ( ! this . rootInternal_ ) {
86
+ this . rootInternal_ = new Reference ( this . repo_ , Path . Empty ) ;
87
+ }
88
+
89
+ return this . rootInternal_ ;
67
90
}
68
91
69
92
get app ( ) : FirebaseApp {
70
93
return this . repo_ . app ;
71
94
}
72
95
96
+ /**
97
+ * Modify this instance to communicate with the Realtime Database emulator.
98
+ *
99
+ * <p>Note: This method must be called before performing any other operation.
100
+ *
101
+ * @param host the emulator host (ex: localhost)
102
+ * @param port the emulator port (ex: 8080)
103
+ */
104
+ useEmulator ( host : string , port : number ) : void {
105
+ this . checkDeleted_ ( 'useEmulator' ) ;
106
+ if ( this . instanceStarted_ ) {
107
+ fatal (
108
+ 'Cannot call useEmulator() after instance has already been initialized.'
109
+ ) ;
110
+ return ;
111
+ }
112
+
113
+ // Modify the repo to apply emulator settings
114
+ RepoManager . getInstance ( ) . applyEmulatorSettings (
115
+ this . repoInternal_ ,
116
+ host ,
117
+ port
118
+ ) ;
119
+ }
120
+
73
121
/**
74
122
* Returns a reference to the root or to the path specified in the provided
75
123
* argument.
@@ -109,14 +157,14 @@ export class Database implements FirebaseService {
109
157
validateUrl ( apiName , 1 , parsedURL ) ;
110
158
111
159
const repoInfo = parsedURL . repoInfo ;
112
- if ( repoInfo . host !== this . repo_ . repoInfo_ . host ) {
160
+ if ( ! repoInfo . isCustomHost ( ) && repoInfo . host !== this . repo_ . repoInfo_ . host ) {
113
161
fatal (
114
162
apiName +
115
163
': Host name does not match the current database: ' +
116
164
'(found ' +
117
165
repoInfo . host +
118
166
' but expected ' +
119
- ( this . repo_ . repoInfo_ as RepoInfo ) . host +
167
+ this . repo_ . repoInfo_ . host +
120
168
')'
121
169
) ;
122
170
}
@@ -128,7 +176,7 @@ export class Database implements FirebaseService {
128
176
* @param {string } apiName
129
177
*/
130
178
private checkDeleted_ ( apiName : string ) {
131
- if ( this . repo_ === null ) {
179
+ if ( this . repoInternal_ === null ) {
132
180
fatal ( 'Cannot call ' + apiName + ' on a deleted database.' ) ;
133
181
}
134
182
}
@@ -146,22 +194,3 @@ export class Database implements FirebaseService {
146
194
this . repo_ . resume ( ) ;
147
195
}
148
196
}
149
-
150
- export class DatabaseInternals {
151
- /** @param {!Database } database */
152
- constructor ( public database : Database ) { }
153
-
154
- /** @return {Promise<void> } */
155
- async delete ( ) : Promise < void > {
156
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
157
- ( this . database as any ) . checkDeleted_ ( 'delete' ) ;
158
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
159
- RepoManager . getInstance ( ) . deleteRepo ( ( this . database as any ) . repo_ as Repo ) ;
160
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
161
- ( this . database as any ) . repo_ = null ;
162
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
- ( this . database as any ) . root_ = null ;
164
- this . database . INTERNAL = null ;
165
- this . database = null ;
166
- }
167
- }
0 commit comments