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

Add 'idle' event: fires when no further rendering is expected without further interaction. #7625

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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