Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning a Collection of key, location and distance #5

Closed
marcoredoxigen opened this issue May 24, 2018 · 3 comments
Closed

Returning a Collection of key, location and distance #5

marcoredoxigen opened this issue May 24, 2018 · 3 comments
Labels
wontfix This will not be worked on

Comments

@marcoredoxigen
Copy link

I want to wrap your library in a google cloud function and return an array with the results to the client.

I do the following setup.

    const geoQuery = geofire.query({
        center: [parseFloat(req.query.lat), parseFloat(req.query.lon)],
        radius: parseFloat(req.query.radius)
    })

Is there any way to get the result, instead of waiting for all onKeyEnteredRegistration Events?
I need to do a response.send(results), but I Do not know which is the last item of the result set.

I had tried to put the response.send() inside the onReadyRegistration, but it happens sometimes before receiving all events in onKeyEnteredRegistration;

Any guidance appreciated.

@MichaelSolati MichaelSolati added the wontfix This will not be worked on label May 24, 2018
@MichaelSolati
Copy link
Owner

Unfortunately the answer would be no. The library is based off of the official firebase geofire library and makes multiple queries around the area you selected as well as validates that it is really within the radius you provided. However the turn around time is fairly quick. What I would suggest is to wrap the response.send(result) in a setTimeout after maybe 4 seconds? (Maybe more??)

Then as the onKeyEnteredRegistration is fired push the results into the results array.

@uturnr
Copy link

uturnr commented Sep 8, 2018

Here's my ugly solution.

I create a promise with a timer that cancels the query and resolves in 10000ms.
Every time key_entered is fired, I reset the timer to 500ms.
When ready fires, I reset the timer to 1000ms.

It needs work, but it my most reliable solution so far, and my function currently returns data for a location with 21 documents in about 1600ms (3700ms on a cold start), and for a location with 0 documents in about 1100ms.*

return new Promise(resolve => {
    const geoQuery = geoFirestore.query({
      center: geoPoint,
      radius: 30
    });

    let geoQueryTimer = setTimeout(() => {
      geoQuery.cancel();
      resolve()
    }, 10000 );

    const onKeyEnteredRegistration = geoQuery.on(
      'key_entered',
      (key, doc, distance) => {
        clearTimeout(geoQueryTimer);
	// do stuff with doc
	// push to array, etc
        geoQueryTimer = setTimeout(() => {
          geoQuery.cancel();
          resolve()
        }, 500 );
      }
    );

    const onReadyRegistration = geoQuery.on(
      'ready',
      () => {
        clearTimeout(geoQueryTimer);
        geoQueryTimer = setTimeout(() => {
          geoQuery.cancel();
          resolve()
        }, 1000 );
      }
    )

  }).then(() => {
    // return data
  });

*edit: Times when testing functions locally in functions shell.

@graig12
Copy link

graig12 commented Nov 18, 2018

What if you need to do another Firestore database call within the key_entred event? How do u suggest we do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants