Skip to content

Commit f46202c

Browse files
committed
fix: update onCodeScanned
1 parent 3500f5a commit f46202c

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

packages/pluggableWidgets/barcode-scanner-native/src/BarcodeScanner.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ export function BarcodeScanner(props: Props): ReactElement {
1616

1717
const styles = useMemo(() => flattenStyles(defaultBarcodeScannerStyle, props.style), [props.style]);
1818

19-
// Ref to track last scan time
20-
const lastScanRef = useRef(0);
19+
// Ref to track the lock state
20+
const isLockedRef = useRef(false);
2121

2222
const onCodeScanned = useCallback(
2323
(codes: Code[]) => {
24-
const now = Date.now();
25-
26-
// throttle: only allow once every 2000ms
27-
if (now - lastScanRef.current < 2000) {
24+
// Block if still in cooldown
25+
if (isLockedRef.current) {
2826
return;
2927
}
30-
lastScanRef.current = now;
3128

3229
if (props.barcode.status !== ValueStatus.Available || codes.length === 0 || !codes[0].value) {
3330
return;
@@ -39,6 +36,12 @@ export function BarcodeScanner(props: Props): ReactElement {
3936
}
4037

4138
executeAction(props.onDetect);
39+
40+
// Lock further scans for 2 seconds
41+
isLockedRef.current = true;
42+
setTimeout(() => {
43+
isLockedRef.current = false;
44+
}, 2000);
4245
},
4346
[props.barcode, props.onDetect]
4447
);

0 commit comments

Comments
 (0)