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

Capture initial resize event #2552

Merged
merged 10 commits into from
May 19, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- _...Add new stuff here..._

### 🐞 Bug fixes
- Fixes issue with ResizeObserver firing an initial 'resize' event (since 3.0.0-pre.5) ([#2551](https://github.com/maplibre/maplibre-gl-js/issues/2551))
- _...Add new stuff here..._

## 3.0.0-pre.8
Expand Down
10 changes: 9 additions & 1 deletion src/ui/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ describe('Map', () => {
expect(spyC).not.toHaveBeenCalled();
});

test('do resize if trackResize is true (default)', () => {
test('do resize if trackResize is true (default)', async () => {
let observerCallback: Function = null;
global.ResizeObserver = jest.fn().mockImplementation((c) => ({
observe: () => { observerCallback = c; }
Expand All @@ -814,8 +814,16 @@ describe('Map', () => {
const spyA = jest.spyOn(map, '_update');
const spyB = jest.spyOn(map, 'resize');

// The initial "observe" event fired by ResizeObserver should be captured/muted
// in the map constructor

observerCallback();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another test or and expectations between the first and second callback?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, I added these so it now says:

            // The initial "observe" event fired by ResizeObserver should be captured/muted
            // in the map constructor

            observerCallback();
            expect(spyA).not.toHaveBeenCalled();
            expect(spyB).not.toHaveBeenCalled();

            // Following "observe" events should fire a resize / _update

            observerCallback();
            expect(spyA).toHaveBeenCalled();
            expect(spyB).toHaveBeenCalled();
            ```

expect(spyA).not.toHaveBeenCalled();
expect(spyB).not.toHaveBeenCalled();

// Following "observe" events should fire a resize / _update

observerCallback();
expect(spyA).toHaveBeenCalled();
expect(spyB).toHaveBeenCalled();
});
Expand Down
6 changes: 6 additions & 0 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,13 @@ class Map extends Camera {

if (typeof window !== 'undefined') {
addEventListener('online', this._onWindowOnline, false);
let initialResizeEventCaptured = false;
this._resizeObserver = new ResizeObserver((entries) => {
if (!initialResizeEventCaptured) {
initialResizeEventCaptured = true;
return;
}

if (this._trackResize) {
this.resize(entries)._update();
}
Expand Down
17 changes: 17 additions & 0 deletions test/integration/browser/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ describe('Browser tests', () => {
});
}, 40000);

test('Load should fire before resize and moveend', async () => {
const firstFiredEvent = await page.evaluate(() => {
const map2 = new maplibregl.Map({
container: 'map',
style: 'https://demotiles.maplibre.org/style.json',
center: [10, 10],
zoom: 10
});
return new Promise<string>((resolve, _reject) => {
map2.once('resize', () => resolve('resize'));
map2.once('moveend', () => resolve('moveend'));
map2.once('load', () => resolve('load'));
});
});
expect(firstFiredEvent).toBe('load');
}, 20000);

test('Drag to the left', async () => {

const canvas = await page.$('.maplibregl-canvas');
Expand Down