Skip to content

Commit f8423a2

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

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

lib/Minimap.js

Lines changed: 51 additions & 11 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,44 @@ 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 (this._firstTimeoutStart === null) {
450+
this._firstTimeoutStart = now;
451+
}
452+
453+
// if we've been debouncing for a while already, force update
454+
if (now - this._firstTimeoutStart >= debounceSkipDelay) {
455+
return this._update();
456+
}
457+
458+
// clear previous timeout
459+
if (this._updateTimeout) {
460+
clearTimeout(this._updateTimeout);
461+
}
462+
463+
// fire debounced update
464+
this._firstTimeoutStart = this._firstTimeoutStart || now;
465+
this._updateTimeout = setTimeout(() => {
466+
this._update();
467+
}, debounceDelay);
468+
};
469+
438470
Minimap.prototype._update = function() {
471+
472+
// if update was forced, clear any timeouts
473+
if (this._updateTimeout) {
474+
clearTimeout(this._updateTimeout);
475+
this._firstTimeoutStart = null;
476+
this._updateTimeout = null;
477+
}
478+
439479
var viewbox = this._canvas.viewbox(),
440480
innerViewbox = viewbox.inner,
441481
outerViewbox = viewbox.outer;
@@ -536,7 +576,7 @@ Minimap.prototype.open = function() {
536576

537577
domAttr(this._toggle, 'title', translate('Close minimap'));
538578

539-
this._update();
579+
this._requestUpdate();
540580

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

0 commit comments

Comments
 (0)