mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Port the autoreload script to RapydScript
This commit is contained in:
parent
79f47bfd8a
commit
00147c8e6c
@ -1,55 +0,0 @@
|
|||||||
/* vim:fileencoding=utf-8
|
|
||||||
*
|
|
||||||
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
|
|
||||||
*
|
|
||||||
* Distributed under terms of the GPLv3 license
|
|
||||||
*/
|
|
||||||
|
|
||||||
(function(autoreload_port) {
|
|
||||||
"use strict";
|
|
||||||
var host = document.location.host.split(':')[0];
|
|
||||||
var url = 'ws://' + host + ':' + autoreload_port;
|
|
||||||
var MAX_RETRIES = 10;
|
|
||||||
|
|
||||||
function ReconnectingWebSocket() {
|
|
||||||
self = this;
|
|
||||||
self.retries = 0;
|
|
||||||
self.interval = 100;
|
|
||||||
self.disable = false;
|
|
||||||
self.opened_at_least_once = false;
|
|
||||||
|
|
||||||
self.reconnect = function() {
|
|
||||||
self.ws = new WebSocket(url);
|
|
||||||
|
|
||||||
self.ws.onopen = function(event) {
|
|
||||||
self.retries = 0;
|
|
||||||
self.opened_at_least_once = true;
|
|
||||||
self.interval = 100;
|
|
||||||
console.log('Connected to reloading WebSocket server at: ' + url);
|
|
||||||
window.addEventListener('beforeunload', function (event) {
|
|
||||||
console.log('Shutting down connection to reload server, before page unload');
|
|
||||||
self.disable = true;
|
|
||||||
self.ws.close();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
self.ws.onmessage = function(event) {
|
|
||||||
if (event.data !== 'ping') console.log('Received mesasge from reload server: ' + event.data);
|
|
||||||
if (event.data === 'reload') window.location.reload(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
self.ws.onclose = function(event) {
|
|
||||||
if (self.disable || !self.opened_at_least_once) return;
|
|
||||||
console.log('Connection to reload server closed with code: ' + event.code + ' and reason: ' + event.reason);
|
|
||||||
self.retries += 1;
|
|
||||||
if (self.retries < MAX_RETRIES) {
|
|
||||||
setTimeout(self.reconnect, self.interval);
|
|
||||||
} else window.location.reload(true);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
self.reconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
var sock = new ReconnectingWebSocket();
|
|
||||||
})(AUTORELOAD_PORT);
|
|
||||||
|
|
@ -28,12 +28,16 @@ def index(ctx, rd):
|
|||||||
return lopen(P('content-server/index-generated.html'), 'rb')
|
return lopen(P('content-server/index-generated.html'), 'rb')
|
||||||
|
|
||||||
|
|
||||||
@endpoint('/auto-reload', auth_required=False)
|
@endpoint('/calibre.appcache', auth_required=False, cache_control='no-cache')
|
||||||
|
def appcache(ctx, rd):
|
||||||
|
return lopen(P('content-server/calibre.appcache'), 'rb')
|
||||||
|
|
||||||
|
|
||||||
|
@endpoint('/auto-reload-port', auth_required=False, cache_control='no-cache')
|
||||||
def auto_reload(ctx, rd):
|
def auto_reload(ctx, rd):
|
||||||
auto_reload_port = getattr(rd.opts, 'auto_reload_port', 0)
|
auto_reload_port = getattr(rd.opts, 'auto_reload_port', 0)
|
||||||
if auto_reload_port > 0:
|
rd.outheaders.set('Content-Type', 'text/plain')
|
||||||
rd.outheaders.set('Calibre-Auto-Reload-Port', type('')(auto_reload_port), replace_all=True)
|
return str(max(0, auto_reload_port))
|
||||||
return lopen(P('content-server/autoreload.js'), 'rb')
|
|
||||||
|
|
||||||
|
|
||||||
@endpoint('/console-print', methods=('POST',))
|
@endpoint('/console-print', methods=('POST',))
|
||||||
|
55
src/pyj/autoreload.pyj
Normal file
55
src/pyj/autoreload.pyj
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
from __python__ import bound_methods, hash_literals
|
||||||
|
|
||||||
|
MAX_RETRIES = 10
|
||||||
|
|
||||||
|
class Watcher:
|
||||||
|
|
||||||
|
def __init__(self, autoreload_port):
|
||||||
|
host = document.location.host.split(':')[0]
|
||||||
|
self.url = 'ws://' + host + ':' + autoreload_port
|
||||||
|
self.retries = 0
|
||||||
|
self.interval = 100
|
||||||
|
self.disable = False
|
||||||
|
self.opened_at_least_once = False
|
||||||
|
self.reconnect()
|
||||||
|
|
||||||
|
def reconnect(self):
|
||||||
|
self.ws = WebSocket(self.url)
|
||||||
|
|
||||||
|
self.ws.onopen = def(event):
|
||||||
|
self.retries = 0
|
||||||
|
self.opened_at_least_once = True
|
||||||
|
self.interval = 100
|
||||||
|
print('Connected to reloading WebSocket server at: ' + self.url)
|
||||||
|
window.addEventListener('beforeunload', def (event):
|
||||||
|
print('Shutting down connection to reload server, before page unload')
|
||||||
|
self.disable = True
|
||||||
|
self.ws.close()
|
||||||
|
)
|
||||||
|
|
||||||
|
self.ws.onmessage = def(event):
|
||||||
|
if event.data is not 'ping':
|
||||||
|
print('Received mesasge from reload server: ' + event.data)
|
||||||
|
if event.data is 'reload':
|
||||||
|
self.reload_app()
|
||||||
|
|
||||||
|
self.ws.onclose = def (event):
|
||||||
|
if self.disable or not self.opened_at_least_once:
|
||||||
|
return
|
||||||
|
print('Connection to reload server closed with code: ' + event.code + ' and reason: ' + event.reason)
|
||||||
|
self.retries += 1
|
||||||
|
if (self.retries < MAX_RETRIES):
|
||||||
|
setTimeout(self.reconnect, self.interval)
|
||||||
|
else:
|
||||||
|
self.reload_app()
|
||||||
|
|
||||||
|
def reload_app(self):
|
||||||
|
window.location.reload(True)
|
||||||
|
|
||||||
|
|
||||||
|
def create_auto_reload_watcher(autoreload_port):
|
||||||
|
if not create_auto_reload_watcher.watcher:
|
||||||
|
create_auto_reload_watcher.watcher = Watcher(autoreload_port)
|
||||||
|
return create_auto_reload_watcher.watcher
|
@ -4,7 +4,7 @@ from __python__ import hash_literals
|
|||||||
|
|
||||||
import initialize # noqa: unused-import
|
import initialize # noqa: unused-import
|
||||||
from ajax import ajax
|
from ajax import ajax
|
||||||
|
from autoreload import create_auto_reload_watcher
|
||||||
from book_list.globals import main_js
|
from book_list.globals import main_js
|
||||||
from book_list.main import main
|
from book_list.main import main
|
||||||
from read_book.iframe import init
|
from read_book.iframe import init
|
||||||
@ -23,10 +23,9 @@ else:
|
|||||||
# we know are going to be needed immediately.
|
# we know are going to be needed immediately.
|
||||||
window.addEventListener('load', main)
|
window.addEventListener('load', main)
|
||||||
|
|
||||||
ajax('auto-reload', def(end_type, xhr, event):
|
ajax('auto-reload-port', def(end_type, xhr, event):
|
||||||
if end_type is 'load':
|
if end_type is 'load':
|
||||||
port = xhr.getResponseHeader('Calibre-Auto-Reload-Port')
|
port = parseInt(xhr.responseText)
|
||||||
if port:
|
if not isNaN(port) and port > 0:
|
||||||
src = xhr.responseText.replace('AUTORELOAD_PORT', port)
|
create_auto_reload_watcher(port)
|
||||||
eval(src)
|
|
||||||
).send() # We must bypass cache as otherwise we could get stale port info
|
).send() # We must bypass cache as otherwise we could get stale port info
|
||||||
|
Loading…
x
Reference in New Issue
Block a user