diff options
author | Timotej Lazar <timotej.lazar@araneo.org> | 2015-10-05 10:11:26 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@araneo.org> | 2015-10-05 10:11:26 +0200 |
commit | 194f3449751b90c1f8d7ec3625ab26594bc808c6 (patch) | |
tree | ae45810c5448ea45d9b7cf3c6c7c63a03d1de16e /js/codeq | |
parent | 5877ffd8aaffdf3e416dbb36e766736b866e5963 (diff) |
Move makeActivityHandler to core.js
There were three identical copies for {prolog,python,robot}.js.
Diffstat (limited to 'js/codeq')
-rw-r--r-- | js/codeq/core.js | 47 | ||||
-rw-r--r-- | js/codeq/prolog.js | 47 | ||||
-rw-r--r-- | js/codeq/python.js | 47 | ||||
-rw-r--r-- | js/codeq/robot.js | 65 |
4 files changed, 50 insertions, 156 deletions
diff --git a/js/codeq/core.js b/js/codeq/core.js index 03c5539..72b4532 100644 --- a/js/codeq/core.js +++ b/js/codeq/core.js @@ -336,6 +336,51 @@ } }; + var makeActivityHandler = function (editor, problem_id) { + var lastActivityMillis = Date.now(), + deltaActivityMillis = function deltaActivityMillisFunc () { + var now = Date.now(), + dt = now - lastActivityMillis; + lastActivityMillis = now; + return dt; + }, + queue = [], + ts = null, + timer = function () { + var promise; + ts = null; + if (queue.length === 0) return Q(true); + promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id); + queue.length = 0; + return promise; + }, + flush = function () { + clearTimeout(ts); + return timer(); + }; + + return { + 'queueTrace': function (trace) { + trace['dt'] = deltaActivityMillis(); + queue.push(trace); + if (ts === null) ts = setTimeout(timer, 10000); // flush every 10 seconds + return this; + }, + 'flush': flush, + 'addAndPurge': function (trace) { + var accumulatedTrace = queue; + queue = []; + trace['dt'] = deltaActivityMillis(); + accumulatedTrace.push(trace); + if (ts !== null) { + clearTimeout(ts); + ts = null; + } + return accumulatedTrace; + } + }; + }; + window.codeq = { 'jsonize': jsonize, @@ -389,6 +434,8 @@ return n; }, + 'makeActivityHandler': makeActivityHandler, + 'wait': function (promise) { jqDisabled.css(waitCssEnter); return promise.fin(function () { diff --git a/js/codeq/prolog.js b/js/codeq/prolog.js index a42bfc3..d9b76d1 100644 --- a/js/codeq/prolog.js +++ b/js/codeq/prolog.js @@ -198,51 +198,6 @@ return terminal; }; - var makeActivityHandler = function (editor, problem_id) { - var lastActivityMillis = Date.now(), - deltaActivityMillis = function deltaActivityMillisFunc () { - var now = Date.now(), - dt = now - lastActivityMillis; - lastActivityMillis = now; - return dt; - }, - queue = [], - ts = null, - timer = function () { - var promise; - ts = null; - if (queue.length === 0) return Q(true); - promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id); - queue.length = 0; - return promise; - }, - flush = function () { - clearTimeout(ts); - return timer(); - }; - - return { - 'queueTrace': function (trace) { - trace['dt'] = deltaActivityMillis(); - queue.push(trace); - if (ts === null) ts = setTimeout(timer, 10000); // flush every 10 seconds - return this; - }, - 'flush': flush, - 'addAndPurge': function (trace) { - var accumulatedTrace = queue; - queue = []; - trace['dt'] = deltaActivityMillis(); - accumulatedTrace.push(trace); - if (ts !== null) { - clearTimeout(ts); - ts = null; - } - return accumulatedTrace; - } - }; - }; - codeq.on('init', function (args) { codeq.tr.registerDictionary('prolog', codeq.tr.emptyDictionary); // to make the translator happy, when this screen is not active }); @@ -264,7 +219,7 @@ indentUnit: 4, value: currentSolution || '' }), - activityHandler = makeActivityHandler(editor, problemDef.id), + activityHandler = codeq.makeActivityHandler(editor, problemDef.id), terminal = makePrologTerminalHandler(jqTerminal, editor, problemDef.id, activityHandler), hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'prolog_hints', problemDef, commonDef), commError = function (error) { diff --git a/js/codeq/python.js b/js/codeq/python.js index 214b249..4254d8c 100644 --- a/js/codeq/python.js +++ b/js/codeq/python.js @@ -156,51 +156,6 @@ return terminal; }; - var makeActivityHandler = function (editor, problem_id) { - var lastActivityMillis = Date.now(), - deltaActivityMillis = function deltaActivityMillisFunc () { - var now = Date.now(), - dt = now - lastActivityMillis; - lastActivityMillis = now; - return dt; - }, - queue = [], - ts = null, - timer = function () { - var promise; - ts = null; - if (queue.length === 0) return Q(true); - promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id); - queue.length = 0; - return promise; - }, - flush = function () { - clearTimeout(ts); - return timer(); - }; - - return { - 'queueTrace': function (trace) { - trace['dt'] = deltaActivityMillis(); - queue.push(trace); - if (ts === null) ts = setTimeout(timer, 10000); // flush every 10 seconds - return this; - }, - 'flush': flush, - 'addAndPurge': function (trace) { - var accumulatedTrace = queue; - queue = []; - trace['dt'] = deltaActivityMillis(); - accumulatedTrace.push(trace); - if (ts !== null) { - clearTimeout(ts); - ts = null; - } - return accumulatedTrace; - } - }; - }; - codeq.on('init', function (args) { codeq.tr.registerDictionary('python', codeq.tr.emptyDictionary); // to make the translator happy, when this screen is not active }); @@ -222,7 +177,7 @@ indentUnit: 4, value: currentSolution || '' }), - activityHandler = makeActivityHandler(editor, problemDef.id), + activityHandler = codeq.makeActivityHandler(editor, problemDef.id), terminal = makePythonTerminalHandler(jqTerminal, editor, problemDef.id, activityHandler), hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'python_hints', problemDef, commonDef), commError = function (error) { diff --git a/js/codeq/robot.js b/js/codeq/robot.js index c2d23e1..8854855 100644 --- a/js/codeq/robot.js +++ b/js/codeq/robot.js @@ -118,73 +118,10 @@ // a constant var firstCharacterPos = {'line': 0, 'ch': 0}; - var makeRobotTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) { - var terminal = codeq.makeConsole(jqConsole, { - 'greeting': 'CodeQ Robot terminal proxy', - 'autoHistory': true - }); - - terminal.onInput = function (text) { - terminal.append('Not implemented.\n', 'output'); - }; - return terminal; - }; - - var makeActivityHandler = function (editor, problem_id) { - var lastActivityMillis = Date.now(), - deltaActivityMillis = function deltaActivityMillisFunc () { - var now = Date.now(), - dt = now - lastActivityMillis; - lastActivityMillis = now; - return dt; - }, - queue = [], - ts = null, - timer = function () { - var promise; - ts = null; - if (queue.length === 0) return Q(true); - promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id); - queue.length = 0; - return promise; - }, - flush = function () { - clearTimeout(ts); - return timer(); - }; - - return { - 'queueTrace': function (trace) { - trace['dt'] = deltaActivityMillis(); - queue.push(trace); - if (ts === null) ts = setTimeout(timer, 10000); // flush every 10 seconds - return this; - }, - 'flush': flush, - 'addAndPurge': function (trace) { - var accumulatedTrace = queue; - queue = []; - trace['dt'] = deltaActivityMillis(); - accumulatedTrace.push(trace); - if (ts !== null) { - clearTimeout(ts); - ts = null; - } - return accumulatedTrace; - } - }; - }; - codeq.on('init', function (args) { codeq.tr.registerDictionary('robot', codeq.tr.emptyDictionary); // to make the translator happy, when this screen is not active }); - /** - * Creates a new handler for the given Prolog assignment definition. - * - * @param {PrologTaskDef} info - * @returns {{destroy: Function, processServerHints: Function}} - */ var createRobotHandler = function (problemDef, commonDef, currentSolution) { var //problem = info.problem, jqDescriptionContent = jqDescription.find('.description'), @@ -196,7 +133,7 @@ indentUnit: 4, value: currentSolution || '' }), - activityHandler = makeActivityHandler(editor, problemDef.id), + activityHandler = codeq.makeActivityHandler(editor, problemDef.id), terminal = makeRobotTerminalHandler(jqTerminal, editor, problemDef.id, activityHandler), hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'robot_hints', problemDef, commonDef), commError = function (error) { |