Skip to content

Commit f1fe1d7

Browse files
committed
Add state trasnfer to storageUrl
1 parent 94cb910 commit f1fe1d7

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/compat/storage/pipes/storageUrl.pipe.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { AsyncPipe } from '@angular/common';
22
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';
46
import { AngularFireStorage } from '../storage';
57

68
/** to be used with in combination with | async */
@@ -14,14 +16,18 @@ export class GetDownloadURLPipe implements PipeTransform, OnDestroy {
1416
private path: string;
1517
private downloadUrl$: Observable<any>;
1618

17-
constructor(private storage: AngularFireStorage, cdr: ChangeDetectorRef) {
19+
constructor(private storage: AngularFireStorage, cdr: ChangeDetectorRef, private state: TransferState) {
1820
this.asyncPipe = new AsyncPipe(cdr);
1921
}
2022

2123
transform(path: string) {
2224
if (path !== this.path) {
2325
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+
);
2531
}
2632
return this.asyncPipe.transform(this.downloadUrl$);
2733
}

0 commit comments

Comments
 (0)