Skip to content

Commit bd73d1e

Browse files
committed
fix: debounce minimap rendering
Related to #96
1 parent 5430390 commit bd73d1e

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

lib/Minimap.js

Lines changed: 48 additions & 10 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

@@ -89,7 +92,7 @@ export default function Minimap(
8992

9093
setViewboxCenteredAroundPoint(diagramPoint, self._canvas);
9194

92-
self._update();
95+
self._requestUpdate();
9396
}
9497

9598
function mousedown(center) {
@@ -198,7 +201,7 @@ export default function Minimap(
198201
centerViewbox(event);
199202
}
200203

201-
self._update();
204+
self._requestUpdate();
202205

203206
// end dragging
204207
assign(self._state, {
@@ -281,7 +284,7 @@ export default function Minimap(
281284

282285
setViewboxCenteredAroundPoint(diagramPoint, self._canvas);
283286

284-
self._update();
287+
self._requestUpdate();
285288
}
286289
});
287290

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

299302
self._addElement(element);
300303

301-
self._update();
304+
self._requestUpdate();
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._requestUpdate();
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._requestUpdate();
322325
});
323326

324327
// update on element ID update
@@ -332,7 +335,7 @@ export default function Minimap(
332335
// update on viewbox changed
333336
eventBus.on('canvas.viewbox.changed', function() {
334337
if (!self._state.isDragging) {
335-
self._update();
338+
self._requestUpdate();
336339
}
337340
});
338341

@@ -341,7 +344,7 @@ export default function Minimap(
341344
// only update if present in DOM
342345
if (document.body.contains(self._parent)) {
343346
if (!self._state.isDragging) {
344-
self._update();
347+
self._requestUpdate();
345348
}
346349

347350
self._state._svgClientRect = self._svg.getBoundingClientRect();
@@ -358,7 +361,7 @@ export default function Minimap(
358361
self._addElement(el);
359362
});
360363

361-
self._update();
364+
self._requestUpdate();
362365
});
363366

364367
}
@@ -435,7 +438,41 @@ Minimap.prototype._init = function() {
435438
this._parent.appendChild(overlay);
436439
};
437440

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

537574
domAttr(this._toggle, 'title', translate('Close minimap'));
538575

576+
// open forces an update to ensure a viewbox is set and current
539577
this._update();
540578

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

0 commit comments

Comments
 (0)