@swan-admin/swan-ai-measurements 1.1.72 → 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;
package/dist/esm/tryOn.js CHANGED
@@ -18,74 +18,84 @@ 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 _TryOn_instances, _TryOn_tryOnSocketRef, _TryOn_timerWaitingRef, _TryOn_accessKey, _TryOn_stagingUrl, _TryOn_getSignedUrl, _TryOn_s3Upload, _TryOn_disconnectSocket, _TryOn_handleTimeOut, _TryOn_handleGetTryOnResult;
21
+ var _TryOn_instances, _TryOn_socketMap, _TryOn_timerMap, _TryOn_accessKey, _TryOn_stagingUrl, _TryOn_getSignedUrl, _TryOn_s3Upload, _TryOn_disconnectSocket, _TryOn_handleTimeOut, _TryOn_handleGetTryOnResult;
22
22
  import axios from "axios";
23
23
  import { API_ENDPOINTS, APP_AUTH_BASE_URL, APP_BASE_WEBSOCKET_URL, REQUIRED_ERROR_MESSAGE_INVALID_EMAIL, REQUIRED_MESSAGE } from "./constants.js";
24
24
  import { checkParameters, getUrl, isValidEmail } from "./utils.js";
25
25
  class TryOn {
26
26
  constructor(accessKey, stagingUrl = false) {
27
27
  _TryOn_instances.add(this);
28
- _TryOn_tryOnSocketRef.set(this, null);
29
- _TryOn_timerWaitingRef.set(this, null);
28
+ _TryOn_socketMap.set(this, new Map());
29
+ _TryOn_timerMap.set(this, new Map());
30
30
  _TryOn_accessKey.set(this, void 0);
31
31
  _TryOn_stagingUrl.set(this, void 0);
32
- _TryOn_disconnectSocket.set(this, () => {
33
- var _a;
34
- (_a = __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f")) === null || _a === void 0 ? void 0 : _a.close();
35
- if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
36
- clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
32
+ _TryOn_disconnectSocket.set(this, (tryonId) => {
33
+ if (tryonId) {
34
+ const socket = __classPrivateFieldGet(this, _TryOn_socketMap, "f").get(tryonId);
35
+ const timer = __classPrivateFieldGet(this, _TryOn_timerMap, "f").get(tryonId);
36
+ socket === null || socket === void 0 ? void 0 : socket.close();
37
+ if (timer)
38
+ clearTimeout(timer);
39
+ __classPrivateFieldGet(this, _TryOn_socketMap, "f").delete(tryonId);
40
+ __classPrivateFieldGet(this, _TryOn_timerMap, "f").delete(tryonId);
41
+ }
42
+ else {
43
+ // Disconnect all
44
+ __classPrivateFieldGet(this, _TryOn_socketMap, "f").forEach((socket) => socket.close());
45
+ __classPrivateFieldGet(this, _TryOn_timerMap, "f").forEach((timer) => clearTimeout(timer));
46
+ __classPrivateFieldGet(this, _TryOn_socketMap, "f").clear();
47
+ __classPrivateFieldGet(this, _TryOn_timerMap, "f").clear();
37
48
  }
38
49
  });
39
50
  _TryOn_handleTimeOut.set(this, ({ onSuccess, onError, tryonId }) => {
40
- __classPrivateFieldSet(this, _TryOn_timerWaitingRef, setTimeout(() => {
51
+ const timer = setTimeout(() => {
41
52
  __classPrivateFieldGet(this, _TryOn_handleGetTryOnResult, "f").call(this, { onSuccess, onError, tryonId });
42
- __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
43
- }, 300000), "f");
53
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this, tryonId);
54
+ }, 300000);
55
+ __classPrivateFieldGet(this, _TryOn_timerMap, "f").set(tryonId, timer);
44
56
  });
45
57
  this.handleTryOnWebSocket = ({ tryonId, onError, onSuccess, onClose, onOpen }) => {
46
58
  if (checkParameters(tryonId) === false) {
47
59
  throw new Error(REQUIRED_MESSAGE);
48
60
  }
49
- __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this);
61
+ __classPrivateFieldGet(this, _TryOn_disconnectSocket, "f").call(this, tryonId);
50
62
  const url = `${getUrl({ urlName: APP_BASE_WEBSOCKET_URL, stagingUrl: __classPrivateFieldGet(this, _TryOn_stagingUrl, "f") })}${API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
51
- __classPrivateFieldSet(this, _TryOn_tryOnSocketRef, new WebSocket(url), "f");
52
- if (__classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f")) {
53
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onopen = () => __awaiter(this, void 0, void 0, function* () {
54
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
55
- __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, tryonId });
56
- });
57
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onmessage = (event) => {
58
- let data;
59
- try {
60
- data = JSON.parse(event.data);
61
- }
62
- catch (error) {
63
- console.log(data, error, "not correct format for data");
64
- return;
65
- }
66
- if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
67
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
68
- }
69
- else {
70
- onError === null || onError === void 0 ? void 0 : onError(data);
71
- }
72
- if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
73
- clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
74
- }
75
- };
76
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onclose = () => {
77
- onClose === null || onClose === void 0 ? void 0 : onClose();
78
- };
79
- __classPrivateFieldGet(this, _TryOn_tryOnSocketRef, "f").onerror = (event) => {
80
- onError === null || onError === void 0 ? void 0 : onError(event);
81
- if (__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f")) {
82
- clearTimeout(__classPrivateFieldGet(this, _TryOn_timerWaitingRef, "f"));
83
- }
84
- };
85
- }
86
- else {
87
- console.log("no connection made for websocket");
88
- }
63
+ const socket = new WebSocket(url);
64
+ __classPrivateFieldGet(this, _TryOn_socketMap, "f").set(tryonId, socket);
65
+ socket.onopen = () => {
66
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
67
+ __classPrivateFieldGet(this, _TryOn_handleTimeOut, "f").call(this, { onSuccess, onError, tryonId });
68
+ };
69
+ socket.onmessage = (event) => {
70
+ let data;
71
+ try {
72
+ data = JSON.parse(event.data);
73
+ }
74
+ catch (error) {
75
+ console.log("Invalid JSON:", event.data);
76
+ return;
77
+ }
78
+ if ((data === null || data === void 0 ? void 0 : data.status) === "success") {
79
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
80
+ }
81
+ else {
82
+ onError === null || onError === void 0 ? void 0 : onError(data);
83
+ }
84
+ const timer = __classPrivateFieldGet(this, _TryOn_timerMap, "f").get(tryonId);
85
+ if (timer)
86
+ clearTimeout(timer);
87
+ __classPrivateFieldGet(this, _TryOn_timerMap, "f").delete(tryonId);
88
+ };
89
+ socket.onclose = () => {
90
+ onClose === null || onClose === void 0 ? void 0 : onClose();
91
+ // this.#disconnectSocket(tryonId);
92
+ };
93
+ socket.onerror = (event) => {
94
+ onError === null || onError === void 0 ? void 0 : onError(event);
95
+ // const timer = this.#timerMap.get(tryonId);
96
+ // if (timer) clearTimeout(timer);
97
+ // this.#timerMap.delete(tryonId);
98
+ };
89
99
  };
90
100
  _TryOn_handleGetTryOnResult.set(this, (_a) => __awaiter(this, [_a], void 0, function* ({ onSuccess, onError, tryonId }) {
91
101
  try {
@@ -201,7 +211,7 @@ class TryOn {
201
211
  });
202
212
  }
203
213
  }
204
- _TryOn_tryOnSocketRef = new WeakMap(), _TryOn_timerWaitingRef = new WeakMap(), _TryOn_accessKey = new WeakMap(), _TryOn_stagingUrl = new WeakMap(), _TryOn_disconnectSocket = new WeakMap(), _TryOn_handleTimeOut = new WeakMap(), _TryOn_handleGetTryOnResult = new WeakMap(), _TryOn_instances = new WeakSet(), _TryOn_getSignedUrl = function _TryOn_getSignedUrl(payload) {
214
+ _TryOn_socketMap = new WeakMap(), _TryOn_timerMap = new WeakMap(), _TryOn_accessKey = new WeakMap(), _TryOn_stagingUrl = new WeakMap(), _TryOn_disconnectSocket = new WeakMap(), _TryOn_handleTimeOut = new WeakMap(), _TryOn_handleGetTryOnResult = new WeakMap(), _TryOn_instances = new WeakSet(), _TryOn_getSignedUrl = function _TryOn_getSignedUrl(payload) {
205
215
  if (checkParameters(payload) === false) {
206
216
  throw new Error(REQUIRED_MESSAGE);
207
217
  }
@@ -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/dist/tryOn.js CHANGED
@@ -7,8 +7,8 @@ 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 TryOn {
10
- #tryOnSocketRef = null;
11
- #timerWaitingRef = null;
10
+ #socketMap = new Map();
11
+ #timerMap = new Map();
12
12
  #accessKey;
13
13
  #stagingUrl;
14
14
  constructor(accessKey, stagingUrl = false) {
@@ -97,62 +97,73 @@ class TryOn {
97
97
  data: payload,
98
98
  });
99
99
  }
100
- #disconnectSocket = () => {
101
- this.#tryOnSocketRef?.close();
102
- if (this.#timerWaitingRef) {
103
- clearTimeout(this.#timerWaitingRef);
100
+ #disconnectSocket = (tryonId) => {
101
+ if (tryonId) {
102
+ const socket = this.#socketMap.get(tryonId);
103
+ const timer = this.#timerMap.get(tryonId);
104
+ socket?.close();
105
+ if (timer)
106
+ clearTimeout(timer);
107
+ this.#socketMap.delete(tryonId);
108
+ this.#timerMap.delete(tryonId);
109
+ }
110
+ else {
111
+ // Disconnect all
112
+ this.#socketMap.forEach((socket) => socket.close());
113
+ this.#timerMap.forEach((timer) => clearTimeout(timer));
114
+ this.#socketMap.clear();
115
+ this.#timerMap.clear();
104
116
  }
105
117
  };
106
118
  #handleTimeOut = ({ onSuccess, onError, tryonId }) => {
107
- this.#timerWaitingRef = setTimeout(() => {
119
+ const timer = setTimeout(() => {
108
120
  this.#handleGetTryOnResult({ onSuccess, onError, tryonId });
109
- this.#disconnectSocket();
121
+ this.#disconnectSocket(tryonId);
110
122
  }, 300000);
123
+ this.#timerMap.set(tryonId, timer);
111
124
  };
112
125
  handleTryOnWebSocket = ({ tryonId, onError, onSuccess, onClose, onOpen }) => {
113
126
  if ((0, utils_js_1.checkParameters)(tryonId) === false) {
114
127
  throw new Error(constants_js_1.REQUIRED_MESSAGE);
115
128
  }
116
- this.#disconnectSocket();
129
+ this.#disconnectSocket(tryonId);
117
130
  const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_BASE_WEBSOCKET_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.TRY_ON}?tryonId=${tryonId}`;
118
- this.#tryOnSocketRef = new WebSocket(url);
119
- if (this.#tryOnSocketRef) {
120
- this.#tryOnSocketRef.onopen = async () => {
121
- onOpen?.();
122
- this.#handleTimeOut({ onSuccess, onError, tryonId });
123
- };
124
- this.#tryOnSocketRef.onmessage = (event) => {
125
- let data;
126
- try {
127
- data = JSON.parse(event.data);
128
- }
129
- catch (error) {
130
- console.log(data, error, "not correct format for data");
131
- return;
132
- }
133
- if (data?.status === "success") {
134
- onSuccess?.(data);
135
- }
136
- else {
137
- onError?.(data);
138
- }
139
- if (this.#timerWaitingRef) {
140
- clearTimeout(this.#timerWaitingRef);
141
- }
142
- };
143
- this.#tryOnSocketRef.onclose = () => {
144
- onClose?.();
145
- };
146
- this.#tryOnSocketRef.onerror = (event) => {
147
- onError?.(event);
148
- if (this.#timerWaitingRef) {
149
- clearTimeout(this.#timerWaitingRef);
150
- }
151
- };
152
- }
153
- else {
154
- console.log("no connection made for websocket");
155
- }
131
+ const socket = new WebSocket(url);
132
+ this.#socketMap.set(tryonId, socket);
133
+ socket.onopen = () => {
134
+ onOpen?.();
135
+ this.#handleTimeOut({ onSuccess, onError, tryonId });
136
+ };
137
+ socket.onmessage = (event) => {
138
+ let data;
139
+ try {
140
+ data = JSON.parse(event.data);
141
+ }
142
+ catch (error) {
143
+ console.log("Invalid JSON:", event.data);
144
+ return;
145
+ }
146
+ if (data?.status === "success") {
147
+ onSuccess?.(data);
148
+ }
149
+ else {
150
+ onError?.(data);
151
+ }
152
+ const timer = this.#timerMap.get(tryonId);
153
+ if (timer)
154
+ clearTimeout(timer);
155
+ this.#timerMap.delete(tryonId);
156
+ };
157
+ socket.onclose = () => {
158
+ onClose?.();
159
+ // this.#disconnectSocket(tryonId);
160
+ };
161
+ socket.onerror = (event) => {
162
+ onError?.(event);
163
+ // const timer = this.#timerMap.get(tryonId);
164
+ // if (timer) clearTimeout(timer);
165
+ // this.#timerMap.delete(tryonId);
166
+ };
156
167
  };
157
168
  handleTryOnSubmit({ shopDomain, products, selectedUserImages, requestSource, callbackUrl, openTryonId, selectedProductImageUrl, token }) {
158
169
  if ((0, utils_js_1.checkParameters)(shopDomain, products, token) === false) {
@@ -165,7 +176,7 @@ class TryOn {
165
176
  ...(requestSource !== undefined && requestSource !== null && { requestSource }),
166
177
  ...(callbackUrl !== undefined && callbackUrl !== null && { callbackUrl }),
167
178
  ...(openTryonId !== undefined && openTryonId !== null && { openTryonId }),
168
- ...(selectedProductImageUrl !== undefined && selectedProductImageUrl !== null && { selectedProductImageUrl })
179
+ ...(selectedProductImageUrl !== undefined && selectedProductImageUrl !== null && { selectedProductImageUrl }),
169
180
  };
170
181
  const url = `${(0, utils_js_1.getUrl)({ urlName: constants_js_1.APP_AUTH_BASE_URL, stagingUrl: this.#stagingUrl })}${constants_js_1.API_ENDPOINTS.TRY_ON}`;
171
182
  const headers = { "X-Api-Key": this.#accessKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swan-admin/swan-ai-measurements",
3
- "version": "1.1.72",
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",