@sound2score/player 0.0.5 → 0.2.2

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/README.md CHANGED
@@ -72,10 +72,13 @@ work even when the embed's options hide that control.
72
72
 
73
73
  - `ready(state)`: the score loaded and takes commands.
74
74
  - `change(state)`: what the Player shows changed, at most 4 times a second.
75
- - `unavailable({ reason, resetsAt })`: the embed can't play. `reason` is
76
- `not-found`, `site` (your site isn't allowed), `capped` (the owner's
77
- spending limit, until `resetsAt`) or `busy` (too many new viewers from one
78
- network, until `resetsAt`).
75
+ - `unavailable({ reason, resetsAt, upgradeTo })`: the embed can't play.
76
+ `reason` is `not-found`, `site` (your site isn't allowed), `capped` (the
77
+ owner's spending limit, until `resetsAt`), `busy` (too many new viewers
78
+ from one network, until `resetsAt`) or `upgrade` (this copy of
79
+ `@sound2score/player` and the embed speak incompatible protocol versions;
80
+ when `upgradeTo` is set, install `@sound2score/player@<upgradeTo>` or
81
+ later).
79
82
 
80
83
  ## Students
81
84
 
package/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // src/protocol.ts
2
2
  var PROTOCOL_VERSION = 1;
3
+ var MIN_PROTOCOL_VERSION = 1;
4
+ var MIN_PLAYER_VERSION = "0.0.3";
3
5
  var PROTOCOL_TAG = "sound2score-player";
4
6
 
5
7
  // src/embed-message-reader.ts
@@ -7,7 +9,8 @@ var REASONS = [
7
9
  "not-found",
8
10
  "site",
9
11
  "capped",
10
- "busy"
12
+ "busy",
13
+ "upgrade"
11
14
  ];
12
15
 
13
16
  class EmbedMessageReader {
@@ -111,7 +114,13 @@ class EmbedMessageReader {
111
114
  if (!reason || fields?.resetsAt !== null && resetsAt === null) {
112
115
  return null;
113
116
  }
114
- return { reason, resetsAt };
117
+ if (fields?.upgradeTo === undefined) {
118
+ return { reason, resetsAt };
119
+ }
120
+ if (typeof fields.upgradeTo !== "string" || fields.upgradeTo.length === 0) {
121
+ return null;
122
+ }
123
+ return { reason, resetsAt, upgradeTo: fields.upgradeTo };
115
124
  }
116
125
  bars(value) {
117
126
  const fields = this.fields(value);
@@ -521,9 +530,12 @@ class Player {
521
530
  frame;
522
531
  dispatcher = new Dispatcher;
523
532
  disposer = new Disposer;
533
+ protocolVersion;
534
+ onError;
524
535
  state = null;
525
536
  ready = false;
526
537
  unavailable = null;
538
+ upgradeLogged = false;
527
539
  waiting = [];
528
540
  static create(container, opts) {
529
541
  const origin = opts.origin ?? DEFAULT_ORIGIN;
@@ -557,8 +569,10 @@ class Player {
557
569
  look: { ...opts.look }
558
570
  };
559
571
  }
560
- constructor(frame) {
572
+ constructor(frame, config) {
561
573
  this.frame = frame;
574
+ this.protocolVersion = config?.protocolVersion ?? PROTOCOL_VERSION;
575
+ this.onError = config?.onError ?? ((message) => console.error(message));
562
576
  this.disposer.use(this.dispatcher);
563
577
  this.disposer.use(frame);
564
578
  this.disposer.defer(frame.events.on("message", (message) => this.receive(message)));
@@ -613,9 +627,24 @@ class Player {
613
627
  }
614
628
  }
615
629
  send(command) {
616
- this.frame.post({ tag: PROTOCOL_TAG, version: PROTOCOL_VERSION, command });
630
+ this.frame.post({
631
+ tag: PROTOCOL_TAG,
632
+ version: this.protocolVersion,
633
+ command
634
+ });
617
635
  }
618
- receive({ event }) {
636
+ receive({ version, event }) {
637
+ if (event.kind === "unavailable" && event.unavailable.reason === "upgrade") {
638
+ this.rejectUpgrade(event.unavailable, event.unavailable.upgradeTo ? `@sound2score/player: This version is no longer supported by the embed. Upgrade to @sound2score/player@${event.unavailable.upgradeTo} or later (npm install @sound2score/player@${event.unavailable.upgradeTo}).` : "@sound2score/player: This version is no longer supported by the embed. Upgrade to the latest @sound2score/player (npm install @sound2score/player@latest).");
639
+ return;
640
+ }
641
+ if (version < this.protocolVersion) {
642
+ if (this.unavailable?.reason === "upgrade") {
643
+ return;
644
+ }
645
+ this.rejectUpgrade({ reason: "upgrade", resetsAt: null }, `@sound2score/player: The embed speaks protocol version ${version}, which is older than this package's protocol version ${this.protocolVersion}. Wait for the embed to update or install a matching version of @sound2score/player.`);
646
+ return;
647
+ }
619
648
  switch (event.kind) {
620
649
  case "ready": {
621
650
  this.state = event.state;
@@ -644,9 +673,20 @@ class Player {
644
673
  return;
645
674
  }
646
675
  }
676
+ rejectUpgrade(unavailable, message) {
677
+ this.ready = false;
678
+ this.waiting = [];
679
+ this.unavailable = unavailable;
680
+ if (!this.upgradeLogged) {
681
+ this.upgradeLogged = true;
682
+ this.onError(message);
683
+ }
684
+ this.dispatcher.dispatch("unavailable", unavailable);
685
+ }
647
686
  loaded() {
648
687
  this.ready = false;
649
688
  this.unavailable = null;
689
+ this.upgradeLogged = false;
650
690
  this.send({ kind: "hello" });
651
691
  }
652
692
  }
@@ -660,6 +700,8 @@ export {
660
700
  IframeEmbedFrame,
661
701
  MAX_SEMITONES,
662
702
  MAX_SPEED,
703
+ MIN_PLAYER_VERSION,
704
+ MIN_PROTOCOL_VERSION,
663
705
  MIN_SPEED,
664
706
  PROTOCOL_TAG,
665
707
  PROTOCOL_VERSION,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sound2score/player",
3
- "version": "0.0.5",
3
+ "version": "0.2.2",
4
4
  "description": "Embed sound2score scores on your site and control playback from your page.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: sound2score-player
3
3
  description: Embed sound2score sheet music on a website and control it from the page with @sound2score/player. Use when adding a sound2score score to a site, creating or attaching its embed, passing the viewer's user ID, playing, pausing, seeking, looping, changing speed, transposing or toggling the metronome from page code, or reacting to the embed's ready, change and unavailable events.
4
- version: 0.0.5
4
+ version: 0.2.2
5
5
  ---
6
6
 
7
7
  # sound2score Player
@@ -110,12 +110,18 @@ function show(state: PlayerState): void {
110
110
  player.events.on("ready", show);
111
111
  // At most 4 times a second, whenever what the Player shows changed.
112
112
  player.events.on("change", show);
113
- player.events.on("unavailable", ({ reason, resetsAt }) => {
113
+ player.events.on("unavailable", ({ reason, resetsAt, upgradeTo }) => {
114
114
  // "not-found": no such score, or it can't be embedded.
115
115
  // "site": this site isn't one the owner allows.
116
116
  // "capped": the owner reached their spending limit until resetsAt.
117
117
  // "busy": too many new viewers from one network, until resetsAt.
118
- console.log(reason, resetsAt === null ? null : new Date(resetsAt));
118
+ // "upgrade": this package and the embed speak different protocol versions;
119
+ // upgradeTo names the @sound2score/player version to install when set.
120
+ console.log(
121
+ reason,
122
+ resetsAt === null ? null : new Date(resetsAt),
123
+ upgradeTo ?? null,
124
+ );
119
125
  });
120
126
  ```
121
127
 
package/types/index.d.ts CHANGED
@@ -5,5 +5,5 @@ export { EmbedOptionsCodec } from "./src/embed-options-codec.js";
5
5
  export { EmbedUrlBuilder } from "./src/embed-url-builder.js";
6
6
  export { FakeEmbedFrame } from "./src/fake-embed-frame.js";
7
7
  export { IframeEmbedFrame } from "./src/iframe-embed-frame.js";
8
- export { type CreateOptions, Player, type PlayerEvents, type PlayerOptions, } from "./src/player.js";
9
- export { type Command, type EmbedEvent, type EmbedMessage, type HostMessage, type PlayerState, PROTOCOL_TAG, PROTOCOL_VERSION, type Unavailable, type UnavailableReason, } from "./src/protocol.js";
8
+ export { type CreateOptions, Player, type PlayerConfig, type PlayerEvents, type PlayerOptions, } from "./src/player.js";
9
+ export { type Command, type EmbedEvent, type EmbedMessage, type HostMessage, MIN_PLAYER_VERSION, MIN_PROTOCOL_VERSION, type PlayerState, PROTOCOL_TAG, PROTOCOL_VERSION, type Unavailable, type UnavailableReason, } from "./src/protocol.js";
@@ -34,6 +34,11 @@ export type CreateOptions = Readonly<{
34
34
  /** Where sound2score is served from; `https://sound2score.com` by default. */
35
35
  origin?: string;
36
36
  }>;
37
+ /** Optional runtime settings for a Player instance. */
38
+ export type PlayerConfig = Readonly<{
39
+ protocolVersion?: number;
40
+ onError?: (message: string) => void;
41
+ }>;
37
42
  /**
38
43
  * A sound2score score embedded on your page, which your page can play, pause,
39
44
  * seek, loop, slow down, transpose and click along to, and hear back from.
@@ -45,9 +50,12 @@ export declare class Player implements Eventful<PlayerEvents>, Resource {
45
50
  private readonly frame;
46
51
  private readonly dispatcher;
47
52
  private readonly disposer;
53
+ private readonly protocolVersion;
54
+ private readonly onError;
48
55
  private state;
49
56
  private ready;
50
57
  private unavailable;
58
+ private upgradeLogged;
51
59
  private waiting;
52
60
  /** Adds an embed of a score to `container`, filling its width. */
53
61
  static create(container: HTMLElement, opts: CreateOptions): Player;
@@ -55,7 +63,7 @@ export declare class Player implements Eventful<PlayerEvents>, Resource {
55
63
  static attach(iframe: HTMLIFrameElement): Player;
56
64
  private static frame;
57
65
  private static options;
58
- constructor(frame: EmbedFrame);
66
+ constructor(frame: EmbedFrame, config?: PlayerConfig);
59
67
  get events(): Events<PlayerEvents>;
60
68
  /** What the Player last showed, or null until it is ready. */
61
69
  getState(): PlayerState | null;
@@ -85,6 +93,7 @@ export declare class Player implements Eventful<PlayerEvents>, Resource {
85
93
  private command;
86
94
  private send;
87
95
  private receive;
96
+ private rejectUpgrade;
88
97
  /** A page loading again starts over, so the Player asks again whether it is ready. */
89
98
  private loaded;
90
99
  }
@@ -5,6 +5,16 @@ import type { BarRange } from "./embed-options.js";
5
5
  * with the copy of this package it was built with.
6
6
  */
7
7
  export declare const PROTOCOL_VERSION = 1;
8
+ /**
9
+ * The oldest protocol version the embed still answers. A page speaking an
10
+ * older version receives an `unavailable` event with reason `"upgrade"`.
11
+ */
12
+ export declare const MIN_PROTOCOL_VERSION = 1;
13
+ /**
14
+ * The oldest `@sound2score/player` package version that speaks at least
15
+ * `MIN_PROTOCOL_VERSION`.
16
+ */
17
+ export declare const MIN_PLAYER_VERSION = "0.0.3";
8
18
  /** Marks a message as an embed's, among everything else a page posts. */
9
19
  export declare const PROTOCOL_TAG = "sound2score-player";
10
20
  /** What the Player is showing. */
@@ -35,11 +45,15 @@ export type UnavailableReason =
35
45
  /** The owner has reached their spending limit until `resetsAt`. */
36
46
  | "capped"
37
47
  /** Too many new viewers came from one network, until `resetsAt`. */
38
- | "busy";
48
+ | "busy"
49
+ /** This copy of `@sound2score/player` and the embed speak incompatible protocol versions. */
50
+ | "upgrade";
39
51
  export type Unavailable = Readonly<{
40
52
  reason: UnavailableReason;
41
53
  /** When it can play again, in milliseconds since the epoch, or null. */
42
54
  resetsAt: number | null;
55
+ /** The `@sound2score/player` version to install when `reason` is `"upgrade"`. */
56
+ upgradeTo?: string;
43
57
  }>;
44
58
  /** What your page asks the embed to do. */
45
59
  export type Command = Readonly<{