Skip to content

Variables improvement #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions template.html
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,11 @@
const noop = () => null;
let cloudHost = {CLOUD_HOST};
let ws;
let connectionAttempts = 0;
let queuedData = {};
window.queuedData = queuedData;
function openConnection() {
connectionAttempts++;
try {
ws = new WebSocket(cloudHost);
} catch (err) {
Expand All @@ -667,13 +671,28 @@
function sendData(data) {
data.user = DESIRED_USERNAME;
data.project_id = PROJECT_ID;
ws.send(JSON.stringify(data) + '\n');
// https://github.com/LLK/scratch-gui/blob/develop/src/lib/cloud-provider.js#L141-L148
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data) + '\n');
} else {
// This way, multiple changes to the same variable only are changed once
queuedData[`${data.method}:${data.name}`] = data;
}
}
function onOpen() {
connectionAttempts = 1;
sendData({method: 'handshake'});
// https://github.com/LLK/scratch-gui/blob/develop/src/lib/cloud-provider.js#L79-L85
for (const data of Object.values(queuedData)) {
ws.send(JSON.stringify(data) + '\n');
}
queuedData = [];
}
function onClose() {
setTimeout(openConnection, 500);
// https://github.com/LLK/scratch-gui/blob/develop/src/lib/cloud-provider.js#L90-L100
const randomizedTimeout = Math.random() * (Math.pow(2, Math.min(connectionAttempts, 5)) - 1) * 1000;
setTimeout(openConnection, randomizedTimeout);
console.log(`Reconnecting in ${(randomizedTimeout / 1000).toFixed(1)}s (attempt #${connectionAttempts})`);
}
% special-cloud %
function postError(err) {
Expand Down