From 2217f92df570fba3dcf4fd59ccaec9720e9256c8 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 23 Nov 2022 20:41:14 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=A1=8C=E9=9D=A2=E7=AB=AF=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E4=BC=91=E7=9C=A0=E5=94=A4=E9=86=92=E5=90=8E=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=BD=91=E7=BB=9C=E8=BF=9E=E9=80=9A=E6=80=A7=E5=90=8E?= =?UTF-8?q?=E5=86=8D=E6=89=A7=E8=A1=8C=E6=95=B0=E6=8D=AE=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=20Fix=20https://github.com/siyuan-note/siyuan/issues/6687?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/electron/main.js | 39 ++++++++++++++++++++++++++++++++------- app/pnpm-lock.yaml | 4 ++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index fb27463d8..c154859ed 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -583,10 +583,6 @@ const initKernel = (initData) => { }) } - const sleep = (ms) => { - return new Promise(resolve => setTimeout(resolve, ms)) - } - const getKernelPort = async () => { if (isDevEnv) { return kernelPort @@ -797,15 +793,31 @@ app.on('before-quit', (event) => { }) const {powerMonitor} = require('electron') -const cp = require("child_process"); powerMonitor.on('suspend', () => { writeLog('system suspend') - fetch(getServer() + '/api/sync/performSync', {method: 'POST'}) }) -powerMonitor.on('resume', () => { +powerMonitor.on('resume', async () => { writeLog('system resume') + let online = false + for (let i = 0; i < 7; i++) { + if (await isOnline()) { + online = true + break; + } + + writeLog("network is offline") + await sleep(1000) + } + + if (!online) { + writeLog("network is offline, do not sync after system resume") + return; + } + + writeLog("sync after system resume") + // 桌面端系统休眠唤醒后同步延时 7s 后再执行 https://github.com/siyuan-note/siyuan/issues/6687 fetch(getServer() + '/api/sync/performSync', {method: 'POST'}) }) @@ -813,3 +825,16 @@ powerMonitor.on('shutdown', () => { writeLog('system shutdown') fetch(getServer() + '/api/system/exit', {method: 'POST'}) }) + +const sleep = (ms) => { + return new Promise(resolve => setTimeout(resolve, ms)) +} + +const isOnline = async () => { + try { + const result = await fetch("https://icanhazip.com", {timeout: 1000}) + return 200 === result.status + } catch (e) { + return false; + } +} diff --git a/app/pnpm-lock.yaml b/app/pnpm-lock.yaml index 1223b8de6..719239048 100644 --- a/app/pnpm-lock.yaml +++ b/app/pnpm-lock.yaml @@ -1906,10 +1906,10 @@ packages: resolution: {integrity: sha512-m0+M53+HYMzqKxwNQZT143K7WwXEGUy9LY31l8dJphXx2P/FQod615mVbxHyqbDCG4J5bHdWm21qZ0e2DVY6CQ==} engines: {node: '>=14.0.0'} dependencies: + 7zip-bin: 5.1.1 '@develar/schema-utils': 2.6.5 '@electron/universal': 1.2.1 '@malept/flatpak-bundler': 0.4.0 - 7zip-bin: 5.1.1 async-exit-hook: 2.0.1 bluebird-lst: 1.0.9 builder-util: 23.3.3 @@ -2193,9 +2193,9 @@ packages: /builder-util/23.3.3: resolution: {integrity: sha512-MJZlUiq2PY5hjYv9+XNaoYdsITqvLgRDoHSFg/4nzpInbNxNjLQOolL04Zsyp+hgfcbFvMC4h0KkR1CMPHLWbA==} dependencies: + 7zip-bin: 5.1.1 '@types/debug': 4.1.7 '@types/fs-extra': 9.0.13 - 7zip-bin: 5.1.1 app-builder-bin: 4.0.0 bluebird-lst: 1.0.9 builder-util-runtime: 9.0.3