Skip to content

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

desktop: wait for bridge to be ready before sending events

Abdullah Atta0191e5be0065
diff --git a/apps/desktop/src/api/bridge.ts b/apps/desktop/src/api/bridge.ts
index d1f1c3b5c..185e99b52 100644
--- a/apps/desktop/src/api/bridge.ts
+++ b/apps/desktop/src/api/bridge.ts
@@ -25,22 +25,42 @@ import TypedEventEmitter from "typed-emitter";
export type AppEvents = {
onCreateItem(name: "note" | "notebook" | "reminder"): void;
onOpenLink(url: string): void;
+ bridgeReady(): void;
};
-
-const emitter = new EventEmitter();
-const typedEmitter = emitter as TypedEventEmitter<AppEvents>;
+let isBridgeReady = false;
+const pendingEvents: { name: string; args: unknown[] }[] = [];
+const _emitter = new EventEmitter();
+const emitter = _emitter as TypedEventEmitter<AppEvents>;
const t = initTRPC.create();
export const bridgeRouter = t.router({
onCreateItem: createSubscription("onCreateItem"),
- onOpenLink: createSubscription("onOpenLink")
+ onOpenLink: createSubscription("onOpenLink"),
+ ready: t.procedure.query(() => {
+ isBridgeReady = true;
+ if (pendingEvents.length > 0) {
+ console.log(
+ "Emitting pending events",
+ pendingEvents.map((e) => e.name)
+ );
+ pendingEvents.forEach((event) => {
+ emitter.emit(event.name as any, ...(event.args as any[]));
+ });
+ pendingEvents.length = 0;
+ }
+ return true;
+ })
});
export const bridge: AppEvents = new Proxy({} as AppEvents, {
get(_t, name) {
if (typeof name === "symbol") return;
return (...args: unknown[]) => {
- emitter.emit(name, ...args);
+ if (!isBridgeReady) {
+ pendingEvents.push({ name, args });
+ return;
+ }
+ _emitter.emit(name, ...args);
};
}
});
@@ -51,9 +71,9 @@ function createSubscription<TName extends keyof AppEvents>(eventName: TName) {
const listener: AppEvents[TName] = (...args: any[]) => {
emit.next(args[0]);
};
- typedEmitter.addListener(eventName, listener);
+ emitter.addListener(eventName, listener);
return () => {
- typedEmitter.removeListener(eventName, listener);
+ emitter.removeListener(eventName, listener);
};
});
});
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index c5ffbb088..9514a1bca 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -148,11 +148,6 @@ async function createWindow() {
await mainWindow.webContents.loadURL(`${createURL(cliOptions, "/")}`);
mainWindow.setOpacity(1);
- if (pendingNNLink) {
- bridge.onOpenLink(pendingNNLink);
- pendingNNLink = undefined;
- }
-
if (config.privacyMode) {
await api.integration.setPrivacyMode({ enabled: config.privacyMode });
}
@@ -202,6 +197,11 @@ async function createWindow() {
setupTray();
setupJumplist();
});
+
+ if (pendingNNLink) {
+ bridge.onOpenLink(pendingNNLink);
+ pendingNNLink = undefined;
+ }
}
app.once("ready", async () => {
@@ -282,7 +282,7 @@ function createURL(options: CLIOptions, path = "/") {
else if (typeof options.note === "string")
url.hash = `/notes/${options.note}/edit`;
else if (typeof options.notebook === "string")
- url.hash = `/notebooks/${options.notebook}`;
+ url.pathname = `/notebooks/${options.notebook}`;
return url;
}