Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix sync not running on page focus

Abdullah Atta26f9e08c23f6
diff --git a/apps/desktop/src/api/sqlite-kysely.ts b/apps/desktop/src/api/sqlite-kysely.ts
index 9498bd055..94e3dc626 100644
--- a/apps/desktop/src/api/sqlite-kysely.ts
+++ b/apps/desktop/src/api/sqlite-kysely.ts
@@ -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
+ });
}
}
diff --git a/apps/web/src/stores/app-store.ts b/apps/web/src/stores/app-store.ts
index fc7761a14..d3e971c40 100644
--- a/apps/web/src/stores/app-store.ts
+++ b/apps/web/src/stores/app-store.ts
@@ -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);
diff --git a/apps/web/src/utils/page-visibility.ts b/apps/web/src/utils/page-visibility.ts
index c74b1b6d6..818c1c360 100644
--- a/apps/web/src/utils/page-visibility.ts
+++ b/apps/web/src/utils/page-visibility.ts
@@ -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);
};
}