mirror of
				https://github.com/videojs/video.js.git
				synced 2025-10-31 00:08:01 +02:00 
			
		
		
		
	feat(middleware): make setSource be optional (#5295)
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.
This commit is contained in:
		| @@ -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) { | ||||
| @@ -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: | ||||
| @@ -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`. | ||||
|   | ||||
| @@ -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 | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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) { | ||||
| @@ -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(); | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user