jukebox
All repositories: gitoria
4.0 KB
// lib/queue.hl — THE SHUFFLED QUEUE, kept on the server. A shuffle never sends its songs to the browser: the browser holds a small context `{ kind: 'shuffle', seed, spec }`// and asks for the next items by index. The server makes the order from the seed (the same seed gives the same order, so a restarted server or a second tab finds the same queue),// every song of the pool comes once before any comes again (a new round gets a new order from seed + round), and the last orders are kept in memory.// spec = { kind: 'favourites', minPlays } (every song played at least N times, file count + jukebox count)// | { kind: 'list', ctx } (the list a screen shows: songs with a minimum, an album, an artist's songs, a search)import { floor } from 'hl:math'import { orderList, songsPage, searchAll } from './browse.hl'import { cache } from './state.hl'static keepOrders = 6static roundStep = 7919// the ids of a pool, in list orderstatic poolFor = (spec) => {out = []if (spec.kind == 'favourites') {for (r of orderList('title')) {if (r.pc >= spec.minPlays) { out.push(r.id) }}return out}ctx = spec.ctxif (ctx.kind == 'songs') {min = ctx.minPlays == null ? 0 : ctx.minPlaysfor (r of orderList('title')) {if (r.pc >= min) { out.push(r.id) }}return out}let off = 0let total = 1while (off < total) {let page = nullif (ctx.kind == 'album') { page = songsPage('artist', off, 200, null, null, ctx.album) }else if (ctx.kind == 'artist-songs') { page = songsPage('artist', off, 200, null, ctx.artist, null) }else if (ctx.kind == 'search') { page = searchAll(ctx.q, off, 200).songs }else { return out }total = page.totalif (page.items.length == 0) { break }for (s of page.items) { out.push(s.id) }off = off + page.items.length}return out}// the pool in a random order made from the seed (Fisher-Yates with a Park-Miller generator: numbers stay far below 2^53).// `first` (a song id) goes to the front of the order: a shuffle turned on while that song plays keeps it playing (it is added when the pool lacks it).static shuffled = (spec, seed, first) => {ids = poolFor(spec)let x = seed % 2147483646 + 1let i = ids.length - 1while (i > 0) {x = x * 48271 % 2147483647j = floor(x / 2147483647 * (i + 1))t = ids[i]ids[i] = ids[j]ids[j] = ti = i - 1}if (first == null || first == '') { return ids }out = [first]for (id of ids) {if (id != first) { out.push(id) }}return out}static orderKey = (spec, seed, first) => { return seed + ':' + JSON.stringify(spec) + ':' + (first == null ? '' : first) }// the order of one round, from the memory or made nowstatic roundOrder = (spec, seed, first) => {k = orderKey(spec, seed, first)q = cache.queues[k]if (q != null) { return q }ids = shuffled(spec, seed, first)qs = cache.queuesqs[k] = idslet names = cache.queueNamesnames.push(k)if (names.length > keepOrders) {old = names[0]names = names.slice(1)qs[old] = null}cache.queues = qscache.queueNames = namesreturn ids}// the song id at a place of the endless queue (round after round), or null when the pool is emptystatic idAt = (ctx, index) => {firstRound = roundOrder(ctx.spec, ctx.seed, ctx.first)n = firstRound.lengthif (n == 0) { return null }round = floor(index / n)ids = round == 0 ? firstRound : roundOrder(ctx.spec, ctx.seed + round * roundStep, null)return ids[index - round * n]}// how many songs the pool holdsstatic poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed, ctx.first).length }// The requested slice of a favourites shuffle, using the same context as the player.static queueIds = (Number seed, Number minPlays, Number offset, Number count) => {ctx = { kind = 'shuffle' seed = seed spec = { kind = 'favourites' minPlays = minPlays == null ? 5 : minPlays } }start = offset == null ? 0 : offsetlimit = count == null ? 50 : countids = []let i = startwhile (i < start + limit) {ids.push(idAt(ctx, i))i = i + 1}return { pool = poolSize(ctx) start = start ids = ids }}
Branches
Latest commits
- 78f0cc86|||PR records 2026-11-24: inbox-thread rule, "Not playing" confirmed and closedmre
- c1b153c4Merge branch 'records-1124c' into mainJukebox
- d7b5e780|||PR Records 2026-11-24 (2): live day72, PR #19 merge, #10 newsmre
- 43b564dbMerge branch 'records-1124b' into mainJukebox
- fa86b08c|||PR Records 2026-11-24: LOG, STATUS, missions 020-023mre
- 0a2ae3daMerge remote-tracking branch 'origin/records-1123' into records-1124mre
- 9a48cfcaMerge branch 'vendor-theme' into mainJukebox
- 170d3735Merge branch 'code-order-2' into mainJukebox
- 3f1918a7|||PR Re-sync layouts theme 4099e64 with Segoe UI firstmre
- 2e7f6a43Vendor virtual-list 489afcf from components.hybrielmre
- 0ba85799Merge branch 'reset-classes' into mainJukebox
- 502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre
- 6d5ce287|||PR jukebox#9 Centralize songs table reads and writes in db.hlmre
- dcca21abjukebox#8 Move copies and queue logic into their topicsmre
- 3b76651cjukebox#20: update virtual-list README pin and resetKey optionsmre
- bc09e1e8|||PR jukebox#20: reset virtual lists through resetKey (partial validation)mre
- a3796dc5Records: live step waits for idlemre
- 3b158e16Records: merges of #9 and #11mre
- 27204167Merge main into records-1123mre
- 10f8a8c8Merge branch 'code-order' into mainJukebox