From 9f640c2225e60169519bfca479aea20a85a784d9 Mon Sep 17 00:00:00 2001 From: Sarah Rimron-Soutter Date: Tue, 17 Jan 2023 14:52:52 +0000 Subject: [PATCH] Add middleware method to override currentSource() --- src/js/player.js | 4 +++- src/js/tech/middleware.js | 4 ++++ test/unit/tech/middleware.test.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/js/player.js b/src/js/player.js index e8e5b30c4..c5cd4253f 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -3569,7 +3569,9 @@ class Player extends Component { * The current source object */ currentSource() { - return this.cache_.source || {}; + const mw = this.middleware_; + + return middleware.currentSource(this.cache_.source, mw) || this.cache_.source || {}; } /** diff --git a/src/js/tech/middleware.js b/src/js/tech/middleware.js index bb7f3ad94..fac3e8f2c 100644 --- a/src/js/tech/middleware.js +++ b/src/js/tech/middleware.js @@ -82,6 +82,10 @@ export function setSource(player, src, next) { player.setTimeout(() => setSourceHelper(src, middlewares[src.type], next, player), 1); } +export function currentSource(src, middleware) { + return middleware.reduceRight(middlewareIterator('currentSource'), src); +} + /** * When the tech is set, passes the tech to each middleware's `setTech` method. * diff --git a/test/unit/tech/middleware.test.js b/test/unit/tech/middleware.test.js index 473d91e37..112b854d0 100644 --- a/test/unit/tech/middleware.test.js +++ b/test/unit/tech/middleware.test.js @@ -536,3 +536,17 @@ QUnit.test('a middleware without a setSource gets chosen implicitly', function(a middleware.getMiddleware('video/foo').pop(); }); + +QUnit.test('middleware can override currentSource', function(assert) { + const middlewares = [ + { + currentSource: (src) => { + return `foo${src}`; + } + } + ]; + + const result = middleware.currentSource('bar', middlewares); + + assert.equal(result, 'foobar', 'value of currentSource() is overriden'); +});