jbccollins / leaflet-tile-loading-progress-control

🍃 A leaflet control that indicates tile loading progress for a group of tile layers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Progress bar is shown too late after layer was added

m-mohr opened this issue · comments

Sorry, I have to come up with an issue again. ;-)

I was wondering why the progress bar doesn't show up immediately for layers added later (see #4).

I would propose the following change in onAdd:

Replace

                map.on('layeradd', function(e) {
                    var currentLayers = leafletElt.getLayers();
                    for (var i = 0; i < currentLayers.length; i++) {
                        // If the layer added to the map is part of the layer group then we rebind the loading triggers.
                        if (currentLayers[i] === e.layer) {
                            controlInstance.unbindLoadEventTriggers();
                            controlInstance.bindLoadEventTriggers();
                            break;
                        }
                    }
                });

with

                var self = this;
                map.on('layeradd', function(e) {
                    var currentLayers = leafletElt.getLayers();
                    for (var i = 0; i < currentLayers.length; i++) {
                        // If the layer added to the map is part of the layer group then we rebind the loading triggers.
                        if (currentLayers[i] === e.layer) {
                            controlInstance.unbindLoadEventTriggers();
                            controlInstance.bindLoadEventTriggers();
                            break;
                        }
                    }
                    self.handleLoadingStatusUpdate();
                });

It seems the first event fires quite late after the (slow loading) layer has been added. I could fix it by calling handleLoadingStatusUpdate() once after the layer has been added.

Still looking into the back-and-forth issue.

Seems like reasonable logic. But wouldn't it only be necessary to handle the loading status update inside of the conditional?
Like so:

                map.on('layeradd', function(e) {
                    var currentLayers = leafletElt.getLayers();
                    for (var i = 0; i < currentLayers.length; i++) {
                        // If the layer added to the map is part of the layer group then we rebind the loading triggers.
                        if (currentLayers[i] === e.layer) {
                            controlInstance.unbindLoadEventTriggers();
                            controlInstance.bindLoadEventTriggers();
                            controlInstance.handleLoadingStatusUpdate();
                            break;
                        }
                    }
                });

Yes, indeed.

Fixed in 1.0.8

Great, thanks.