Skip to content

Commit e981128

Browse files
committed
more adapting of try_hook into ExecutionVisualizer
1 parent 7f8cefa commit e981128

2 files changed

Lines changed: 40 additions & 51 deletions

File tree

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

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -306,42 +306,6 @@ function initTogetherJS() {
306306
}
307307
});
308308

309-
// Global hook for ExecutionVisualizer.
310-
// Set it here after TogetherJS gets defined, hopefully!
311-
try_hook = function(hook_name, args) {
312-
if (hook_name == "end_updateOutput") {
313-
if (updateOutputSignalFromRemote) {
314-
return;
315-
}
316-
if (TogetherJS.running && !isExecutingCode) {
317-
TogetherJS.send({type: "updateOutput", step: args.myViz.curInstr});
318-
}
319-
320-
// debounce to compress a bit ... 250ms feels "right"
321-
$.doTimeout('updateOutputLogEvent', 250, function() {
322-
logEvent({type: 'updateOutput', step: args.myViz.curInstr});
323-
});
324-
325-
// 2014-05-25: implemented more detailed tracing for surveys
326-
if (args.myViz.creationTime) {
327-
var deltaMs = (new Date()) - args.myViz.creationTime;
328-
329-
var uh = args.myViz.updateHistory;
330-
assert(uh.length > 0); // should already be seeded with an initial value
331-
var lastDeltaMs = uh[uh.length - 1][1];
332-
333-
// ("debounce" entries that are less than 1 second apart to
334-
// compress the logs a bit when there's rapid scrubbing or scrolling)
335-
if ((deltaMs - lastDeltaMs) < 1000) {
336-
uh.pop(); // get rid of last entry before pushing a new entry
337-
}
338-
uh.push([args.myViz.curInstr, deltaMs]);
339-
//console.log(JSON.stringify(uh));
340-
}
341-
}
342-
return [false];
343-
}
344-
345309
TogetherJS.hub.on("updateOutput", function(msg) {
346310
// do NOT use a msg.sameUrl guard since that will miss some signals
347311
// due to our funky URLs
@@ -1222,6 +1186,38 @@ function executePythonCode(pythonSourceCode,
12221186
myVisualizer = new HolisticVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
12231187
} else {
12241188
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
1189+
1190+
myVisualizer.add_pytutor_hook("end_updateOutput", function(args) {
1191+
if (updateOutputSignalFromRemote) {
1192+
return;
1193+
}
1194+
if (TogetherJS.running && !isExecutingCode) {
1195+
TogetherJS.send({type: "updateOutput", step: args.myViz.curInstr});
1196+
}
1197+
1198+
// debounce to compress a bit ... 250ms feels "right"
1199+
$.doTimeout('updateOutputLogEvent', 250, function() {
1200+
logEvent({type: 'updateOutput', step: args.myViz.curInstr});
1201+
});
1202+
1203+
// 2014-05-25: implemented more detailed tracing for surveys
1204+
if (args.myViz.creationTime) {
1205+
var deltaMs = (new Date()) - args.myViz.creationTime;
1206+
1207+
var uh = args.myViz.updateHistory;
1208+
assert(uh.length > 0); // should already be seeded with an initial value
1209+
var lastDeltaMs = uh[uh.length - 1][1];
1210+
1211+
// ("debounce" entries that are less than 1 second apart to
1212+
// compress the logs a bit when there's rapid scrubbing or scrolling)
1213+
if ((deltaMs - lastDeltaMs) < 1000) {
1214+
uh.pop(); // get rid of last entry before pushing a new entry
1215+
}
1216+
uh.push([args.myViz.curInstr, deltaMs]);
1217+
//console.log(JSON.stringify(uh));
1218+
}
1219+
return [false]; // pass through to let other hooks keep handling
1220+
});
12251221
}
12261222
// SUPER HACK -- slip in backendOptionsObj as an extra field
12271223
myVisualizer.backendOptionsObj = backendOptionsObj;

‎v3/js/pytutor.js‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6363
6464
*/
6565

66-
// include hooks.js after you include pytutor.js, if you want to use hooks
67-
// see hooks.js for more instructions. (hooking code created by David Pritchard)
68-
var try_hook = function(hook_name, args) {
69-
return [false]; // just a stub
70-
}
71-
7266
var SVG_ARROW_POLYGON = '0,3 12,3 12,0 18,5 12,10 12,7 0,7';
7367
var SVG_ARROW_HEIGHT = 10; // must match height of SVG_ARROW_POLYGON
7468

@@ -252,7 +246,7 @@ function ExecutionVisualizer(domRootID, dat, params) {
252246
// API for adding a hook, created by David Pritchard
253247
this.pytutor_hooks = {}; // keys, hook names; values, list of functions
254248

255-
try_hook("end_constructor", {myViz:this});
249+
this.try_hook("end_constructor", {myViz:this});
256250

257251
this.hasRendered = false;
258252

@@ -724,7 +718,7 @@ ExecutionVisualizer.prototype.render = function() {
724718
this.domRoot.find('#vizLayoutTdFirst').hide(); // gigantic hack!
725719
}
726720

727-
try_hook("end_render", {myViz:this});
721+
this.try_hook("end_render", {myViz:this});
728722

729723
this.precomputeCurTraceLayouts();
730724

@@ -1404,7 +1398,7 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
14041398
else {
14051399
this.updateOutputFull(smoothTransition);
14061400
}
1407-
try_hook("end_updateOutput", {myViz:this});
1401+
this.try_hook("end_updateOutput", {myViz:this});
14081402
}
14091403

14101404
ExecutionVisualizer.prototype.updateOutputFull = function(smoothTransition) {
@@ -2005,7 +1999,7 @@ ExecutionVisualizer.prototype.precomputeCurTraceLayouts = function() {
20051999
}
20062000

20072001
function isLinearObj(heapObj) {
2008-
var hook_result = try_hook("isLinearObj", {heapObj:heapObj});
2002+
var hook_result = myViz.try_hook("isLinearObj", {heapObj:heapObj});
20092003
if (hook_result[0]) return hook_result[1];
20102004

20112005
return heapObj[0] == 'LIST' || heapObj[0] == 'TUPLE' || heapObj[0] == 'SET';
@@ -3005,8 +2999,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function(curEntry, curTople
30052999
highlight_frame(myViz.generateID('globals'));
30063000
}
30073001

3008-
try_hook("end_renderDataStructures", {myViz:myViz});
3009-
3002+
myViz.try_hook("end_renderDataStructures", {myViz:myViz});
30103003
}
30113004

30123005

@@ -3185,7 +3178,7 @@ ExecutionVisualizer.prototype.renderTabularView = function() {
31853178
// rendering functions, which all take a d3 dom element to anchor the
31863179
// new element to render
31873180
ExecutionVisualizer.prototype.renderPrimitiveObject = function(obj, d3DomElement) {
3188-
if (try_hook("renderPrimitiveObject", {obj:obj, d3DomElement:d3DomElement})[0])
3181+
if (this.try_hook("renderPrimitiveObject", {obj:obj, d3DomElement:d3DomElement})[0])
31893182
return;
31903183

31913184
var typ = typeof obj;
@@ -3300,7 +3293,7 @@ function(objID, stepNum, d3DomElement, isTopLevel) {
33003293
typeLabelPrefix = 'id' + objID + ':';
33013294
}
33023295

3303-
var hook_result = try_hook("renderCompoundObject",
3296+
var hook_result = myViz.try_hook("renderCompoundObject",
33043297
{objID:objID, d3DomElement:d3DomElement,
33053298
isTopLevel:isTopLevel, obj:obj,
33063299
typeLabelPrefix:typeLabelPrefix,
@@ -3745,7 +3738,7 @@ ExecutionVisualizer.prototype.structurallyEquivalent = function(obj1, obj2) {
37453738

37463739

37473740
ExecutionVisualizer.prototype.isPrimitiveType = function(obj) {
3748-
var hook_result = try_hook("isPrimitiveType", {obj:obj});
3741+
var hook_result = this.try_hook("isPrimitiveType", {obj:obj});
37493742
if (hook_result[0]) return hook_result[1];
37503743

37513744
// null is a primitive

0 commit comments

Comments
 (0)