Skip to content

Commit

Permalink
feat(multiple): Minor additions (#2921)
Browse files Browse the repository at this point in the history
* Increase the accuracy of performance marks, by leaning on Zone's start marker
* Add automatic transfer state support to the getDownloadURL pipe
* Storage.child type fix
  • Loading branch information
jamesdaniels committed Sep 11, 2021
1 parent 3277cf2 commit d6cfe16
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
15 changes: 2 additions & 13 deletions src/compat/performance/performance.service.ts
Expand Up @@ -2,27 +2,16 @@ import { ApplicationRef, Injectable, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { first, tap } from 'rxjs/operators';

const IS_STABLE_START_MARK = '_isStableStart';
const IS_STABLE_START_MARK = 'Zone';
const IS_STABLE_END_MARK = '_isStableEnd';

function markStarts() {
if (typeof(window) !== 'undefined' && window.performance && window.performance.mark) {
window.performance.mark(IS_STABLE_START_MARK);
return true;
} else {
return false;
}
}

const started = markStarts();

@Injectable()
export class PerformanceMonitoringService implements OnDestroy {

private disposable: Subscription|undefined;

constructor(appRef: ApplicationRef) {
if (started && window.performance.mark) {
if (typeof window !== 'undefined' && window.performance?.mark) {
this.disposable = appRef.isStable.pipe(
first(it => it),
tap(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/compat/performance/performance.ts
Expand Up @@ -52,7 +52,7 @@ export class AngularFirePerformance {
}

const trace$ = (traceId: string) => {
if (typeof window !== 'undefined' && window.performance && window.performance.mark) {
if (typeof window !== 'undefined' && window.performance?.mark) {
const entries = window.performance.getEntriesByName(traceId, 'measure') || [];
const startMarkName = `_${traceId}Start[${entries.length}]`;
const endMarkName = `_${traceId}End[${entries.length}]`;
Expand Down
18 changes: 14 additions & 4 deletions src/compat/storage/pipes/storageUrl.pipe.ts
@@ -1,6 +1,8 @@
import { AsyncPipe } from '@angular/common';
import { ChangeDetectorRef, NgModule, OnDestroy, Pipe, PipeTransform } from '@angular/core';
import { Observable } from 'rxjs';
import { ChangeDetectorRef, NgModule, OnDestroy, Optional, Pipe, PipeTransform } from '@angular/core';
import { makeStateKey, TransferState } from '@angular/platform-browser';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { AngularFireStorage } from '../storage';

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

constructor(private storage: AngularFireStorage, cdr: ChangeDetectorRef) {
constructor(
private storage: AngularFireStorage,
cdr: ChangeDetectorRef,
@Optional() private state: TransferState
) {
this.asyncPipe = new AsyncPipe(cdr);
}

transform(path: string) {
if (path !== this.path) {
this.path = path;
this.downloadUrl$ = this.storage.ref(path).getDownloadURL();
const key = makeStateKey<string>(`|getDownloadURL|${path}`);
const existing = this.state?.get(key, undefined);
this.downloadUrl$ = existing ? of(existing) : this.storage.ref(path).getDownloadURL().pipe(
tap(it => this.state?.set(key, it))
);
}
return this.asyncPipe.transform(this.downloadUrl$);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compat/storage/ref.ts
Expand Up @@ -8,7 +8,7 @@ export interface AngularFireStorageReference {
getDownloadURL(): Observable<any>;
getMetadata(): Observable<any>;
delete(): Observable<any>;
child(path: string): any;
child(path: string): AngularFireStorageReference;
updateMetadata(meta: SettableMetadata): Observable<any>;
put(data: any, metadata?: UploadMetadata | undefined): AngularFireUploadTask;
putString(data: string, format?: string | undefined, metadata?: UploadMetadata | undefined): AngularFireUploadTask;
Expand Down

0 comments on commit d6cfe16

Please sign in to comment.