diff options
Diffstat (limited to 'js/codeq')
-rw-r--r-- | js/codeq/core.js | 17 | ||||
-rw-r--r-- | js/codeq/prolog.js | 41 | ||||
-rw-r--r-- | js/codeq/python.js | 17 |
3 files changed, 53 insertions, 22 deletions
diff --git a/js/codeq/core.js b/js/codeq/core.js index 84bbfa8..b1d4a5c 100644 --- a/js/codeq/core.js +++ b/js/codeq/core.js @@ -329,8 +329,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ }); }, - // codeq event handling + // resolve a promise not sooner than after [timeout] seconds + // from https://github.com/kriskowal/q/commit/681f6d201842d4b99bab5451ebc57ef0c3c0d49a + 'throttle': function (promise, timeout) { + if (timeout === undefined) { + timeout = promise; + promise = undefined; + } + var deferred = Q.defer(); + Q.when(promise, undefined, undefined, deferred.notify); + setTimeout(function () { + deferred.resolve(promise); + }, timeout); + return deferred.promise; + }, + + // codeq event handling 'fire': function (eventName, args) { queuedEvents.push({'name': eventName, 'args': args}); fireEvents(); diff --git a/js/codeq/prolog.js b/js/codeq/prolog.js index 3d34423..ea4c9b2 100644 --- a/js/codeq/prolog.js +++ b/js/codeq/prolog.js @@ -284,28 +284,35 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ }); jqBtnTest.on('click', function () { + // random timeout before returning results to the user, helps + // maintain the illusion + var timeout = 500 + Math.random() * 1000; + editor.setOption('readOnly', true); terminal.inputDisable(); jqBtnTest.ladda('start'); - codeq.comms.sendTest({ + + codeq.throttle( + codeq.comms.sendTest({ 'program': editor.getDoc().getValue(), 'problem_id': problemDef.id - }) - .then(function (data) { - if (data.code === 0) { - hinter.handle(data.hints); - } - else { - terminal.append(data.message + '\n', 'error'); - } - }) - .fail(commError) - .fin(function () { - editor.setOption('readOnly', false); - terminal.inputEnable(); - jqBtnTest.ladda('stop'); - }) - .done(); + }), + timeout) + .then(function (data) { + if (data.code === 0) { + hinter.handle(data.hints); + } + else { + terminal.append(data.message + '\n', 'error'); + } + }) + .fail(commError) + .fin(function () { + editor.setOption('readOnly', false); + terminal.inputEnable(); + jqBtnTest.ladda('stop'); + }) + .done(); }); codeq.comms.loadProblem(problemDef.id).done(); diff --git a/js/codeq/python.js b/js/codeq/python.js index f2b0184..2ea960a 100644 --- a/js/codeq/python.js +++ b/js/codeq/python.js @@ -240,12 +240,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ } }); jqBtnTest.on('click', function () { + // random timeout before returning results to the user, helps + // maintain the illusion + var timeout = 500 + Math.random() * 1000; + editor.setOption('readOnly', true); + terminal.inputDisable(); jqBtnTest.ladda('start'); - codeq.comms.sendTest({ - 'program': editor.getDoc().getValue(), - 'problem_id': problemDef.id - }) + + codeq.throttle( + codeq.comms.sendTest({ + 'program': editor.getDoc().getValue(), + 'problem_id': problemDef.id + }), + timeout) .then(function (data) { if (data.code === 0) { hinter.handle(data.hints); @@ -257,6 +265,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ .fail(commError) .fin(function () { editor.setOption('readOnly', false); + terminal.inputEnable(); jqBtnTest.ladda('stop'); }) .done(); |