@sdeck/plugin-spotify 0.1.0 → 0.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdeck/plugin-spotify",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Built-in sdeck integration. Spotify: now playing and playback control.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -32,8 +32,8 @@
32
32
  "test": "bun test"
33
33
  },
34
34
  "peerDependencies": {
35
- "@sdeck/plugin": "^0.1.0",
36
- "@sdeck/schema": "^0.1.0",
35
+ "@sdeck/plugin": "^0.3.0",
36
+ "@sdeck/schema": "^0.3.0",
37
37
  "react": ">=19.3"
38
38
  },
39
39
  "peerDependenciesMeta": {
@@ -48,8 +48,8 @@
48
48
  }
49
49
  },
50
50
  "devDependencies": {
51
- "@sdeck/plugin": "0.1.0",
52
- "@sdeck/schema": "0.1.0"
51
+ "@sdeck/plugin": "0.3.0",
52
+ "@sdeck/schema": "0.3.0"
53
53
  },
54
54
  "repository": {
55
55
  "type": "git",
package/src/api.ts CHANGED
@@ -60,7 +60,7 @@ export class RateLimited extends Error {
60
60
 
61
61
  /** Not signed in: there is nothing to retry until the user signs in. */
62
62
  export class SignedOut extends Error {
63
- constructor(message = "Sign in to Spotify from the plugin's page") {
63
+ constructor(message = "Sign in to Spotify from the plugin's settings") {
64
64
  super(message);
65
65
  }
66
66
  }
@@ -131,7 +131,7 @@ export class SpotifyClient {
131
131
  return page("sdeck is connected to Spotify. You can close this tab.");
132
132
  } catch (error) {
133
133
  close(error as Error);
134
- return page("Signing in failed. See the plugin's page in sdeck.");
134
+ return page("Signing in failed. See the plugin's settings in sdeck.");
135
135
  }
136
136
  },
137
137
  });
@@ -272,6 +272,11 @@ export interface Player {
272
272
  progress: number;
273
273
  positionMs: number;
274
274
  durationMs: number;
275
+ /** The same in seconds, as published. */
276
+ position: number;
277
+ duration: number;
278
+ /** When it would have started to be where it is now; only while playing. */
279
+ startedAt: number | undefined;
275
280
  volume: number | undefined;
276
281
  shuffle: boolean;
277
282
  repeat: "off" | "track" | "context";
@@ -290,6 +295,9 @@ export const IDLE: Player = {
290
295
  art: "",
291
296
  progress: 0,
292
297
  positionMs: 0,
298
+ position: 0,
299
+ duration: 0,
300
+ startedAt: undefined,
293
301
  durationMs: 0,
294
302
  volume: undefined,
295
303
  shuffle: false,
@@ -299,8 +307,8 @@ export const IDLE: Player = {
299
307
  context: "",
300
308
  };
301
309
 
302
- /** Reads the answer of `GET /me/player` (empty when nothing is playing anywhere). */
303
- export function parsePlayer(body: unknown): Player {
310
+ /** Reads the answer of `GET /me/player` (empty when nothing is playing), as it was at `at`. */
311
+ export function parsePlayer(body: unknown, at = Date.now()): Player {
304
312
  if (typeof body !== "object" || body === null) return IDLE;
305
313
  const raw = body as {
306
314
  is_playing?: boolean;
@@ -335,6 +343,9 @@ export function parsePlayer(body: unknown): Player {
335
343
  art,
336
344
  progress: duration > 0 ? Math.round((100 * position) / duration) : 0,
337
345
  positionMs: position,
346
+ position: position / 1000,
347
+ duration: duration / 1000,
348
+ startedAt: raw.is_playing && item ? Math.round(at - position) : undefined,
338
349
  durationMs: duration,
339
350
  volume: raw.device?.volume_percent ?? undefined,
340
351
  shuffle: raw.shuffle_state === true,
package/src/index.tsx CHANGED
@@ -66,8 +66,14 @@ const player = new Shared<Player | undefined>((emit) => {
66
66
  clearTimeout(timer);
67
67
  let wait = POLL_MS;
68
68
  try {
69
- const next = parsePlayer(await spotify.request("GET", "/me/player"));
69
+ const asked = Date.now();
70
+ const body = await spotify.request("GET", "/me/player");
71
+ // Spotify read the position about halfway through the request.
72
+ const next = parsePlayer(body, (asked + Date.now()) / 2);
70
73
  if (!stopped) emit(next);
74
+ // Look again right after the track ends, rather than up to a whole wait later.
75
+ const left = next.durationMs - next.positionMs;
76
+ if (next.playing && left > 0) wait = Math.min(wait, left + 400);
71
77
  context?.status("Signed in to Spotify", "ok");
72
78
  } catch (error) {
73
79
  if (!stopped) emit(undefined);
@@ -209,7 +215,7 @@ const playPause = defineAction({
209
215
  icon: "play",
210
216
  slots: ["key", "sensor"],
211
217
  settings: {
212
- cover: { type: "boolean", label: "Show the cover art", default: true },
218
+ cover: { type: "boolean", label: "Show the cover art", default: true, appearance: true },
213
219
  },
214
220
  run: async () => {
215
221
  const before = (await readPlayer()).playing;
@@ -292,6 +298,7 @@ const nowPlaying = defineAction({
292
298
  ],
293
299
  },
294
300
  },
301
+ display: true,
295
302
  run: async () => ({ result: await readPlayer() }),
296
303
  component: ({ settings, slot, plugin }) => {
297
304
  const act = () => {
@@ -539,7 +546,7 @@ const addToPlaylist = defineAction({
539
546
  source: "playlists",
540
547
  description: "Yours, and ones you collaborate on.",
541
548
  },
542
- text: { type: "text", label: "Text", placeholder: "Shown under the icon" },
549
+ text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true },
543
550
  },
544
551
  run: async ({ settings }) => {
545
552
  if (!settings.playlist) throw new Error("Pick a playlist in the key's settings");
@@ -588,7 +595,7 @@ const play = defineAction({
588
595
  default: false,
589
596
  visibleIf: { mode: "play" },
590
597
  },
591
- text: { type: "text", label: "Text", placeholder: "Shown under the icon" },
598
+ text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true },
592
599
  },
593
600
  run: async ({ settings }) => {
594
601
  const target = spotifyUri(settings.item);
@@ -674,7 +681,7 @@ const device = defineAction({
674
681
  source: "devices",
675
682
  description: "Only devices with Spotify open are listed.",
676
683
  },
677
- text: { type: "text", label: "Text", placeholder: "Shown under the icon" },
684
+ text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true },
678
685
  },
679
686
  run: async ({ settings }) => {
680
687
  if (!settings.device) throw new Error("Pick a device in the key's settings");
@@ -722,7 +729,7 @@ const playlistOptions = async (writable: boolean): Promise<SelectOption[]> =>
722
729
  export default definePlugin({
723
730
  id: "@sdeck/plugin-spotify",
724
731
  name: "Spotify",
725
- version: "0.1.0",
732
+ version: "0.3.0",
726
733
  namespace: "spotify",
727
734
  description:
728
735
  "Now playing, playback, Liked Songs, playlists and devices through the Spotify Web API.",
@@ -843,6 +850,13 @@ export default definePlugin({
843
850
  stateOf("album", "Album", "string"),
844
851
  stateOf("art", "Cover art", "string"),
845
852
  stateOf("progress", "Progress", "number"),
853
+ { ...stateOf("position", "Position", "number"), unit: "s" },
854
+ { ...stateOf("duration", "Duration", "number"), unit: "s" },
855
+ {
856
+ ...stateOf("startedAt", "Started at", "number"),
857
+ description:
858
+ "When the track would have started to be where it is now, in ms since 1970. Only while playing: now() - spotify.startedAt is the exact position.",
859
+ },
846
860
  stateOf("volume", "Volume", "number"),
847
861
  stateOf("shuffle", "Shuffle", "boolean"),
848
862
  stateOf("repeat", "Repeat", "string"),