Always use latest websocket connection for Turtle events

This commit is contained in:
Sebastian Serth
2020-03-07 23:24:33 +01:00
parent 0b9cf0bd45
commit 24b9148a42
2 changed files with 16 additions and 13 deletions

View File

@ -20,6 +20,7 @@ CodeOceanEditorWebsocket = {
this.resetOutputTab(); this.resetOutputTab();
}.bind(this) }.bind(this)
); );
CodeOceanEditorWebsocket.websocket = this.websocket;
this.websocket.onError(this.showWebsocketError.bind(this)); this.websocket.onError(this.showWebsocketError.bind(this));
}, },

View File

@ -7,7 +7,9 @@ var height;
var width; var width;
var devicePixelRatio = window.devicePixelRatio || 1; var devicePixelRatio = window.devicePixelRatio || 1;
function Turtle(pipe, canvas) { // The `unused_pipe_reference` might get outdated. Somehow...
// Hence, we use the `CodeOceanEditorWebsocket.websocket`
function Turtle(unused_pipe_reference, canvas) {
var dx, dy, xpos, ypos; var dx, dy, xpos, ypos;
this.canvas = canvas; // jQuery object this.canvas = canvas; // jQuery object
@ -15,13 +17,13 @@ function Turtle(pipe, canvas) {
this.canvas.off('click'); this.canvas.off('click');
let sendEvent = function (x, y) { let sendEvent = function (x, y) {
pipe.send(JSON.stringify({ CodeOceanEditorWebsocket.websocket.send(JSON.stringify({
'cmd': 'canvasevent', 'cmd': 'canvasevent',
'type': '<Button-1>', 'type': '<Button-1>',
'x': x, 'x': x,
'y': y 'y': y
})); }));
pipe.send('\n'); CodeOceanEditorWebsocket.websocket.send('\n');
}; };
$(document).keydown(function(e) { $(document).keydown(function(e) {
@ -116,21 +118,21 @@ Turtle.prototype.update = function () {
break; break;
} }
} }
} };
Turtle.prototype.get_width = function () { Turtle.prototype.get_width = function () {
if (width === undefined) { if (width === undefined) {
width = this.canvas[0].width; width = this.canvas[0].width;
} }
return width; return width;
} };
Turtle.prototype.get_height = function () { Turtle.prototype.get_height = function () {
if (height === undefined) { if (height === undefined) {
height = this.canvas[0].height; height = this.canvas[0].height;
} }
return height; return height;
} };
Turtle.prototype.delete = function (item) { Turtle.prototype.delete = function (item) {
if (item == 'all') { if (item == 'all') {
@ -138,12 +140,12 @@ Turtle.prototype.delete = function (item) {
} else { } else {
delete this.items[item]; delete this.items[item];
} }
} };
Turtle.prototype.create_image = function (image) { Turtle.prototype.create_image = function (image) {
this.items.push({type:'image',image:image}); this.items.push({type:'image',image:image});
return this.items.length - 1; return this.items.length - 1;
} };
Turtle.prototype.create_line = function () { Turtle.prototype.create_line = function () {
this.items.push({type:'line', this.items.push({type:'line',
@ -152,7 +154,7 @@ Turtle.prototype.create_line = function () {
width:2, width:2,
capstyle:'round'}); capstyle:'round'});
return this.items.length - 1; return this.items.length - 1;
} };
Turtle.prototype.create_polygon = function () { Turtle.prototype.create_polygon = function () {
this.items.push({type:'polygon', this.items.push({type:'polygon',
@ -161,7 +163,7 @@ Turtle.prototype.create_polygon = function () {
coords:[0,0,0,0,0,0] coords:[0,0,0,0,0,0]
}); });
return this.items.length - 1; return this.items.length - 1;
} };
// XXX might make this varargs as in Tkinter // XXX might make this varargs as in Tkinter
Turtle.prototype.coords = function (item, coords) { Turtle.prototype.coords = function (item, coords) {
@ -169,11 +171,11 @@ Turtle.prototype.coords = function (item, coords) {
return this.items[item].coords; return this.items[item].coords;
} }
this.items[item].coords = coords; this.items[item].coords = coords;
} };
Turtle.prototype.itemconfigure = function (item, key, value) { Turtle.prototype.itemconfigure = function (item, key, value) {
this.items[item][key] = value; this.items[item][key] = value;
} };
// value might be undefined // value might be undefined
Turtle.prototype.css = function (key, value) { Turtle.prototype.css = function (key, value) {
@ -183,7 +185,7 @@ Turtle.prototype.css = function (key, value) {
// jQuery return value is confusing when the css is set // jQuery return value is confusing when the css is set
this.canvas.css(key, value); this.canvas.css(key, value);
} }
} };
function run(launchmsg) { function run(launchmsg) {
var i, turtlescreen, msg, result, cmd; var i, turtlescreen, msg, result, cmd;