Skip to content

Commit 8b381bb

Browse files
committed
ugly hack to deal with a circular reference causing an exception
- the exception was "harmless", but bad anyways (would slow things down a few seconds randomly)
1 parent 7f03d67 commit 8b381bb

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/packages/frontend/nats/connection.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function connectingMessage({ server, project_ids }) {
9494
}
9595

9696
const getNewNatsConn = reuseInFlight(async ({ cache, user }) => {
97-
const { account_id } = webapp_client;
97+
const account_id = await getAccountId();
9898
if (!account_id) {
9999
throw Error("you must be signed in before connecting to NATS");
100100
}
@@ -119,12 +119,23 @@ const getNewNatsConn = reuseInFlight(async ({ cache, user }) => {
119119
}
120120
});
121121

122+
// This is a hack to get around circular import during initial page load.
123+
// TODO: properly clean up the import order
124+
async function getAccountId() {
125+
try {
126+
return webapp_client.account_id;
127+
} catch {
128+
await delay(1);
129+
return webapp_client.account_id;
130+
}
131+
}
132+
122133
let cachedConnection: CoCalcNatsConnection | null = null;
123134
export const connect = reuseInFlight(async () => {
124135
if (cachedConnection != null) {
125136
return cachedConnection;
126137
}
127-
const { account_id } = webapp_client;
138+
const account_id = await getAccountId();
128139
const cache = getPermissionsCache();
129140
const project_ids = cache.get();
130141
const user = { account_id, project_ids };
@@ -283,7 +294,7 @@ class CoCalcNatsConnection extends EventEmitter implements NatsConnection {
283294
// nothing to do
284295
return;
285296
}
286-
const { account_id } = webapp_client;
297+
const account_id = await getAccountId();
287298
if (!account_id) {
288299
throw Error("you must be signed in before connecting to NATS");
289300
}

0 commit comments

Comments
 (0)