@ancon/wildcat-utils 1.50.11 → 1.50.13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.50.11",
3
+ "version": "1.50.13",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -9,7 +9,16 @@ declare type ReduxHooksArg<RootState = unknown> = {
9
9
  };
10
10
  useStarPrinterDiscover: () => (timeout: number) => Promise<DiscoveredStarPrinter[]>;
11
11
  };
12
- declare function createUseStarCloudScanHook<RootState = unknown>(initialStatus: StarCloudScanStatus, hooks: ReduxHooksArg<RootState>, selectors: ReduxSelectorsArg<RootState>, initialReading?: string, shouldSanitizeRawOutput?: boolean): () => {
12
+ export declare type SanitizeRawOutputOptions = {
13
+ /** Enable or disable sanitization of raw output */
14
+ sanitizeEnabled: boolean;
15
+ /**
16
+ * Custom regex to sanitize raw output. Default removes all non-digit characters.
17
+ * @default /[\D]/gi
18
+ */
19
+ sanitizeRegex?: RegExp;
20
+ };
21
+ declare function createUseStarCloudScanHook<RootState = unknown>(initialStatus: StarCloudScanStatus, hooks: ReduxHooksArg<RootState>, selectors: ReduxSelectorsArg<RootState>, initialReading?: string, sanitizeRawOutputOptions?: SanitizeRawOutputOptions): () => {
13
22
  startScan: () => Promise<void>;
14
23
  changeDeviceStatus: (nextStatus: StarCloudScanStatus) => void;
15
24
  scanStatus: StarCloudScanStatus;
@@ -1 +1 @@
1
- "use strict";const o=require("react"),k=require("dayjs"),R=require("@ancon/wildcat-printing"),j=require("../shared/hooks/usePrevious.js"),G=require("../shared/hooks/useMountedRef.js"),e=require("./types.js"),I=["TSP654"],V=1;function z(b,q,L,O,T=!1){const{useAppStore:N,useStarPrinterDiscover:_}=q,{selectedClientPrinterSelector:F}=L;function M(){const n=G(),v=N(),D=_(),f=o.useRef(void 0),[s,x]=o.useState(b),[a,l]=o.useState(),[S,P]=o.useState(),g=j(S),[A,p]=o.useState(),[U,w]=o.useState(O),r=o.useCallback(c=>{n!=null&&n.current&&x(c)},[n]),y=o.useCallback(async c=>{var i;(i=f.current)==null||i.stop(),r(e.StarCloudScanStatus.ScanningComplete);const u=String.fromCharCode(...c).trim();w(T?u.replace(/[\D]/gi,""):u)},[r]),h=o.useCallback(async({macAddress:c,ipAddress:u})=>{function i(t){r(e.StarCloudScanStatus.CommunicationError),n!=null&&n.current&&p(t==null?void 0:t.message)}const C={onStarted:t=>{if(t&&t.inputDevicesCount>0){const{inputDevicesCount:d}=t;r(e.StarCloudScanStatus.ReadyToScan),l({inputDeviceCount:d,lastConnectedOn:new Date})}},onStopped:t=>{t&&i(t),l(void 0)},inputDevice:{onConnected:()=>{const t=((a==null?void 0:a.inputDeviceCount)||0)+1;r(t>0?e.StarCloudScanStatus.ReadyToScan:e.StarCloudScanStatus.PrinterDetected),l({inputDeviceCount:t,lastConnectedOn:new Date})},onDisconnected:()=>{const t=((a==null?void 0:a.inputDeviceCount)||0)-1>=0?((a==null?void 0:a.inputDeviceCount)||0)-1:0;r(t>0?e.StarCloudScanStatus.ReadyToScan:e.StarCloudScanStatus.PrinterDetected),l({inputDeviceCount:t})},onCommunicationError:i,onDataReceived:t=>P({data:t,lastReceivedOn:new Date})}};if(f.current=R.monitor(u||c,C),n!=null&&n.current)try{await f.current.start()}catch(t){r(e.StarCloudScanStatus.CommunicationError),n!=null&&n.current&&p(t==null?void 0:t.message)}},[a,r,n]),m=o.useCallback(async()=>{n!=null&&n.current&&(r(e.StarCloudScanStatus.WaitingForPrinter),p(void 0),P(void 0),l(void 0),w(void 0));const c=F(v.getState()),{macAddress:u,serialNumber:i}=c;try{const t=(await D(2e3)).find(d=>u&&d.macAddress.toLowerCase()===u.toLowerCase()||i&&d.serialNumber===i);if(n!=null&&n.current){if(t){if(I.some(d=>{var E;return(E=t.model)==null?void 0:E.toLowerCase().includes(d.toLowerCase())})){r(e.StarCloudScanStatus.UnsupportedPrinter);return}h(t)}r(t?e.StarCloudScanStatus.PrinterDetected:e.StarCloudScanStatus.PrinterNotDetected)}}catch{n!=null&&n.current&&r(e.StarCloudScanStatus.PrinterNotDetected)}},[n,v,r,D,h]),W=s===e.StarCloudScanStatus.WaitingForPrinter||s===e.StarCloudScanStatus.PrinterDetected||s===e.StarCloudScanStatus.ReadyToScan||s===e.StarCloudScanStatus.ScanningComplete,H=s===e.StarCloudScanStatus.PrinterNotDetected||s===e.StarCloudScanStatus.ScanFailed||s===e.StarCloudScanStatus.CommunicationError||s===e.StarCloudScanStatus.UnsupportedPrinter;return o.useEffect(()=>{if(!g&&(S==null?void 0:S.data)&&a&&(a!=null&&a.lastConnectedOn)){const{lastConnectedOn:u}=a,{lastReceivedOn:i,data:C}=S;k(i).diff(k(u),"second",!0)>V?y(C):P(void 0)}},[S,g,a,y]),o.useEffect(()=>(s===e.StarCloudScanStatus.WaitingForPrinter&&m(),()=>{var c;(c=f.current)==null||c.stop()}),[]),{startScan:m,changeDeviceStatus:r,scanStatus:s,reading:U,connectionError:A,isLoading:W,isError:H}}return M}module.exports=z;
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,134 +1,138 @@
1
- import { useRef as V, useState as S, useCallback as p, useEffect as O } from "react";
2
- import T from "dayjs";
3
- import { monitor as q } from "@ancon/wildcat-printing";
4
- import z from "../shared/hooks/usePrevious.mjs";
5
- import B from "../shared/hooks/useMountedRef.mjs";
6
- import { StarCloudScanStatus as e } from "./types.mjs";
7
- const J = ["TSP654"], K = 1;
8
- function tt(N, k, F, M, b = !1) {
9
- const { useAppStore: x, useStarPrinterDiscover: A } = k, { selectedClientPrinterSelector: U } = F;
10
- function W() {
11
- const n = B(), P = x(), D = A(), f = V(void 0), [i, H] = S(N), [o, C] = S(), [d, m] = S(), g = z(d), [R, v] = S(), [j, w] = S(M), r = p(
1
+ import { useRef as K, useState as f, useCallback as v, useEffect as N } from "react";
2
+ import b from "dayjs";
3
+ import { monitor as Q } from "@ancon/wildcat-printing";
4
+ import V from "../shared/hooks/usePrevious.mjs";
5
+ import X from "../shared/hooks/useMountedRef.mjs";
6
+ import { StarCloudScanStatus as t } from "./types.mjs";
7
+ const Y = ["TSP654"], Z = 1;
8
+ function ne(k, F, M, A, u) {
9
+ const { useAppStore: U, useStarPrinterDiscover: W } = F, { selectedClientPrinterSelector: w } = M;
10
+ function H() {
11
+ const n = X(), g = U(), y = W(), m = K(void 0), [s, j] = f(k), [r, C] = f(), [l, P] = f(), E = V(l), [G, D] = f(), [I, p] = f(A), o = v(
12
12
  (c) => {
13
- n != null && n.current && H(c);
13
+ n != null && n.current && j(c);
14
14
  },
15
15
  [n]
16
- ), y = p(
16
+ ), q = (c) => {
17
+ if ((u == null ? void 0 : u.sanitizeEnabled) ?? !1) {
18
+ const i = (u == null ? void 0 : u.sanitizeRegex) ?? /[\D]/gi;
19
+ return c.replace(i, "");
20
+ }
21
+ return c;
22
+ }, h = v(
17
23
  async (c) => {
18
- var s;
19
- (s = f.current) == null || s.stop(), r(e.ScanningComplete);
20
- const a = String.fromCharCode(...c).trim();
21
- w(
22
- b ? a.replace(/[\D]/gi, "") : a
23
- );
24
+ var d;
25
+ (d = m.current) == null || d.stop(), o(t.ScanningComplete);
26
+ const a = String.fromCharCode(...c).trim(), i = q(a);
27
+ p(i);
24
28
  },
25
- [r]
26
- ), E = p(
29
+ [o]
30
+ ), L = v(
27
31
  async ({ macAddress: c, ipAddress: a }) => {
28
- function s(t) {
29
- r(e.CommunicationError), n != null && n.current && v(t == null ? void 0 : t.message);
32
+ function i(e) {
33
+ o(t.CommunicationError), n != null && n.current && D(e == null ? void 0 : e.message);
30
34
  }
31
- const l = {
32
- onStarted: (t) => {
33
- if (t && t.inputDevicesCount > 0) {
34
- const { inputDevicesCount: u } = t;
35
- r(e.ReadyToScan), C({
36
- inputDeviceCount: u,
35
+ const d = {
36
+ onStarted: (e) => {
37
+ if (e && e.inputDevicesCount > 0) {
38
+ const { inputDevicesCount: S } = e;
39
+ o(t.ReadyToScan), C({
40
+ inputDeviceCount: S,
37
41
  lastConnectedOn: /* @__PURE__ */ new Date()
38
42
  });
39
43
  }
40
44
  },
41
- onStopped: (t) => {
42
- t && s(t), C(void 0);
45
+ onStopped: (e) => {
46
+ e && i(e), C(void 0);
43
47
  },
44
48
  inputDevice: {
45
49
  onConnected: () => {
46
- const t = ((o == null ? void 0 : o.inputDeviceCount) || 0) + 1;
47
- r(
48
- t > 0 ? e.ReadyToScan : e.PrinterDetected
50
+ const e = ((r == null ? void 0 : r.inputDeviceCount) || 0) + 1;
51
+ o(
52
+ e > 0 ? t.ReadyToScan : t.PrinterDetected
49
53
  ), C({
50
- inputDeviceCount: t,
54
+ inputDeviceCount: e,
51
55
  lastConnectedOn: /* @__PURE__ */ new Date()
52
56
  });
53
57
  },
54
58
  onDisconnected: () => {
55
- const t = ((o == null ? void 0 : o.inputDeviceCount) || 0) - 1 >= 0 ? ((o == null ? void 0 : o.inputDeviceCount) || 0) - 1 : 0;
56
- r(
57
- t > 0 ? e.ReadyToScan : e.PrinterDetected
59
+ const e = ((r == null ? void 0 : r.inputDeviceCount) || 0) - 1 >= 0 ? ((r == null ? void 0 : r.inputDeviceCount) || 0) - 1 : 0;
60
+ o(
61
+ e > 0 ? t.ReadyToScan : t.PrinterDetected
58
62
  ), C({
59
- inputDeviceCount: t
63
+ inputDeviceCount: e
60
64
  });
61
65
  },
62
- onCommunicationError: s,
63
- onDataReceived: (t) => m({ data: t, lastReceivedOn: /* @__PURE__ */ new Date() })
66
+ onCommunicationError: i,
67
+ onDataReceived: (e) => P({ data: e, lastReceivedOn: /* @__PURE__ */ new Date() })
64
68
  }
65
69
  };
66
- if (f.current = q(a || c, l), n != null && n.current)
70
+ if (m.current = Q(a || c, d), n != null && n.current)
67
71
  try {
68
- await f.current.start();
69
- } catch (t) {
70
- r(e.CommunicationError), n != null && n.current && v(t == null ? void 0 : t.message);
72
+ await m.current.start();
73
+ } catch (e) {
74
+ o(t.CommunicationError), n != null && n.current && D(e == null ? void 0 : e.message);
71
75
  }
72
76
  },
73
- [o, r, n]
74
- ), h = p(async () => {
75
- n != null && n.current && (r(e.WaitingForPrinter), v(void 0), m(void 0), C(void 0), w(void 0));
76
- const c = U(P.getState()), { macAddress: a, serialNumber: s } = c;
77
+ [r, o, n]
78
+ ), T = v(async () => {
79
+ n != null && n.current && (o(t.WaitingForPrinter), D(void 0), P(void 0), C(void 0), p(void 0));
80
+ const c = w(g.getState()), { macAddress: a, serialNumber: i } = c;
77
81
  try {
78
- const t = (await D(2e3)).find(
79
- (u) => a && u.macAddress.toLowerCase() === a.toLowerCase() || s && u.serialNumber === s
82
+ const e = (await y(2e3)).find(
83
+ (S) => a && S.macAddress.toLowerCase() === a.toLowerCase() || i && S.serialNumber === i
80
84
  );
81
85
  if (n != null && n.current) {
82
- if (t) {
83
- if (J.some(
84
- (u) => {
85
- var L;
86
- return (L = t.model) == null ? void 0 : L.toLowerCase().includes(u.toLowerCase());
86
+ if (e) {
87
+ if (Y.some(
88
+ (S) => {
89
+ var x;
90
+ return (x = e.model) == null ? void 0 : x.toLowerCase().includes(S.toLowerCase());
87
91
  }
88
92
  )) {
89
- r(e.UnsupportedPrinter);
93
+ o(t.UnsupportedPrinter);
90
94
  return;
91
95
  }
92
- E(t);
96
+ L(e);
93
97
  }
94
- r(
95
- t ? e.PrinterDetected : e.PrinterNotDetected
98
+ o(
99
+ e ? t.PrinterDetected : t.PrinterNotDetected
96
100
  );
97
101
  }
98
102
  } catch {
99
- n != null && n.current && r(e.PrinterNotDetected);
103
+ n != null && n.current && o(t.PrinterNotDetected);
100
104
  }
101
105
  }, [
102
106
  n,
103
- P,
104
- r,
105
- D,
106
- E
107
- ]), G = i === e.WaitingForPrinter || i === e.PrinterDetected || i === e.ReadyToScan || i === e.ScanningComplete, I = i === e.PrinterNotDetected || i === e.ScanFailed || i === e.CommunicationError || i === e.UnsupportedPrinter;
108
- return O(() => {
109
- if (!g && (d == null ? void 0 : d.data) && o && (o != null && o.lastConnectedOn)) {
110
- const { lastConnectedOn: a } = o, { lastReceivedOn: s, data: l } = d;
111
- T(s).diff(
112
- T(a),
107
+ g,
108
+ o,
109
+ y,
110
+ L
111
+ ]), B = s === t.WaitingForPrinter || s === t.PrinterDetected || s === t.ReadyToScan || s === t.ScanningComplete, J = s === t.PrinterNotDetected || s === t.ScanFailed || s === t.CommunicationError || s === t.UnsupportedPrinter;
112
+ return N(() => {
113
+ if (!E && (l == null ? void 0 : l.data) && r && (r != null && r.lastConnectedOn)) {
114
+ const { lastConnectedOn: a } = r, { lastReceivedOn: i, data: d } = l;
115
+ b(i).diff(
116
+ b(a),
113
117
  "second",
114
118
  !0
115
- ) > K ? y(l) : m(void 0);
119
+ ) > Z ? h(d) : P(void 0);
116
120
  }
117
- }, [d, g, o, y]), O(() => (i === e.WaitingForPrinter && h(), () => {
121
+ }, [l, E, r, h]), N(() => (s === t.WaitingForPrinter && T(), () => {
118
122
  var c;
119
- (c = f.current) == null || c.stop();
123
+ (c = m.current) == null || c.stop();
120
124
  }), []), {
121
- startScan: h,
122
- changeDeviceStatus: r,
123
- scanStatus: i,
124
- reading: j,
125
- connectionError: R,
126
- isLoading: G,
127
- isError: I
125
+ startScan: T,
126
+ changeDeviceStatus: o,
127
+ scanStatus: s,
128
+ reading: I,
129
+ connectionError: G,
130
+ isLoading: B,
131
+ isError: J
128
132
  };
129
133
  }
130
- return W;
134
+ return H;
131
135
  }
132
136
  export {
133
- tt as default
137
+ ne as default
134
138
  };