Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Fix app init in web (#165)

Hakan Shehue8f168304fb4
diff --git a/apps/web/src/lib/types.ts b/apps/web/src/lib/types.ts
index 495284bc..4207b06f 100644
--- a/apps/web/src/lib/types.ts
+++ b/apps/web/src/lib/types.ts
@@ -5,6 +5,7 @@ import { Event, TempFile } from '@colanode/client/types';
import { ColanodeWindowApi } from '@colanode/ui';
export interface ColanodeWorkerApi {
+ init: () => Promise<void>;
executeMutation: <T extends MutationInput>(
input: T
) => Promise<MutationResult<T>>;
diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx
index 79aaba18..ba832333 100644
--- a/apps/web/src/main.tsx
+++ b/apps/web/src/main.tsx
@@ -28,7 +28,9 @@ const initializeApp = async () => {
const workerApi = Comlink.wrap<ColanodeWorkerApi>(worker);
window.colanode = {
- init: async () => {},
+ init: async () => {
+ await workerApi.init();
+ },
executeMutation: async (input) => {
return workerApi.executeMutation(input);
},
diff --git a/apps/web/src/workers/dedicated.ts b/apps/web/src/workers/dedicated.ts
index a3e09288..86d397f3 100644
--- a/apps/web/src/workers/dedicated.ts
+++ b/apps/web/src/workers/dedicated.ts
@@ -24,6 +24,7 @@ const pendingPromises = new Map<string, PendingPromise>();
const fs = new WebFileSystem();
const path = new WebPathService();
let app: AppService | null = null;
+let appInitialized = false;
const broadcast = new BroadcastChannel('colanode');
broadcast.onmessage = (event) => {
@@ -40,6 +41,7 @@ navigator.locks.request('colanode', async () => {
await app.migrate();
await app.init();
+ appInitialized = true;
const ids = Array.from(pendingPromises.keys());
for (const id of ids) {
@@ -163,6 +165,24 @@ const handleMessage = async (message: BroadcastMessage) => {
};
const api: ColanodeWorkerApi = {
+ async init() {
+ if (!app) {
+ return;
+ }
+
+ if (appInitialized) {
+ return;
+ }
+
+ let count = 0;
+ while (!appInitialized) {
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ count++;
+ if (count > 100) {
+ throw new Error('App initialization timed out');
+ }
+ }
+ },
executeMutation(input) {
if (app) {
return app.mediator.executeMutation(input);