mirror of
https://github.com/slynn1324/tinypin.git
synced 2026-01-23 02:25:08 +00:00
fixed up visibility change detection
This commit is contained in:
parent
020aadf509
commit
261f4dca4a
4 changed files with 54 additions and 22 deletions
|
|
@ -134,6 +134,9 @@ app.ws('/ws/:uid', (ws, req) => {
|
|||
ws.on("message", (msg) => {
|
||||
//console.log("received messsage: " + msg);
|
||||
});
|
||||
ws.on("close", () => {
|
||||
console.log("socket closed for user " + req.params.uid);
|
||||
});
|
||||
console.log("socket opened for user " + req.params.uid);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -278,9 +278,25 @@ store.do('hash.update');
|
|||
appComponent.render();
|
||||
|
||||
|
||||
// refresh on focus
|
||||
window.addEventListener("focus", () => {
|
||||
store.do("load.boards");
|
||||
store.do("load.board");
|
||||
dispatchSocketConnect();
|
||||
// refresh on load.
|
||||
window.lastVisibilityChange = new Date().getTime();
|
||||
document.addEventListener("visibilitychange", async () => {
|
||||
|
||||
let now = new Date().getTime();
|
||||
|
||||
// only run if we haven't run in the last second.. prevent double updates
|
||||
if ( document.visibilityState === 'visible' && (now - window.lastVisibilityChange) > 1000) {
|
||||
window.lastVisibilityChange = now;
|
||||
|
||||
let connected = false;
|
||||
if ( dispatchSocketConnect ){ // maybe we stripped out web sockets
|
||||
connected = await dispatchSocketConnect();
|
||||
}
|
||||
|
||||
if ( !connected ){
|
||||
store.do("load.boards");
|
||||
store.do("load.board");
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
|
@ -61,6 +61,21 @@ function getPinById(id){
|
|||
return store.data.board.pins[getPinIndexById(id)];
|
||||
}
|
||||
|
||||
async function sleep(ms){ return new Promise((resolve) => setTimeout(resolve, ms)); }
|
||||
|
||||
// async function runAfter(f, ms){
|
||||
// return new Promise( (resolve,reject) => {
|
||||
// setTimeout( () => {
|
||||
// try {
|
||||
// await f();
|
||||
// resolve();
|
||||
// } catch (e){
|
||||
// reject(e);
|
||||
// }
|
||||
// }, ms);
|
||||
// });
|
||||
// }
|
||||
|
||||
// feature detection
|
||||
if ( 'ontouchstart' in window ){
|
||||
window.isTouch = true;
|
||||
|
|
|
|||
32
static/ws.js
32
static/ws.js
|
|
@ -23,39 +23,37 @@ window.addEventListener("socket-connect", () => {
|
|||
socketConnect();
|
||||
});
|
||||
|
||||
function socketConnect(){
|
||||
async function socketConnect(){
|
||||
|
||||
if ( !window.uid ){
|
||||
console.log("no user id, can't open a socket");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( window.socketConnected ){
|
||||
console.log("web socket already connected");
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
window.socketConnected = false;
|
||||
|
||||
let s = new WebSocket(getSocketUrl());
|
||||
|
||||
s.onopen = (e) => {
|
||||
s.onopen = async (e) => {
|
||||
console.log("web socket connected");
|
||||
|
||||
// wait 10ms to see if the socket stays connected
|
||||
setTimeout( () => {
|
||||
if ( s.readyState == WebSocket.OPEN ){
|
||||
console.log("web socket appears operational");
|
||||
document.body.classList.add("socketConnected");
|
||||
window.socketConnected = true;
|
||||
window.socketConnectFailureCount = 0;
|
||||
// wait 50ms to see if the socket stays connected
|
||||
await sleep(50);
|
||||
|
||||
store.do("load.boards");
|
||||
store.do("load.board");
|
||||
} else {
|
||||
console.log("web socket connect failed");
|
||||
}
|
||||
}, 10);
|
||||
if ( s.readyState == WebSocket.OPEN ){
|
||||
console.log("web socket appears operational");
|
||||
document.body.classList.add("socketConnected");
|
||||
window.socketConnected = true;
|
||||
return true;
|
||||
} else {
|
||||
console.log("web socket connect failed");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
s.onmessage = (e) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue