mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-16 11:47:54 +01:00
web: fix sync not running on page focus
This commit is contained in:
committed by
Abdullah Atta
parent
f719bebe5a
commit
26f9e08c23
@@ -19,8 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import type { Database, Statement } from "better-sqlite3-multiple-ciphers";
|
||||
import type { QueryResult } from "kysely";
|
||||
const rm = require("fs/promises").rm;
|
||||
const sqlite3 = require("better-sqlite3-multiple-ciphers");
|
||||
|
||||
type SQLiteCompatibleType =
|
||||
| number
|
||||
@@ -45,7 +43,9 @@ export class SQLite {
|
||||
return;
|
||||
}
|
||||
|
||||
this.sqlite = sqlite3(filePath).unsafeMode(true);
|
||||
this.sqlite = require("better-sqlite3-multiple-ciphers")(
|
||||
filePath
|
||||
).unsafeMode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,6 +134,10 @@ export class SQLite {
|
||||
|
||||
async delete(filePath: string) {
|
||||
await this.close();
|
||||
await rm(filePath, { force: true, maxRetries: 5, retryDelay: 500 });
|
||||
await require("fs/promises").rm(filePath, {
|
||||
force: true,
|
||||
maxRetries: 5,
|
||||
retryDelay: 500
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ class AppStore extends BaseStore<AppStore> {
|
||||
});
|
||||
|
||||
onPageVisibilityChanged(async (_, documentHidden) => {
|
||||
if (!documentHidden) return;
|
||||
if (documentHidden) return;
|
||||
|
||||
logger.info("Page visibility changed. Reconnecting SSE...");
|
||||
await db.connectSSE({ force: false }).catch(logger.error);
|
||||
|
||||
@@ -66,24 +66,28 @@ export function onPageVisibilityChanged(
|
||||
if (isEventIgnored()) return;
|
||||
|
||||
if (!window.document.hasFocus()) return;
|
||||
handler("focus", isDocumentHidden());
|
||||
handler("focus", false);
|
||||
}, 1000);
|
||||
|
||||
const onBlur = debounce((_) => {
|
||||
if (isEventIgnored()) return;
|
||||
|
||||
if (window.document.hasFocus()) return;
|
||||
handler("blur", isDocumentHidden());
|
||||
handler("blur", true);
|
||||
}, 1000);
|
||||
|
||||
document.addEventListener(visibilityChange(), onVisibilityChanged);
|
||||
window.addEventListener("focus", onFocus);
|
||||
window.addEventListener("blur", onBlur);
|
||||
window.addEventListener("pageshow", onFocus);
|
||||
window.addEventListener("pagehide", onBlur);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener(visibilityChange(), onVisibilityChanged);
|
||||
window.removeEventListener("focus", onFocus);
|
||||
window.removeEventListener("blur", onBlur);
|
||||
window.removeEventListener("pageshow", onFocus);
|
||||
window.removeEventListener("pagehide", onBlur);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user