@go-avro/avro-js 0.0.2-beta.132 → 0.0.2-beta.134

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.
@@ -299,7 +299,14 @@ export declare class AvroQueryClient {
299
299
  _isAuthenticated: boolean;
300
300
  companyId: string | null;
301
301
  company: Company | null;
302
+ private initialized;
303
+ private initializedListeners;
302
304
  constructor(config: AvroQueryClientConfig);
305
+ private computeInitialized;
306
+ private updateInitialized;
307
+ onInitializedChange(cb: (v: boolean) => void): () => void;
308
+ offInitializedChange(cb: (v: boolean) => void): void;
309
+ isInitialized(): boolean;
303
310
  emit(eventName: string, data: unknown): void;
304
311
  on<T>(eventName: string, callback: (data: T) => void): void;
305
312
  off(eventName: string, callback?: Function): void;
@@ -335,6 +342,7 @@ export declare class AvroQueryClient {
335
342
  setCache(data: Partial<CacheData>): Promise<void>;
336
343
  getCache(key?: keyof CacheData | undefined): Promise<CacheData | string | null>;
337
344
  setCompanyId(companyId: string): Promise<void[]>;
345
+ getCompanyId(): Promise<string | null>;
338
346
  clearCache(): Promise<void>;
339
347
  isAuthenticated(): boolean;
340
348
  isAuthenticatedAsync(): Promise<boolean>;
@@ -7,6 +7,8 @@ export class AvroQueryClient {
7
7
  this._isAuthenticated = false;
8
8
  this.companyId = null;
9
9
  this.company = null;
10
+ this.initialized = false;
11
+ this.initializedListeners = [];
10
12
  this.config = {
11
13
  baseUrl: config.baseUrl,
12
14
  authManager: config.authManager,
@@ -15,13 +17,14 @@ export class AvroQueryClient {
15
17
  retryStrategy: config.retryStrategy ?? 'fixed',
16
18
  timeout: config.timeout ?? 0,
17
19
  };
20
+ this.socket = io(config.baseUrl, { autoConnect: false, transports: ["websocket"], });
18
21
  config.authManager.isAuthenticated().then(isAuth => {
19
22
  this._isAuthenticated = isAuth;
23
+ this.getCompanyId().then(id => {
24
+ this.companyId = id;
25
+ this.updateInitialized();
26
+ });
20
27
  });
21
- config.authManager.getCompanyId().then(companyId => {
22
- this.companyId = companyId;
23
- });
24
- this.socket = io(config.baseUrl, { autoConnect: false, transports: ["websocket"], });
25
28
  if (!this.socket.connected) {
26
29
  this.config.authManager.accessToken().then(token => {
27
30
  console.log('Initializing socket connection with token...');
@@ -32,9 +35,11 @@ export class AvroQueryClient {
32
35
  this.socket.on('connect', () => {
33
36
  this._isAuthenticated = true;
34
37
  console.log(`Socket connected with ID: ${this.socket?.id}`);
38
+ this.updateInitialized();
35
39
  });
36
40
  this.socket.on('disconnect', (reason) => {
37
41
  console.log(`Socket disconnected: ${reason}`);
42
+ this.updateInitialized();
38
43
  });
39
44
  this.socket.on('connect_error', (err) => {
40
45
  console.error(`Socket connection error: ${err.message}`);
@@ -54,6 +59,40 @@ export class AvroQueryClient {
54
59
  }
55
60
  });
56
61
  }
62
+ computeInitialized() {
63
+ return !!(this.socket?.connected && this.companyId !== null);
64
+ }
65
+ updateInitialized() {
66
+ const next = this.computeInitialized();
67
+ if (this.initialized !== next) {
68
+ this.initialized = next;
69
+ this.initializedListeners.forEach(cb => {
70
+ try {
71
+ cb(next);
72
+ }
73
+ catch (_) {
74
+ console.error(_);
75
+ }
76
+ });
77
+ }
78
+ }
79
+ onInitializedChange(cb) {
80
+ this.initializedListeners.push(cb);
81
+ // call immediately with current value
82
+ try {
83
+ cb(this.initialized);
84
+ }
85
+ catch (_) {
86
+ console.error(_);
87
+ }
88
+ return () => this.offInitializedChange(cb);
89
+ }
90
+ offInitializedChange(cb) {
91
+ this.initializedListeners = this.initializedListeners.filter(c => c !== cb);
92
+ }
93
+ isInitialized() {
94
+ return this.initialized;
95
+ }
57
96
  emit(eventName, data) {
58
97
  if (!this.socket?.connected) {
59
98
  console.error('Socket is not connected. Cannot emit event.');
@@ -175,8 +214,15 @@ export class AvroQueryClient {
175
214
  }
176
215
  setCompanyId(companyId) {
177
216
  this.companyId = companyId;
217
+ this.updateInitialized();
178
218
  return this.config.authManager.setCompanyId(companyId);
179
219
  }
220
+ getCompanyId() {
221
+ if (this.companyId) {
222
+ return Promise.resolve(this.companyId);
223
+ }
224
+ return this.config.authManager.getCompanyId();
225
+ }
180
226
  clearCache() {
181
227
  return this.config.authManager.clearCache();
182
228
  }
@@ -18,6 +18,9 @@ AvroQueryClient.prototype.useGetCurrentCompany = function () {
18
18
  return useQuery({
19
19
  queryKey: ['company', 'current'],
20
20
  queryFn: async () => {
21
+ if (!this.companyId) {
22
+ this.companyId = await this.config.authManager.getCompanyId();
23
+ }
21
24
  if (!this.companyId) {
22
25
  const companyList = await this.get(`/company/list`);
23
26
  if (companyList.length > 0) {
@@ -58,7 +61,7 @@ AvroQueryClient.prototype.useUpdateCompany = function () {
58
61
  queryClient.setQueryData(['/company/list'], (oldList) => {
59
62
  if (!oldList)
60
63
  return oldList;
61
- return oldList.map((company) => company.id === companyId ? { ...company, ...companyData } : company);
64
+ return oldList.map((company) => company?.id === companyId ? { ...company, ...companyData } : company);
62
65
  });
63
66
  return { previousCompany, previousCompanyList };
64
67
  },
@@ -120,7 +123,7 @@ AvroQueryClient.prototype.useDeleteCompany = function () {
120
123
  queryClient.setQueryData(['/company/list'], (oldList) => {
121
124
  if (!oldList)
122
125
  return oldList;
123
- return oldList.filter((company) => company.id !== companyId);
126
+ return oldList.filter((company) => company?.id !== companyId);
124
127
  });
125
128
  return { previousCompanyList };
126
129
  },
@@ -11,9 +11,9 @@ AvroQueryClient.prototype.useGetJobs = function (onProgress) {
11
11
  queryKey: ['jobs', this.companyId, this.company?.num_jobs],
12
12
  queryFn: async () => {
13
13
  onProgress?.(0);
14
- const pageCount = amt ? Math.ceil((this.company?.num_jobs ?? 0) / amt) : 1;
14
+ const pageCount = amt ? Math.ceil((this.company?.num_jobs ?? 0) / amt) + 1 : 1;
15
15
  let completed = 0;
16
- const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * amt, amt }));
16
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * (amt ?? 1), amt }));
17
17
  const trackedPromises = promises.map((promise) => promise.then((result) => {
18
18
  completed++;
19
19
  const fraction = completed / pageCount;
@@ -8,11 +8,11 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
8
8
  return this.fetchRoutes({ ...body, offset: 0 });
9
9
  }
10
10
  onProgress?.(0);
11
- const pageCount = body.amt ? Math.ceil(total / body.amt) : 1;
11
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
12
12
  let completed = 0;
13
13
  const promises = Array.from({ length: pageCount }, (_, i) => this.fetchRoutes({
14
14
  ...body,
15
- offset: i * (body.amt ?? 0),
15
+ offset: i * (body.amt ?? 1),
16
16
  }));
17
17
  const trackedPromises = promises.map((promise) => promise.then((result) => {
18
18
  completed++;
@@ -22,11 +22,11 @@ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
22
22
  return this.fetchSkills({ ...body, offset: 0 });
23
23
  }
24
24
  onProgress?.(0);
25
- const pageCount = body.amt ? Math.ceil(total / body.amt) : 1;
25
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
26
26
  let completed = 0;
27
27
  const promises = Array.from({ length: pageCount }, (_, i) => this.fetchSkills({
28
28
  ...body,
29
- offset: i * (body.amt ?? 0),
29
+ offset: i * (body.amt ?? 1),
30
30
  }));
31
31
  const trackedPromises = promises.map((promise) => promise.then((result) => {
32
32
  completed++;
@@ -8,11 +8,11 @@ AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
8
8
  return this.fetchTeams({ ...body, offset: 0 });
9
9
  }
10
10
  onProgress?.(0);
11
- const pageCount = body.amt ? Math.ceil(total / body.amt) : 1;
11
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
12
12
  let completed = 0;
13
13
  const promises = Array.from({ length: pageCount }, (_, i) => this.fetchTeams({
14
14
  ...body,
15
- offset: i * (body.amt ?? 0),
15
+ offset: i * (body.amt ?? 1),
16
16
  }));
17
17
  const trackedPromises = promises.map((promise) => promise.then((result) => {
18
18
  completed++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.132",
3
+ "version": "0.0.2-beta.134",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",