@swan-admin/swan-ai-measurements 1.1.73 → 1.1.74

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.
@@ -18,17 +18,17 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
18
18
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
19
19
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
20
20
  };
21
- var _Measurement_instances, _Measurement_measurementSocketRef, _Measurement_timerPollingRef, _Measurement_timerWaitingRef, _Measurement_count, _Measurement_accessKey, _Measurement_stagingUrl, _Measurement_getMeasurementsCheck, _Measurement_handlePolling, _Measurement_disconnectSocket, _Measurement_handleTimeOut, _Measurement_handleSocket;
21
+ var _Measurement_instances, _Measurement_socketRefs, _Measurement_waitingTimers, _Measurement_pollingTimers, _Measurement_pollingCounts, _Measurement_accessKey, _Measurement_stagingUrl, _Measurement_disconnectSocket, _Measurement_handleTimeOut, _Measurement_handlePolling, _Measurement_getMeasurementsCheck, _Measurement_handleSocket;
22
22
  import axios from "axios";
23
23
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_MESSAGE } from "./constants.js";
24
24
  import { checkParameters, getUrl } from "./utils.js";
25
25
  class Measurement {
26
26
  constructor(accessKey, stagingUrl = false) {
27
27
  _Measurement_instances.add(this);
28
- _Measurement_measurementSocketRef.set(this, null);
29
- _Measurement_timerPollingRef.set(this, null);
30
- _Measurement_timerWaitingRef.set(this, null);
31
- _Measurement_count.set(this, 1);
28
+ _Measurement_socketRefs.set(this, {});
29
+ _Measurement_waitingTimers.set(this, {});
30
+ _Measurement_pollingTimers.set(this, {});
31
+ _Measurement_pollingCounts.set(this, {});
32
32
  _Measurement_accessKey.set(this, void 0);
33
33
  _Measurement_stagingUrl.set(this, void 0);
34
34
  __classPrivateFieldSet(this, _Measurement_accessKey, accessKey, "f");
@@ -66,93 +66,84 @@ class Measurement {
66
66
  __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleSocket).call(this, { onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false, delay: 1000 });
67
67
  }
68
68
  }
69
- _Measurement_measurementSocketRef = new WeakMap(), _Measurement_timerPollingRef = new WeakMap(), _Measurement_timerWaitingRef = new WeakMap(), _Measurement_count = new WeakMap(), _Measurement_accessKey = new WeakMap(), _Measurement_stagingUrl = new WeakMap(), _Measurement_instances = new WeakSet(), _Measurement_getMeasurementsCheck = function _Measurement_getMeasurementsCheck(options) {
69
+ _Measurement_socketRefs = new WeakMap(), _Measurement_waitingTimers = new WeakMap(), _Measurement_pollingTimers = new WeakMap(), _Measurement_pollingCounts = new WeakMap(), _Measurement_accessKey = new WeakMap(), _Measurement_stagingUrl = new WeakMap(), _Measurement_instances = new WeakSet(), _Measurement_disconnectSocket = function _Measurement_disconnectSocket(key) {
70
+ var _a;
71
+ (_a = __classPrivateFieldGet(this, _Measurement_socketRefs, "f")[key]) === null || _a === void 0 ? void 0 : _a.close();
72
+ __classPrivateFieldGet(this, _Measurement_socketRefs, "f")[key] = null;
73
+ if (__classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key]) {
74
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key]);
75
+ __classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key] = null;
76
+ }
77
+ }, _Measurement_handleTimeOut = function _Measurement_handleTimeOut(options, key) {
78
+ const { scanId, onSuccess, onError } = options;
79
+ __classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] = 1;
80
+ __classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key] = setTimeout(() => {
81
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError }, key);
82
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this, key);
83
+ }, 2 * 60000);
84
+ }, _Measurement_handlePolling = function _Measurement_handlePolling(options, key) {
85
+ const { scanId, onSuccess, onError } = options;
86
+ if (__classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key]) {
87
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key]);
88
+ }
89
+ __classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key] = setTimeout(() => {
90
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_getMeasurementsCheck).call(this, { scanId, onSuccess, onError }, key);
91
+ }, (__classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] || 1) * 5000);
92
+ }, _Measurement_getMeasurementsCheck = function _Measurement_getMeasurementsCheck(options, key) {
70
93
  return __awaiter(this, void 0, void 0, function* () {
71
94
  var _a;
72
- var _b;
73
95
  const { scanId, onSuccess, onError } = options;
74
96
  try {
75
97
  const res = yield this.getMeasurementResult(scanId);
76
98
  if ((res === null || res === void 0 ? void 0 : res.data) && ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.isMeasured) === true) {
77
99
  onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(res.data);
78
- if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
79
- clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
80
- }
100
+ clearInterval(__classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key]);
81
101
  }
82
102
  else {
83
- if (__classPrivateFieldGet(this, _Measurement_count, "f") < 8) {
84
- __classPrivateFieldSet(this, _Measurement_count, (_b = __classPrivateFieldGet(this, _Measurement_count, "f"), _b++, _b), "f");
85
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError });
103
+ if ((__classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] || 1) < 8) {
104
+ __classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] = (__classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] || 1) + 1;
105
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError }, key);
86
106
  }
87
107
  else {
88
- __classPrivateFieldSet(this, _Measurement_count, 1, "f");
89
- if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
90
- clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
91
- }
108
+ __classPrivateFieldGet(this, _Measurement_pollingCounts, "f")[key] = 1;
109
+ clearInterval(__classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key]);
92
110
  onError === null || onError === void 0 ? void 0 : onError({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
93
111
  }
94
112
  }
95
113
  }
96
114
  catch (e) {
97
- if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
98
- clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
99
- }
115
+ clearInterval(__classPrivateFieldGet(this, _Measurement_pollingTimers, "f")[key]);
100
116
  onError === null || onError === void 0 ? void 0 : onError(e);
101
117
  }
102
118
  });
103
- }, _Measurement_handlePolling = function _Measurement_handlePolling(options) {
104
- const { scanId, onSuccess, onError } = options;
105
- if (__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f")) {
106
- clearInterval(__classPrivateFieldGet(this, _Measurement_timerPollingRef, "f"));
107
- }
108
- __classPrivateFieldSet(this, _Measurement_timerPollingRef, setTimeout(() => {
109
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_getMeasurementsCheck).call(this, { scanId, onSuccess, onError });
110
- }, __classPrivateFieldGet(this, _Measurement_count, "f") * 5000), "f");
111
- }, _Measurement_disconnectSocket = function _Measurement_disconnectSocket() {
112
- var _a;
113
- (_a = __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f")) === null || _a === void 0 ? void 0 : _a.close();
114
- if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
115
- clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
116
- }
117
- }, _Measurement_handleTimeOut = function _Measurement_handleTimeOut(options) {
118
- const { scanId, onSuccess, onError } = options;
119
- __classPrivateFieldSet(this, _Measurement_count, 1, "f");
120
- __classPrivateFieldSet(this, _Measurement_timerWaitingRef, setTimeout(() => {
121
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handlePolling).call(this, { scanId, onSuccess, onError });
122
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
123
- }, 2 * 60000), "f");
124
119
  }, _Measurement_handleSocket = function _Measurement_handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId, delay }) {
120
+ const key = isFallback ? "measurement" : "faceScan";
125
121
  setTimeout(() => {
126
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this);
122
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_disconnectSocket).call(this, key);
127
123
  const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _Measurement_stagingUrl, "f") })}${API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
128
- __classPrivateFieldSet(this, _Measurement_measurementSocketRef, new WebSocket(url), "f");
129
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onopen = () => {
124
+ const socket = new WebSocket(url);
125
+ __classPrivateFieldGet(this, _Measurement_socketRefs, "f")[key] = socket;
126
+ socket.onopen = () => {
130
127
  onOpen === null || onOpen === void 0 ? void 0 : onOpen();
131
128
  if (isFallback && scanId) {
132
- __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleTimeOut).call(this, { scanId, onSuccess, onError });
129
+ __classPrivateFieldGet(this, _Measurement_instances, "m", _Measurement_handleTimeOut).call(this, { scanId, onSuccess, onError }, key);
133
130
  }
134
131
  };
135
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onmessage = (event) => {
132
+ socket.onmessage = (event) => {
136
133
  const data = JSON.parse(event.data);
137
134
  if ((data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success") {
138
135
  onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
139
136
  }
140
137
  else {
141
- if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f")) {
142
- clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
143
- }
138
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key]);
144
139
  onError === null || onError === void 0 ? void 0 : onError(data);
145
140
  }
146
- if (__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f") && (data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success" && (data === null || data === void 0 ? void 0 : data.resultType) === "final") {
147
- clearTimeout(__classPrivateFieldGet(this, _Measurement_timerWaitingRef, "f"));
141
+ if ((data === null || data === void 0 ? void 0 : data.code) === 200 && (data === null || data === void 0 ? void 0 : data.scanStatus) === "success" && (data === null || data === void 0 ? void 0 : data.resultType) === "final") {
142
+ clearTimeout(__classPrivateFieldGet(this, _Measurement_waitingTimers, "f")[key]);
148
143
  }
149
144
  };
150
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onclose = () => {
151
- onClose === null || onClose === void 0 ? void 0 : onClose();
152
- };
153
- __classPrivateFieldGet(this, _Measurement_measurementSocketRef, "f").onerror = (event) => {
154
- // onError?.(event);
155
- };
145
+ socket.onclose = () => onClose === null || onClose === void 0 ? void 0 : onClose();
146
+ socket.onerror = () => { };
156
147
  }, delay);
157
148
  };
158
149
  export default Measurement;
@@ -7,10 +7,10 @@ const axios_1 = __importDefault(require("axios"));
7
7
  const constants_js_1 = require("./constants.js");
8
8
  const utils_js_1 = require("./utils.js");
9
9
  class Measurement {
10
- #measurementSocketRef = null;
11
- #timerPollingRef = null;
12
- #timerWaitingRef = null;
13
- #count = 1;
10
+ #socketRefs = {};
11
+ #waitingTimers = {};
12
+ #pollingTimers = {};
13
+ #pollingCounts = {};
14
14
  #accessKey;
15
15
  #stagingUrl;
16
16
  constructor(accessKey, stagingUrl = false) {
@@ -34,60 +34,56 @@ class Measurement {
34
34
  headers: { "X-Api-Key": this.#accessKey },
35
35
  });
36
36
  }
37
- async #getMeasurementsCheck(options) {
37
+ #disconnectSocket(key) {
38
+ this.#socketRefs[key]?.close();
39
+ this.#socketRefs[key] = null;
40
+ if (this.#waitingTimers[key]) {
41
+ clearTimeout(this.#waitingTimers[key]);
42
+ this.#waitingTimers[key] = null;
43
+ }
44
+ }
45
+ #handleTimeOut(options, key) {
46
+ const { scanId, onSuccess, onError } = options;
47
+ this.#pollingCounts[key] = 1;
48
+ this.#waitingTimers[key] = setTimeout(() => {
49
+ this.#handlePolling({ scanId, onSuccess, onError }, key);
50
+ this.#disconnectSocket(key);
51
+ }, 2 * 60000);
52
+ }
53
+ #handlePolling(options, key) {
54
+ const { scanId, onSuccess, onError } = options;
55
+ if (this.#pollingTimers[key]) {
56
+ clearTimeout(this.#pollingTimers[key]);
57
+ }
58
+ this.#pollingTimers[key] = setTimeout(() => {
59
+ this.#getMeasurementsCheck({ scanId, onSuccess, onError }, key);
60
+ }, (this.#pollingCounts[key] || 1) * 5000);
61
+ }
62
+ async #getMeasurementsCheck(options, key) {
38
63
  const { scanId, onSuccess, onError } = options;
39
64
  try {
40
65
  const res = await this.getMeasurementResult(scanId);
41
66
  if (res?.data && res?.data?.isMeasured === true) {
42
67
  onSuccess?.(res.data);
43
- if (this.#timerPollingRef) {
44
- clearInterval(this.#timerPollingRef);
45
- }
68
+ clearInterval(this.#pollingTimers[key]);
46
69
  }
47
70
  else {
48
- if (this.#count < 8) {
49
- this.#count++;
50
- this.#handlePolling({ scanId, onSuccess, onError });
71
+ if ((this.#pollingCounts[key] || 1) < 8) {
72
+ this.#pollingCounts[key] = (this.#pollingCounts[key] || 1) + 1;
73
+ this.#handlePolling({ scanId, onSuccess, onError }, key);
51
74
  }
52
75
  else {
53
- this.#count = 1;
54
- if (this.#timerPollingRef) {
55
- clearInterval(this.#timerPollingRef);
56
- }
76
+ this.#pollingCounts[key] = 1;
77
+ clearInterval(this.#pollingTimers[key]);
57
78
  onError?.({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
58
79
  }
59
80
  }
60
81
  }
61
82
  catch (e) {
62
- if (this.#timerPollingRef) {
63
- clearInterval(this.#timerPollingRef);
64
- }
83
+ clearInterval(this.#pollingTimers[key]);
65
84
  onError?.(e);
66
85
  }
67
86
  }
68
- #handlePolling(options) {
69
- const { scanId, onSuccess, onError } = options;
70
- if (this.#timerPollingRef) {
71
- clearInterval(this.#timerPollingRef);
72
- }
73
- this.#timerPollingRef = setTimeout(() => {
74
- this.#getMeasurementsCheck({ scanId, onSuccess, onError });
75
- }, this.#count * 5000);
76
- }
77
- #disconnectSocket() {
78
- this.#measurementSocketRef?.close();
79
- if (this.#timerWaitingRef) {
80
- clearTimeout(this.#timerWaitingRef);
81
- }
82
- }
83
- #handleTimeOut(options) {
84
- const { scanId, onSuccess, onError } = options;
85
- this.#count = 1;
86
- this.#timerWaitingRef = setTimeout(() => {
87
- this.#handlePolling({ scanId, onSuccess, onError });
88
- this.#disconnectSocket();
89
- }, 2 * 60000);
90
- }
91
87
  handleMeasurementSocket(options) {
92
88
  const { scanId, onError, onSuccess, onClose, onOpen } = options;
93
89
  if (!(0, utils_js_1.checkParameters)(scanId)) {
@@ -103,37 +99,33 @@ class Measurement {
103
99
  this.#handleSocket({ onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false, delay: 1000 });
104
100
  }
105
101
  #handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId, delay }) {
102
+ const key = isFallback ? "measurement" : "faceScan";
106
103
  setTimeout(() => {
107
- this.#disconnectSocket();
104
+ this.#disconnectSocket(key);
108
105
  const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
109
- this.#measurementSocketRef = new WebSocket(url);
110
- this.#measurementSocketRef.onopen = () => {
106
+ const socket = new WebSocket(url);
107
+ this.#socketRefs[key] = socket;
108
+ socket.onopen = () => {
111
109
  onOpen?.();
112
110
  if (isFallback && scanId) {
113
- this.#handleTimeOut({ scanId, onSuccess, onError });
111
+ this.#handleTimeOut({ scanId, onSuccess, onError }, key);
114
112
  }
115
113
  };
116
- this.#measurementSocketRef.onmessage = (event) => {
114
+ socket.onmessage = (event) => {
117
115
  const data = JSON.parse(event.data);
118
116
  if (data?.code === 200 && data?.scanStatus === "success") {
119
117
  onSuccess?.(data);
120
118
  }
121
119
  else {
122
- if (this.#timerWaitingRef) {
123
- clearTimeout(this.#timerWaitingRef);
124
- }
120
+ clearTimeout(this.#waitingTimers[key]);
125
121
  onError?.(data);
126
122
  }
127
- if (this.#timerWaitingRef && data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final") {
128
- clearTimeout(this.#timerWaitingRef);
123
+ if (data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final") {
124
+ clearTimeout(this.#waitingTimers[key]);
129
125
  }
130
126
  };
131
- this.#measurementSocketRef.onclose = () => {
132
- onClose?.();
133
- };
134
- this.#measurementSocketRef.onerror = (event) => {
135
- // onError?.(event);
136
- };
127
+ socket.onclose = () => onClose?.();
128
+ socket.onerror = () => { };
137
129
  }, delay);
138
130
  }
139
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.1.73",
3
+ "version": "1.1.74",
4
4
  "description": "provides ai measurement suggestion",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -24,7 +24,7 @@ interface HandleSocket extends Callbacks {
24
24
  scanId?: string;
25
25
  faceScanId?: string;
26
26
  paramsKey?: string;
27
- delay:number
27
+ delay: number;
28
28
  }
29
29
  interface GetMeasurementsCheckOptions {
30
30
  scanId: string;
@@ -45,10 +45,10 @@ interface HandleTimeOutOptions {
45
45
  }
46
46
 
47
47
  class Measurement {
48
- #measurementSocketRef: WebSocket | null = null;
49
- #timerPollingRef: NodeJS.Timeout | null = null;
50
- #timerWaitingRef: NodeJS.Timeout | null = null;
51
- #count: number = 1;
48
+ #socketRefs: Record<string, WebSocket | null> = {};
49
+ #waitingTimers: Record<string, NodeJS.Timeout | null> = {};
50
+ #pollingTimers: Record<string, NodeJS.Timeout | null> = {};
51
+ #pollingCounts: Record<string, number> = {};
52
52
  #accessKey: string;
53
53
  #stagingUrl: boolean;
54
54
 
@@ -76,115 +76,103 @@ class Measurement {
76
76
  });
77
77
  }
78
78
 
79
- async #getMeasurementsCheck(options: GetMeasurementsCheckOptions): Promise<void> {
79
+ #disconnectSocket(key: string): void {
80
+ this.#socketRefs[key]?.close();
81
+ this.#socketRefs[key] = null;
82
+ if (this.#waitingTimers[key]) {
83
+ clearTimeout(this.#waitingTimers[key]!);
84
+ this.#waitingTimers[key] = null;
85
+ }
86
+ }
87
+
88
+ #handleTimeOut(options: HandleTimeOutOptions, key: string): void {
80
89
  const { scanId, onSuccess, onError } = options;
90
+ this.#pollingCounts[key] = 1;
91
+ this.#waitingTimers[key] = setTimeout(() => {
92
+ this.#handlePolling({ scanId, onSuccess, onError }, key);
93
+ this.#disconnectSocket(key);
94
+ }, 2 * 60000);
95
+ }
96
+
97
+ #handlePolling(options: HandlePollingOptions, key: string): void {
98
+ const { scanId, onSuccess, onError } = options;
99
+ if (this.#pollingTimers[key]) {
100
+ clearTimeout(this.#pollingTimers[key]!);
101
+ }
102
+ this.#pollingTimers[key] = setTimeout(() => {
103
+ this.#getMeasurementsCheck({ scanId, onSuccess, onError }, key);
104
+ }, (this.#pollingCounts[key] || 1) * 5000);
105
+ }
81
106
 
107
+ async #getMeasurementsCheck(options: GetMeasurementsCheckOptions, key: string): Promise<void> {
108
+ const { scanId, onSuccess, onError } = options;
82
109
  try {
83
110
  const res = await this.getMeasurementResult(scanId);
84
111
  if (res?.data && res?.data?.isMeasured === true) {
85
112
  onSuccess?.(res.data);
86
- if (this.#timerPollingRef) {
87
- clearInterval(this.#timerPollingRef);
88
- }
113
+ clearInterval(this.#pollingTimers[key]!);
89
114
  } else {
90
- if (this.#count < 8) {
91
- this.#count++;
92
- this.#handlePolling({ scanId, onSuccess, onError });
115
+ if ((this.#pollingCounts[key] || 1) < 8) {
116
+ this.#pollingCounts[key] = (this.#pollingCounts[key] || 1) + 1;
117
+ this.#handlePolling({ scanId, onSuccess, onError }, key);
93
118
  } else {
94
- this.#count = 1;
95
- if (this.#timerPollingRef) {
96
- clearInterval(this.#timerPollingRef);
97
- }
119
+ this.#pollingCounts[key] = 1;
120
+ clearInterval(this.#pollingTimers[key]!);
98
121
  onError?.({ scanStatus: "failed", message: "Scan not found", isMeasured: false });
99
122
  }
100
123
  }
101
124
  } catch (e) {
102
- if (this.#timerPollingRef) {
103
- clearInterval(this.#timerPollingRef);
104
- }
125
+ clearInterval(this.#pollingTimers[key]!);
105
126
  onError?.(e);
106
127
  }
107
128
  }
108
129
 
109
- #handlePolling(options: HandlePollingOptions): void {
110
- const { scanId, onSuccess, onError } = options;
111
-
112
- if (this.#timerPollingRef) {
113
- clearInterval(this.#timerPollingRef);
114
- }
115
-
116
- this.#timerPollingRef = setTimeout(() => {
117
- this.#getMeasurementsCheck({ scanId, onSuccess, onError });
118
- }, this.#count * 5000);
119
- }
120
-
121
- #disconnectSocket(): void {
122
- this.#measurementSocketRef?.close();
123
- if (this.#timerWaitingRef) {
124
- clearTimeout(this.#timerWaitingRef);
125
- }
126
- }
127
-
128
- #handleTimeOut(options: HandleTimeOutOptions): void {
129
- const { scanId, onSuccess, onError } = options;
130
-
131
- this.#count = 1;
132
- this.#timerWaitingRef = setTimeout(() => {
133
- this.#handlePolling({ scanId, onSuccess, onError });
134
- this.#disconnectSocket();
135
- }, 2 * 60000);
136
- }
137
-
138
130
  handleMeasurementSocket(options: MeasurementSocketOptions): void {
139
131
  const { scanId, onError, onSuccess, onClose, onOpen } = options;
140
-
141
132
  if (!checkParameters(scanId)) {
142
133
  throw new Error(REQUIRED_MESSAGE);
143
134
  }
144
- this.#handleSocket({ onOpen, scanId, onSuccess, onError, onClose, paramsKey: "scanId", isFallback: true,delay:5000 });
135
+ this.#handleSocket({ onOpen, scanId, onSuccess, onError, onClose, paramsKey: "scanId", isFallback: true, delay: 5000 });
145
136
  }
137
+
146
138
  handlFaceScaneSocket(options: FaceScanSocketOptions): void {
147
139
  const { faceScanId, onError, onSuccess, onClose, onOpen } = options;
148
-
149
140
  if (!checkParameters(faceScanId)) {
150
141
  throw new Error(REQUIRED_MESSAGE);
151
142
  }
152
- this.#handleSocket({ onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false,delay:1000 });
143
+ this.#handleSocket({ onOpen, faceScanId, onSuccess, onError, onClose, paramsKey: "faceScanId", isFallback: false, delay: 1000 });
153
144
  }
154
- #handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId,delay }: HandleSocket) {
145
+
146
+ #handleSocket({ onOpen, isFallback, scanId, onSuccess, onError, onClose, paramsKey, faceScanId, delay }: HandleSocket) {
147
+ const key = isFallback ? "measurement" : "faceScan";
155
148
  setTimeout(() => {
156
- this.#disconnectSocket();
149
+ this.#disconnectSocket(key);
157
150
  const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${API_ENDPOINTS.SCANNING}?${paramsKey}=${scanId || faceScanId}`;
158
- this.#measurementSocketRef = new WebSocket(url);
159
- this.#measurementSocketRef.onopen = () => {
151
+ const socket = new WebSocket(url);
152
+ this.#socketRefs[key] = socket;
153
+
154
+ socket.onopen = () => {
160
155
  onOpen?.();
161
156
  if (isFallback && scanId) {
162
- this.#handleTimeOut({ scanId, onSuccess, onError });
157
+ this.#handleTimeOut({ scanId, onSuccess, onError }, key);
163
158
  }
164
159
  };
165
160
 
166
- this.#measurementSocketRef.onmessage = (event: MessageEvent) => {
161
+ socket.onmessage = (event: MessageEvent) => {
167
162
  const data = JSON.parse(event.data);
168
163
  if (data?.code === 200 && data?.scanStatus === "success") {
169
164
  onSuccess?.(data);
170
165
  } else {
171
- if (this.#timerWaitingRef) {
172
- clearTimeout(this.#timerWaitingRef);
173
- }
166
+ clearTimeout(this.#waitingTimers[key]!);
174
167
  onError?.(data);
175
168
  }
176
- if (this.#timerWaitingRef && data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final") {
177
- clearTimeout(this.#timerWaitingRef);
169
+ if (data?.code === 200 && data?.scanStatus === "success" && data?.resultType === "final") {
170
+ clearTimeout(this.#waitingTimers[key]!);
178
171
  }
179
172
  };
180
173
 
181
- this.#measurementSocketRef.onclose = () => {
182
- onClose?.();
183
- };
184
-
185
- this.#measurementSocketRef.onerror = (event: Event) => {
186
- // onError?.(event);
187
- };
174
+ socket.onclose = () => onClose?.();
175
+ socket.onerror = () => {};
188
176
  }, delay);
189
177
  }
190
178
  }