Skip to content

Commit

Permalink
Add 'idle' event: fires when no further rendering is expected without…
Browse files Browse the repository at this point in the history
… further interaction.
  • Loading branch information
ChrisLoer committed Nov 28, 2018
1 parent 44b3848 commit ff5eae5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ui/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,20 @@ export type MapEvent =
*/
| 'render'

/**
* Fired after the last frame rendered before the map enters an
* "idle" state:
*
* - No camera transitions are in progress
* - All currently requested tiles have loaded
* - All fade/transition animations have completed
*
* @event idle
* @memberof Map
* @instance
*/
| 'idle'

/**
* Fired immediately after the map has been removed with {@link Map.event:remove}.
*
Expand Down
2 changes: 2 additions & 0 deletions src/ui/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,8 @@ class Map extends Camera {
// Style#_updateSources could have caused them to be set again.
if (this._sourcesDirty || this._repaint || this._styleDirty || this._placementDirty) {
this.triggerRepaint();
} else if (!this.isMoving() && this.loaded()) {
this.fire(new Event('idle'));
}

return this;
Expand Down
24 changes: 24 additions & 0 deletions test/unit/ui/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,30 @@ test('Map', (t) => {
});
});

t.test('no render after idle event', (t) => {
const style = createStyle();
const map = createMap(t, { style });
map.on('idle', () => {
map.on('render', t.fail);
setTimeout(() => {
t.end();
}, 100);
});
});

t.test('no idle event during move', (t) => {
const style = createStyle();
const map = createMap(t, { style, fadeDuration: 0 });
map.once('idle', () => {
map.zoomTo(0.5, { duration: 100 });
t.ok(map.isMoving(), "map starts moving immediately after zoomTo");
map.once('idle', () => {
t.ok(!map.isMoving(), "map stops moving before firing idle event");
t.end();
});
});
});

t.test('#removeLayer restores Map#loaded() to true', (t) => {
const map = createMap(t, {
style: extend(createStyle(), {
Expand Down

0 comments on commit ff5eae5

Please sign in to comment.