Skip to content

Commit 4a34b6a

Browse files
committed
Adding in volume mapping and reset video when the playback has ended
1 parent 910c696 commit 4a34b6a

File tree

2 files changed

+105
-50
lines changed

2 files changed

+105
-50
lines changed

app/store/mapStoreToPlayer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
import { hasStopped } from '../actions/hasStopped';
4+
35
function setupPlayer(player) {
46
return {
57
play: (isPlaying) => {
@@ -11,10 +13,17 @@ function setupPlayer(player) {
1113
player.pause();
1214
player.currentTime = 0;
1315
}
16+
},
17+
volume: (volume) => {
18+
player.volume = volume / 100;
1419
}
1520
};
1621
}
1722

23+
function setupDispatch(store, player) {
24+
player.addEventListener('ended', () => store.dispatch(hasStopped(true)));
25+
}
26+
1827
function isPlayingChanged(previousState, currentState) {
1928
return (previousState.playback.isPlaying !== currentState.playback.isPlaying);
2029
}
@@ -23,15 +32,22 @@ function hasStoppedChanged(previousState, currentState) {
2332
return (previousState.playback.hasStopped !== currentState.playback.hasStopped);
2433
}
2534

35+
function volumeChanged(previousState, currentState) {
36+
return (previousState.playback.volume !== currentState.playback.volume);
37+
}
38+
2639
export default (store, video) => {
2740
const player = setupPlayer(video);
2841
let currentState = store.getState();
2942

43+
setupDispatch(store, video);
44+
3045
return store.subscribe(() => {
3146
const previousState = currentState;
3247
currentState = store.getState();
3348

3449
if (isPlayingChanged(previousState, currentState)) player.play(currentState.playback.isPlaying);
3550
if (hasStoppedChanged(previousState, currentState)) player.stop(currentState.playback.hasStopped);
51+
if (volumeChanged(previousState, currentState)) player.volume(currentState.playback.volume);
3652
});
3753
};

test/store/mapStoreToPlayer.js

Lines changed: 89 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ function setupPlayer() {
1717
play: playSpy,
1818
pause: pauseSpy,
1919
playSpy: playSpy,
20-
pauseSpy: pauseSpy
20+
pauseSpy: pauseSpy,
21+
addEventListener: function addEventListener(name, cb) {
22+
this[name] = cb;
23+
}
2124
};
2225
}
2326

@@ -30,64 +33,100 @@ describe('mapStoreToPlayer', () => {
3033
sandbox.restore();
3134
});
3235

33-
it('triggers the pause method when the dispatching isPlaying: false', () => {
34-
const player = setupPlayer();
35-
store.getState.onCall(0).returns({
36-
playback: {
37-
isPlaying: true
38-
}
36+
describe('store dispatches', () => {
37+
it('triggers the pause method when the dispatching IS_PLAYING: false', () => {
38+
const player = setupPlayer();
39+
store.getState.onCall(0).returns({
40+
playback: {
41+
isPlaying: true
42+
}
43+
});
44+
store.getState.onCall(1).returns({
45+
playback: {
46+
isPlaying: false
47+
}
48+
});
49+
const unsubscribe = mapStoreToPlayer(store, player);
50+
51+
store.dispatch({type: 'FAKE_DISPATCH'});
52+
assert(player.pauseSpy.calledOnce);
53+
54+
unsubscribe();
3955
});
40-
store.getState.onCall(1).returns({
41-
playback: {
42-
isPlaying: false
43-
}
56+
57+
it('triggers the play method when the dispatching IS_PLAYING: true', () => {
58+
const player = setupPlayer();
59+
store.getState.onCall(0).returns({
60+
playback: {
61+
isPlaying: false
62+
}
63+
});
64+
store.getState.onCall(1).returns({
65+
playback: {
66+
isPlaying: true
67+
}
68+
});
69+
const unsubscribe = mapStoreToPlayer(store, player);
70+
71+
store.dispatch({type: 'FAKE_DISPATCH'});
72+
assert(player.playSpy.calledOnce);
73+
74+
unsubscribe();
4475
});
45-
const unsubscribe = mapStoreToPlayer(store, player);
4676

47-
store.dispatch({type: 'FAKE_DISPATCH'});
48-
assert(player.pauseSpy.calledOnce);
77+
it('triggers the pause method and resets the player when dispatching HAS_STOPPED: true', () => {
78+
const player = setupPlayer();
79+
store.getState.onCall(0).returns({
80+
playback: {
81+
hasStopped: false
82+
}
83+
});
84+
store.getState.onCall(1).returns({
85+
playback: {
86+
hasStopped: true
87+
}
88+
});
89+
const unsubscribe = mapStoreToPlayer(store, player);
90+
91+
store.dispatch({type: 'FAKE_DISPATCH'});
92+
assert(player.pauseSpy.calledOnce);
93+
assert.equal(player.currentTime, 0);
94+
95+
unsubscribe();
96+
});
4997

50-
unsubscribe();
98+
it('updates the volume of the player when dispatching SET_VOLUME', () => {
99+
const player = setupPlayer();
100+
store.getState.onCall(0).returns({
101+
playback: {
102+
volume: 50
103+
}
104+
});
105+
store.getState.onCall(1).returns({
106+
playback: {
107+
volume: 75
108+
}
109+
});
110+
const unsubscribe = mapStoreToPlayer(store, player);
111+
112+
store.dispatch({type: 'FAKE_DISPATCH'});
113+
assert.equal(player.volume, 0.75);
114+
115+
unsubscribe();
116+
});
51117
});
52118

53-
it('triggers the play method when the dispatching isPlaying: true', () => {
54-
const player = setupPlayer();
55-
store.getState.onCall(0).returns({
56-
playback: {
57-
isPlaying: false
58-
}
59-
});
60-
store.getState.onCall(1).returns({
61-
playback: {
62-
isPlaying: true
63-
}
64-
});
65-
const unsubscribe = mapStoreToPlayer(store, player);
119+
describe('player events', () => {
120+
beforeEach(() => sinon.stub(store, 'dispatch'));
66121

67-
store.dispatch({type: 'FAKE_DISPATCH'});
68-
assert(player.playSpy.calledOnce);
122+
it('dispatches HAS_STOPPED: true when the player ends', () => {
123+
const player = setupPlayer();
124+
const unsubscribe = mapStoreToPlayer(store, player);
69125

70-
unsubscribe();
71-
});
126+
player.ended();
127+
assert(store.dispatch.calledOnce);
72128

73-
it('triggers the pause method and resets the player when dispatching hasStopped: true', () => {
74-
const player = setupPlayer();
75-
store.getState.onCall(0).returns({
76-
playback: {
77-
hasStopped: false
78-
}
79-
});
80-
store.getState.onCall(1).returns({
81-
playback: {
82-
hasStopped: true
83-
}
129+
unsubscribe();
84130
});
85-
const unsubscribe = mapStoreToPlayer(store, player);
86-
87-
store.dispatch({type: 'FAKE_DISPATCH'});
88-
assert(player.pauseSpy.calledOnce);
89-
assert.equal(player.currentTime, 0);
90-
91-
unsubscribe();
92131
});
93132
});

0 commit comments

Comments
 (0)