@hautechai/sdk 0.3.6 → 0.3.8

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.
@@ -1218,7 +1218,7 @@ export declare const createSDK: (options: SDKOptions) => {
1218
1218
  metadata?: PoseMetadata;
1219
1219
  }) => Promise<void>;
1220
1220
  };
1221
- close: () => void;
1221
+ close: () => Promise<void>;
1222
1222
  };
1223
1223
  export type SDK = ReturnType<typeof createSDK>;
1224
1224
  export interface CollectionMetadata {
package/dist/sdk/index.js CHANGED
@@ -41,6 +41,7 @@ export const createSDK = (options) => {
41
41
  : null,
42
42
  // TODO: Refactor the API initialization
43
43
  operations: () => getAPI(OperationsApi, optionsWithTokenRefresher),
44
+ allowPollingFallback: options.allowPollingFallback ?? true,
44
45
  });
45
46
  operationsListener.subscribe();
46
47
  return {
@@ -56,8 +57,8 @@ export const createSDK = (options) => {
56
57
  storage: storage(optionsWithTokenRefresher),
57
58
  utils: utils(optionsWithTokenRefresher),
58
59
  poses: poses(optionsWithTokenRefresher),
59
- close: () => {
60
- operationsListener.close();
60
+ close: async () => {
61
+ await operationsListener.close();
61
62
  },
62
63
  };
63
64
  };
@@ -7,12 +7,14 @@ export declare class OperationsListener {
7
7
  } | null;
8
8
  ws: WebSocketClient | null;
9
9
  operations: () => Promise<OperationsApi>;
10
- constructor({ ws, operations, }: {
10
+ allowPollingFallback: boolean;
11
+ constructor({ ws, operations, allowPollingFallback, }: {
11
12
  ws: {
12
13
  endpoint: string;
13
14
  token: () => string | Promise<string>;
14
15
  } | null;
15
16
  operations: () => Promise<OperationsApi>;
17
+ allowPollingFallback: boolean;
16
18
  });
17
19
  operationsStore: Record<string, OperationEntity>;
18
20
  getOperation(id: string): Promise<OperationEntity | null>;
@@ -21,5 +23,5 @@ export declare class OperationsListener {
21
23
  onOpen(): void;
22
24
  onMessage(msg: string): void;
23
25
  onClose(number: number, reason: string): void;
24
- close(): void;
26
+ close(): Promise<void>;
25
27
  }
@@ -2,10 +2,12 @@ import { w3cwebsocket as WebSocketClient } from 'websocket';
2
2
  import { HautechError } from '../errors';
3
3
  // This is pretty much a dirty solution until we need to rework this part.
4
4
  export class OperationsListener {
5
- constructor({ ws, operations, }) {
5
+ constructor({ ws, operations, allowPollingFallback = false, }) {
6
6
  this.useWebsocket = null;
7
7
  this.ws = null;
8
8
  this.operationsStore = {};
9
+ if (!ws)
10
+ allowPollingFallback = true;
9
11
  if (ws) {
10
12
  this.useWebsocket = {
11
13
  endpoint: ws?.endpoint,
@@ -13,10 +15,11 @@ export class OperationsListener {
13
15
  };
14
16
  }
15
17
  this.operations = operations;
18
+ this.allowPollingFallback = allowPollingFallback;
16
19
  }
17
20
  async getOperation(id) {
18
- const isWsReady = this.ws?.readyState === WebSocket.OPEN;
19
- if (!this.operationsStore[id] || !isWsReady) {
21
+ const fallbackToPolling = this.allowPollingFallback && !(this.ws?.readyState === WebSocketClient.OPEN);
22
+ if (!this.operationsStore[id] || fallbackToPolling) {
20
23
  const api = await this.operations();
21
24
  const operation = await api.operationsControllerGetOperationV1(id);
22
25
  if (operation.status == 200)
@@ -34,7 +37,8 @@ export class OperationsListener {
34
37
  if (!this.useWebsocket)
35
38
  return;
36
39
  try {
37
- this.ws = new WebSocketClient(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
40
+ const token = await this.useWebsocket.token();
41
+ this.ws = new WebSocketClient(this.useWebsocket.endpoint, ['1', token]);
38
42
  this.ws.onopen = () => {
39
43
  this.onOpen();
40
44
  };
@@ -90,7 +94,8 @@ export class OperationsListener {
90
94
  this.operationsStore = {};
91
95
  this.ws = null;
92
96
  }
93
- close() {
97
+ async close() {
94
98
  this.ws?.close();
99
+ await new Promise((resolve) => setTimeout(resolve, 1000));
95
100
  }
96
101
  }
package/dist/types.d.ts CHANGED
@@ -2,6 +2,7 @@ export interface SDKOptions {
2
2
  authToken: () => string | Promise<string>;
3
3
  endpoint?: string;
4
4
  useWebsocket?: boolean;
5
+ allowPollingFallback?: boolean;
5
6
  }
6
7
  export type ListProps = {
7
8
  cursor?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hautechai/sdk",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "license": "MIT",
5
5
  "keywords": [],
6
6
  "repository": {