Skip to content

Commit 20fac19

Browse files
committed
moar
1 parent fd336ad commit 20fac19

2 files changed

Lines changed: 118 additions & 46 deletions

File tree

‎v3/js/opt-frontend-common.js‎

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,15 @@ function executeCodeAndCreateViz(codeToExec,
15031503

15041504
var uh = args.myViz.updateHistory;
15051505
assert(uh.length > 0); // should already be seeded with an initial value
1506-
var lastTs = uh[uh.length - 1][1];
1507-
1508-
// (debounce entries that are less than 1 second apart to
1509-
// compress the logs a bit when there's rapid scrubbing or scrolling)
1510-
if ((curTs - lastTs) < 1000) {
1511-
uh.pop(); // get rid of last entry before pushing a new entry
1506+
if (uh.length > 1) { // don't try to "compress" the very first entry
1507+
var lastTs = uh[uh.length - 1][1];
1508+
// (debounce entries that are less than 1 second apart to
1509+
// compress the logs a bit when there's rapid scrubbing or scrolling)
1510+
if ((curTs - lastTs) < 1000) {
1511+
uh.pop(); // get rid of last entry before pushing a new entry
1512+
}
15121513
}
15131514
uh.push([args.myViz.curInstr, curTs]);
1514-
//console.log(JSON.stringify(uh));
15151515
}
15161516
return [false]; // pass through to let other hooks keep handling
15171517
});
@@ -1662,6 +1662,7 @@ function executeCodeAndCreateViz(codeToExec,
16621662
options_json: JSON.stringify(backendOptionsObj),
16631663
user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
16641664
session_uuid: sessionUUID,
1665+
exeTime: new Date().getTime(),
16651666
diffs_json: deltaObjStringified},
16661667
function(dat) {} /* don't do anything since this is a dummy call */, "text");
16671668

@@ -1685,6 +1686,7 @@ function executeCodeAndCreateViz(codeToExec,
16851686
options_json: JSON.stringify(backendOptionsObj),
16861687
user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
16871688
session_uuid: sessionUUID,
1689+
exeTime: new Date().getTime(),
16881690
diffs_json: deltaObjStringified},
16891691
execCallback, "json");
16901692
}
@@ -1693,29 +1695,34 @@ function executeCodeAndCreateViz(codeToExec,
16931695
}
16941696

16951697

1698+
// Compress updateHistory before encoding and sending to
1699+
// the server so that it takes up less room in the URL. Have each
1700+
// entry except for the first be a delta from the FIRST entry.
1701+
function compressUpdateHistoryList(myVisualizer) {
1702+
assert(myVisualizer);
1703+
var uh = myVisualizer.updateHistory;
1704+
var encodedUh = [];
1705+
if (uh) {
1706+
encodedUh.push(uh[0]);
1707+
1708+
var firstTs = uh[0][1];
1709+
for (var i = 1; i < uh.length; i++) {
1710+
var e = uh[i];
1711+
encodedUh.push([e[0], e[1] - firstTs]);
1712+
}
1713+
1714+
// finally push a final entry with the current timestamp delta
1715+
var curTs = new Date().getTime();
1716+
encodedUh.push([myVisualizer.curInstr, curTs - firstTs]);
1717+
}
1718+
return encodedUh;
1719+
}
1720+
16961721
// this feature was deployed on 2015-09-17, so check logs for
16971722
// viz_interaction.py
16981723
function submitUpdateHistory(why) {
16991724
if (myVisualizer) {
1700-
// Compress updateHistory before encoding and sending to
1701-
// the server so that it takes up less room in the URL. Have each
1702-
// entry except for the first be a delta from the FIRST entry.
1703-
var uh = myVisualizer.updateHistory;
1704-
var encodedUh = [];
1705-
if (uh) {
1706-
encodedUh.push(uh[0]);
1707-
1708-
var firstTs = uh[0][1];
1709-
for (var i = 1; i < uh.length; i++) {
1710-
var e = uh[i];
1711-
encodedUh.push([e[0], e[1] - firstTs]);
1712-
}
1713-
1714-
// finally push a final entry with the current timestamp delta
1715-
var curTs = new Date().getTime();
1716-
encodedUh.push([myVisualizer.curInstr, curTs - firstTs]);
1717-
}
1718-
1725+
var encodedUh = compressUpdateHistoryList(myVisualizer);
17191726
var encodedUhJSON = JSON.stringify(encodedUh);
17201727

17211728
var myArgs = {session_uuid: sessionUUID,

‎v3/js/opt-live.js‎

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,13 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3939
File "<string>", line 1
4040
x~=1
4141
^
42-
- for slower frontends, add an unobtrusive "running ..." indicator
43-
4442
- make Ace editor resizable width-wise using jQuery resizable
4543
(stackoverflow has some tips for how to do that)
4644
4745
- support pasting in code via URL, which will be important for
4846
transporting the user from regular OPT to live mode
4947
- add "Generate permanent link" button, but no need for iframe embed btn
5048
51-
- make sure server logging works properly, esp. session and user IDs,
52-
and slider interactions
53-
5449
- if these Ace enhancements look good, then I can also use them for
5550
Codeopticon as well!
5651
@@ -103,6 +98,17 @@ var prevVisualizer = null; // the visualizer object from the previous execution
10398

10499
var aceEditorWidth = '600px';
105100

101+
var hasSyntaxError = false;
102+
function toggleSyntaxError(x) {
103+
if (x) {
104+
hasSyntaxError = true;
105+
$("#dataViz,#curInstr").addClass('dimmed'); // dim the visualization until we fix the error
106+
} else {
107+
hasSyntaxError = false;
108+
$("#dataViz,#curInstr").removeClass('dimmed'); // un-dim the visualization
109+
}
110+
}
111+
106112
var allMarkerIds = [];
107113

108114
function removeAllGutterDecorations() {
@@ -130,7 +136,7 @@ function updateStepLabels() {
130136
var isLastInstr = myVisualizer.curInstr === (totalInstrs-1);
131137
if (isLastInstr) {
132138
if (myVisualizer.promptForUserInput || myVisualizer.promptForMouseInput) {
133-
$("#curInstr").html('<b><font color="' + brightRed + '">Enter user input:</font></b>');
139+
$("#curInstr").html('<font color="' + brightRed + '">input/raw_input not supported in live editor</font>');
134140
} else if (myVisualizer.instrLimitReached) {
135141
$("#curInstr").html("Instruction limit reached (" + String(totalInstrs-1) + " steps)");
136142
} else {
@@ -185,7 +191,7 @@ function optliveFinishSuccessfulExecution() {
185191
$("#pyOutputPane").show();
186192
doneExecutingCode();
187193

188-
$("#dataViz,#curInstr").removeClass('dimmed'); // un-dim the visualization
194+
toggleSyntaxError(false);
189195

190196
// set up execution slider, code inspired by pytutor.js:
191197
var sliderDiv = $('#executionSlider');
@@ -208,15 +214,6 @@ function optliveFinishSuccessfulExecution() {
208214
}
209215
});
210216

211-
myVisualizer.add_pytutor_hook(
212-
"end_updateOutput",
213-
function(args) {
214-
// PROGRAMMATICALLY change the value, so evt.originalEvent should be undefined
215-
$('#executionSlider').slider('value', args.myViz.curInstr);
216-
return [false];
217-
}
218-
);
219-
220217
// do this AFTER making #pyOutputPane visible, or else
221218
// jsPlumb connectors won't render properly
222219

@@ -239,16 +236,67 @@ function optliveFinishSuccessfulExecution() {
239236
}
240237

241238
updateStepLabels(); // do it once
239+
240+
241+
// initialize this at the VERY END after jumping to the proper initial step
242+
// above, perhaps using renderStep()
243+
244+
// copied from opt-frontend-common.js
245+
myVisualizer.creationTime = new Date().getTime();
246+
// each element will be a two-element list consisting of:
247+
// [step number, timestamp]
248+
// (debounce entries that are less than 1 second apart to
249+
// compress the logs a bit when there's rapid scrubbing or scrolling)
250+
//
251+
// the first entry has a THIRD field:
252+
// [step number, timestamp, total # steps]
253+
//
254+
// subsequent entries don't need it since it will always be the same.
255+
// the invariant is that step number < total # steps (since it's
256+
// zero-indexed
257+
myVisualizer.updateHistory = [];
258+
myVisualizer.updateHistory.push([myVisualizer.curInstr,
259+
myVisualizer.creationTime,
260+
myVisualizer.curTrace.length]);
261+
262+
// add this hook at the VERY END after jumping to the proper initial step
263+
// above, perhaps using renderStep()
264+
myVisualizer.add_pytutor_hook(
265+
"end_updateOutput",
266+
function(args) {
267+
// copied from opt-frontend-common.js
268+
if (args.myViz.creationTime) {
269+
var curTs = new Date().getTime();
270+
271+
var uh = args.myViz.updateHistory;
272+
assert(uh.length > 0); // should already be seeded with an initial value
273+
if (uh.length > 1) { // don't try to "compress" the very first entry
274+
var lastTs = uh[uh.length - 1][1];
275+
// (debounce entries that are less than 1 second apart to
276+
// compress the logs a bit when there's rapid scrubbing or scrolling)
277+
if ((curTs - lastTs) < 1000) {
278+
uh.pop(); // get rid of last entry before pushing a new entry
279+
}
280+
}
281+
uh.push([args.myViz.curInstr, curTs]);
282+
}
283+
284+
return [false];
285+
}
286+
);
287+
288+
$('#executionSlider').slider('value', myVisualizer.curInstr); // update slider
242289
}
243290

291+
// a syntax-/compile-time error, rather than a runtime error
244292
function optliveHandleUncaughtExceptionFunc(trace) {
245293
if (trace.length == 1 && trace[0].line) {
246294
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
247295
if (errorLineNo !== undefined && errorLineNo != NaN) {
248296
removeAllGutterDecorations();
249297

250298
if (myVisualizer) {
251-
$("#dataViz,#curInstr").addClass('dimmed'); // dim the visualization until we fix the error
299+
toggleSyntaxError(true);
252300
myVisualizer.updateOutput(); // to update arrows
253301
}
254302

@@ -323,7 +371,6 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
323371
function execCallback(dataFromBackend) {
324372
var trace = dataFromBackend.trace;
325373

326-
// don't enter visualize mode if there are killer errors:
327374
if (!trace ||
328375
(trace.length == 0) ||
329376
(trace[trace.length - 1].event == 'uncaught_exception')) {
@@ -355,6 +402,8 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
355402
clearFrontendError();
356403
startExecutingCode();
357404

405+
setFronendError(['Running your code ...']);
406+
358407
jsonp_endpoint = null;
359408

360409
// hacky!
@@ -385,6 +434,18 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
385434
assert(false);
386435
}
387436

437+
// submit update history of the "previous" visualizer whenever you
438+
// run the code and hopefully get a new visualizer back
439+
//
440+
// don't bother if we're currently on a syntax error since the
441+
// displayed visualization is no longer relevant
442+
var prevUpdateHistoryJSON = undefined;
443+
if (myVisualizer && !hasSyntaxError) {
444+
var encodedUh = compressUpdateHistoryList(myVisualizer);
445+
prevUpdateHistoryJSON = JSON.stringify(encodedUh);
446+
console.log('prevUpdateHistoryJSON', prevUpdateHistoryJSON);
447+
}
448+
388449
if (backendScript === js_backend_script ||
389450
backendScript === ts_backend_script ||
390451
backendScript === java_backend_script ||
@@ -396,7 +457,9 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
396457
{user_script : codeToExec,
397458
options_json: JSON.stringify(backendOptionsObj),
398459
user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
399-
session_uuid: sessionUUID},
460+
session_uuid: sessionUUID,
461+
prevUpdateHistoryJSON: prevUpdateHistoryJSON,
462+
exeTime: new Date().getTime()},
400463
function(dat) {} /* don't do anything since this is a dummy call */, "text");
401464

402465
// the REAL call uses JSONP
@@ -418,7 +481,9 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
418481
raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '',
419482
options_json: JSON.stringify(backendOptionsObj),
420483
user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
421-
session_uuid: sessionUUID},
484+
session_uuid: sessionUUID,
485+
prevUpdateHistoryJSON: prevUpdateHistoryJSON,
486+
exeTime: new Date().getTime()},
422487
execCallback, "json");
423488
}
424489
}

0 commit comments

Comments
 (0)