Skip to content

Commit

Permalink
Allows middleware to handle muted setter/getter (#6177)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjanblaszczyk authored and gkatsev committed Aug 29, 2019
1 parent 8129f03 commit f324d1f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/guides/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Middleware is a Video.js feature that allows interaction with and modification o

## Understanding Middleware

Middleware are functions that return an object, a class instance, a prototype, etc, scoped to the Player with methods matching those on the `Tech`. There are currently a limited set of allowed methods that will be understood by middleware. These are: `buffered`, `currentTime`, `setCurrentTime`, `duration`, `seekable`, `played`, `play`, `pause` and `paused`. These allowed methods are split into three categories: [getters](#middleware-getters), [setters](#middleware-setters), and [mediators](#middleware-mediators).
Middleware are functions that return an object, a class instance, a prototype, etc, scoped to the Player with methods matching those on the `Tech`. There are currently a limited set of allowed methods that will be understood by middleware. These are: `buffered`, `currentTime`, `setCurrentTime`, `setMuted`, `setVolume`, `duration`, `muted`, `seekable`, `played`, `play`, `pause`, `paused` and `volume`. These allowed methods are split into three categories: [getters](#middleware-getters), [setters](#middleware-setters), and [mediators](#middleware-mediators).

There are a few special methods that affect middleware: `setSource` and `setTech`. These are called internally by Video.js when you call `player.src()`.

Expand Down
4 changes: 3 additions & 1 deletion src/js/tech/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ export const allowedGetters = {
buffered: 1,
currentTime: 1,
duration: 1,
seekable: 1,
muted: 1,
played: 1,
paused: 1,
seekable: 1,
volume: 1
};

Expand All @@ -197,6 +198,7 @@ export const allowedGetters = {
*/
export const allowedSetters = {
setCurrentTime: 1,
setMuted: 1,
setVolume: 1
};

Expand Down
19 changes: 16 additions & 3 deletions test/unit/player.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,7 @@ QUnit.test('should not allow to register custom player when any player has been

QUnit.test('techGet runs through middleware if allowedGetter', function(assert) {
let cts = 0;
let muts = 0;
let vols = 0;
let durs = 0;
let lps = 0;
Expand All @@ -1772,14 +1773,17 @@ QUnit.test('techGet runs through middleware if allowedGetter', function(assert)
currentTime() {
cts++;
},
volume() {
vols++;
},
duration() {
durs++;
},
loop() {
lps++;
},
muted() {
muts++;
},
volume() {
vols++;
}
}));

Expand All @@ -1798,10 +1802,12 @@ QUnit.test('techGet runs through middleware if allowedGetter', function(assert)
player.techGet_('volume');
player.techGet_('duration');
player.techGet_('loop');
player.techGet_('muted');

assert.equal(cts, 1, 'currentTime is allowed');
assert.equal(vols, 1, 'volume is allowed');
assert.equal(durs, 1, 'duration is allowed');
assert.equal(muts, 1, 'muted is allowed');
assert.equal(lps, 0, 'loop is not allowed');

middleware.getMiddleware('video/foo').pop();
Expand All @@ -1810,6 +1816,7 @@ QUnit.test('techGet runs through middleware if allowedGetter', function(assert)

QUnit.test('techCall runs through middleware if allowedSetter', function(assert) {
let cts = 0;
let muts = false;
let vols = 0;
let prs = 0;

Expand All @@ -1822,6 +1829,10 @@ QUnit.test('techCall runs through middleware if allowedSetter', function(assert)
vols++;
return vols;
},
setMuted() {
muts = true;
return muts;
},
setPlaybackRate() {
prs++;
return prs;
Expand All @@ -1843,12 +1854,14 @@ QUnit.test('techCall runs through middleware if allowedSetter', function(assert)

player.techCall_('setCurrentTime', 10);
player.techCall_('setVolume', 0.5);
player.techCall_('setMuted', true);
player.techCall_('setPlaybackRate', 0.75);

this.clock.tick(1);

assert.equal(cts, 1, 'setCurrentTime is allowed');
assert.equal(vols, 1, 'setVolume is allowed');
assert.equal(muts, true, 'setMuted is allowed');
assert.equal(prs, 0, 'setPlaybackRate is not allowed');

middleware.getMiddleware('video/foo').pop();
Expand Down

0 comments on commit f324d1f

Please sign in to comment.