react-zxing
Advanced tools
Comparing version 2.0.0 to 2.0.2
@@ -43,3 +43,4 @@ "use strict"; | ||
var resetStream = _a.resetStream; | ||
var _b = (0, react_1.useState)("idle"), status = _b[0], setStatus = _b[1]; | ||
var _b = (0, react_1.useState)(false), isOn = _b[0], setIsOn = _b[1]; | ||
var _c = (0, react_1.useState)(null), isAvailable = _c[0], setIsAvailable = _c[1]; | ||
var videoTrackRef = (0, react_1.useRef)(null); | ||
@@ -49,6 +50,5 @@ var resetStreamRef = (0, react_1.useRef)(resetStream); | ||
videoTrackRef.current = videoTrack; | ||
var available = typeof videoTrack.getCapabilities === "function" | ||
setIsAvailable(typeof videoTrack.getCapabilities === "function" | ||
? videoTrack.getCapabilities().torch !== undefined | ||
: false; | ||
setStatus(available ? "off" : "unavailable"); | ||
: false); | ||
}, []); | ||
@@ -59,3 +59,3 @@ var on = (0, react_1.useCallback)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
case 0: | ||
if (!videoTrackRef.current || status !== "off") | ||
if (!videoTrackRef.current || !isAvailable) | ||
return [2 /*return*/]; | ||
@@ -67,7 +67,7 @@ return [4 /*yield*/, videoTrackRef.current.applyConstraints({ | ||
_a.sent(); | ||
setStatus("on"); | ||
setIsOn(true); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, [status]); | ||
}); }, [isAvailable]); | ||
var off = (0, react_1.useCallback)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -77,3 +77,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (!videoTrackRef.current || status !== "on") | ||
if (!videoTrackRef.current || !isAvailable) | ||
return [2 /*return*/]; | ||
@@ -84,3 +84,4 @@ // applyConstraints with torch: false does not work to turn the flashlight off, as a stream's torch stays | ||
videoTrackRef.current = null; | ||
setStatus("off"); | ||
setIsAvailable(null); | ||
setIsOn(false); | ||
return [4 /*yield*/, resetStreamRef.current()]; | ||
@@ -92,8 +93,8 @@ case 1: | ||
}); | ||
}); }, [status]); | ||
}); }, [isAvailable]); | ||
(0, react_1.useEffect)(function () { | ||
resetStreamRef.current = resetStream; | ||
}, [resetStream]); | ||
return { init: init, status: status, on: on, off: off }; | ||
return { init: init, isOn: isOn, isAvailable: isAvailable, on: on, off: off }; | ||
}; | ||
exports.useTorch = useTorch; |
@@ -40,3 +40,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
var resetStream = _a.resetStream; | ||
var _b = useState("idle"), status = _b[0], setStatus = _b[1]; | ||
var _b = useState(false), isOn = _b[0], setIsOn = _b[1]; | ||
var _c = useState(null), isAvailable = _c[0], setIsAvailable = _c[1]; | ||
var videoTrackRef = useRef(null); | ||
@@ -46,6 +47,5 @@ var resetStreamRef = useRef(resetStream); | ||
videoTrackRef.current = videoTrack; | ||
var available = typeof videoTrack.getCapabilities === "function" | ||
setIsAvailable(typeof videoTrack.getCapabilities === "function" | ||
? videoTrack.getCapabilities().torch !== undefined | ||
: false; | ||
setStatus(available ? "off" : "unavailable"); | ||
: false); | ||
}, []); | ||
@@ -56,3 +56,3 @@ var on = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
case 0: | ||
if (!videoTrackRef.current || status !== "off") | ||
if (!videoTrackRef.current || !isAvailable) | ||
return [2 /*return*/]; | ||
@@ -64,7 +64,7 @@ return [4 /*yield*/, videoTrackRef.current.applyConstraints({ | ||
_a.sent(); | ||
setStatus("on"); | ||
setIsOn(true); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, [status]); | ||
}); }, [isAvailable]); | ||
var off = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -74,3 +74,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (!videoTrackRef.current || status !== "on") | ||
if (!videoTrackRef.current || !isAvailable) | ||
return [2 /*return*/]; | ||
@@ -81,3 +81,4 @@ // applyConstraints with torch: false does not work to turn the flashlight off, as a stream's torch stays | ||
videoTrackRef.current = null; | ||
setStatus("off"); | ||
setIsAvailable(null); | ||
setIsOn(false); | ||
return [4 /*yield*/, resetStreamRef.current()]; | ||
@@ -89,7 +90,7 @@ case 1: | ||
}); | ||
}); }, [status]); | ||
}); }, [isAvailable]); | ||
useEffect(function () { | ||
resetStreamRef.current = resetStream; | ||
}, [resetStream]); | ||
return { init: init, status: status, on: on, off: off }; | ||
return { init: init, isOn: isOn, isAvailable: isAvailable, on: on, off: off }; | ||
}; |
@@ -6,3 +6,4 @@ interface UseTorchOptions { | ||
init: (videoTrack: MediaStreamTrack) => void; | ||
status: "idle" | "on" | "off" | "unavailable"; | ||
isOn: boolean; | ||
isAvailable: boolean | null; | ||
on: () => Promise<void>; | ||
@@ -9,0 +10,0 @@ off: () => Promise<void>; |
@@ -20,3 +20,4 @@ /// <reference types="react" /> | ||
torch: { | ||
status: "idle" | "on" | "off" | "unavailable"; | ||
isOn: boolean; | ||
isAvailable: boolean | null; | ||
on: () => Promise<void>; | ||
@@ -23,0 +24,0 @@ off: () => Promise<void>; |
{ | ||
"name": "react-zxing", | ||
"version": "2.0.0", | ||
"version": "2.0.2", | ||
"description": "Integrate zxing to your React application using a custom hook", | ||
@@ -76,5 +76,5 @@ "license": "MIT", | ||
"engines": { | ||
"node": "^18 || ^20", | ||
"node": "^18 || ^20 || ^22", | ||
"yarn": "^1.22" | ||
} | ||
} |
@@ -58,3 +58,3 @@ <div align="center"> | ||
export const BarcodeScanner = () => { | ||
const { devices } = useMediaDevices(constraints); | ||
const { devices } = useMediaDevices({ constraints }); | ||
const deviceId = devices?.[0]?.deviceId; | ||
@@ -61,0 +61,0 @@ const { ref } = useZxing({ |
39419
22
597