jukebox
All repositories: gitoria
3.5 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 }
Branches
- code-order
- day39
- day41
- day72
- mainmain branch
- reconnect-retry
- records-1119
- records-1123
- records-day39
- records-day41
- status-22-pr
Latest commits
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre
- a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
- f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
- d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
- df7920ceRecords: R3 startedmre