From 680d4bf96c1bf3b5f8a4a0e8941db2d5bcf1bd74 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Sat, 16 May 2015 09:39:16 +0000 Subject: [PATCH] Make player creation code more intention revealing --- app/assets/javascripts/asciicasts.js | 61 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/asciicasts.js b/app/assets/javascripts/asciicasts.js index 3ac41d9..d8d94a2 100644 --- a/app/assets/javascripts/asciicasts.js +++ b/app/assets/javascripts/asciicasts.js @@ -1,30 +1,37 @@ -function createPlayer(parentNode, asciicast, options) { - asciinema.CreatePlayer( - parentNode, - asciicast.width, asciicast.height, - asciicast.stdout_frames_url, - asciicast.duration, - { - snapshot: asciicast.snapshot, - speed: options.speed, - autoPlay: options.autoPlay, - loop: options.loop, - fontSize: options.fontSize, - theme: options.theme - } - ); -} - function tryCreatePlayer(parentNode, asciicast, options) { - if (asciicast.stdout_frames_url) { - $('.processing-info').remove(); - createPlayer(parentNode, asciicast, options); - } else { - $('.processing-info').show(); - setTimeout(function() { - $.get('/api/asciicasts/' + asciicast.id + '.json', function(data) { - tryCreatePlayer(parentNode, data, options); - }); - }, 2000); + function createPlayer() { + asciinema.CreatePlayer( + parentNode, + asciicast.width, asciicast.height, + asciicast.stdout_frames_url, + asciicast.duration, + { + snapshot: asciicast.snapshot, + speed: options.speed, + autoPlay: options.autoPlay, + loop: options.loop, + fontSize: options.fontSize, + theme: options.theme + } + ); + } + + function fetch() { + $.get('/api/asciicasts/' + asciicast.id + '.json', function(data) { + asciicast = data; + checkReadiness(); + }); } + + function checkReadiness() { + if (asciicast.stdout_frames_url) { + $('.processing-info').remove(); + createPlayer(); + } else { + $('.processing-info').show(); + setTimeout(fetch, 2000); + } + } + + checkReadiness(); }