Skip to content

Commit 5a3aebd

Browse files
authored
use ResizeObserver instead of setInterval watch map resize/show/hide (#1298)
* use ResizeObserver instead of setInterval about map reizie/show/hide * adapt for IE * update map show/hide test case
1 parent d72150f commit 5a3aebd

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/renderer/map/MapCanvasRenderer.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,35 @@ class MapCanvasRenderer extends MapRenderer {
794794
}
795795

796796
_setCheckSizeInterval(interval) {
797-
clearInterval(this._resizeInterval);
798-
this._checkSizeInterval = interval;
799-
this._resizeInterval = setInterval(() => {
800-
if (!this.map || this.map.isRemoved()) {
801-
//is deleted
802-
clearInterval(this._resizeInterval);
803-
} else {
804-
this._checkSize();
797+
// ResizeObserver priority of use
798+
// https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserver
799+
if (typeof window !== 'undefined' && window.ResizeObserver) {
800+
if (this._resizeObserver) {
801+
this._resizeObserver.disconnect();
802+
}
803+
if (this.map) {
804+
// eslint-disable-next-line no-unused-vars
805+
this._resizeObserver = new ResizeObserver((entries) => {
806+
if (!this.map || this.map.isRemoved()) {
807+
this._resizeObserver.disconnect();
808+
} else if (entries.length) {
809+
this._checkSize(entries[0].contentRect);
810+
}
811+
});
812+
this._resizeObserver.observe(this.map._containerDOM);
805813
}
806-
}, this._checkSizeInterval);
814+
} else {
815+
clearInterval(this._resizeInterval);
816+
this._checkSizeInterval = interval;
817+
this._resizeInterval = setInterval(() => {
818+
if (!this.map || this.map.isRemoved()) {
819+
//is deleted
820+
clearInterval(this._resizeInterval);
821+
} else {
822+
this._checkSize();
823+
}
824+
}, this._checkSizeInterval);
825+
}
807826
}
808827

809828
_registerEvents() {

test/map/MapSpec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,13 @@ describe('Map.Spec', function () {
553553
expect(map.getCenter().toArray()).to.be.eql(center.toArray());
554554
done();
555555
});
556-
container.style.display = '';
556+
setTimeout(function () {
557+
container.style.display = '';
558+
}, 17);
557559
});
558-
container.style.display = 'none';
560+
setTimeout(function () {
561+
container.style.display = 'none';
562+
}, 17);
559563
});
560564

561565
it('event properties', function (done) {

0 commit comments

Comments
 (0)