-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathdatabase.component.ts
More file actions
40 lines (35 loc) · 1.44 KB
/
database.component.ts
File metadata and controls
40 lines (35 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Component, inject, makeStateKey, PLATFORM_ID, TransferState } from '@angular/core';
import { startWith, tap } from 'rxjs';
import { connectDatabaseEmulator, getDatabase, objectVal, ref } from '@angular/fire/database';
import { AsyncPipe, isPlatformServer, JsonPipe } from '@angular/common';
import { FirebaseApp } from '@angular/fire/app';
import { environment } from '../../environments/environment';
@Component({
selector: 'app-database',
template: `
<p>
Database!
<code>{{ testObjectValue$ | async | json }}</code>
</p>
`,
imports: [AsyncPipe, JsonPipe]
})
export class DatabaseComponent {
private readonly database;
private readonly transferState = inject(TransferState);
private readonly transferStateKey = makeStateKey<unknown|undefined>("database:test");
protected readonly testObjectValue$;
constructor() {
this.database = getDatabase(inject(FirebaseApp));
if (!(this.database as any)._instanceStarted && environment.emulatorPorts?.database) {
connectDatabaseEmulator(this.database, "localhost", environment.emulatorPorts.database);
}
this.testObjectValue$ = objectVal(ref(this.database, "test")).pipe(
isPlatformServer(inject(PLATFORM_ID)) ?
tap(it => this.transferState.set(this.transferStateKey, it)) :
this.transferState.hasKey(this.transferStateKey) ?
startWith(this.transferState.get(this.transferStateKey, undefined)) :
tap()
);
}
}