Skip to content

Commit 144b78c

Browse files
committed
Call the poll method once when the tab is visible again (similar to what happens on page load).
1 parent 9349e33 commit 144b78c

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

django_unicorn/static/js/component.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -328,40 +328,32 @@ export class Component {
328328
clearInterval(this.poll.timer);
329329
}
330330
} else {
331-
// tab gets focus -> call poll method once and
332-
// start polling at intervals
333-
if (this.isPollEnabled()) {
334-
this.callMethod(
335-
this.poll.method,
336-
this.poll.partials,
337-
this.handlePollError
338-
);
339-
}
340-
this.startPolling();
331+
// Call the poll method once the tab is visible again
332+
this.startPolling(true);
341333
}
342334
},
343335
false
344336
);
345337

346338
this.poll.partials = rootElement.partials;
347339

348-
if (this.isPollEnabled()) {
349-
// Call the method once before the timer starts
350-
this.callMethod(
351-
this.poll.method,
352-
this.poll.partials,
353-
this.handlePollError
354-
);
355-
}
356-
357-
this.startPolling();
340+
// Call the method once before the timer starts
341+
this.startPolling(true);
358342
}
359343
}
360344

361345
/**
362346
* Starts polling and handles stopping the polling if there is an error.
363347
*/
364-
startPolling() {
348+
startPolling(fireImmediately) {
349+
if (fireImmediately && this.isPollEnabled()) {
350+
this.callMethod(
351+
this.poll.method,
352+
this.poll.partials,
353+
this.handlePollError
354+
);
355+
}
356+
365357
this.poll.timer = setInterval(() => {
366358
if (this.isPollEnabled()) {
367359
this.callMethod(

0 commit comments

Comments
 (0)