Skip to content

Commit bb1c7b9

Browse files
committed
add waitUntilConnected to conat client
1 parent c2fd3bd commit bb1c7b9

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/packages/nats/server/client.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ import {
100100
isValidSubject,
101101
isValidSubjectWithoutWildcards,
102102
} from "@cocalc/nats/util";
103+
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
104+
import { once } from "@cocalc/util/async-utils";
103105

104106
export function connect(address = "http://localhost:3000", options?) {
105107
return new Client(address, options);
@@ -150,11 +152,19 @@ export class Client {
150152
});
151153
}
152154

155+
waitUntilConnected = reuseInFlight(async () => {
156+
if (this.conn.connected) {
157+
return;
158+
}
159+
// @ts-ignore
160+
await once(this.conn, "connect");
161+
});
162+
153163
// syncSubscriptions ensures that we're subscribed on server
154164
// to what we think we're subscribed to.
155165
private syncSubscriptions = async () => {
156166
const subs = await this.getSubscriptions();
157-
console.log({subs})
167+
console.log({ subs });
158168
for (const subject in this.queueGroups) {
159169
// subscribe on backend to all subscriptions we think we should have that
160170
// the server does not have

0 commit comments

Comments
 (0)