@go-avro/avro-js 0.0.38 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -229,13 +229,16 @@ export class AvroQueryClient {
229
229
  });
230
230
  config.authManager.isAuthenticated().then((isAuth) => {
231
231
  this.setAuthState(isAuth);
232
- this.getCompanyId().then((id) => {
233
- this.companyId = id;
234
- });
235
232
  if (!this.socket.connected && isAuth === AuthState.AUTHENTICATED) {
236
- this.config.authManager
237
- .accessToken()
238
- .then((token) => {
233
+ // Resolve both companyId and access token before connecting so
234
+ // the 'connect' handler in setupSocketInvalidation can immediately
235
+ // join the company room.
236
+ Promise.all([
237
+ this.getCompanyId(),
238
+ this.config.authManager.accessToken(),
239
+ ])
240
+ .then(([id, token]) => {
241
+ this.companyId = id;
239
242
  console.log("Initializing socket connection with token:", token);
240
243
  this.socket.auth = { token: token };
241
244
  this.socket.connect();
@@ -244,6 +247,12 @@ export class AvroQueryClient {
244
247
  console.error("Not logged in:", err);
245
248
  });
246
249
  }
250
+ else {
251
+ // Not authenticated – still cache the companyId for later.
252
+ this.getCompanyId().then((id) => {
253
+ this.companyId = id;
254
+ });
255
+ }
247
256
  });
248
257
  this.socket.on("connect", () => {
249
258
  this.setAuthState(AuthState.AUTHENTICATED);
@@ -556,7 +565,15 @@ export class AvroQueryClient {
556
565
  return this.config.authManager.getCache(key);
557
566
  }
558
567
  setCompanyId(companyId) {
568
+ const previousId = this.companyId;
559
569
  this.companyId = companyId;
570
+ // If the socket is already connected, leave the old room and join the new one.
571
+ if (this.socket.connected) {
572
+ if (previousId && previousId !== companyId) {
573
+ this.socket.emit("leave_company", { company_id: previousId });
574
+ }
575
+ this.socket.emit("join_company", { company_id: companyId });
576
+ }
560
577
  return this.config.authManager.setCompanyId(companyId);
561
578
  }
562
579
  getCompanyId() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.38",
3
+ "version": "0.0.39",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",