What's causing lag between observable returning result and displaying result in browser?

687 views Asked by At

I'm using Angular 2 and GeoFire to display nearby professionals in an area. I created a service to use GeoFire to return an observable of a list of professionals, but I'm getting a massive delay between getting that list and having it displayed.

This is my method to get the professionals in my service:

public getKeysFromGeoQuery(): Observable<any> {
    var keys = new Array();

    return Observable.create(observer => {
        this.geoQuery.on("key_entered", (key, location, distance) =>  {
            keys.push(key);                
            observer.next(keys);
        });

        this.geoQuery.on("key_exited", (key, location, distance) => {
            var index = keys.indexOf(key);
            if (index > -1) {
                keys.splice(index, 1);
            }              
            observer.next(keys);
        });
    });
}

This is my test component to use the service:

import { Component } from '@angular/core';
import { LocationService } from '../../core/location.service';
import { Observable, Observer } from 'rxjs';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent {
  mapCenter = [34.021256, -118.403630];
  searchRadius = 4;

  result;
  error;
  time;

  constructor(private locationService: LocationService) {
    const startTime = Date.now();
    this.locationService.getGeoQuery({ center: this.mapCenter, radius: this.searchRadius });

    this.locationService.getKeysFromGeoQuery()
      .subscribe(keys => {
        this.result = keys;
        console.log("keys: ", this.result);
        this.time = Date.now() - startTime;
        console.log("time: ", this.time);
      });
  }
}

test.component.html:

<p>Result: {{result}} </p>
<p>Time: {{time}} </p>
<p>Error: {{error}} </p>

In my console, it shows the keys getting retrieved within 500ms. However, the browser does not show the result until 3-4 seconds later. Does anyone know what is causing this delay? Screen and console

1

There are 1 answers

5
cartant On BEST ANSWER

It sounds like the GeoFire on event is being fired outside of a zone.

You could verify this by injecting a ChangeDetectorRef and explicitly triggering change detection:

import { ChangeDetectorRef } from '@angular/core';
...
@Component({
  ...
})
export class TestComponent {
  ...
  constructor(
    private locationService: LocationService,
    private changeDetectorRef: ChangeDetectorRef
  ) {
    ...
    this.locationService.getKeysFromGeoQuery()
      .subscribe(keys => {
        this.result = keys;
        console.log("keys: ", this.result);
        this.time = Date.now() - startTime;
        console.log("time: ", this.time);
        this.changeDetectorRef.detectChanges();
      });
  }
}

If it is the case that the event is being fired outside a zone, it could be related to how the Firebase and GeoFire scripts are incorporated into your build. Perhaps they are being loaded before zone.js?