@doitian/dsh-music 0.1.10 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -256,15 +256,23 @@ window.__ModuleLoader__.load({
256
256
  * so an attempt can legitimately fail and has to be repeatable. Both
257
257
  * outcomes are reported at once, so the host — and the panel's hint —
258
258
  * learns the real state instead of waiting for the next scheduled report.
259
+ * `blocked` says which: only the autoplay policy's `NotAllowedError` is a
260
+ * reason to ask for a click, not an attempt still under way or one
261
+ * interrupted by a new source.
259
262
  */
260
263
  function attemptPlay() {
261
264
  if (playing || disposed) return;
262
265
  playing = true;
263
- const done = () => {
264
- playing = false;
265
- void report({});
266
- };
267
- audio.play().then(done, done);
266
+ audio.play().then(
267
+ () => {
268
+ playing = false;
269
+ void report({ blocked: false });
270
+ },
271
+ (error) => {
272
+ playing = false;
273
+ void report(error?.name === 'NotAllowedError' ? { blocked: true } : {});
274
+ },
275
+ );
268
276
  }
269
277
 
270
278
  /**
package/lib/panel.html CHANGED
@@ -241,9 +241,20 @@
241
241
  .about .writing { color: var(--muted); margin: 0; }
242
242
  .details .source { align-self: flex-start; font-size: 12px; }
243
243
 
244
+ /* Notices float over the page just below the header instead of taking
245
+ a place in it: one that comes and goes, such as the autoplay banner,
246
+ must not move what the listener is about to click. */
247
+ header { position: relative; }
248
+ #notices {
249
+ position: absolute; top: calc(100% + 8px); left: 50%; transform: translateX(-50%); z-index: 30;
250
+ width: max-content; max-width: calc(100% - 2 * var(--gap));
251
+ display: flex; flex-direction: column; gap: 6px; pointer-events: none;
252
+ }
253
+ #notices > * { pointer-events: auto; }
244
254
  .need-gesture {
245
- background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
255
+ background: color-mix(in srgb, var(--accent) 13%, var(--bg)); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
246
256
  border-radius: var(--radius); padding: 8px 12px; font-size: 12px;
257
+ box-shadow: 0 6px 20px color-mix(in srgb, #000 18%, transparent);
247
258
  }
248
259
 
249
260
  /* -------------------------------------------------------------- side */
@@ -353,11 +364,11 @@
353
364
  <button id="scrobble-toggle" class="switch" role="switch" aria-labelledby="history-label"></button>
354
365
  </div>
355
366
  <div id="account"></div>
367
+ <div id="notices" aria-live="polite"></div>
356
368
  </header>
357
369
 
358
370
  <main>
359
371
  <section class="stage">
360
- <div id="notices"></div>
361
372
  <div class="np">
362
373
  <div class="cover"><span id="cover-icon"></span><img id="cover" alt="" /></div>
363
374
  <div class="np-text">
@@ -900,6 +911,8 @@
900
911
  let detailsRequest = 0;
901
912
  /** Details already fetched, by track id, so switching back is instant. */
902
913
  const detailsCache = new Map();
914
+ /** Details being fetched, by track id, so a prefetch and a load share one request. */
915
+ const detailsLoads = new Map();
903
916
  /**
904
917
  * Model-written introductions by `<track id>:<language>`: `'pending'`
905
918
  * while one is being written, then `{text}`, empty when none could be.
@@ -1025,11 +1038,20 @@
1025
1038
  let busy = false;
1026
1039
  let disposed = false;
1027
1040
 
1041
+ /** Start audio, and report a refusal by the autoplay policy as the engine does. */
1028
1042
  function attemptPlay() {
1029
1043
  if (busy || disposed) return;
1030
1044
  busy = true;
1031
- const done = () => { busy = false; };
1032
- audio.play().then(done, done);
1045
+ audio.play().then(
1046
+ () => {
1047
+ busy = false;
1048
+ report({ blocked: false });
1049
+ },
1050
+ (error) => {
1051
+ busy = false;
1052
+ if (error?.name === 'NotAllowedError') report({ blocked: true });
1053
+ },
1054
+ );
1033
1055
  }
1034
1056
 
1035
1057
  function reconcile(next) {
@@ -1175,19 +1197,16 @@
1175
1197
  }
1176
1198
 
1177
1199
  /**
1178
- * The host wants playback, and the transport's own report for *this*
1179
- * track says it is not playing. In practice that means the browser
1180
- * refused to start audio until the page has been interacted with.
1200
+ * The host wants playback, and the transport reported that the browser
1201
+ * refused to start it until the page is interacted with.
1202
+ *
1203
+ * Only the refusal counts. "Wanted but not playing" alone is also true
1204
+ * for the half second after any Play click, before the transport has
1205
+ * caught up — which flashed this hint on every press.
1181
1206
  */
1182
1207
  function autoplayBlocked() {
1183
1208
  const reported = state?.reported;
1184
- return Boolean(
1185
- state?.playing &&
1186
- state?.current &&
1187
- reported?.playing === false &&
1188
- Number(reported.at) > 0 &&
1189
- reported.trackId === state.current.id,
1190
- );
1209
+ return Boolean(state?.playing && state?.current && reported?.blocked === true && reported.playing === false);
1191
1210
  }
1192
1211
 
1193
1212
  /**
@@ -1226,6 +1245,7 @@
1226
1245
  // pane stayed empty forever.
1227
1246
  syncInfo(track?.id ?? null);
1228
1247
  loadIntro();
1248
+ prefetchNextDetails();
1229
1249
  setIcon($('play'), state.playing ? 'pause' : 'play', { filled: true });
1230
1250
  $('mode').textContent = { list: t('modeList'), single: t('modeSingle'), shuffle: t('modeShuffle') }[state.mode] || t('modeList');
1231
1251
  $('counts').textContent = state.counts ? `(${state.counts.queued})` : '';
@@ -1535,6 +1555,35 @@
1535
1555
  if (prose) prose.classList.toggle('open');
1536
1556
  };
1537
1557
 
1558
+ /** One track's details, fetched once however many ask, and kept. */
1559
+ function fetchDetails(id) {
1560
+ if (!detailsLoads.has(id)) {
1561
+ const load = api('/info/' + id)
1562
+ .then((info) => {
1563
+ detailsCache.set(id, info);
1564
+ if (detailsCache.size > 30) detailsCache.delete(detailsCache.keys().next().value);
1565
+ return info;
1566
+ })
1567
+ .finally(() => detailsLoads.delete(id));
1568
+ detailsLoads.set(id, load);
1569
+ }
1570
+ return detailsLoads.get(id);
1571
+ }
1572
+
1573
+ /**
1574
+ * Fetch the next track's details ahead, so they show the moment it
1575
+ * starts. Lazy like the introduction: only on a poll after the shown
1576
+ * track's details have landed, and only while the details are shown.
1577
+ * Shuffle has no known next track, and repeat-one's next is this one.
1578
+ */
1579
+ function prefetchNextDetails() {
1580
+ if (infoView !== 'details' || !detailsTrackId || !detailsCache.has(detailsTrackId)) return;
1581
+ if (!state || state.mode !== 'list') return;
1582
+ const next = state.queue?.[state.index + 1];
1583
+ if (!next?.id || detailsCache.has(next.id) || detailsLoads.has(next.id)) return;
1584
+ fetchDetails(next.id).catch(() => { /* the track's own load retries */ });
1585
+ }
1586
+
1538
1587
  async function loadDetails(id) {
1539
1588
  const request = ++detailsRequest;
1540
1589
  introSource = 'ai';
@@ -1545,9 +1594,7 @@
1545
1594
  }
1546
1595
  $('details').innerHTML = `<p class="hint">${esc(t('loading'))}</p>`;
1547
1596
  try {
1548
- const info = await api('/info/' + id);
1549
- detailsCache.set(id, info);
1550
- if (detailsCache.size > 30) detailsCache.delete(detailsCache.keys().next().value);
1597
+ const info = await fetchDetails(id);
1551
1598
  if (request !== detailsRequest) return;
1552
1599
  paintDetails(info);
1553
1600
  } catch (error) {
package/lib/state.js CHANGED
@@ -61,8 +61,11 @@ export class Player {
61
61
  this.muted = false;
62
62
  /** One-shot absolute seek target in milliseconds. */
63
63
  this.pendingSeek = null;
64
- /** Last state the panel reported back. */
65
- this.reported = { trackId: null, position: 0, duration: 0, playing: false, at: 0, error: null };
64
+ /**
65
+ * Last state the panel reported back. `blocked` is whether the browser
66
+ * refused the last attempt to start audio for want of a user gesture.
67
+ */
68
+ this.reported = { trackId: null, position: 0, duration: 0, playing: false, blocked: false, at: 0, error: null };
66
69
  /**
67
70
  * Last playback failure, preserved across the automatic skip that follows
68
71
  * it. `reported.error` is per-track and cleared on every move, so without
@@ -464,7 +467,7 @@ export class Player {
464
467
  * did in response, so the caller can run follow-up work such as a DJ top-up.
465
468
  * @returns {{ended: boolean, failed: boolean, advanced: boolean}}
466
469
  */
467
- report({ trackId, position, duration, playing, ended, error } = {}) {
470
+ report({ trackId, position, duration, playing, blocked, ended, error } = {}) {
468
471
  const current = this.current();
469
472
  // Ignore reports for a track the user already moved away from; they arrive
470
473
  // for a moment after every skip.
@@ -474,6 +477,9 @@ export class Player {
474
477
  if (Number.isFinite(position)) this.reported.position = Number(position);
475
478
  if (Number.isFinite(duration) && duration > 0) this.reported.duration = Number(duration);
476
479
  if (typeof playing === 'boolean') this.reported.playing = playing;
480
+ // Audio that plays is the end of a block, whichever report says so.
481
+ if (playing === true) this.reported.blocked = false;
482
+ else if (typeof blocked === 'boolean') this.reported.blocked = blocked;
477
483
  this.reported.trackId = current?.id ?? null;
478
484
  this.reported.at = Date.now();
479
485
  if (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doitian/dsh-music",
3
- "version": "0.1.10",
3
+ "version": "1.0.0",
4
4
  "description": "NetEase Cloud Music (music.163.com) player and AI DJ for DeepSeek Harness: a sidebar page with in-app playback, QR sign-in, and agent tools that curate the queue.",
5
5
  "keywords": [
6
6
  "dsh",