From f5dd70a6f32ac36f0820badc170832f27242a09d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Tue, 26 Sep 2017 17:31:26 +0200 Subject: [PATCH] Fix strange behavior with loading screens showing when they should not etc --- js/term/connection.js | 15 ++++++++++----- js/term/index.js | 36 +++++++++++++++++++++++++----------- js/term/screen.js | 9 +++++++++ js/term/screen_parser.js | 2 +- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/js/term/connection.js b/js/term/connection.js index 7b799ca..8e8f7be 100644 --- a/js/term/connection.js +++ b/js/term/connection.js @@ -19,12 +19,18 @@ module.exports = class TermConnection extends EventEmitter { this.pageShown = false + this.disconnectTimeout = null + document.addEventListener('visibilitychange', () => { if (document.hidden === true) { console.info('Window lost focus, freeing socket') - this.closeSocket() - clearTimeout(this.heartbeatTimeout) + // Delayed, avoid disconnecting if the background time is short + this.disconnectTimeout = setTimeout(() => { + this.closeSocket() + clearTimeout(this.heartbeatTimeout) + }, 1000) } else { + clearTimeout(this.disconnectTimeout) console.info('Window got focus, re-connecting') this.init() } @@ -80,7 +86,6 @@ module.exports = class TermConnection extends EventEmitter { break default: - this.emit('load') this.screen.load(evt.data) if (!this.pageShown) { window.showPage() @@ -118,7 +123,7 @@ module.exports = class TermConnection extends EventEmitter { console.error('Socket not ready') return false } - if (typeof message != 'string') { + if (typeof message !== 'string') { message = JSON.stringify(message) } this.ws.send(message) @@ -161,7 +166,7 @@ module.exports = class TermConnection extends EventEmitter { heartbeat () { clearTimeout(this.heartbeatTimeout) - this.heartbeatTimeout = setTimeout(() => this.onHeartbeatFail(), 2000) + this.heartbeatTimeout = setTimeout(() => this.onHeartbeatFail(), 2500) } onHeartbeatFail () { diff --git a/js/term/index.js b/js/term/index.js index 3a7a6c1..41593ac 100644 --- a/js/term/index.js +++ b/js/term/index.js @@ -14,33 +14,47 @@ module.exports = function (opts) { const input = TermInput(conn, screen) const termUpload = TermUpload(conn, input, screen) screen.input = input + screen.conn = conn input.termUpload = termUpload - // we delay the display of "connecting" to avoid flash when changing tabs with the terminal open - let showConnectingTimeout = -1 + let showSplashTimeout = null + let showSplash = (obj, delay = 250) => { + clearTimeout(showSplashTimeout) + showSplashTimeout = setTimeout(() => { + screen.window.statusScreen = obj + }, delay) + } + conn.on('open', () => { - showConnectingTimeout = setTimeout(() => { - screen.window.statusScreen = { title: 'Connecting', loading: true } - }, 250) + // console.log('*open') + showSplash({ title: 'Connecting', loading: true }) }) conn.on('connect', () => { - clearTimeout(showConnectingTimeout) - screen.window.statusScreen = { title: 'Waiting for content', loading: true } + // console.log('*connect') + showSplash({ title: 'Waiting for content', loading: true }) }) conn.on('load', () => { + // console.log('*load') + clearTimeout(showSplashTimeout) if (screen.window.statusScreen) screen.window.statusScreen = null }) conn.on('disconnect', () => { - clearTimeout(showConnectingTimeout) - screen.window.statusScreen = { title: 'Disconnected' } + // console.log('*disconnect') + showSplash({ title: 'Disconnected' }) screen.screen = [] screen.screenFG = [] screen.screenBG = [] screen.screenAttrs = [] }) - conn.on('silence', () => { screen.window.statusScreen = { title: 'Waiting for server', loading: true } }) + conn.on('silence', () => { + // console.log('*silence') + showSplash({ title: 'Waiting for server', loading: true }, 0) + }) // conn.on('ping-fail', () => { screen.window.statusScreen = { title: 'Disconnected' } }) - conn.on('ping-success', () => { screen.window.statusScreen = { title: 'Re-connecting', loading: true } }) + conn.on('ping-success', () => { + // console.log('*ping-success') + showSplash({ title: 'Re-connecting', loading: true }, 0) + }) conn.init() input.init(opts) diff --git a/js/term/screen.js b/js/term/screen.js index e71a7e3..b24ce98 100644 --- a/js/term/screen.js +++ b/js/term/screen.js @@ -30,6 +30,15 @@ module.exports = class TermScreen extends EventEmitter { return () => console.warn('TermScreen#input not set!') } }) + // dummy. Handle for Conn + this.conn = new Proxy({}, { + get () { + return () => console.warn('TermScreen#conn not set!') + }, + set (a, b) { + return () => console.warn('TermScreen#conn not set!') + } + }) this.cursor = { x: 0, diff --git a/js/term/screen_parser.js b/js/term/screen_parser.js index 9e83e12..c210e00 100644 --- a/js/term/screen_parser.js +++ b/js/term/screen_parser.js @@ -199,7 +199,7 @@ module.exports = class ScreenParser { if (this.screen.window.debug) console.log(`Blinky cells: ${this.screen.blinkingCellCount}`) this.screen.renderer.scheduleDraw('load', 16) - this.screen.emit('load') + this.screen.conn.emit('load') } /**