Skip to content

Instantly share code, notes, and snippets.

@brianpmarks
Created October 29, 2015 16:34
Show Gist options
  • Save brianpmarks/9941a849b5350982795e to your computer and use it in GitHub Desktop.
Save brianpmarks/9941a849b5350982795e to your computer and use it in GitHub Desktop.
shows replit connection and timeout logic
function runConsole() {
var prog = preRun()
, serializedCode = api.getValue()
, commands = [], history = []
, input;
if (prog) {
history = prog.split('\n');
}
$('#reset-output').hide();
$("#errorConnecting").hide();
var done = initConsole("Connecting to server");
var replitTimeout = function() {
var state = jqconsole.GetState();
if (state === "input" || state === "prompt") {
jqconsole.AbortPrompt();
}
jqconsole.Write("Connection to server timed out. Click run to reconnect.\n");
api.replit.disconnect();
}
api.replit.getConnection().then(function() {
done();
api.replit.connected(true);
api.replit.running(true);
api.replit.reconnect(false);
for (var i = 0; i < history.length; i++) {
input = history[i]
.replace(/([^\\])\\n/g, '$1\n')
.replace(/([^\\])\\r/g, '$1\r')
.replace(/\\(\\[nr])/g, '$1');
commands.push(input);
}
api.replit.setHistory(commands);
if (replitTimer) {
clearTimeout(replitTimer);
}
replitTimer = setTimeout(replitTimeout, 30000);
api.replit.startPrompt(jqconsole, function() {
api.sendAnalytics('Interaction', {
action : 'Modify',
label : 'Code'
});
if (replitTimer) {
clearTimeout(replitTimer);
}
replitTimer = setTimeout(replitTimeout, 90000);
});
}, function() {
// connection failed
$("#errorConnecting").show();
api.replit.reconnect(true);
}).then(function(result) {
if (result && result.error) {
api.showMessage('alert', result.error);
}
}).catch(function(err) {
console.log('caught err:', err);
}).finally(function() {
$('#python3-console-output').removeClass('console-active');
api.replit.running(false);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment