deck.gl icon indicating copy to clipboard operation
deck.gl copied to clipboard

Map Idle Event

Open cschlueter opened this issue 7 years ago • 8 comments

Using deck.gl with MapBox offers the ability to listen to an idle event

map.once('idle', function() {…})

I’m not aware, looking at the documentation of deck.gl, if and what event to listen for without the usage of MapBox. Using e.g. time out is error prone and obviously not the way to go. I tried to use a combination of onLoad onViewportStateChange and render but with no success.

Any idea on how to achieve the the result of MapBoxs function with pure deck.gl?

cschlueter avatar Jul 10 '19 10:07 cschlueter

I needed this for a project recently. I ended up using a custom react hook that leverages createActivityDetector package. You may need to change the options to suit your needs.

import { useState, useEffect } from "react";
import createActivityDetector from "activity-detector";

export default function useIdle(options) {
  const [isIdle, setIsIdle] = useState(false);
  useEffect(() => {
    const activityDetector = createActivityDetector(options);
    activityDetector.on("idle", () => setIsIdle(true));
    activityDetector.on("active", () => setIsIdle(false));
    return () => activityDetector.stop();
  }, []);

  return isIdle;
}

mayteio avatar Jul 10 '19 21:07 mayteio

Hej @mayteio thanks for the tipp, not exactly what I’m looking for though. This detects if a user is interacting with the page, I want to know when it is safe to assume that the map has rendered everything.

cschlueter avatar Jul 11 '19 10:07 cschlueter

@cschlueter Just to clarify, do you want to know if the map has finished loading, or if deck.gl has rendered?

For the former, you have to listen to mapbox's own event. For the latter, you can use Deck's onAfterRender prop.

Pessimistress avatar Jul 11 '19 18:07 Pessimistress

Hi @Pessimistress,

I think onAfterRender would be the one! I don’t use MapBox integration into Deck.gl at all, so the method above which I was referring to isn’t available to me, even though with MapBox it does exactly what I want.

Do you have any documentation hint for onAfterRender?

I did search the github repo: https://github.com/uber/deck.gl/search?q=onafterrender&unscoped_q=onafterrender

But to no avail, the function signature is unclear to me. The function itself seems to be indeed called like three times during my rendering example, so it’s going into the right direction.

cschlueter avatar Jul 12 '19 15:07 cschlueter

Any pointer on how to configure the callback of onAfterRender? It seems to pass back the 2d rendering context, but that doesn’t help me figuring out if layers are finished rendering or not?

onAfterRender: () => {
            testCase.onAfterRender({
              deck,
              layers: deck.layerManager.getLayers(),
              done
            });
          }

This seems to be what I want, but I have no clue how to get it :)

cschlueter avatar Jul 15 '19 08:07 cschlueter

The callback is only called after all layers are rendered. What do you need to access in your test case?

Pessimistress avatar Jul 15 '19 18:07 Pessimistress

My mistake, it is actually sufficient for my use-case to "just know" that the layers have finished rendering. I just pass on the information and go from there. Thanks for you help.

cschlueter avatar Jul 17 '19 07:07 cschlueter

Closing this issue, for future reference onAfterRender documentation can be found here: https://deck.gl/#/documentation/deckgl-api-reference/deck?section=onafterrender-function-optional-

tgorkin avatar Aug 06 '19 20:08 tgorkin