Last active
January 4, 2017 17:01
-
-
Save annatomka/659736144028fc63f41c6e62f7251dc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bufferingSimulator = (function () { | |
var facade = require('logic/event/facade'); | |
var bufferingIntervalId, | |
lastBufferingTimeoutId, | |
bufferingFrequencyInMs = 5000, | |
bufferingDurationInMs = 2000; | |
function startBuffering () { | |
clear(); | |
var counter = 0; | |
bufferingIntervalId = setInterval(function () { | |
counter++; | |
if (counter % (bufferingFrequencyInMs / 10) === 0) { | |
sendBufferEmpty(); | |
} else if ((counter > bufferingFrequencyInMs / 10) && (counter % (bufferingFrequencyInMs / 10) === bufferingDurationInMs / 10)) { | |
sendBufferFull(); | |
} | |
}, 10); | |
} | |
function stopBuffering () { | |
clear(); | |
sendBufferFull(); | |
} | |
function sendBufferEmpty() { | |
console.info('#buffer_hunt simulate buffer_empty true ' + new Date()); | |
facade.send(facade.types.REPORT, facade.events.REPORT_BUFFER_EMPTY); | |
} | |
function sendBufferFull() { | |
console.info('#buffer_hunt send buffer_empty false' + new Date()); | |
facade.send(facade.types.REPORT, facade.events.REPORT_BUFFER_FULL); | |
} | |
function clear () { | |
clearBufferingInterval(); | |
clearLastBufferingTimeout(); | |
} | |
function clearBufferingInterval () { | |
if (bufferingIntervalId) { | |
clearInterval(bufferingIntervalId); | |
} | |
} | |
function clearLastBufferingTimeout () { | |
if (lastBufferingTimeoutId) { | |
clearTimeout(lastBufferingTimeoutId); | |
} | |
} | |
function addListeners () { | |
$('#startBuffering').click(function () { | |
startBuffering(); | |
}); | |
$('#stopBuffering').click(function () { | |
stopBuffering(); | |
}); | |
$('#bufferingFrequencyInput').change(function () { | |
bufferingFrequencyInMs = $(this).val(); | |
}); | |
$('#bufferingDurationInput').change(function () { | |
bufferingDurationInMs = $(this).val(); | |
}); | |
} | |
function addElements () { | |
var htmlContent = [ | |
'<div style="position: fixed;top: 30%;left: 20px;padding: 11px;background: rgba(255, 255, 255, 0.54);">', | |
'Buffering frequency', | |
'<input id="bufferingFrequencyInput" type="number" value="', bufferingFrequencyInMs, '" style="margin-bottom: 10px;"/>', | |
'<br />', | |
'Buffering duration', | |
'<input id="bufferingDurationInput" type="number" value="', bufferingDurationInMs, '" style="margin-bottom: 10px;"/>', | |
'<br />', | |
'<button type="submit" id="startBuffering">', | |
'Start Buffering', | |
'</button>', | |
'<button type="submit" id="stopBuffering">', | |
'Stop Buffering', | |
'</button>', | |
'</div>' | |
].join(''); | |
$('body').append(htmlContent); | |
} | |
function init () { | |
addElements(); | |
addListeners(); | |
} | |
return { | |
init: init | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment