@ldelia/react-media 0.4.2 → 0.4.4

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.
Files changed (140) hide show
  1. package/dist/components/__tests__/timeline/timeline.test.d.ts +1 -0
  2. package/dist/components/reproduction-widget/ReproductionWidget.d.ts +17 -0
  3. package/dist/components/reproduction-widget/index.js +1 -0
  4. package/dist/components/reproduction-widget/inner-players/PlayAlongInnerPlayer.d.ts +8 -0
  5. package/dist/components/reproduction-widget/inner-players/YouTubeInnerPlayer.d.ts +10 -0
  6. package/dist/components/reproduction-widget/models/Player/PlayAlongPlayer.d.ts +30 -0
  7. package/dist/components/reproduction-widget/models/Player/PlayerEvents.d.ts +4 -0
  8. package/dist/components/reproduction-widget/models/Player/YouTubePlayer.d.ts +30 -0
  9. package/dist/components/reproduction-widget/models/Reproduction.d.ts +66 -0
  10. package/dist/components/reproduction-widget/models/ReproductionBuilder.d.ts +16 -0
  11. package/dist/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.d.ts +8 -0
  12. package/dist/components/timeline/Timeline.d.ts +12 -0
  13. package/dist/components/timeline/TimelineCanvas/TickTime.d.ts +7 -0
  14. package/dist/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.d.ts +6 -0
  15. package/dist/components/timeline/TimelineCanvas/TimelineCanvas.d.ts +7 -0
  16. package/dist/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.d.ts +2 -0
  17. package/dist/components/timeline/TimelineValue/TimelineValue.d.ts +7 -0
  18. package/dist/components/timeline/ZoomContext/ZoomContext.d.ts +7 -0
  19. package/dist/components/timeline/constants.d.ts +1 -0
  20. package/dist/components/timeline/utils/utils.d.ts +7 -0
  21. package/dist/setupTests.d.ts +1 -0
  22. package/dist/stories/reproduction-widget.stories.d.ts +4 -0
  23. package/dist/stories/timeline.stories.d.ts +9 -0
  24. package/package.json +45 -35
  25. package/.bitmap +0 -48
  26. package/.eslintcache +0 -1
  27. package/.prettierignore +0 -3
  28. package/.prettierrc +0 -7
  29. package/.storybook/main.js +0 -38
  30. package/.storybook/preview.js +0 -4
  31. package/config/env.js +0 -104
  32. package/config/getHttpsConfig.js +0 -66
  33. package/config/jest/babelTransform.js +0 -29
  34. package/config/jest/cssTransform.js +0 -14
  35. package/config/jest/fileTransform.js +0 -40
  36. package/config/modules.js +0 -134
  37. package/config/paths.js +0 -77
  38. package/config/webpack/persistentCache/createEnvironmentHash.js +0 -9
  39. package/config/webpack.config.js +0 -755
  40. package/config/webpackDevServer.config.js +0 -127
  41. package/scripts/build.js +0 -217
  42. package/scripts/start.js +0 -154
  43. package/scripts/test.js +0 -52
  44. package/src/components/__tests__/timeline/timeline.test.tsx +0 -26
  45. package/src/components/reproduction-widget/README.md +0 -27
  46. package/src/components/reproduction-widget/ReproductionWidget.tsx +0 -68
  47. package/src/components/reproduction-widget/index.tsx +0 -1
  48. package/src/components/reproduction-widget/inner-players/PlayAlongInnerPlayer.tsx +0 -10
  49. package/src/components/reproduction-widget/inner-players/YouTubeInnerPlayer.tsx +0 -36
  50. package/src/components/reproduction-widget/models/Player/PlayAlongPlayer.ts +0 -129
  51. package/src/components/reproduction-widget/models/Player/PlayerEvents.ts +0 -4
  52. package/src/components/reproduction-widget/models/Player/YouTubePlayer.ts +0 -171
  53. package/src/components/reproduction-widget/models/Reproduction.ts +0 -272
  54. package/src/components/reproduction-widget/models/ReproductionBuilder.ts +0 -70
  55. package/src/components/timeline/README.md +0 -111
  56. package/src/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.tsx +0 -166
  57. package/src/components/timeline/Timeline.tsx +0 -139
  58. package/src/components/timeline/TimelineCanvas/TickTime.tsx +0 -45
  59. package/src/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.tsx +0 -42
  60. package/src/components/timeline/TimelineCanvas/TimelineCanvas.tsx +0 -79
  61. package/src/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.ts +0 -43
  62. package/src/components/timeline/TimelineValue/TimelineValue.tsx +0 -87
  63. package/src/components/timeline/ZoomContext/ZoomContext.ts +0 -10
  64. package/src/components/timeline/constants.ts +0 -13
  65. package/src/components/timeline/utils/utils.ts +0 -49
  66. package/src/modules.d.ts +0 -1
  67. package/src/react-app-env.d.ts +0 -71
  68. package/src/react-docgen-types.d.ts +0 -37
  69. package/src/setupTests.ts +0 -5
  70. package/src/stories/Configure.mdx +0 -364
  71. package/src/stories/reproduction-widget.stories.tsx +0 -23
  72. package/src/stories/timeline.stories.custom.css +0 -29
  73. package/src/stories/timeline.stories.tsx +0 -66
  74. package/storybook-static/favicon.svg +0 -1
  75. package/storybook-static/index.html +0 -173
  76. package/storybook-static/index.json +0 -1
  77. package/storybook-static/nunito-sans-bold-italic.woff2 +0 -0
  78. package/storybook-static/nunito-sans-bold.woff2 +0 -0
  79. package/storybook-static/nunito-sans-italic.woff2 +0 -0
  80. package/storybook-static/nunito-sans-regular.woff2 +0 -0
  81. package/storybook-static/project.json +0 -1
  82. package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js +0 -3
  83. package/storybook-static/sb-addons/essentials-actions-3/manager-bundle.js.LEGAL.txt +0 -0
  84. package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js +0 -12
  85. package/storybook-static/sb-addons/essentials-backgrounds-4/manager-bundle.js.LEGAL.txt +0 -0
  86. package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js +0 -412
  87. package/storybook-static/sb-addons/essentials-controls-2/manager-bundle.js.LEGAL.txt +0 -0
  88. package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js +0 -3
  89. package/storybook-static/sb-addons/essentials-measure-7/manager-bundle.js.LEGAL.txt +0 -0
  90. package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js +0 -3
  91. package/storybook-static/sb-addons/essentials-outline-8/manager-bundle.js.LEGAL.txt +0 -0
  92. package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js +0 -3
  93. package/storybook-static/sb-addons/essentials-toolbars-6/manager-bundle.js.LEGAL.txt +0 -0
  94. package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js +0 -3
  95. package/storybook-static/sb-addons/essentials-viewport-5/manager-bundle.js.LEGAL.txt +0 -0
  96. package/storybook-static/sb-addons/links-1/manager-bundle.js +0 -3
  97. package/storybook-static/sb-addons/links-1/manager-bundle.js.LEGAL.txt +0 -0
  98. package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js +0 -3
  99. package/storybook-static/sb-addons/storybook-core-core-server-presets-0/common-manager-bundle.js.LEGAL.txt +0 -0
  100. package/storybook-static/sb-common-assets/favicon.svg +0 -1
  101. package/storybook-static/sb-common-assets/nunito-sans-bold-italic.woff2 +0 -0
  102. package/storybook-static/sb-common-assets/nunito-sans-bold.woff2 +0 -0
  103. package/storybook-static/sb-common-assets/nunito-sans-italic.woff2 +0 -0
  104. package/storybook-static/sb-common-assets/nunito-sans-regular.woff2 +0 -0
  105. package/storybook-static/sb-manager/globals-module-info.js +0 -995
  106. package/storybook-static/sb-manager/globals-runtime.js +0 -53527
  107. package/storybook-static/sb-manager/globals.js +0 -48
  108. package/storybook-static/sb-manager/runtime.js +0 -11885
  109. package/storybook-static/sb-preview/globals.js +0 -33
  110. package/storybook-static/sb-preview/runtime.js +0 -9483
  111. package/tsconfig.json +0 -14
  112. /package/{src → dist}/components/__tests__/timeline/timeline.test.js +0 -0
  113. /package/{src/components/index.js → dist/components/index.d.ts} +0 -0
  114. /package/{src/components/index.ts → dist/components/index.js} +0 -0
  115. /package/{src → dist}/components/reproduction-widget/ReproductionWidget.js +0 -0
  116. /package/{src/components/reproduction-widget/index.js → dist/components/reproduction-widget/index.d.ts} +0 -0
  117. /package/{src → dist}/components/reproduction-widget/inner-players/PlayAlongInnerPlayer.js +0 -0
  118. /package/{src → dist}/components/reproduction-widget/inner-players/YouTubeInnerPlayer.js +0 -0
  119. /package/{src → dist}/components/reproduction-widget/models/Player/PlayAlongPlayer.js +0 -0
  120. /package/{src → dist}/components/reproduction-widget/models/Player/PlayerEvents.js +0 -0
  121. /package/{src → dist}/components/reproduction-widget/models/Player/YouTubePlayer.js +0 -0
  122. /package/{src → dist}/components/reproduction-widget/models/Reproduction.js +0 -0
  123. /package/{src → dist}/components/reproduction-widget/models/ReproductionBuilder.js +0 -0
  124. /package/{src → dist}/components/timeline/RangeSelectorCanvas/RangeSelectorCanvas.js +0 -0
  125. /package/{src → dist}/components/timeline/Timeline.js +0 -0
  126. /package/{src → dist}/components/timeline/TimelineCanvas/TickTime.js +0 -0
  127. /package/{src → dist}/components/timeline/TimelineCanvas/TickTimeCollectionDisplay.js +0 -0
  128. /package/{src → dist}/components/timeline/TimelineCanvas/TimelineCanvas.js +0 -0
  129. /package/{src → dist}/components/timeline/TimelineCanvas/drawTimeBlocksOnCanvas.js +0 -0
  130. /package/{src → dist}/components/timeline/TimelineValue/TimelineValue.js +0 -0
  131. /package/{src → dist}/components/timeline/ZoomContext/ZoomContext.js +0 -0
  132. /package/{src → dist}/components/timeline/constants.js +0 -0
  133. /package/{src/components/timeline/index.js → dist/components/timeline/index.d.ts} +0 -0
  134. /package/{src/components/timeline/index.tsx → dist/components/timeline/index.js} +0 -0
  135. /package/{src → dist}/components/timeline/utils/utils.js +0 -0
  136. /package/{src/index.js → dist/index.d.ts} +0 -0
  137. /package/{src/index.ts → dist/index.js} +0 -0
  138. /package/{src → dist}/setupTests.js +0 -0
  139. /package/{src → dist}/stories/reproduction-widget.stories.js +0 -0
  140. /package/{src → dist}/stories/timeline.stories.js +0 -0
@@ -1,36 +0,0 @@
1
- import React, { useEffect, useState } from 'react';
2
- import YouTube, { YouTubePlayer as InnerYouTubePlayer } from 'react-youtube';
3
-
4
- interface Props {
5
- videoId: string;
6
- onReady: (event: { target: InnerYouTubePlayer }) => void;
7
- }
8
- export const YouTubeInnerPlayer = ({ videoId, onReady }: Props) => {
9
- const [origin, setOrigin] = useState('');
10
-
11
- useEffect(() => {
12
- if (typeof window !== 'undefined') {
13
- setOrigin(window.location.href);
14
- }
15
- }, []);
16
-
17
- const opts = {
18
- playerVars: {
19
- // https://developers.google.com/youtube/player_parameters
20
- autoplay: 0,
21
- controls: 0,
22
- showinfo: 0,
23
- enablejsapi: 1,
24
- origin: origin,
25
- },
26
- };
27
- return (
28
- <YouTube
29
- id={'YT-' + videoId}
30
- className='youtube-player'
31
- videoId={videoId}
32
- opts={opts}
33
- onReady={onReady}
34
- />
35
- );
36
- };
@@ -1,129 +0,0 @@
1
- import { PLAYER_EVENTS } from './PlayerEvents';
2
-
3
- const dispatchOnReadyHandlers = Symbol();
4
- const dispatchOnFinishHandlers = Symbol();
5
-
6
- export class PlayAlongPlayer {
7
- private readonly duration: number;
8
- private currentTime: number;
9
- private isRunning: boolean;
10
- private currentPlaybackRate: number;
11
- private innerPlayer: string | null;
12
- private interval: ReturnType<typeof setInterval> | null = null;
13
- private [dispatchOnReadyHandlers]: (() => void)[];
14
- private [dispatchOnFinishHandlers]: (() => void)[];
15
-
16
- static get EVENTS() {
17
- return PLAYER_EVENTS;
18
- }
19
-
20
- constructor(duration: number, innerPlayer: string) {
21
- this.interval = null;
22
- this.currentTime = 0;
23
- this.isRunning = false;
24
- this.duration = duration;
25
- this.currentPlaybackRate = 1;
26
- this.innerPlayer = null;
27
-
28
- this[dispatchOnFinishHandlers] = [];
29
- this[dispatchOnReadyHandlers] = [];
30
-
31
- this.setInnerPlayer(innerPlayer);
32
- }
33
-
34
- private setInnerPlayer(innerPlayer: string) {
35
- this.innerPlayer = innerPlayer;
36
- this.dispatch(PlayAlongPlayer.EVENTS.READY);
37
- }
38
-
39
- play() {
40
- this.isRunning = true;
41
-
42
- const intervalTimeout = 1000;
43
-
44
- this.interval = setInterval(() => {
45
- if (this.isRunning) {
46
- this.currentTime++;
47
- if (this.currentTime >= this.getDuration()) {
48
- this.stop();
49
- this.dispatch(PlayAlongPlayer.EVENTS.FINISH);
50
- }
51
- }
52
- }, intervalTimeout);
53
- }
54
-
55
- pause() {
56
- this.isRunning = false;
57
- }
58
-
59
- stop() {
60
- this.currentTime = 0;
61
- this.isRunning = false;
62
-
63
- if (this.interval !== null) {
64
- clearInterval(this.interval as NodeJS.Timeout);
65
- this.interval = null;
66
- }
67
- }
68
-
69
- seekTo(seconds: number) {
70
- this.currentTime = seconds;
71
- }
72
-
73
- getCurrentTime() {
74
- return this.currentTime;
75
- }
76
-
77
- getDuration() {
78
- return this.duration;
79
- }
80
-
81
- isAvailable() {
82
- return true;
83
- }
84
-
85
- getAvailablePlaybackRates() {
86
- return [1];
87
- }
88
-
89
- setPlaybackRate(playbackRate: number) {
90
- if (!this.getAvailablePlaybackRates().includes(playbackRate)) {
91
- throw new Error(
92
- `The PlayAlongPlayer doesn't support a playbackRate with value ${playbackRate}`
93
- );
94
- }
95
- this.currentPlaybackRate = playbackRate;
96
- }
97
-
98
- on(eventName: keyof typeof PlayAlongPlayer.EVENTS, handler: () => void) {
99
- switch (eventName) {
100
- case PlayAlongPlayer.EVENTS.READY:
101
- return this[dispatchOnReadyHandlers].push(handler);
102
- case PlayAlongPlayer.EVENTS.FINISH:
103
- return this[dispatchOnFinishHandlers].push(handler);
104
- default:
105
- break;
106
- }
107
- }
108
-
109
- dispatch(eventName: keyof typeof PlayAlongPlayer.EVENTS) {
110
- let handler, i, len;
111
- let ref: (() => void)[] = [];
112
-
113
- switch (eventName) {
114
- case PlayAlongPlayer.EVENTS.READY:
115
- ref = this[dispatchOnReadyHandlers];
116
- break;
117
- case PlayAlongPlayer.EVENTS.FINISH:
118
- ref = this[dispatchOnFinishHandlers];
119
- break;
120
- default:
121
- break;
122
- }
123
-
124
- for (i = 0, len = ref.length; i < len; i++) {
125
- handler = ref[i];
126
- handler();
127
- }
128
- }
129
- }
@@ -1,4 +0,0 @@
1
- export const PLAYER_EVENTS = {
2
- READY: 'READY',
3
- FINISH: 'FINISH',
4
- } as const;
@@ -1,171 +0,0 @@
1
- import { YouTubePlayer as InnerYouTubePlayer } from 'react-youtube';
2
- import { PlayAlongPlayer } from './PlayAlongPlayer';
3
- import { PLAYER_EVENTS } from './PlayerEvents';
4
-
5
- // https://developers.google.com/youtube/iframe_api_reference
6
- enum INNER_YOUTUBE_PLAYER_EVENTS {
7
- VIDEO_UNSTARTED = -1,
8
- VIDEO_ENDED = 0,
9
- VIDEO_PLAYING = 1,
10
- VIDEO_PAUSED = 2,
11
- VIDEO_BUFFERING = 3,
12
- VIDEO_CUED = 5,
13
- }
14
-
15
- interface INNER_YOUTUBE_PLAYER_StateChangeEvent {
16
- data: INNER_YOUTUBE_PLAYER_EVENTS;
17
- }
18
-
19
- const dispatchOnReadyHandlers = Symbol();
20
- const dispatchOnFinishHandlers = Symbol();
21
-
22
- export class YouTubePlayer {
23
- private currentTime: number;
24
- private isRunning: boolean;
25
- private innerPlayer: InnerYouTubePlayer | number;
26
- private [dispatchOnReadyHandlers]: (() => void)[];
27
- private [dispatchOnFinishHandlers]: (() => void)[];
28
-
29
- static get EVENTS() {
30
- return PLAYER_EVENTS;
31
- }
32
-
33
- constructor(innerPlayer: InnerYouTubePlayer) {
34
- this[dispatchOnFinishHandlers] = [];
35
- this[dispatchOnReadyHandlers] = [];
36
-
37
- this.currentTime = 0;
38
- this.isRunning = false;
39
-
40
- this.setInnerPlayer(innerPlayer);
41
- }
42
-
43
- private setInnerPlayer(innerPlayer: InnerYouTubePlayer) {
44
- this.innerPlayer = innerPlayer;
45
- this.dispatch(YouTubePlayer.EVENTS.READY);
46
-
47
- // this is necessary for avoiding the state video cued.
48
- // When a video is in this state, when user seek to X, the song is played
49
- this.innerPlayer.playVideo();
50
- this.innerPlayer.pauseVideo();
51
-
52
- this.innerPlayer.addEventListener(
53
- 'onStateChange',
54
- (videoState: INNER_YOUTUBE_PLAYER_StateChangeEvent) => {
55
- switch (videoState.data) {
56
- case INNER_YOUTUBE_PLAYER_EVENTS.VIDEO_ENDED:
57
- this.dispatch(YouTubePlayer.EVENTS.FINISH);
58
- this.isRunning = false;
59
- this.currentTime = 0;
60
- break;
61
- default:
62
- break;
63
- }
64
- }
65
- );
66
- }
67
-
68
- getInnerPlayer() {
69
- return this.innerPlayer;
70
- }
71
-
72
- play() {
73
- const videoPlayer = this.getInnerPlayer();
74
- videoPlayer.playVideo();
75
-
76
- this.isRunning = true;
77
- }
78
-
79
- pause() {
80
- this.isRunning = false;
81
-
82
- this.getInnerPlayer().pauseVideo();
83
-
84
- this.currentTime = this.getInnerPlayer().getCurrentTime();
85
- }
86
-
87
- stop() {
88
- this.isRunning = false;
89
-
90
- /**
91
- * Hay un issue al llamar a getDuration del video luego de reanudar una canción pausada (devuelve siempre 0)
92
- * Para evitar que se pierda la información y tener que cargarla de nuevo, se simula un stop pausando y llevando al comienzo
93
- * videoPlayer.stopVideo();
94
- */
95
- this.getInnerPlayer().pauseVideo();
96
-
97
- this.seekTo(0);
98
- }
99
-
100
- seekTo(seconds: number) {
101
- const videoPlayer = this.getInnerPlayer();
102
- this.currentTime = seconds;
103
-
104
- videoPlayer.seekTo(this.currentTime);
105
-
106
- if (this.isRunning) {
107
- this.play();
108
- }
109
- }
110
-
111
- getCurrentTime() {
112
- return this.isRunning
113
- ? this.getInnerPlayer().getCurrentTime()
114
- : this.currentTime;
115
- }
116
-
117
- getDuration() {
118
- if (this.isAvailable()) {
119
- return this.getInnerPlayer().getDuration();
120
- }
121
- }
122
-
123
- getAvailablePlaybackRates() {
124
- return this.getInnerPlayer().getAvailablePlaybackRates();
125
- }
126
-
127
- setPlaybackRate(playbackRate: number) {
128
- if (!this.getAvailablePlaybackRates().includes(playbackRate)) {
129
- throw new Error(
130
- `The PlayAlongPlayer doesn't support a playbackRate with value ${playbackRate}`
131
- );
132
- }
133
- this.getInnerPlayer().setPlaybackRate(playbackRate);
134
- }
135
-
136
- isAvailable() {
137
- return this.getInnerPlayer() !== null;
138
- }
139
-
140
- on(eventName: keyof typeof PlayAlongPlayer.EVENTS, handler: () => void) {
141
- switch (eventName) {
142
- case PlayAlongPlayer.EVENTS.READY:
143
- return this[dispatchOnReadyHandlers].push(handler);
144
- case PlayAlongPlayer.EVENTS.FINISH:
145
- return this[dispatchOnFinishHandlers].push(handler);
146
- default:
147
- break;
148
- }
149
- }
150
-
151
- dispatch(eventName: keyof typeof PlayAlongPlayer.EVENTS) {
152
- let handler, i, len;
153
- let ref: (() => void)[] = [];
154
-
155
- switch (eventName) {
156
- case YouTubePlayer.EVENTS.READY:
157
- ref = this[dispatchOnReadyHandlers];
158
- break;
159
- case YouTubePlayer.EVENTS.FINISH:
160
- ref = this[dispatchOnFinishHandlers];
161
- break;
162
- default:
163
- break;
164
- }
165
-
166
- for (i = 0, len = ref.length; i < len; i++) {
167
- handler = ref[i];
168
- setTimeout(handler, 0);
169
- }
170
- }
171
- }
@@ -1,272 +0,0 @@
1
- import { PLAYER_EVENTS } from './Player/PlayerEvents';
2
- import { ReproductionBuilder } from './ReproductionBuilder';
3
- import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
4
- import { YouTubePlayer } from './Player/YouTubePlayer';
5
-
6
- type Player = PlayAlongPlayer | YouTubePlayer;
7
-
8
- const STATES = {
9
- STOPPED: 0,
10
- COUNTING_IN: 1,
11
- PLAYING: 2,
12
- PAUSED: 3,
13
- };
14
-
15
- const EVENTS = {
16
- READY: 'READY',
17
- START: 'START',
18
- COUNTING_IN: 'COUNTING_IN',
19
- PLAY: 'PLAY',
20
- PLAYING: 'PLAYING',
21
- PAUSED: 'PAUSED',
22
- FINISH: 'FINISH',
23
- } as const;
24
-
25
- const dispatchOnReadyHandlers = Symbol();
26
- const dispatchOnSongStartHandlers = Symbol();
27
- const dispatchOnCountingInHandlers = Symbol();
28
- const dispatchOnPlayHandlers = Symbol();
29
- const dispatchOnPlayingHandlers = Symbol();
30
- const dispatchOnPausedHandlers = Symbol();
31
- const dispatchOnFinishHandlers = Symbol();
32
-
33
- export class Reproduction {
34
- private player: Player;
35
- private requiresCountingIn: boolean;
36
-
37
- private songTempo: number;
38
- private state: number;
39
- private ready: boolean;
40
- private interval: ReturnType<typeof setInterval> | null;
41
- private countingInCounter: number;
42
- private [dispatchOnReadyHandlers]: (() => void)[];
43
- private [dispatchOnSongStartHandlers]: (() => void)[];
44
- private [dispatchOnCountingInHandlers]: (() => void)[];
45
- private [dispatchOnPlayHandlers]: (() => void)[];
46
- private [dispatchOnPlayingHandlers]: (() => void)[];
47
- private [dispatchOnPausedHandlers]: (() => void)[];
48
- private [dispatchOnFinishHandlers]: (() => void)[];
49
-
50
- static get EVENTS() {
51
- return EVENTS;
52
- }
53
-
54
- static get STATES() {
55
- return STATES;
56
- }
57
-
58
- constructor(player: Player, requiresCountingIn: boolean, songTempo: number) {
59
- this[dispatchOnReadyHandlers] = [];
60
- this[dispatchOnSongStartHandlers] = [];
61
- this[dispatchOnCountingInHandlers] = [];
62
- this[dispatchOnPlayHandlers] = [];
63
- this[dispatchOnPlayingHandlers] = [];
64
- this[dispatchOnPausedHandlers] = [];
65
- this[dispatchOnFinishHandlers] = [];
66
-
67
- this.songTempo = songTempo;
68
- this.player = player;
69
- this.ready = false;
70
-
71
- this.state = Reproduction.STATES.STOPPED;
72
- this.interval = null;
73
-
74
- this.requiresCountingIn = requiresCountingIn;
75
- this.countingInCounter = 0;
76
-
77
- this.player.on(PLAYER_EVENTS.READY, () => {
78
- this.ready = true;
79
- this.dispatch(Reproduction.EVENTS.READY);
80
- });
81
-
82
- this.player.on(PLAYER_EVENTS.FINISH, () => {
83
- this.state = Reproduction.STATES.STOPPED;
84
- clearInterval(this.interval as NodeJS.Timeout);
85
- this.dispatch(Reproduction.EVENTS.FINISH);
86
- });
87
- }
88
-
89
- on(eventName: keyof typeof Reproduction.EVENTS, handler: () => void) {
90
- switch (eventName) {
91
- case Reproduction.EVENTS.READY:
92
- return this[dispatchOnReadyHandlers].push(handler);
93
- case Reproduction.EVENTS.START:
94
- return this[dispatchOnSongStartHandlers].push(handler);
95
- case Reproduction.EVENTS.COUNTING_IN:
96
- return this[dispatchOnCountingInHandlers].push(handler);
97
- case Reproduction.EVENTS.PLAY:
98
- return this[dispatchOnPlayHandlers].push(handler);
99
- case Reproduction.EVENTS.PLAYING:
100
- return this[dispatchOnPlayingHandlers].push(handler);
101
- case Reproduction.EVENTS.PAUSED:
102
- return this[dispatchOnPausedHandlers].push(handler);
103
- case Reproduction.EVENTS.FINISH:
104
- return this[dispatchOnFinishHandlers].push(handler);
105
- default:
106
- break;
107
- }
108
- }
109
-
110
- dispatch(eventName: keyof typeof Reproduction.EVENTS) {
111
- let handler, i, len;
112
-
113
- let ref: (() => void)[] = [];
114
-
115
- switch (eventName) {
116
- case Reproduction.EVENTS.READY:
117
- ref = this[dispatchOnReadyHandlers];
118
- break;
119
- case Reproduction.EVENTS.START:
120
- ref = this[dispatchOnSongStartHandlers];
121
- break;
122
- case Reproduction.EVENTS.COUNTING_IN:
123
- ref = this[dispatchOnCountingInHandlers];
124
- break;
125
- case Reproduction.EVENTS.PLAY:
126
- ref = this[dispatchOnPlayHandlers];
127
- break;
128
- case Reproduction.EVENTS.PLAYING:
129
- ref = this[dispatchOnPlayingHandlers];
130
- break;
131
- case Reproduction.EVENTS.PAUSED:
132
- ref = this[dispatchOnPausedHandlers];
133
- break;
134
- case Reproduction.EVENTS.FINISH:
135
- ref = this[dispatchOnFinishHandlers];
136
- break;
137
- default:
138
- break;
139
- }
140
-
141
- for (i = 0, len = ref.length; i < len; i++) {
142
- handler = ref[i];
143
- handler();
144
- // setTimeout(handler, 0);
145
- }
146
- }
147
-
148
- private countIn(timeout: number, limit: number) {
149
- // the initial count starts instantly, no waiting
150
- this.countingInCounter++;
151
- this.dispatch(Reproduction.EVENTS.COUNTING_IN);
152
-
153
- const interval = setInterval(() => {
154
- this.countingInCounter++;
155
- if (this.countingInCounter === limit) {
156
- clearInterval(interval);
157
- this.countingInCounter = 0;
158
- if (limit !== 5) {
159
- this.countIn(this.getBPMInterval(), 5);
160
- } else {
161
- this.play();
162
- }
163
- } else {
164
- this.dispatch(Reproduction.EVENTS.COUNTING_IN);
165
- }
166
- }, timeout);
167
- }
168
-
169
- start() {
170
- if (this.state === Reproduction.STATES.STOPPED) {
171
- this.dispatch(Reproduction.EVENTS.START);
172
- }
173
-
174
- if (this.requiresCountingIn && this.getCurrentTime() === 0) {
175
- this.state = Reproduction.STATES.COUNTING_IN;
176
- this.countIn(this.getBPMInterval() * 2, 3);
177
- } else {
178
- this.play();
179
- }
180
- }
181
-
182
- play() {
183
- this.state = Reproduction.STATES.PLAYING;
184
- this.dispatch(Reproduction.EVENTS.PLAY);
185
- this.player.play();
186
-
187
- const intervalTimeout = 200;
188
-
189
- this.interval = setInterval(() => {
190
- if (this.isPlaying()) {
191
- this.dispatch(Reproduction.EVENTS.PLAYING);
192
- }
193
- }, intervalTimeout);
194
- }
195
-
196
- pause() {
197
- this.state = Reproduction.STATES.PAUSED;
198
- this.player.pause();
199
- clearInterval(this.interval as NodeJS.Timeout);
200
-
201
- this.dispatch(Reproduction.EVENTS.PAUSED);
202
- }
203
-
204
- stop() {
205
- this.state = Reproduction.STATES.STOPPED;
206
- this.player.stop();
207
- clearInterval(this.interval as NodeJS.Timeout);
208
- this.dispatch(Reproduction.EVENTS.FINISH);
209
- }
210
-
211
- isReady() {
212
- // It's necessary to avoid play the reproduction-widget when the player is not ready
213
- return this.ready;
214
- }
215
-
216
- isPlaying() {
217
- return this.state === Reproduction.STATES.PLAYING;
218
- }
219
-
220
- isStopped() {
221
- return this.state === Reproduction.STATES.STOPPED;
222
- }
223
-
224
- isPaused() {
225
- return this.state === Reproduction.STATES.PAUSED;
226
- }
227
-
228
- isCountingIn() {
229
- return this.state === Reproduction.STATES.COUNTING_IN;
230
- }
231
-
232
- getPlayer() {
233
- return this.player;
234
- }
235
-
236
- getTempo() {
237
- return this.songTempo;
238
- }
239
-
240
- getCurrentTime() {
241
- // in seconds with milliseconds.
242
- return this.player.getCurrentTime();
243
- }
244
-
245
- getDuration() {
246
- return this.player.getDuration();
247
- }
248
-
249
- seekTo(seconds: number) {
250
- this.player.seekTo(seconds);
251
- }
252
-
253
- getAvailablePlaybackRates() {
254
- return this.player.getAvailablePlaybackRates();
255
- }
256
-
257
- setPlaybackRate(playbackRate: number) {
258
- this.player.setPlaybackRate(playbackRate);
259
- }
260
-
261
- isAvailable() {
262
- return this.player.isAvailable();
263
- }
264
-
265
- getBPMInterval() {
266
- return 60000 / this.getTempo();
267
- }
268
-
269
- static newBuilder() {
270
- return new ReproductionBuilder();
271
- }
272
- }
@@ -1,70 +0,0 @@
1
- import { YouTubePlayer } from './Player/YouTubePlayer';
2
- import { PlayAlongPlayer } from './Player/PlayAlongPlayer';
3
- import { Reproduction } from './Reproduction';
4
- import { YouTubePlayer as InnerYouTubePlayer } from 'react-youtube';
5
-
6
- export class ReproductionBuilder {
7
- private trainingMode: boolean;
8
- private requiresCountingIn: boolean;
9
- private songDuration: number | null;
10
- private songTempo: number | null;
11
- private innerPlayer: InnerYouTubePlayer | string;
12
-
13
- constructor() {
14
- this.trainingMode = false;
15
- this.requiresCountingIn = false;
16
- this.songDuration = null;
17
- this.songTempo = null;
18
- this.innerPlayer = null;
19
- }
20
-
21
- withSongDuration(songDuration: number) {
22
- this.songDuration = songDuration;
23
- return this;
24
- }
25
-
26
- withSongTempo(songTempo: number) {
27
- this.songTempo = songTempo;
28
- return this;
29
- }
30
-
31
- withTrainingMode(trainingMode: boolean) {
32
- this.trainingMode = trainingMode;
33
- return this;
34
- }
35
-
36
- withCountingIn(requiresCountingIn: boolean) {
37
- this.requiresCountingIn = requiresCountingIn;
38
- return this;
39
- }
40
-
41
- withInnerPlayer(innerPlayer: InnerYouTubePlayer | string) {
42
- this.innerPlayer = innerPlayer;
43
- return this;
44
- }
45
-
46
- createReproduction() {
47
- if (this.requiresCountingIn && this.songTempo === null) {
48
- throw new Error('The song tempo is mandatory');
49
- }
50
-
51
- if (this.innerPlayer === null) {
52
- throw new Error('The inner player was not provided.');
53
- }
54
-
55
- let player;
56
- if (this.trainingMode) {
57
- player = new YouTubePlayer(this.innerPlayer);
58
- } else {
59
- if (this.songDuration === null) {
60
- throw new Error('The song duration is mandatory');
61
- }
62
- player = new PlayAlongPlayer(this.songDuration, this.innerPlayer);
63
- }
64
- return new Reproduction(
65
- player,
66
- this.requiresCountingIn,
67
- this.songTempo || 0
68
- );
69
- }
70
- }