Up Next: a queue that plays on by itself (#473)

Playing was one episode at a time: when one ended nothing followed. Up Next
is a per-account queue on the server (table `queue`, GET and PUT
/api/queue), so the phone and the browser share it. The page adds to it from
a row or the reader, reorders it by drag or arrow keys, and plays the next
when one ends; what is playing leaves it as it starts, as Overcast's does. An
episode 90% played leaves it on the server too, which covers the phone
playing on while the page is frozen.

The iOS app is told what follows (`{t:'next'}`, gated on the app saying it
can) and starts it at the end of an episode; the page takes it up through
nowPlaying as it does the car's pick. The Swift is not compiled here.

The page stub answered every unknown address with [], whose `.entries` is
Array.prototype.entries, so the queue read a function as its list; it now
answers /api/queue as an entry list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 14:31:40 +00:00
parent 2ebe77b21e
commit c65b80a06c
24 changed files with 445 additions and 41 deletions

121
web/src/upnext.ts Normal file
View File

@@ -0,0 +1,121 @@
/* ---------------- Up Next ---------------- */
/// Up Next (#473): the episodes lined up to play, kept on the server so the phone and the browser
/// share one. What is playing is not in it: an episode leaves Up Next as it starts, as Overcast's
/// does, so the first one in it is always the one that follows.
let upNext: Entry[]=[];
/// The next one in the list that can play now, the one playing aside. One not downloaded yet is
/// passed over, to wait in the list until it is.
const nextUp=(list: Entry[], playing: string|null)=>list.find(e=>entryKey(e)!==playing&&e.enclosures.some(isPlayable));
const queued=(e: Entry)=>upNext.some(x=>entryKey(x)===entryKey(e));
function loadUpNext(){
api('/api/queue').then((r: ListPage)=>{ upNext=r.entries; upNextChanged(); }).catch(()=>{});
}
/// Saves Up Next in this order and takes the server's answer, which leaves out what was not
/// yours to queue. Drawn at once from what was asked, so a drag lands where it was dropped.
async function saveUpNext(list: Entry[]){
upNext=list; upNextChanged();
try{
const r: ListPage=await api('/api/queue',{method:'PUT',body:JSON.stringify(list.map(e=>({feed_id:e.feed_id,guid:e.guid})))});
upNext=r.entries;
}catch(err){ toast(why(err),true); loadUpNext(); return; }
upNextChanged();
}
function upNextChanged(){
renderFeeds();
if(S.feed===':upnext') drawUpNext();
// The rows' and the reader's buttons say whether each is in it.
for(const b of $$<HTMLButtonElement>('[data-a=queue]')) paintQueueButton(b);
hostNext();
}
/// Adds an episode at the end, or first with `next`, or takes it out if it is in already and
/// neither was asked. A file still on the feed's server is fetched now, so it is here by its turn.
async function toggleQueued(e: Entry, next=false){
const rest=upNext.filter(x=>entryKey(x)!==entryKey(e));
if(queued(e)&&!next){ await saveUpNext(rest); toast('Removed from Up Next'); return; }
const enc=e.enclosures.find(isPlayable)||e.enclosures.find(playsKind);
if(!enc){ toast('There is nothing to play in this item.',true); return; }
await saveUpNext(next?[e,...rest]:[...rest,e]);
if(!enc.path){
await api(`/api/enclosures/${enc.id}/download`,{method:'POST'}).catch(()=>{});
enc.state='pending';
}
toast((next?'Plays next':'Added to Up Next')+(enc.path?'':'. Downloading it first.'));
}
function paintQueueButton(b: HTMLButtonElement){
const key=b.dataset.key, on=!!key&&upNext.some(x=>entryKey(x)===key);
const label=on?'Remove from Up Next':'Add to Up Next';
b.setAttribute('aria-pressed',String(on)); b.title=label; b.setAttribute('aria-label',label);
b.innerHTML=on?ICON.queued:ICON.queue;
}
/// The toggle on a row or in the reader, drawn with its state.
const queueButton=(e: Entry,cls='iconbtn')=>`<button class="${cls}" data-a="queue" data-key="${esc(entryKey(e))}" aria-pressed="${queued(e)}"
title="${queued(e)?'Remove from Up Next':'Add to Up Next'}" aria-label="${queued(e)?'Remove from Up Next':'Add to Up Next'}">${queued(e)?ICON.queued:ICON.queue}</button>`;
/// What plays after this one ends, or the next button is pressed.
function playNext(){
const e=nextUp(upNext,player.guid?player.feed+'\n'+player.guid:null);
if(e) play(e);
return !!e;
}
/// Playing takes an episode out of Up Next: it is under way, and Currently Listening has it now.
function startedPlaying(e: Entry){
if(queued(e)) saveUpNext(upNext.filter(x=>entryKey(x)!==entryKey(e)));
}
/// The phone's player is told what follows (#473), so with the screen locked, when the page is
/// frozen and cannot answer the end of an episode, it goes on to the next by itself.
function hostNext(){
const next=nextUp(upNext,player.guid?player.feed+'\n'+player.guid:null);
$<HTMLButtonElement>('#pnext').hidden=!next;
try{
if(!ipxHost||!hostCan.has('next')) return;
const e=next, enc=e?.enclosures.find(isPlayable);
ipxHost(e&&enc?{t:'next',...hostItem(e,enc,mediaSrc(enc))}:{t:'next'});
}catch{}
}
/// Up Next's place: the list, a handle on each row to drag it up or down (or its arrow keys),
/// and a button to take it out.
function drawUpNext(){
const box=$('#listening'); if(!box) return;
$('#count').textContent=`Up Next: ${plural(upNext.length,'episode')}`;
box.innerHTML=upNext.length?'':'<p class="hint">Nothing lined up. Add an episode with its Up Next button, and it plays when the one before it ends.</p>';
for(const e of upNext){
const el=listenRow(e,'Remove from Up Next',()=>saveUpNext(upNext.filter(x=>entryKey(x)!==entryKey(e))));
el.insertAdjacentHTML('afterbegin',`<button class="iconbtn grip" title="Drag to move, or use the arrow keys" aria-label="Move ${esc(entryName(e).text)}">${ICON.grip}</button>`);
box.appendChild(el);
}
for(const g of $$<HTMLButtonElement>('.grip',box)) grip(g,box);
}
/// One row's handle. A drag moves the row among the others as the pointer goes, and saves where
/// it is let go; Up and Down move it one place, for a keyboard.
function grip(g: HTMLButtonElement,box: HTMLElement){
const row=g.closest<HTMLElement>('.childrow')!;
const save=()=>{
const list=$$<HTMLElement & {entry?: Entry}>('.childrow',box).map(r=>r.entry!);
if(list.some((e,i)=>e!==upNext[i])) saveUpNext(list);
};
g.onclick=ev=>ev.stopPropagation();
g.onkeydown=ev=>{
const [up,down]=[row.previousElementSibling,row.nextElementSibling];
if(ev.key==='ArrowUp'&&up) box.insertBefore(row,up);
else if(ev.key==='ArrowDown'&&down) box.insertBefore(down,row);
else return;
ev.preventDefault(); g.focus(); save();
};
g.onpointerdown=ev=>{
ev.preventDefault(); ev.stopPropagation();
g.setPointerCapture(ev.pointerId);
row.classList.add('dragging');
g.onpointermove=m=>{
const over=$$<HTMLElement>('.childrow',box).find(r=>r!==row&&m.clientY<r.getBoundingClientRect().bottom);
if(over&&m.clientY>over.getBoundingClientRect().top+over.offsetHeight/2) box.insertBefore(row,over.nextElementSibling);
else if(over) box.insertBefore(row,over);
else box.appendChild(row);
};
g.onpointerup=g.onpointercancel=()=>{ g.onpointermove=null; row.classList.remove('dragging'); save(); };
};
}