jukebox
All repositories: gitoria
3.1 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)static shuffled = (spec, seed) => {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}return ids}static orderKey = (spec, seed) => { return seed + ':' + JSON.stringify(spec) }// the order of one round, from the memory or made nowstatic roundOrder = (spec, seed) => {k = orderKey(spec, seed)q = cache.queues[k]if (q != null) { return q }ids = shuffled(spec, seed)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) => {first = roundOrder(ctx.spec, ctx.seed)n = first.lengthif (n == 0) { return null }round = floor(index / n)ids = round == 0 ? first : roundOrder(ctx.spec, ctx.seed + round * roundStep)return ids[index - round * n]}// how many songs the pool holdsstatic poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed).length }
Branches
Latest commits
- 9bcbb017Records: day38b waits for day39 fixmre
- f80504e1Records: day38b repro sentmre
- 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
- 68aace7fRecords: mission 003 day38bmre
- d105d8a9Records: mpackdb integrity check cleanmre
- 6e980c53Records: mpackdb rule refinementmre
- 6d335ac4Records: storage copy rulingmre
- 7eb2898dRecords: mpackdb second-opener rule until day38mre
- b011f98eRecords: style form live on day35mre
- 60d90cb2Records: L1 woken for the restartmre
- 75ae544fRecords: restart waits for the connection to closemre
- e0b75951Records: L1 merged and pushed, restart waits for an open connectionmre
- f2b5db98styles: format styles with nested rules and bare queriesmre
- f036bab4search: format styles with nested rules and bare queriesmre
- ef9a9d4asongs: format styles with nested rules and bare queriesmre
- f22f9aacmain: format styles with nested rules and bare queriesmre
- f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmre
- d56e5054Sync theme and shared tokens from layouts 17e0f03mre
- b1362547Re-vendor to Hybriel set day35mre
- 89e10f08Records: mission 002 rollback wordingmre