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