Skip to content

Commit

Permalink
Merge pull request #1361 from appsembler/bugfix/ENG-97-catch-and-retr…
Browse files Browse the repository at this point in the history
…y-yt-api-errs

Catch and retry video module YT IFrame API ready errs
  • Loading branch information
bryanlandia authored Jul 20, 2023
2 parents b6fcd12 + b1b3621 commit ea61433
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion common/lib/xmodule/xmodule/js/src/video/01_initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function(VideoPlayer, i18n, moment, _) {

_youtubeApiDeferred = null,
_oldOnYouTubeIframeAPIReady;
const setupOnYouTubeIframeAPIReadyMaxCalls=3;

Initialize.prototype = methodsDict;

Expand Down Expand Up @@ -165,14 +166,33 @@ function(VideoPlayer, i18n, moment, _) {
//
// If this global function is already defined, we store it first, and make
// sure that it gets executed when our Deferred object is resolved.
let setupOnYouTubeIframeAPIReadyCallsCount = 0;

setupOnYouTubeIframeAPIReady = function() {
setupOnYouTubeIframeAPIReadyCallsCount++;
if (setupOnYouTubeIframeAPIReadyCallsCount > setupOnYouTubeIframeAPIReadyMaxCalls) {
throw new Error('Too many OnYouTubeIframeAPIReady retries after TypeError...giving up.');
}

_oldOnYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady || undefined;

window.onYouTubeIframeAPIReady = function() {
window.onYouTubeIframeAPIReady.resolve();
};

window.onYouTubeIframeAPIReady.resolve = _youtubeApiDeferred.resolve;
try {
window.onYouTubeIframeAPIReady.resolve = _youtubeApiDeferred.resolve;
} catch (e) {
console.error('Error while trying to resolve the Deferred object responsible for calling OnYouTubeIframeAPIReady callbacks.');
console.error('window.onYouTubeIframeAPIReady is ' + window.onYouTubeIframeAPIReady);
console.error(e);
if (e instanceof TypeError) {
setupOnYouTubeIframeAPIReady(); // Try again up to defined max calls.
}
else {
throw e;
}
}
window.onYouTubeIframeAPIReady.done = _youtubeApiDeferred.done;

if (_oldOnYouTubeIframeAPIReady) {
Expand Down

0 comments on commit ea61433

Please sign in to comment.