1
1
import { AsyncPipe } from '@angular/common' ;
2
2
import { ChangeDetectorRef , NgModule , OnDestroy , Pipe , PipeTransform } from '@angular/core' ;
3
- import { Observable } from 'rxjs' ;
3
+ import { makeStateKey , TransferState } from '@angular/platform-browser' ;
4
+ import { Observable , of } from 'rxjs' ;
5
+ import { tap } from 'rxjs/operators' ;
4
6
import { AngularFireStorage } from '../storage' ;
5
7
6
8
/** to be used with in combination with | async */
@@ -14,14 +16,18 @@ export class GetDownloadURLPipe implements PipeTransform, OnDestroy {
14
16
private path : string ;
15
17
private downloadUrl$ : Observable < any > ;
16
18
17
- constructor ( private storage : AngularFireStorage , cdr : ChangeDetectorRef ) {
19
+ constructor ( private storage : AngularFireStorage , cdr : ChangeDetectorRef , private state : TransferState ) {
18
20
this . asyncPipe = new AsyncPipe ( cdr ) ;
19
21
}
20
22
21
23
transform ( path : string ) {
22
24
if ( path !== this . path ) {
23
25
this . path = path ;
24
- this . downloadUrl$ = this . storage . ref ( path ) . getDownloadURL ( ) ;
26
+ const key = makeStateKey < string > ( `|getDownloadURL|${ path } ` ) ;
27
+ const existing = this . state . get ( key , undefined ) ;
28
+ this . downloadUrl$ = existing ? of ( existing ) : this . storage . ref ( path ) . getDownloadURL ( ) . pipe (
29
+ tap ( it => this . state . set ( key , it ) )
30
+ ) ;
25
31
}
26
32
return this . asyncPipe . transform ( this . downloadUrl$ ) ;
27
33
}
0 commit comments