From 6221a8f07892585fedb68d5702b858ee83801425 Mon Sep 17 00:00:00 2001 From: BrainCrumbz Date: Sat, 6 Jul 2024 08:44:37 +0200 Subject: [PATCH] fix(middleware): cache grows even if no middleware created (#8674) ## Description See issue #8653 ## Specific Changes proposed When in `middleware.js` the function `clearCacheForPlayer` runs, before setting a value to null in middlware caches, it checks if the key exists in the first place. ## Requirements Checklist - [x] Feature implemented / Bug fixed - [x] If necessary, more likely in a feature request than a bug fix - [x] Change has been verified in an actual browser (Chrome, Firefox, IE) - [ ] Unit Tests updated or fixed - [ ] Docs/guides updated - [x] Example created ([starter template on JSBin](https://codepen.io/gkatsev/pen/GwZegv?editors=1000#0)) - [ ] Reviewed by Two Core Contributors --------- Co-authored-by: Giuseppe Piscopo Co-authored-by: mister-ben --- sandbox/middleware-instances.html.example | 71 +++++++++++++++++++++++ src/js/tech/middleware.js | 4 +- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 sandbox/middleware-instances.html.example diff --git a/sandbox/middleware-instances.html.example b/sandbox/middleware-instances.html.example new file mode 100644 index 0000000000..b33e6b6169 --- /dev/null +++ b/sandbox/middleware-instances.html.example @@ -0,0 +1,71 @@ + + + + + Video.js Sandbox + + + + + +
+

You can use /sandbox/ for writing and testing your own code. Nothing in /sandbox/ will get checked into the repo, except files that end in .example (so don't edit or add those files). To get started run `npm start` and open the index.html

+
npm start
+
open http://localhost:9999/sandbox/index.html
+
+ +
+

+ In developer console, Sources tab, look for clearCacheForPlayer function. + Place a logpoint at function closing. Logpoint content should be: +

+
'middlewareInstances nr', Object.keys(middlewareInstances).length
+

+ When one or more players are removed, the number of instances should *NOT* grow. +

+
+ +
+ + +
+ +
+ + + + + diff --git a/src/js/tech/middleware.js b/src/js/tech/middleware.js index bb034ad3b4..78182c4a28 100644 --- a/src/js/tech/middleware.js +++ b/src/js/tech/middleware.js @@ -247,7 +247,9 @@ function executeRight(mws, method, value, terminated) { * A {@link Player} instance. */ export function clearCacheForPlayer(player) { - middlewareInstances[player.id()] = null; + if (middlewareInstances.hasOwnProperty(player.id())) { + delete middlewareInstances[player.id()]; + } } /**