Published
Edited
Jul 14, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable something_count = NaN
Insert cell
something_counter = 'something'
Insert cell
something_increment = {
let queue = [];
let socket;
let timeout;
let delay = 250;

(function open() {
socket = new WebSocket(`wss://${gateway}.execute-api.us-east-1.amazonaws.com/prod?id=${encodeURIComponent(something_counter)}`);
socket.onopen = () => {
delay = 250;
queue.forEach(message => socket.send(message));
queue = null;
};
socket.onclose = () => {
delay *= 2;
timeout = setTimeout(open, delay + Math.random() * delay);
queue = [];
};
socket.onmessage = event => {
const data = JSON.parse(event.data);
if (isNaN(mutable something_count) || data.count > mutable something_count) {
mutable something_count = data.count;
}
};
})();

invalidation.then(() => {
socket.onclose = null;
socket.close();
clearTimeout(timeout);
});

return () => {
const message = JSON.stringify({type: "increment"});
if (queue) queue.push(message);
else socket.send(message);
++mutable something_count;
};
}
Insert cell
Insert cell
mutable example_count = NaN
Insert cell
example_counter = 'example'
Insert cell
example_increment = {
let queue = [];
let socket;
let timeout;
let delay = 250;

(function open() {
socket = new WebSocket(`wss://${gateway}.execute-api.us-east-1.amazonaws.com/prod?id=${encodeURIComponent(example_counter)}`);
socket.onopen = () => {
delay = 250;
queue.forEach(message => socket.send(message));
queue = null;
};
socket.onclose = () => {
delay *= 2;
timeout = setTimeout(open, delay + Math.random() * delay);
queue = [];
};
socket.onmessage = event => {
const data = JSON.parse(event.data);
if (isNaN(mutable example_count) || data.count > mutable example_count) {
mutable example_count = data.count;
}
};
})();

invalidation.then(() => {
socket.onclose = null;
socket.close();
clearTimeout(timeout);
});

return () => {
const message = JSON.stringify({type: "increment"});
if (queue) queue.push(message);
else socket.send(message);
++mutable example_count;
};
}
Insert cell
Insert cell
gateway = "im8slzlq50" // Replace this with your Gateway identifier.
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more