Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b3394d

Browse files
committed
Improve: Add batching to modal progress updates to reduce DOM overhead
1 parent 17a958d commit 5b3394d

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

assets/js/mainwp-popup.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@
103103

104104
this.initElements();
105105

106+
this.initProgressBatch();
107+
106108
if (this.title) {
107109
this.$header.html(this.title);
108110
}
109111

110-
this.$progress.toggle(!!this.progressMax);
112+
this.$progress.show();
111113

112114
this.$overlayElementId
113115
.modal({
@@ -148,15 +150,41 @@
148150
typeof this.actionsCloseCallback === 'function' && this.actionsCloseCallback();
149151
}
150152
},
153+
initProgressBatch: function () {
154+
this._pendingCount = 0;
155+
this._lastValue = 0;
156+
this._flushTimer = null;
157+
},
151158
setProgressSite: function (value) {
152159
if (!this.$progress) return;
153160

154-
// update label
155-
this.setStatusText(`${value} / ${this.totalSites} ${this.statusText}`);
161+
this._pendingCount = (this._pendingCount || 0) + 1;
162+
this._lastValue = value;
163+
164+
if (this._rafScheduled) return;
165+
166+
this._rafScheduled = true;
167+
168+
requestAnimationFrame(() => {
169+
this.flushProgress();
170+
this._rafScheduled = false;
171+
});
172+
},
173+
flushProgress: function () {
174+
if (!this.$progress) return;
175+
176+
// update label once
177+
this.setStatusText(
178+
`${this._lastValue} / ${this.totalSites} ${this.statusText}`
179+
);
156180

157-
// increment progress safely
181+
// batch increment
158182
const current = this.$progress.progress('get value') || 0;
159-
this.$progress.progress('set progress', current + 1);
183+
this.$progress.progress('set progress', current + this._pendingCount);
184+
185+
// reset
186+
this._pendingCount = 0;
187+
this._flushTimer = null;
160188
},
161189
getProgressValue: function () {
162190
return this.$progress

0 commit comments

Comments
 (0)