Skip to content

Commit 6dea32d

Browse files
committed
more syncing action ... ah my head is spinning!
1 parent 0cef7a9 commit 6dea32d

2 files changed

Lines changed: 106 additions & 34 deletions

File tree

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,27 @@ function updateAppDisplay() {
135135
}
136136
}
137137
else if (appMode == 'display' || appMode == 'visualize' /* 'visualize' is deprecated */) {
138-
$("#pyInputPane").hide();
139-
$("#pyOutputPane,#surveyHeader").show();
140-
$("#embedLinkDiv").show();
141-
142-
$('#executeBtn').html("Visualize Execution");
143-
$('#executeBtn').attr('disabled', false);
144-
145-
146-
// do this AFTER making #pyOutputPane visible, or else
147-
// jsPlumb connectors won't render properly
148-
myVisualizer.updateOutput();
149-
150-
// customize edit button click functionality AFTER rendering (NB: awkward!)
151-
$('#pyOutputPane #editCodeLinkDiv').show();
152-
$('#pyOutputPane #editBtn').click(function() {
153-
enterEditMode();
154-
});
138+
if (!myVisualizer) {
139+
enterEditMode(); // if there's no visualizer, switch back to edit mode
140+
}
141+
else {
142+
$("#pyInputPane").hide();
143+
$("#pyOutputPane,#surveyHeader").show();
144+
$("#embedLinkDiv").show();
145+
146+
$('#executeBtn').html("Visualize Execution");
147+
$('#executeBtn').attr('disabled', false);
148+
149+
// do this AFTER making #pyOutputPane visible, or else
150+
// jsPlumb connectors won't render properly
151+
myVisualizer.updateOutput();
152+
153+
// customize edit button click functionality AFTER rendering (NB: awkward!)
154+
$('#pyOutputPane #editCodeLinkDiv').show();
155+
$('#pyOutputPane #editBtn').click(function() {
156+
enterEditMode();
157+
});
158+
}
155159
}
156160
else if (appMode == 'display_no_frills') {
157161
$("#pyInputPane").hide();

‎v3/js/opt-frontend.js‎

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ var TogetherJSConfig_seenIntroDialog = true;
6262
var TogetherJSConfig_suppressInvite = true;
6363
var TogetherJSConfig_suppressJoinConfirmation = true;
6464

65-
// only clone clicks in the input pane to keep things simple and prevent
66-
// duplicate clicks on, say, the visualizer pane
67-
var TogetherJSConfig_cloneClicks = '#pyInputPane';
65+
// clone clicks ONLY in certain elements to keep things simple:
66+
var TogetherJSConfig_cloneClicks = '#pyInputPane select,#pyInputPane #executeBtn';
6867

6968
var TogetherJSConfig_siteName = "Online Python Tutor live help";
7069
var TogetherJSConfig_toolName = "Online Python Tutor live help";
7170

71+
var isTutor = false;
7272

7373
// TODO: consider deferred initialization later: "TogetherJS starts up
7474
// automatically as soon as it can, especially when continuing a
@@ -82,6 +82,7 @@ var TogetherJSConfig_toolName = "Online Python Tutor live help";
8282
// nasty globals
8383
var updateOutputSignalFromRemote = false;
8484
var hashchangeSignalFromRemote = false;
85+
var nowSyncing = false;
8586

8687
// Global hook for ExecutionVisualizer.
8788
var try_hook = function(hook_name, args) {
@@ -99,10 +100,25 @@ function getAppState() {
99100
textReferences: $('#textualMemoryLabelsSelector').val(),
100101
showOnlyOutputs: $('#showOnlyOutputsSelector').val(),
101102
py: $('#pythonVersionSelector').val(),
103+
curInstr: myVisualizer ? myVisualizer.curInstr : undefined,
102104
// the app state that led to the CURRENT visualization
103105
visualizedAppState: visualizedAppState};
104106
}
105107

108+
// sets the proper GUI features to match the given appState object
109+
function setVisibleAppState(appState) {
110+
setCodeMirrorVal(appState.code);
111+
112+
$('#cumulativeModeSelector').val(appState.cumulative);
113+
$('#heapPrimitivesSelector').val(appState.heapPrimitives);
114+
$('#drawParentPointerSelector').val(appState.drawParentPointers);
115+
$('#textualMemoryLabelsSelector').val(appState.textReferences);
116+
$('#showOnlyOutputsSelector').val(appState.showOnlyOutputs);
117+
$('#pythonVersionSelector').val(appState.py);
118+
119+
// TODO: what to do about mode and curInstr?
120+
}
121+
106122

107123
$(document).ready(function() {
108124
setSurveyHTML();
@@ -125,14 +141,20 @@ $(document).ready(function() {
125141
$("#surveyHeader").remove(); // kill this to save space
126142

127143
var role = $.bbq.getState('role');
128-
var isTutor = (role == 'tutor');
144+
isTutor = (role == 'tutor'); // GLOBAL
145+
129146
if (isTutor) {
130147
$("#togetherBtn").html("TUTOR - Join live help session");
131148
$("#togetherJSHeader").append('<button id="syncBtn"\
132149
type="button">Sync with learner</button>');
133150

134151
$("#syncBtn").click(function() {
135152
console.log("Sync!");
153+
154+
if (TogetherJS.running) {
155+
nowSyncing = true;
156+
TogetherJS.send({type: "requestSync"});
157+
}
136158
});
137159
}
138160

@@ -144,7 +166,7 @@ $(document).ready(function() {
144166
if (updateOutputSignalFromRemote) {
145167
return;
146168
}
147-
if (TogetherJS.running) {
169+
if (TogetherJS.running && !nowSyncing /* don't perterb when syncing */) {
148170
TogetherJS.send({type: "updateOutput", step: args.myViz.curInstr});
149171
}
150172
}
@@ -174,6 +196,7 @@ $(document).ready(function() {
174196
}
175197
hashchangeSignalFromRemote = true;
176198
try {
199+
console.log("TogetherJS RECEIVE hashchange", msg.appMode);
177200
appMode = msg.appMode; // assign this to the GLOBAL appMode
178201
updateAppDisplay();
179202
}
@@ -182,6 +205,52 @@ $(document).ready(function() {
182205
}
183206
});
184207

208+
// learner receives a sync request from tutor and responds by
209+
// sending its current app state
210+
TogetherJS.hub.on("requestSync", function(msg) {
211+
// only a learner should receive sync requests from a tutor
212+
if (isTutor) {
213+
return;
214+
}
215+
216+
if (TogetherJS.running) {
217+
TogetherJS.send({type: "myAppState", myAppState: getAppState()});
218+
}
219+
});
220+
221+
// tutor receives an app state from a learner
222+
// TODO: what happens if more than one learner sends state to tutor?
223+
// i suppose the last one wins at this point :/
224+
TogetherJS.hub.on("myAppState", function(msg) {
225+
// only a tutor should handle receiving an app state from a learner
226+
if (!isTutor) {
227+
return;
228+
}
229+
230+
try {
231+
var learnerAppState = msg.myAppState;
232+
233+
// if available, first set my own app state to visualizedAppState
234+
// and then EXECUTE that code with the given options to create the
235+
// proper myVisualizer object. then adjust app state to the rest
236+
// of learnerAppState
237+
238+
if (learnerAppState.visualizedAppState) {
239+
setVisibleAppState(learnerAppState.visualizedAppState);
240+
// execute code and jump to the learner's curInstr
241+
executeCode(learnerAppState.curInstr);
242+
}
243+
244+
setVisibleAppState(learnerAppState);
245+
246+
appMode = learnerAppState.mode;
247+
updateAppDisplay();
248+
}
249+
finally {
250+
nowSyncing = false; // done with a successful sync round trip
251+
}
252+
});
253+
185254

186255
try {
187256
var source = new EventSource('http://togetherjs.pythontutor.com/learner-SSE');
@@ -227,6 +296,8 @@ $(document).ready(function() {
227296
console.log("TogetherJS ready");
228297
$("#togetherBtn").html("Stop live help session");
229298

299+
$("#togetherJSHeader").fadeIn(750, redrawConnectors); // always show when ready
300+
230301
if (!isTutor) {
231302
$.get("http://togetherjs.pythontutor.com/request-help",
232303
{url: TogetherJS.shareUrl()},
@@ -259,6 +330,7 @@ $(document).ready(function() {
259330
if (enableTogetherJS) {
260331
if (TogetherJS.running &&
261332
!hashchangeSignalFromRemote /* don't double send */) {
333+
console.log("TogetherJS SEND hashchange", appMode);
262334
TogetherJS.send({type: "hashchange", appMode: appMode});
263335
}
264336
}
@@ -354,19 +426,15 @@ $(document).ready(function() {
354426
}
355427

356428
function executeCodeFromScratch() {
357-
// prevent the weird double-execute situation where someone (such as
358-
// a remote collaborator) "clicks" executeBtn while it's disabled
359-
if (!$("#executeBtn").attr('disabled')) {
360-
// don't execute empty string:
361-
if ($.trim(pyInputCodeMirror.getValue()) == '') {
362-
alert('Type in some code to visualize.');
363-
return;
364-
}
365-
366-
// reset these globals
367-
rawInputLst = [];
368-
executeCode();
429+
// don't execute empty string:
430+
if ($.trim(pyInputCodeMirror.getValue()) == '') {
431+
alert('Type in some code to visualize.');
432+
return;
369433
}
434+
435+
// reset these globals
436+
rawInputLst = [];
437+
executeCode();
370438
}
371439

372440
function executeCodeWithRawInput(rawInputStr, curInstr) {

0 commit comments

Comments
 (0)