Skip to content

Commit 49fa632

Browse files
committed
fix: debounce minimap rendering
Related to #96, #81
1 parent 5430390 commit 49fa632

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

lib/Minimap.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ export default function Minimap(
5656
this._elementRegistry = elementRegistry;
5757
this._eventBus = eventBus;
5858
this._injector = injector;
59+
this._config = config || {};
60+
this._updateTimeout = null;
61+
this._firstTimeoutStart = null;
5962

6063
this._state = {
6164
isOpen: undefined,
@@ -73,7 +76,7 @@ export default function Minimap(
7376

7477
this._init();
7578

76-
this.toggle((config && config.open) || false);
79+
this.toggle(this._config.open || false);
7780

7881
function centerViewbox(point) {
7982

@@ -298,7 +301,7 @@ export default function Minimap(
298301

299302
self._addElement(element);
300303

301-
self._update();
304+
self._debouncedUpdate();
302305
});
303306

304307
// remove shape on shape/connection removed
@@ -307,7 +310,7 @@ export default function Minimap(
307310

308311
self._removeElement(element);
309312

310-
self._update();
313+
self._debouncedUpdate();
311314
});
312315

313316
// update on elements changed
@@ -318,7 +321,7 @@ export default function Minimap(
318321
self._updateElement(element);
319322
});
320323

321-
self._update();
324+
self._debouncedUpdate();
322325
});
323326

324327
// update on element ID update
@@ -435,7 +438,42 @@ Minimap.prototype._init = function() {
435438
this._parent.appendChild(overlay);
436439
};
437440

441+
Minimap.prototype._debouncedUpdate = function() {
442+
443+
const {
444+
debounceDelay = 100,
445+
debounceSkipDelay = 2000
446+
} = this._config;
447+
448+
const now = Date.now();
449+
450+
// if we've been debouncing for a while already, force update
451+
if (this._firstTimeoutStart && now - this._firstTimeoutStart >= debounceSkipDelay) {
452+
return this._update();
453+
}
454+
455+
// clear previous timeout
456+
if (this._updateTimeout) {
457+
clearTimeout(this._updateTimeout);
458+
}
459+
460+
// fire debounced update
461+
this._firstTimeoutStart = this._firstTimeoutStart || now;
462+
this._updateTimeout = setTimeout(() => {
463+
this._update();
464+
}, debounceDelay);
465+
};
466+
438467
Minimap.prototype._update = function() {
468+
469+
// if update was forced, clear any timeouts
470+
if (this._updateTimeout) {
471+
clearTimeout(this._updateTimeout);
472+
this._updateTimeout = null;
473+
}
474+
475+
this._firstTimeoutStart = null;
476+
439477
var viewbox = this._canvas.viewbox(),
440478
innerViewbox = viewbox.inner,
441479
outerViewbox = viewbox.outer;
@@ -536,6 +574,7 @@ Minimap.prototype.open = function() {
536574

537575
domAttr(this._toggle, 'title', translate('Close minimap'));
538576

577+
// open forces an update to ensure a viewbox is set and current
539578
this._update();
540579

541580
this._eventBus.fire('minimap.toggle', { open: true });

0 commit comments

Comments
 (0)