增加热更新功能

master 0.2.0
yutent 2023-08-31 18:47:54 +08:00
parent f82c4c00a9
commit 68a773bb0c
5 changed files with 59 additions and 4 deletions

View File

@ -4,7 +4,7 @@ if [ -d unpack ]; then
sudo rm -rf unpack
fi
version="0.1.0"
version="0.2.0"
mkdir -p unpack/DEBIAN

2
debian/control vendored
View File

@ -1,5 +1,5 @@
Package: python3-webengine-gtk3
Version: 0.1.0
Version: 0.2.0
Section: develop
Priority: optional
Maintainer: Yutent <yutent.io@gmail.com>

View File

@ -4,7 +4,8 @@
from webengine.gtk3._webengine import WebEngine
from webengine.gtk3._settings import create_setting
from webengine.gtk3._hotreload import create_hmr_server
build = (0, 1, 0)
build = (0, 2, 0)
version = '.'.join(map(str, build))

View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
import time, os
try:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
except:
Observer = None
FileSystemEventHandler = object
class DogHandler(FileSystemEventHandler):
last_reload = 0
def __init__(self, webview):
super().__init__()
self.webview = webview
def on_modified(self, event):
if os.path.isfile(event.src_path):
stamp = int(time.time())
# 1秒内有多个修改, 不重复reload
if stamp == self.last_reload:
return
self.last_reload = stamp
self.webview.reload()
def create_hmr_server(options = None):
if Observer is None:
print('Warning: HMR server depends on "python-watchdog". Please install it before using it')
dog = None
else:
dog = Observer()
def wrapper(app, extra = None):
if dog is None:
return
if app.root:
dog.schedule(DogHandler(app), app.root, recursive=True)
dog.start()
else:
raise SyntaxError('Web root must be set before HMR server starts.')
return wrapper

View File

@ -22,7 +22,7 @@ class Settings(WebKit2.Settings):
self.set_javascript_can_open_windows_automatically(True)
self.set_user_agent_with_application_details('WebEngine', '0.1.0')
self.set_user_agent_with_application_details('WebEngine', '0.2.0')
# indexedDB 和 localStorage 和 离线缓存