@ancon/wildcat-utils 1.54.0-check-optimizations.5 → 1.54.0

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.
Files changed (41) hide show
  1. package/api/generateBackoffWithFullJitter.d.ts +15 -0
  2. package/api/generateBackoffWithFullJitter.js +1 -0
  3. package/api/generateBackoffWithFullJitter.mjs +7 -0
  4. package/api/generateBackoffWithVariableJitter.d.ts +3 -1
  5. package/api/generateBackoffWithVariableJitter.js +1 -1
  6. package/api/generateBackoffWithVariableJitter.mjs +12 -9
  7. package/api/getRateLimitRetryDelay.d.ts +18 -0
  8. package/api/getRateLimitRetryDelay.js +1 -0
  9. package/api/getRateLimitRetryDelay.mjs +14 -0
  10. package/api/index.d.ts +3 -1
  11. package/api/index.js +1 -1
  12. package/api/index.mjs +20 -14
  13. package/check/getOrganizedCheckItemModifications.d.ts +2 -2
  14. package/check/getOrganizedCheckItemModifications.js +1 -1
  15. package/check/getOrganizedCheckItemModifications.mjs +68 -74
  16. package/check/getUniqueCheckItemModificationId.d.ts +2 -2
  17. package/check/getUniqueCheckItemModificationId.js +1 -1
  18. package/check/getUniqueCheckItemModificationId.mjs +5 -5
  19. package/error/getRateLimit.d.ts +31 -0
  20. package/error/getRateLimit.js +1 -0
  21. package/error/getRateLimit.mjs +60 -0
  22. package/error/index.d.ts +4 -2
  23. package/error/index.js +1 -1
  24. package/error/index.mjs +34 -28
  25. package/error/isRateLimitError.d.ts +7 -0
  26. package/error/isRateLimitError.js +1 -0
  27. package/error/isRateLimitError.mjs +8 -0
  28. package/index.js +1 -1
  29. package/index.mjs +375 -362
  30. package/multilingual/createLocalizedCheckDetails.d.ts +2 -2
  31. package/multilingual/createLocalizedCheckDetails.js +1 -1
  32. package/multilingual/createLocalizedCheckDetails.mjs +27 -31
  33. package/package.json +26 -1
  34. package/printing/StarScanConnection.d.ts +64 -0
  35. package/printing/StarScanConnection.js +1 -0
  36. package/printing/StarScanConnection.mjs +93 -0
  37. package/printing/createStarCloudScanHook.d.ts +10 -0
  38. package/printing/createStarCloudScanHook.js +1 -1
  39. package/printing/createStarCloudScanHook.mjs +131 -93
  40. package/printing/index.js +1 -1
  41. package/printing/index.mjs +15 -14
@@ -1,4 +1,4 @@
1
- import { CheckDetailsV2 } from '@ancon/wildcat-types';
1
+ import { CheckDetails } from '@ancon/wildcat-types';
2
2
  /**
3
3
  * This creates localized check details by replacing following properties with localized values based on current app language.
4
4
  * Affected properties on each check item (and its sub-items):
@@ -12,5 +12,5 @@ import { CheckDetailsV2 } from '@ancon/wildcat-types';
12
12
  * @param locale - locale to be used for localization
13
13
  * @returns localized check details
14
14
  */
15
- declare function createLocalizedCheckDetails(checkDetails: CheckDetailsV2, locale: string): CheckDetailsV2;
15
+ declare function createLocalizedCheckDetails(checkDetails: CheckDetails, locale: string): CheckDetails;
16
16
  export default createLocalizedCheckDetails;
@@ -1 +1 @@
1
- "use strict";const r=require("@ancon/wildcat-types");function d(e){switch(e){case"sv":return r.IETFLanguageTag.Swedish;case"nb":return r.IETFLanguageTag.Norwegian;case"en":return r.IETFLanguageTag.English;default:return null}}function s(e,a){var t,i;if(e){const c=e,n=(t=e.localizedProductNames)==null?void 0:t[a],o=(i=e.localizedVariantNames)==null?void 0:i[a];return n&&(c.productName=n),o&&(c.variantName=o),c}return e}function m(e,a){var n,o;const t=e,i=(n=e.localizedNames)==null?void 0:n[a],c=(o=e.localizedReplacementNames)==null?void 0:o[a];return i&&(t.name=i),c&&(t.replacementName=c),t}function z(e,a){const t=e;return t.product=s(e.product,a),t}function u(e,a){const t=e,{product:i,modifications:c,addOnProducts:n,subCheckItems:o}=e;return t.product=s(i,a),c!=null&&c.length&&(t.modifications=c.map(l=>m(l,a))),n!=null&&n.length&&(t.addOnProducts=n==null?void 0:n.map(l=>z(l,a))),o!=null&&o.length&&(t.subCheckItems=o.map(l=>u(l,a))),t}function f(e,a){var i;const t=d(a);if(t){const c=e;return c.items=(i=e.items)==null?void 0:i.map(n=>u(n,t)),c}return e}module.exports=f;
1
+ "use strict";const l=require("@ancon/wildcat-types");function u(e){switch(e){case"sv":return l.IETFLanguageTag.Swedish;case"nb":return l.IETFLanguageTag.Norwegian;case"en":return l.IETFLanguageTag.English;default:return null}}function s(e,c){var i,o;const t=e,a=(i=e.localizedProductNames)==null?void 0:i[c],n=(o=e.localizedVariantNames)==null?void 0:o[c];return a&&(t.productName=a),n&&(t.variantName=n),t}function m(e,c){var i,o;const t=e,a=(i=e.localizedNames)==null?void 0:i[c],n=(o=e.localizedReplacementNames)==null?void 0:o[c];return a&&(t.name=a),n&&(t.replacementName=n),t}function z(e,c){const t=e;return t.product=s(e.product,c),t}function d(e,c){const t=e,{product:a,modifications:n,addOnProducts:i,subCheckItems:o}=e;return t.product=s(a,c),n.length&&(t.modifications=n.map(r=>m(r,c))),i!=null&&i.length&&(t.addOnProducts=i.map(r=>z(r,c))),o.length&&(t.subCheckItems=o.map(r=>d(r,c))),t}function f(e,c){const t=u(c);if(t){const a=e;return a.items=e.items.map(n=>d(n,t)),a}return e}module.exports=f;
@@ -1,5 +1,5 @@
1
1
  import { IETFLanguageTag as r } from "@ancon/wildcat-types";
2
- function m(e) {
2
+ function u(e) {
3
3
  switch (e) {
4
4
  case "sv":
5
5
  return r.Swedish;
@@ -11,47 +11,43 @@ function m(e) {
11
11
  return null;
12
12
  }
13
13
  }
14
- function u(e, a) {
15
- var t, i;
16
- if (e) {
17
- const c = e, n = (t = e.localizedProductNames) == null ? void 0 : t[a], o = (i = e.localizedVariantNames) == null ? void 0 : i[a];
18
- return n && (c.productName = n), o && (c.variantName = o), c;
19
- }
20
- return e;
14
+ function d(e, c) {
15
+ var i, n;
16
+ const t = e, a = (i = e.localizedProductNames) == null ? void 0 : i[c], o = (n = e.localizedVariantNames) == null ? void 0 : n[c];
17
+ return a && (t.productName = a), o && (t.variantName = o), t;
21
18
  }
22
- function s(e, a) {
23
- var n, o;
24
- const t = e, i = (n = e.localizedNames) == null ? void 0 : n[a], c = (o = e.localizedReplacementNames) == null ? void 0 : o[a];
25
- return i && (t.name = i), c && (t.replacementName = c), t;
19
+ function m(e, c) {
20
+ var i, n;
21
+ const t = e, a = (i = e.localizedNames) == null ? void 0 : i[c], o = (n = e.localizedReplacementNames) == null ? void 0 : n[c];
22
+ return a && (t.name = a), o && (t.replacementName = o), t;
26
23
  }
27
- function f(e, a) {
24
+ function f(e, c) {
28
25
  const t = e;
29
- return t.product = u(
26
+ return t.product = d(
30
27
  e.product,
31
- a
28
+ c
32
29
  ), t;
33
30
  }
34
- function d(e, a) {
35
- const t = e, { product: i, modifications: c, addOnProducts: n, subCheckItems: o } = e;
36
- return t.product = u(i, a), c != null && c.length && (t.modifications = c.map(
37
- (l) => s(l, a)
38
- )), n != null && n.length && (t.addOnProducts = n == null ? void 0 : n.map(
39
- (l) => f(l, a)
40
- )), o != null && o.length && (t.subCheckItems = o.map(
41
- (l) => d(l, a)
31
+ function s(e, c) {
32
+ const t = e, { product: a, modifications: o, addOnProducts: i, subCheckItems: n } = e;
33
+ return t.product = d(a, c), o.length && (t.modifications = o.map(
34
+ (l) => m(l, c)
35
+ )), i != null && i.length && (t.addOnProducts = i.map(
36
+ (l) => f(l, c)
37
+ )), n.length && (t.subCheckItems = n.map(
38
+ (l) => s(l, c)
42
39
  )), t;
43
40
  }
44
- function N(e, a) {
45
- var i;
46
- const t = m(a);
41
+ function p(e, c) {
42
+ const t = u(c);
47
43
  if (t) {
48
- const c = e;
49
- return c.items = (i = e.items) == null ? void 0 : i.map(
50
- (n) => d(n, t)
51
- ), c;
44
+ const a = e;
45
+ return a.items = e.items.map(
46
+ (o) => s(o, t)
47
+ ), a;
52
48
  }
53
49
  return e;
54
50
  }
55
51
  export {
56
- N as default
52
+ p as default
57
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.54.0-check-optimizations.5",
3
+ "version": "1.54.0",
4
4
  "main": "index.js",
5
5
  "module": "index.mjs",
6
6
  "types": "index.d.ts",
@@ -15,6 +15,11 @@
15
15
  "require": "./api/generateBackoffWithEqualJitter.js",
16
16
  "types": "./api/generateBackoffWithEqualJitter.d.ts"
17
17
  },
18
+ "./api/generateBackoffWithFullJitter": {
19
+ "import": "./api/generateBackoffWithFullJitter.mjs",
20
+ "require": "./api/generateBackoffWithFullJitter.js",
21
+ "types": "./api/generateBackoffWithFullJitter.d.ts"
22
+ },
18
23
  "./api/generateBackoffWithVariableJitter": {
19
24
  "import": "./api/generateBackoffWithVariableJitter.mjs",
20
25
  "require": "./api/generateBackoffWithVariableJitter.js",
@@ -30,6 +35,11 @@
30
35
  "require": "./api/getMediaImageClosestSize.js",
31
36
  "types": "./api/getMediaImageClosestSize.d.ts"
32
37
  },
38
+ "./api/getRateLimitRetryDelay": {
39
+ "import": "./api/getRateLimitRetryDelay.mjs",
40
+ "require": "./api/getRateLimitRetryDelay.js",
41
+ "types": "./api/getRateLimitRetryDelay.d.ts"
42
+ },
33
43
  "./api/getUrlAndParams": {
34
44
  "import": "./api/getUrlAndParams.mjs",
35
45
  "require": "./api/getUrlAndParams.js",
@@ -460,6 +470,11 @@
460
470
  "require": "./error/getErrorCode.js",
461
471
  "types": "./error/getErrorCode.d.ts"
462
472
  },
473
+ "./error/getRateLimit": {
474
+ "import": "./error/getRateLimit.mjs",
475
+ "require": "./error/getRateLimit.js",
476
+ "types": "./error/getRateLimit.d.ts"
477
+ },
463
478
  "./error": {
464
479
  "import": "./error/index.mjs",
465
480
  "require": "./error/index.js",
@@ -500,6 +515,11 @@
500
515
  "require": "./error/isNotFoundError.js",
501
516
  "types": "./error/isNotFoundError.d.ts"
502
517
  },
518
+ "./error/isRateLimitError": {
519
+ "import": "./error/isRateLimitError.mjs",
520
+ "require": "./error/isRateLimitError.js",
521
+ "types": "./error/isRateLimitError.d.ts"
522
+ },
503
523
  "./error/isSerializedAPIError": {
504
524
  "import": "./error/isSerializedAPIError.mjs",
505
525
  "require": "./error/isSerializedAPIError.js",
@@ -1025,6 +1045,11 @@
1025
1045
  "require": "./preOrder/isPreOrderSharable.js",
1026
1046
  "types": "./preOrder/isPreOrderSharable.d.ts"
1027
1047
  },
1048
+ "./printing/StarScanConnection": {
1049
+ "import": "./printing/StarScanConnection.mjs",
1050
+ "require": "./printing/StarScanConnection.js",
1051
+ "types": "./printing/StarScanConnection.d.ts"
1052
+ },
1028
1053
  "./printing/createStarCloudScanHook": {
1029
1054
  "import": "./printing/createStarCloudScanHook.mjs",
1030
1055
  "require": "./printing/createStarCloudScanHook.js",
@@ -0,0 +1,64 @@
1
+ import type { StarMonitor } from '@ancon/wildcat-printing';
2
+ export declare type StarScanLogger = {
3
+ info: (message: string, data?: Record<string, unknown>) => void;
4
+ };
5
+ export declare enum StarScanReleaseReason {
6
+ /** A card was read, the scan is done */
7
+ CardRead = "CardRead",
8
+ /** The app went to the background */
9
+ Background = "Background",
10
+ /** The scan screen closed */
11
+ Unmount = "Unmount",
12
+ /** A new scan attempt replaced this one */
13
+ Restart = "Restart"
14
+ }
15
+ /**
16
+ * Owns the printer connection of one card scan screen.
17
+ *
18
+ * A Star printer accepts one connection at a time, and a card scan holds it
19
+ * for as long as it waits for a card. Another app on the device (POS and
20
+ * Express share devices and printers) cannot print meanwhile, so the
21
+ * connection is released when the app goes to the background and taken again
22
+ * when it returns.
23
+ */
24
+ export default class StarScanConnection {
25
+ private logger?;
26
+ private monitor?;
27
+ private identifier?;
28
+ private scanAttempt;
29
+ private isBackground;
30
+ private isPausedForBackground;
31
+ constructor(logger?: StarScanLogger | undefined);
32
+ /**
33
+ * Starts a scan attempt. An earlier attempt that is still discovering the
34
+ * printer may no longer connect.
35
+ */
36
+ beginScan(): number;
37
+ /**
38
+ * Whether this is still the latest scan attempt. Results of an older
39
+ * attempt, such as a failed discover, must not change the screen.
40
+ */
41
+ isLatest(scanAttempt: number): boolean;
42
+ /**
43
+ * Whether the monitor is still the one this screen uses. Events and
44
+ * failures of a released or replaced monitor must not change the screen.
45
+ */
46
+ isAttached(monitor: StarMonitor): boolean;
47
+ /**
48
+ * Whether the scan attempt may connect to the printer now: it is the latest
49
+ * attempt and the app is in the foreground. An attempt held back by the
50
+ * background is started again when the app returns.
51
+ */
52
+ mayConnect(scanAttempt: number): boolean;
53
+ /** Takes over a monitor that is about to connect, releasing any earlier one */
54
+ attach(monitor: StarMonitor, identifier: string): void;
55
+ /** Stops the monitor, which releases the printer connection */
56
+ release(reason: StarScanReleaseReason): void;
57
+ /**
58
+ * Call when the app moves between foreground and background. Returns true
59
+ * when a scan that was paused by the background should start again.
60
+ *
61
+ * @param isScanning - whether the screen is waiting for the printer or a card
62
+ */
63
+ setBackground(isBackground: boolean, isScanning: boolean): boolean;
64
+ }
@@ -0,0 +1 @@
1
+ "use strict";var c=Object.defineProperty;var d=(s,t,e)=>t in s?c(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var r=(s,t,e)=>(d(s,typeof t!="symbol"?t+"":t,e),e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var u=(s=>(s.CardRead="CardRead",s.Background="Background",s.Unmount="Unmount",s.Restart="Restart",s))(u||{});class g{constructor(t){r(this,"monitor");r(this,"identifier");r(this,"scanAttempt",0);r(this,"isBackground",!1);r(this,"isPausedForBackground",!1);this.logger=t}beginScan(){return this.scanAttempt+=1,this.isPausedForBackground=!1,this.scanAttempt}isLatest(t){return t===this.scanAttempt}isAttached(t){return this.monitor===t}mayConnect(t){return this.isLatest(t)?this.isBackground?(this.isPausedForBackground=!0,!1):!0:!1}attach(t,e){var i;this.release("Restart"),this.monitor=t,this.identifier=e,(i=this.logger)==null||i.info("Card scan connecting to printer",{identifier:e,sourceContext:"Printing"})}release(t){var a;const{monitor:e,identifier:i}=this;e&&(this.monitor=void 0,this.identifier=void 0,(a=this.logger)==null||a.info("Card scan released printer",{identifier:i,reason:t,sourceContext:"Printing"}),Promise.resolve(e.stop()).catch(n=>{var o;(o=this.logger)==null||o.info("Card scan release failed",{identifier:i,reason:t,error:(n==null?void 0:n.message)??String(n),sourceContext:"Printing"})}))}setBackground(t,e){if(t===this.isBackground)return!1;if(this.isBackground=t,t)return e&&(this.isPausedForBackground=!0),this.scanAttempt+=1,this.release("Background"),!1;const i=this.isPausedForBackground;return this.isPausedForBackground=!1,i}}exports.StarScanReleaseReason=u;exports.default=g;
@@ -0,0 +1,93 @@
1
+ var u = Object.defineProperty;
2
+ var c = (i, t, s) => t in i ? u(i, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : i[t] = s;
3
+ var r = (i, t, s) => (c(i, typeof t != "symbol" ? t + "" : t, s), s);
4
+ var d = /* @__PURE__ */ ((i) => (i.CardRead = "CardRead", i.Background = "Background", i.Unmount = "Unmount", i.Restart = "Restart", i))(d || {});
5
+ class g {
6
+ constructor(t) {
7
+ r(this, "monitor");
8
+ r(this, "identifier");
9
+ r(this, "scanAttempt", 0);
10
+ r(this, "isBackground", !1);
11
+ r(this, "isPausedForBackground", !1);
12
+ this.logger = t;
13
+ }
14
+ /**
15
+ * Starts a scan attempt. An earlier attempt that is still discovering the
16
+ * printer may no longer connect.
17
+ */
18
+ beginScan() {
19
+ return this.scanAttempt += 1, this.isPausedForBackground = !1, this.scanAttempt;
20
+ }
21
+ /**
22
+ * Whether this is still the latest scan attempt. Results of an older
23
+ * attempt, such as a failed discover, must not change the screen.
24
+ */
25
+ isLatest(t) {
26
+ return t === this.scanAttempt;
27
+ }
28
+ /**
29
+ * Whether the monitor is still the one this screen uses. Events and
30
+ * failures of a released or replaced monitor must not change the screen.
31
+ */
32
+ isAttached(t) {
33
+ return this.monitor === t;
34
+ }
35
+ /**
36
+ * Whether the scan attempt may connect to the printer now: it is the latest
37
+ * attempt and the app is in the foreground. An attempt held back by the
38
+ * background is started again when the app returns.
39
+ */
40
+ mayConnect(t) {
41
+ return this.isLatest(t) ? this.isBackground ? (this.isPausedForBackground = !0, !1) : !0 : !1;
42
+ }
43
+ /** Takes over a monitor that is about to connect, releasing any earlier one */
44
+ attach(t, s) {
45
+ var e;
46
+ this.release(
47
+ "Restart"
48
+ /* Restart */
49
+ ), this.monitor = t, this.identifier = s, (e = this.logger) == null || e.info("Card scan connecting to printer", {
50
+ identifier: s,
51
+ sourceContext: "Printing"
52
+ });
53
+ }
54
+ /** Stops the monitor, which releases the printer connection */
55
+ release(t) {
56
+ var a;
57
+ const { monitor: s, identifier: e } = this;
58
+ s && (this.monitor = void 0, this.identifier = void 0, (a = this.logger) == null || a.info("Card scan released printer", {
59
+ identifier: e,
60
+ reason: t,
61
+ sourceContext: "Printing"
62
+ }), Promise.resolve(s.stop()).catch((n) => {
63
+ var o;
64
+ (o = this.logger) == null || o.info("Card scan release failed", {
65
+ identifier: e,
66
+ reason: t,
67
+ error: (n == null ? void 0 : n.message) ?? String(n),
68
+ sourceContext: "Printing"
69
+ });
70
+ }));
71
+ }
72
+ /**
73
+ * Call when the app moves between foreground and background. Returns true
74
+ * when a scan that was paused by the background should start again.
75
+ *
76
+ * @param isScanning - whether the screen is waiting for the printer or a card
77
+ */
78
+ setBackground(t, s) {
79
+ if (t === this.isBackground)
80
+ return !1;
81
+ if (this.isBackground = t, t)
82
+ return s && (this.isPausedForBackground = !0), this.scanAttempt += 1, this.release(
83
+ "Background"
84
+ /* Background */
85
+ ), !1;
86
+ const e = this.isPausedForBackground;
87
+ return this.isPausedForBackground = !1, e;
88
+ }
89
+ }
90
+ export {
91
+ d as StarScanReleaseReason,
92
+ g as default
93
+ };
@@ -1,5 +1,6 @@
1
1
  import { ClientSettingsPrinter, DiscoveredStarPrinter } from '@ancon/wildcat-types';
2
2
  import { StarCloudScanStatus } from './types';
3
+ import { type StarScanLogger } from './StarScanConnection';
3
4
  declare type ReduxSelectorsArg<RootState = unknown> = {
4
5
  selectedClientPrinterSelector: (state: RootState) => ClientSettingsPrinter | null;
5
6
  };
@@ -8,6 +9,15 @@ declare type ReduxHooksArg<RootState = unknown> = {
8
9
  getState: () => RootState;
9
10
  };
10
11
  useStarPrinterDiscover: () => (timeout: number) => Promise<DiscoveredStarPrinter[]>;
12
+ /**
13
+ * Whether the app is in the background (React Native AppState
14
+ * 'background'). When given, the printer connection is released while the
15
+ * app is in the background, so another app on the device can print, and
16
+ * the scan resumes on return.
17
+ */
18
+ useIsAppInBackground?: () => boolean;
19
+ /** Logs when a scan takes and releases the printer connection */
20
+ logger?: StarScanLogger;
11
21
  };
12
22
  export declare type SanitizeRawOutputOptions = {
13
23
  /** Enable or disable sanitization of raw output */
@@ -1 +1 @@
1
- "use strict";const o=require("react"),L=require("dayjs"),I=require("@ancon/wildcat-printing"),B=require("../shared/hooks/usePrevious.js"),J=require("../shared/hooks/useMountedRef.js"),n=require("./types.js"),K=["TSP654"],Q=1;function V(T,p,x,N,d){const{useAppStore:_,useStarPrinterDiscover:F}=p,{selectedClientPrinterSelector:M}=x;function w(){const e=J(),D=_(),h=F(),P=o.useRef(void 0),[i,A]=o.useState(T),[r,f]=o.useState(),[l,v]=o.useState(),y=B(l),[U,g]=o.useState(),[W,E]=o.useState(N),a=o.useCallback(c=>{e!=null&&e.current&&A(c)},[e]),H=c=>{if((d==null?void 0:d.sanitizeEnabled)??!1){const s=(d==null?void 0:d.sanitizeRegex)??/[\D]/gi;return c.replace(s,"")}return c},m=o.useCallback(async c=>{var u;(u=P.current)==null||u.stop(),a(n.StarCloudScanStatus.ScanningComplete);const S=String.fromCharCode(...c).trim(),s=H(S);E(s)},[a]),k=o.useCallback(async({macAddress:c,ipAddress:S})=>{function s(t){a(n.StarCloudScanStatus.CommunicationError),e!=null&&e.current&&g(t==null?void 0:t.message)}const u={onStarted:t=>{if(t&&t.inputDevicesCount>0){const{inputDevicesCount:C}=t;a(n.StarCloudScanStatus.ReadyToScan),f({inputDeviceCount:C,lastConnectedOn:new Date})}},onStopped:t=>{t&&s(t),f(void 0)},inputDevice:{onConnected:()=>{const t=((r==null?void 0:r.inputDeviceCount)||0)+1;a(t>0?n.StarCloudScanStatus.ReadyToScan:n.StarCloudScanStatus.PrinterDetected),f({inputDeviceCount:t,lastConnectedOn:new Date})},onDisconnected:()=>{const t=((r==null?void 0:r.inputDeviceCount)||0)-1>=0?((r==null?void 0:r.inputDeviceCount)||0)-1:0;a(t>0?n.StarCloudScanStatus.ReadyToScan:n.StarCloudScanStatus.PrinterDetected),f({inputDeviceCount:t})},onCommunicationError:s,onDataReceived:t=>v({data:t,lastReceivedOn:new Date})}};if(P.current=I.monitor(S||c,u),e!=null&&e.current)try{await P.current.start()}catch(t){a(n.StarCloudScanStatus.CommunicationError),e!=null&&e.current&&g(t==null?void 0:t.message)}},[r,a,e]),b=o.useCallback(async()=>{e!=null&&e.current&&(a(n.StarCloudScanStatus.WaitingForPrinter),g(void 0),v(void 0),f(void 0),E(void 0));const c=M(D.getState()),{macAddress:S,serialNumber:s}=c;try{const t=(await h(2e3)).find(C=>S&&C.macAddress.toLowerCase()===S.toLowerCase()||s&&C.serialNumber===s);if(e!=null&&e.current){if(t){if(K.some(C=>{var q;return(q=t.model)==null?void 0:q.toLowerCase().includes(C.toLowerCase())})){a(n.StarCloudScanStatus.UnsupportedPrinter);return}k(t)}a(t?n.StarCloudScanStatus.PrinterDetected:n.StarCloudScanStatus.PrinterNotDetected)}}catch{e!=null&&e.current&&a(n.StarCloudScanStatus.PrinterNotDetected)}},[e,D,a,h,k]),j=i===n.StarCloudScanStatus.WaitingForPrinter||i===n.StarCloudScanStatus.PrinterDetected||i===n.StarCloudScanStatus.ReadyToScan||i===n.StarCloudScanStatus.ScanningComplete,G=i===n.StarCloudScanStatus.PrinterNotDetected||i===n.StarCloudScanStatus.ScanFailed||i===n.StarCloudScanStatus.CommunicationError||i===n.StarCloudScanStatus.UnsupportedPrinter;return o.useEffect(()=>{if(!y&&(l==null?void 0:l.data)&&r&&(r!=null&&r.lastConnectedOn)){const{lastConnectedOn:S}=r,{lastReceivedOn:s,data:u}=l;L(s).diff(L(S),"second",!0)>Q?m(u):v(void 0)}},[l,y,r,m]),o.useEffect(()=>(i===n.StarCloudScanStatus.WaitingForPrinter&&b(),()=>{var c;(c=P.current)==null||c.stop()}),[]),{startScan:b,changeDeviceStatus:a,scanStatus:i,reading:W,connectionError:U,isLoading:j,isError:G}}return w}module.exports=V;
1
+ "use strict";const a=require("react"),x=require("dayjs"),$=require("@ancon/wildcat-printing"),z=require("../shared/hooks/usePrevious.js"),O=require("../shared/hooks/useMountedRef.js"),n=require("./types.js"),A=require("./StarScanConnection.js"),tt=["TSP654"],nt=1,et=[n.StarCloudScanStatus.WaitingForPrinter,n.StarCloudScanStatus.PrinterDetected,n.StarCloudScanStatus.ReadyToScan];function rt(){return!1}function at(I,M,N,U,f){const{useAppStore:W,useStarPrinterDiscover:B,useIsAppInBackground:H=rt,logger:j}=M,{selectedClientPrinterSelector:G}=N;function J(){const t=O(),p=W(),b=B(),q=H(),y=a.useRef(null);y.current||(y.current=new A.default(j));const o=y.current,[i,K]=a.useState(I),[r,D]=a.useState(),[g,m]=a.useState(),L=z(g),[Q,k]=a.useState(),[V,T]=a.useState(U),c=a.useCallback(s=>{t!=null&&t.current&&K(s)},[t]),X=s=>{if((f==null?void 0:f.sanitizeEnabled)??!1){const u=(f==null?void 0:f.sanitizeRegex)??/[\D]/gi;return s.replace(u,"")}return s},w=a.useCallback(async s=>{o.release(A.StarScanReleaseReason.CardRead),c(n.StarCloudScanStatus.ScanningComplete);const d=String.fromCharCode(...s).trim(),u=X(d);T(u)},[c,o]),F=a.useCallback(async({macAddress:s,ipAddress:d})=>{let u;function S(e){return(...E)=>{u&&o.isAttached(u)&&e(...E)}}function v(e){c(n.StarCloudScanStatus.CommunicationError),t!=null&&t.current&&k(e==null?void 0:e.message)}const P={onStarted:S(e=>{if(e&&e.inputDevicesCount>0){const{inputDevicesCount:E}=e;c(n.StarCloudScanStatus.ReadyToScan),D({inputDeviceCount:E,lastConnectedOn:new Date})}}),onStopped:S(e=>{e&&v(e),D(void 0)}),inputDevice:{onConnected:S(()=>{const e=((r==null?void 0:r.inputDeviceCount)||0)+1;c(e>0?n.StarCloudScanStatus.ReadyToScan:n.StarCloudScanStatus.PrinterDetected),D({inputDeviceCount:e,lastConnectedOn:new Date})}),onDisconnected:S(()=>{const e=((r==null?void 0:r.inputDeviceCount)||0)-1>=0?((r==null?void 0:r.inputDeviceCount)||0)-1:0;c(e>0?n.StarCloudScanStatus.ReadyToScan:n.StarCloudScanStatus.PrinterDetected),D({inputDeviceCount:e})}),onCommunicationError:S(v),onDataReceived:S(e=>m({data:e,lastReceivedOn:new Date}))}},l=d||s,C=$.monitor(l,P);if(u=C,o.attach(C,l),t!=null&&t.current)try{await C.start()}catch(e){if(!o.isAttached(C))return;c(n.StarCloudScanStatus.CommunicationError),t!=null&&t.current&&k(e==null?void 0:e.message)}},[r,c,t,o]),h=a.useCallback(async()=>{const s=o.beginScan();t!=null&&t.current&&(c(n.StarCloudScanStatus.WaitingForPrinter),k(void 0),m(void 0),D(void 0),T(void 0));const d=G(p.getState()),{macAddress:u,serialNumber:S}=d;try{const P=(await b(2e3)).find(l=>u&&l.macAddress.toLowerCase()===u.toLowerCase()||S&&l.serialNumber===S);if(!o.mayConnect(s))return;if(t!=null&&t.current){if(P){if(tt.some(l=>{var C;return(C=P.model)==null?void 0:C.toLowerCase().includes(l.toLowerCase())})){c(n.StarCloudScanStatus.UnsupportedPrinter);return}F(P)}c(P?n.StarCloudScanStatus.PrinterDetected:n.StarCloudScanStatus.PrinterNotDetected)}}catch{if(!o.isLatest(s))return;t!=null&&t.current&&c(n.StarCloudScanStatus.PrinterNotDetected)}},[t,p,c,b,F,o]),R=a.useRef(i);R.current=i;const _=a.useRef(h);_.current=h,a.useEffect(()=>{o.setBackground(q,et.includes(R.current))&&(t!=null&&t.current)&&_.current()},[q,o,t]);const Y=i===n.StarCloudScanStatus.WaitingForPrinter||i===n.StarCloudScanStatus.PrinterDetected||i===n.StarCloudScanStatus.ReadyToScan||i===n.StarCloudScanStatus.ScanningComplete,Z=i===n.StarCloudScanStatus.PrinterNotDetected||i===n.StarCloudScanStatus.ScanFailed||i===n.StarCloudScanStatus.CommunicationError||i===n.StarCloudScanStatus.UnsupportedPrinter;return a.useEffect(()=>{if(!L&&(g==null?void 0:g.data)&&r&&(r!=null&&r.lastConnectedOn)){const{lastConnectedOn:d}=r,{lastReceivedOn:u,data:S}=g;x(u).diff(x(d),"second",!0)>nt?w(S):m(void 0)}},[g,L,r,w]),a.useEffect(()=>(i===n.StarCloudScanStatus.WaitingForPrinter&&h(),()=>{o.release(A.StarScanReleaseReason.Unmount)}),[]),{startScan:h,changeDeviceStatus:c,scanStatus:i,reading:V,connectionError:Q,isLoading:Y,isError:Z}}return J}module.exports=at;