Skip to content

Commit

Permalink
chore(hooks): minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed Oct 6, 2023
1 parent d940452 commit fb05e96
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/hooks/useBarcodeScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type UseBarcodeScannerOptions = {
onBarcodeScanned: (barcodes: Barcode[]) => void;
disableHighlighting?: boolean;
defaultResizeMode?: CameraProps["resizeMode"];
scanMode?: "continuous" | "single";
scanMode?: "continuous" | "once";
};

export const useBarcodeScanner = ({
Expand All @@ -44,6 +44,7 @@ export const useBarcodeScanner = ({
resizeModeRef.value = ref.current?.props.resizeMode || defaultResizeMode;
}, [resizeModeRef, ref.current?.props.resizeMode, defaultResizeMode]);

//
const isPristineRef = useSharedValue<boolean>(true);

// Barcode highlights related state
Expand All @@ -66,13 +67,16 @@ export const useBarcodeScanner = ({
const { value: layout } = layoutRef;
const { value: prevBarcodes } = barcodesRef;
const { value: resizeMode } = resizeModeRef;
// Call the native barcode scanner
const barcodes = scanCodes(frame, codeTypes);
// console.log(JSON.stringify(barcodes, null, 2));

if (barcodes.length > 0) {
// If the scanMode is "continuous", we stream all the barcodes responses
if (scanMode === "continuous") {
onBarcodeScanned(barcodes);
} else if (scanMode === "single") {
// If the scanMode is "once", we only call the callback if the barcodes have actually changed
} else if (scanMode === "once") {
const hasChanged =
prevBarcodes.length !== barcodes.length ||
JSON.stringify(prevBarcodes.map(({ value }) => value)) !==
Expand All @@ -83,13 +87,6 @@ export const useBarcodeScanner = ({
}

if (prevBarcodes.length !== barcodes.length) {
// console.log(
// `frame: ${frame.width}x${frame.height} (${
// frame.orientation
// }) -> layout: ${layout.width.toFixed(2)}x${layout.height.toFixed(
// 2,
// )}`,
// );
onBarcodeScanned(barcodes);
}
barcodesRef.value = barcodes;
Expand All @@ -108,8 +105,8 @@ export const useBarcodeScanner = ({
layout,
resizeMode,
);
// Spare a re-render if the highlights didn't change
if (prevHighlights.length && !highlights.length) {
// Spare a re-render if the highlights are both empty
if (prevHighlights.length === 0 && highlights.length === 0) {
return;
}
setHighlightsJS(highlights);
Expand Down

0 comments on commit fb05e96

Please sign in to comment.