Remove manageCover from the managePlayer code

Here are the steps that we are working with:

Step 1: Change needed private functions to be public
Step 2: MoveCopy unsuitable functions to onYouTubeIframeAPIReady
Step 3: Rename managePlayer.add to use the addPlayer function in onYouTubeIframeAPIReady
Step 4: Move parts of the functions back in to manageCover and managePlayer
Step 5: Rename addPlayer in onYouTubeIframeAPIReady back to managePlayer.add
Step 6: Change public functions that no longer need to be public back to private
Step 7: Remove as much of the onYouTubeIframeAPIReady functions as we can

That brings us to step 3.

Step 3: Rename managePlayer.add to use the addPlayer function in onYouTubeIframeAPIReady

Renaming managePlayer.add to instead use addPlayer is not the only thing that happens in this step. The main reason why we are doing this step is to remove the createCoverClickHandler and addPlayer functions from the managePlayer code.

After renaming managePlayer.add to be just addPlayer, we can remove the public method called add from the end of managePlayer, and also remove the addPlayer function from the managePlayer code.

In order to achieve that, we need to update the addPlayer function that’s inside of onYouTubeIframeAPIReady so that it doesn’t refer to managePlayer.createCoverClickHandler. Instead we just use createCoverClickHandler instead so that it’s the separate function that we copied into onYouTubeIframeAPIReady that is being used.

On doing that we can remove the createCoverClickHandler public method from the end of the managePlayer code, and we can now delete the createCoverClickHandler function from inside of managePlayer.

That is all for Step 3. What these first 3 steps has achieved is to extract two functions, createCoverClickHandler and addPlayer, out of managePlayer and into the onYouTubeIframeAPIReady function.

That was quite tricky what with attempting to avoid errors when doing that.

Before dealing with Step 4 I’ll demonstrate how this is more appropriately done, where errors are instead beneficially used to instruct is about what needs to be done next.

2 Likes