Task Progress Button¶
shared.pollTaskProgress(btn, taskKey, label, onComplete) turns a button into a live progress display for a Jellyfin scheduled task.
Decision¶
| When | Use |
|---|---|
| A button that starts a scheduled task and then waits for it | Task Progress Button |
| A task the plugin runs itself | A Poller and a Progress Bar |
Usage¶
<button is="emby-button" type="button" id="btnSync" class="raised stolon-button stolon-primary"><span>Sync</span></button>
var handle = shared.pollTaskProgress(view.querySelector('#btnSync'), taskKey, 'Sync', function () { reloadStatus(); });
// on viewhide: handle.cancel();
Classes: stolon-btn-progress-host, stolon-btn-progress.
States and Modifiers¶
- The button is disabled and shows "Label N%" in its first
span. It is enabled again when the task finishes. - The helper adds
stolon-btn-progress-hostto the button and appends a Progress Bar fill,stolon-progress-bar stolon-btn-progress, that grows with the percent along the bottom edge. A.stolon-btn-progresselement already in the button takes the same classes. - A
smallbutton never draws the bar. A Progress Ring pie,stolon-btn-progress-pie, fills beside it instead. - It uses the dashboard WebSocket (
ScheduledTasksInfoStart) when the event bus is reachable, and pollsApiClient.getScheduledTasks()as a safety net, every second without the socket and every 5 seconds with it. - It gives up after 30 minutes. The progress line is the on-accent color on a submit or destructive button, and the accent on a plain one.
Gotchas¶
- The button needs a
spanfor its label. cancelstops the polling, restores the label, and also runsonComplete.- A plugin that still styles
.btn-progresskeeps working.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
| Class | What it looks like |
|---|---|
stolon-btn-progress-host |
Button that clips and anchors a progress bar along its bottom edge |
stolon-button.stolon-primary |
Progress fill on an accent or red button, in the text color of that fill so it stays visible |
stolon-button.stolon-small |
A small button never draws the bar, since it is too short, and shows a pie beside it instead |
Example¶
Example Code¶
<!-- title: Task Progress Button -->
<button type="button" id="btnSync" class="raised stolon-button stolon-primary"><span>Sync</span></button>
<span id="taskStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
// The dashboard answers getScheduledTasks. This stub runs a task for a few seconds.
let startedAt = 0;
ApiClient.getScheduledTasks = () => {
const pct = Math.min(100, (Date.now() - startedAt) / 40);
return Promise.resolve([{ Key: 'SyncTask', State: pct < 100 ? 'Running' : 'Idle', CurrentProgressPercentage: pct }]);
};
shared.bindClick('btnSync', () => {
startedAt = Date.now();
shared.pollTaskProgress(document.getElementById('btnSync'), 'SyncTask', 'Sync',
() => shared.setStatus('taskStatus', 'Sync finished.', false));
});
</script>