@ldelia/react-media 0.9.0 → 0.10.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.
@@ -2,6 +2,13 @@ import { ReproductionBuilder } from './ReproductionBuilder';
2
2
  import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
3
3
  import { YouTubePlayer } from './Player/YouTubePlayer';
4
4
  type Player = PlayAlongPlayer | YouTubePlayer;
5
+ export declare const REPRODUCTION_STATES: {
6
+ STOPPED: number;
7
+ COUNTING_IN: number;
8
+ PLAYING: number;
9
+ PAUSED: number;
10
+ };
11
+ type ReproductionState = (typeof REPRODUCTION_STATES)[keyof typeof REPRODUCTION_STATES];
5
12
  type Handler = (args: object) => void;
6
13
  declare const dispatchOnReadyHandlers: unique symbol;
7
14
  declare const dispatchOnSongStartHandlers: unique symbol;
@@ -12,9 +19,9 @@ declare const dispatchOnPausedHandlers: unique symbol;
12
19
  declare const dispatchOnFinishHandlers: unique symbol;
13
20
  declare const dispatchOnErrorHandlers: unique symbol;
14
21
  export declare class Reproduction {
15
- private player;
16
- private requiresCountingIn;
17
- private songTempo;
22
+ private readonly player;
23
+ private readonly requiresCountingIn;
24
+ private readonly songTempo;
18
25
  private state;
19
26
  private interval;
20
27
  private loopInterval;
@@ -45,7 +52,8 @@ export declare class Reproduction {
45
52
  PAUSED: number;
46
53
  };
47
54
  static newBuilder(): ReproductionBuilder;
48
- on(eventName: keyof typeof Reproduction.EVENTS, handler: Handler): number | undefined;
55
+ on(eventName: keyof typeof Reproduction.EVENTS, handler: Handler): () => void;
56
+ off(eventName: keyof typeof Reproduction.EVENTS, handler: Handler): void;
49
57
  dispatch(eventName: keyof typeof Reproduction.EVENTS, args?: {}): void;
50
58
  start(): void;
51
59
  play(): void;
@@ -56,6 +64,7 @@ export declare class Reproduction {
56
64
  isStopped(): boolean;
57
65
  isPaused(): boolean;
58
66
  isCountingIn(): boolean;
67
+ getState(): ReproductionState;
59
68
  getPlayer(): Player;
60
69
  getTempo(): number;
61
70
  getCurrentTime(): number;
@@ -1,6 +1,6 @@
1
1
  import { PLAYER_EVENTS } from './Player/PlayerEvents';
2
2
  import { ReproductionBuilder } from './ReproductionBuilder';
3
- const STATES = {
3
+ export const REPRODUCTION_STATES = {
4
4
  STOPPED: 0,
5
5
  COUNTING_IN: 1,
6
6
  PLAYING: 2,
@@ -62,29 +62,75 @@ export class Reproduction {
62
62
  return EVENTS;
63
63
  }
64
64
  static get STATES() {
65
- return STATES;
65
+ return REPRODUCTION_STATES;
66
66
  }
67
67
  static newBuilder() {
68
68
  return new ReproductionBuilder();
69
69
  }
70
70
  on(eventName, handler) {
71
+ if (typeof handler !== 'function') {
72
+ throw new Error('Handler must be a function');
73
+ }
71
74
  switch (eventName) {
72
75
  case Reproduction.EVENTS.START:
73
- return this[dispatchOnSongStartHandlers].push(handler);
76
+ this[dispatchOnSongStartHandlers].push(handler);
77
+ break;
74
78
  case Reproduction.EVENTS.COUNTING_IN:
75
- return this[dispatchOnCountingInHandlers].push(handler);
79
+ this[dispatchOnCountingInHandlers].push(handler);
80
+ break;
76
81
  case Reproduction.EVENTS.PLAY:
77
- return this[dispatchOnPlayHandlers].push(handler);
82
+ this[dispatchOnPlayHandlers].push(handler);
83
+ break;
78
84
  case Reproduction.EVENTS.PLAYING:
79
- return this[dispatchOnPlayingHandlers].push(handler);
85
+ this[dispatchOnPlayingHandlers].push(handler);
86
+ break;
80
87
  case Reproduction.EVENTS.PAUSED:
81
- return this[dispatchOnPausedHandlers].push(handler);
88
+ this[dispatchOnPausedHandlers].push(handler);
89
+ break;
82
90
  case Reproduction.EVENTS.FINISH:
83
- return this[dispatchOnFinishHandlers].push(handler);
91
+ this[dispatchOnFinishHandlers].push(handler);
92
+ break;
84
93
  case Reproduction.EVENTS.ERROR:
85
- return this[dispatchOnErrorHandlers].push(handler);
94
+ this[dispatchOnErrorHandlers].push(handler);
95
+ break;
86
96
  default:
97
+ throw new Error(`Unknown event: ${eventName}`);
98
+ }
99
+ return () => this.off(eventName, handler);
100
+ }
101
+ off(eventName, handler) {
102
+ if (typeof handler !== 'function') {
103
+ throw new Error('Handler must be a function');
104
+ }
105
+ let handlers;
106
+ switch (eventName) {
107
+ case Reproduction.EVENTS.START:
108
+ handlers = this[dispatchOnSongStartHandlers];
109
+ break;
110
+ case Reproduction.EVENTS.COUNTING_IN:
111
+ handlers = this[dispatchOnCountingInHandlers];
112
+ break;
113
+ case Reproduction.EVENTS.PLAY:
114
+ handlers = this[dispatchOnPlayHandlers];
115
+ break;
116
+ case Reproduction.EVENTS.PLAYING:
117
+ handlers = this[dispatchOnPlayingHandlers];
118
+ break;
119
+ case Reproduction.EVENTS.PAUSED:
120
+ handlers = this[dispatchOnPausedHandlers];
87
121
  break;
122
+ case Reproduction.EVENTS.FINISH:
123
+ handlers = this[dispatchOnFinishHandlers];
124
+ break;
125
+ case Reproduction.EVENTS.ERROR:
126
+ handlers = this[dispatchOnErrorHandlers];
127
+ break;
128
+ default:
129
+ throw new Error(`Unknown event: ${eventName}`);
130
+ }
131
+ const index = handlers.indexOf(handler);
132
+ if (index > -1) {
133
+ handlers.splice(index, 1);
88
134
  }
89
135
  }
90
136
  dispatch(eventName, args = {}) {
@@ -136,12 +182,13 @@ export class Reproduction {
136
182
  play() {
137
183
  clearInterval(this.interval);
138
184
  this.player.play();
139
- const intervalTimeout = 200;
185
+ const bpmInterval = this.getBPMInterval();
186
+ const tickInterval = Math.min(bpmInterval / 4, 50);
140
187
  this.interval = setInterval(() => {
141
188
  if (this.isPlaying()) {
142
189
  this.dispatch(Reproduction.EVENTS.PLAYING);
143
190
  }
144
- }, intervalTimeout);
191
+ }, tickInterval);
145
192
  }
146
193
  playLoop(from, to) {
147
194
  if (!Number.isFinite(from) || !Number.isFinite(to)) {
@@ -196,6 +243,9 @@ export class Reproduction {
196
243
  isCountingIn() {
197
244
  return this.state === Reproduction.STATES.COUNTING_IN;
198
245
  }
246
+ getState() {
247
+ return this.state;
248
+ }
199
249
  getPlayer() {
200
250
  return this.player;
201
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldelia/react-media",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "A React components collection for media-related features.",
5
5
  "private": false,
6
6
  "keywords": [