Skip to content

Commit

Permalink
feat(middleware): make setSource be optional (#5295)
Browse files Browse the repository at this point in the history
setSource is useful if you care to be fiddling with the source or doing some work depending on what source is set. However, sometimes, you don't need a setSource and want the middleware to always be selected.
Now, if setSource is missing, it will implicitly be included in the middleware chain.
  • Loading branch information
gkatsev authored Jul 6, 2018
1 parent 361dc76 commit 781a6d8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
26 changes: 23 additions & 3 deletions docs/guides/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ Middleware are functions that return an object, a class instance, a prototype, e
There are a few special methods that affect middleware: `setSource` and `setTech`. These are called internally by Video.js when you call `player.src()`.

### setSource
> *NOTE*: In versions of Video.js 7.0.5 and older, `setSource` was required for all middleware and had be included in the returned objects.
`setSource` is a required method for all middleware and must be included in the returned object. This method will setup the routing between a specific source and middleware and eventually sets the source on the `Tech`.
This method will setup the routing between a specific source and middleware and eventually sets the source on the `Tech`.

If your middleware is not manipulating, redirecting or rejecting the source, you can pass along the source by doing the following:
If your middleware is not manipulating, redirecting or rejecting the source, you may leave this method out on newer versions of Video.js. Doing so will select middleware implicitly.

In versions 7.0.5 and older, to get your middleware selected, you can pass along the source by doing the following:

```javascript
videojs.use('*', function(player) {
Expand Down Expand Up @@ -145,6 +148,23 @@ var myMiddleware = function(player) {
videojs.use('*', myMiddleware);
```

And the same example with `setSource` omitted:

```javascript
var myMiddleware = function(player) {
return {
currentTime: function(ct) {
return ct / 2;
},
setCurrentTime: function(time) {
return time * 2;
}
};
};

videojs.use('*', myMiddleware);
```

This middleware gives the appearance of the video source playing at double its speed, by halving the time we _get_ from the `Tech`, and doubling the time we _set_ on the `Tech`.

An example of a middleware that uses Mediator methods is below:
Expand Down Expand Up @@ -209,4 +229,4 @@ var myMiddleware = function(player) {
videojs.use('*', myMiddleware);
```

This middleware always terminates calls to `play()` by returning the `TERMINATOR` in `callPlay`. In `play` we are able to see that the call to `play()` was terminated and was never called on the `Tech`.
This middleware always terminates calls to `play()` by returning the `TERMINATOR` in `callPlay`. In `play` we are able to see that the call to `play()` was terminated and was never called on the `Tech`.
7 changes: 7 additions & 0 deletions src/js/tech/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ function setSourceHelper(src = {}, middleware = [], next, player, acc = [], last
} else if (mwFactory) {
const mw = getOrCreateFactory(player, mwFactory);

// if setSource isn't present, implicitly select this middleware
if (!mw.setSource) {
acc.push(mw);
return setSourceHelper(src, mwrest, next, player, acc, lastRun);
}

mw.setSource(assign({}, src), function(err, _src) {

// something happened, try the next middleware on the current level
Expand All @@ -172,6 +178,7 @@ function setSourceHelper(src = {}, middleware = [], next, player, acc = [], last
acc,
lastRun);
});

} else if (mwrest.length) {
setSourceHelper(src, mwrest, next, player, acc, lastRun);
} else if (lastRun) {
Expand Down
27 changes: 27 additions & 0 deletions test/unit/tech/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ QUnit.test('setSource will select all middleware of a given type, until src chan

middleware.getMiddleware('video/foo').pop();
middleware.getMiddleware('video/foo').pop();
middleware.getMiddleware('video/foo').pop();
});

QUnit.test('a middleware without a mediator method will not throw an error', function(assert) {
Expand Down Expand Up @@ -509,3 +510,29 @@ QUnit.test('a middleware factory is called on a new source with a new player', f

middleware.getMiddleware('video/foo').pop();
});

QUnit.test('a middleware without a setSource gets chosen implicitly', function(assert) {
let mws = [];
const mw = {
currentTime(ct) {
}
};
const mwFactory = () => mw;

middleware.use('video/foo', mwFactory);

middleware.setSource({
id() {
return 'vid1';
},
setTimeout: window.setTimeout
}, {src: 'foo', type: 'video/foo'}, function(src, _mws) {
mws = _mws;
});

this.clock.tick(1);

assert.equal(mws.length, 1, 'we have 1 middleware set');

middleware.getMiddleware('video/foo').pop();
});

0 comments on commit 781a6d8

Please sign in to comment.