react-zxing
Advanced tools
Comparing version 1.1.4 to 2.0.0
@@ -8,2 +8,20 @@ # Changelog | ||
## [2.0.0] - 2023-08-17 | ||
### Changed | ||
- Updated the `@zxing/library` dependency to `0.20.0`. | ||
- Renamed `onResult` and `onError` to `onDecodeResult` and `onDecodeError` respectively. | ||
- Switched the `onDecodeError` error type to use the `Exception` class from `@zxing/library`. | ||
### Added | ||
- Introduced `onError` to handle errors that occur during initialization. | ||
## [1.1.4] - 2023-03-20 | ||
### Fixed | ||
- Added engines to `package.json` to prevent installation on unsupported Node.js versions. | ||
## [1.1.3] - 2023-02-13 | ||
@@ -10,0 +28,0 @@ |
@@ -43,4 +43,3 @@ "use strict"; | ||
var resetStream = _a.resetStream; | ||
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 _b = (0, react_1.useState)("idle"), status = _b[0], setStatus = _b[1]; | ||
var videoTrackRef = (0, react_1.useRef)(null); | ||
@@ -50,3 +49,6 @@ var resetStreamRef = (0, react_1.useRef)(resetStream); | ||
videoTrackRef.current = videoTrack; | ||
setIsAvailable(videoTrack.getCapabilities().torch !== undefined); | ||
var available = typeof videoTrack.getCapabilities === "function" | ||
? videoTrack.getCapabilities().torch !== undefined | ||
: false; | ||
setStatus(available ? "off" : "unavailable"); | ||
}, []); | ||
@@ -57,3 +59,3 @@ var on = (0, react_1.useCallback)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
case 0: | ||
if (!videoTrackRef.current || !isAvailable) | ||
if (!videoTrackRef.current || status !== "off") | ||
return [2 /*return*/]; | ||
@@ -65,7 +67,7 @@ return [4 /*yield*/, videoTrackRef.current.applyConstraints({ | ||
_a.sent(); | ||
setIsOn(true); | ||
setStatus("on"); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, [isAvailable]); | ||
}); }, [status]); | ||
var off = (0, react_1.useCallback)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -75,3 +77,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (!videoTrackRef.current || !isAvailable) | ||
if (!videoTrackRef.current || status !== "on") | ||
return [2 /*return*/]; | ||
@@ -82,4 +84,3 @@ // applyConstraints with torch: false does not work to turn the flashlight off, as a stream's torch stays | ||
videoTrackRef.current = null; | ||
setIsAvailable(null); | ||
setIsOn(false); | ||
setStatus("off"); | ||
return [4 /*yield*/, resetStreamRef.current()]; | ||
@@ -91,8 +92,8 @@ case 1: | ||
}); | ||
}); }, [isAvailable]); | ||
}); }, [status]); | ||
(0, react_1.useEffect)(function () { | ||
resetStreamRef.current = resetStream; | ||
}, [resetStream]); | ||
return { init: init, isOn: isOn, isAvailable: isAvailable, on: on, off: off }; | ||
return { init: init, status: status, on: on, off: off }; | ||
}; | ||
exports.useTorch = useTorch; |
@@ -56,13 +56,9 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
function isUseZxingOptionsWithDeviceId(options) { | ||
return options.deviceId !== undefined; | ||
} | ||
var useZxing = function (options) { | ||
if (options === void 0) { options = {}; } | ||
var _a = options.paused, paused = _a === void 0 ? false : _a, hints = options.hints, timeBetweenDecodingAttempts = options.timeBetweenDecodingAttempts, _b = options.onResult, onResult = _b === void 0 ? function () { } : _b, _c = options.onError, onError = _c === void 0 ? function () { } : _c; | ||
var deviceId = isUseZxingOptionsWithDeviceId(options) | ||
? options.deviceId | ||
: undefined; | ||
var _d = (0, react_1.useState)(isUseZxingOptionsWithDeviceId(options) ? undefined : options.constraints), constraints = _d[0], setConstraints = _d[1]; | ||
var resultHandlerRef = (0, react_1.useRef)(onResult); | ||
var _a = options.paused, paused = _a === void 0 ? false : _a, hints = options.hints, timeBetweenDecodingAttempts = options.timeBetweenDecodingAttempts, _b = options.onDecodeResult, onDecodeResult = _b === void 0 ? function () { } : _b, _c = options.onDecodeError, onDecodeError = _c === void 0 ? function () { } : _c, _d = options.onError, onError = _d === void 0 ? function () { } : _d; | ||
var deviceId = "deviceId" in options ? options.deviceId : undefined; | ||
var _e = (0, react_1.useState)("constraints" in options ? options.constraints : undefined), constraints = _e[0], setConstraints = _e[1]; | ||
var decodeResultHandlerRef = (0, react_1.useRef)(onDecodeResult); | ||
var decodeErrorHandlerRef = (0, react_1.useRef)(onDecodeError); | ||
var errorHandlerRef = (0, react_1.useRef)(onError); | ||
@@ -74,3 +70,3 @@ var ref = (0, react_1.useRef)(null); | ||
}); | ||
var _e = (0, useTorch_1.useTorch)({ | ||
var _f = (0, useTorch_1.useTorch)({ | ||
resetStream: function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -88,11 +84,11 @@ return __generator(this, function (_a) { | ||
}); }, | ||
}), torchInit = _e.init, torch = __rest(_e, ["init"]); | ||
}), torchInit = _f.init, torch = __rest(_f, ["init"]); | ||
var decodeCallback = (0, react_1.useCallback)(function (result, error) { | ||
if (result) | ||
resultHandlerRef.current(result); | ||
decodeResultHandlerRef.current(result); | ||
if (error) | ||
errorHandlerRef.current(error); | ||
decodeErrorHandlerRef.current(error); | ||
}, []); | ||
var startDecoding = (0, react_1.useCallback)(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var mediaStream, videoTrack; | ||
var e_1, mediaStream, videoTrack; | ||
return __generator(this, function (_a) { | ||
@@ -105,12 +101,22 @@ switch (_a.label) { | ||
return [2 /*return*/]; | ||
if (!deviceId) return [3 /*break*/, 2]; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 6, , 7]); | ||
if (!deviceId) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, reader.decodeFromVideoDevice(deviceId, ref.current, decodeCallback)]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, reader.decodeFromConstraints(constraints !== null && constraints !== void 0 ? constraints : constants_1.DEFAULT_CONSTRAINTS, ref.current, decodeCallback)]; | ||
case 3: | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, reader.decodeFromConstraints(constraints !== null && constraints !== void 0 ? constraints : constants_1.DEFAULT_CONSTRAINTS, ref.current, decodeCallback)]; | ||
case 4: | ||
_a.sent(); | ||
_a.label = 4; | ||
case 4: | ||
_a.label = 5; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: | ||
e_1 = _a.sent(); | ||
errorHandlerRef.current(e_1); | ||
return [2 /*return*/]; | ||
case 7: | ||
if (!ref.current) | ||
return [2 /*return*/]; | ||
mediaStream = ref.current.srcObject; | ||
@@ -128,5 +134,8 @@ videoTrack = mediaStream.getVideoTracks()[0]; | ||
(0, react_1.useEffect)(function () { | ||
resultHandlerRef.current = onResult; | ||
}, [onResult]); | ||
decodeResultHandlerRef.current = onDecodeResult; | ||
}, [onDecodeResult]); | ||
(0, react_1.useEffect)(function () { | ||
decodeErrorHandlerRef.current = onDecodeError; | ||
}, [onDecodeError]); | ||
(0, react_1.useEffect)(function () { | ||
errorHandlerRef.current = onError; | ||
@@ -133,0 +142,0 @@ }, [onError]); |
@@ -40,4 +40,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
var resetStream = _a.resetStream; | ||
var _b = useState(false), isOn = _b[0], setIsOn = _b[1]; | ||
var _c = useState(null), isAvailable = _c[0], setIsAvailable = _c[1]; | ||
var _b = useState("idle"), status = _b[0], setStatus = _b[1]; | ||
var videoTrackRef = useRef(null); | ||
@@ -47,3 +46,6 @@ var resetStreamRef = useRef(resetStream); | ||
videoTrackRef.current = videoTrack; | ||
setIsAvailable(videoTrack.getCapabilities().torch !== undefined); | ||
var available = typeof videoTrack.getCapabilities === "function" | ||
? videoTrack.getCapabilities().torch !== undefined | ||
: false; | ||
setStatus(available ? "off" : "unavailable"); | ||
}, []); | ||
@@ -54,3 +56,3 @@ var on = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
case 0: | ||
if (!videoTrackRef.current || !isAvailable) | ||
if (!videoTrackRef.current || status !== "off") | ||
return [2 /*return*/]; | ||
@@ -62,7 +64,7 @@ return [4 /*yield*/, videoTrackRef.current.applyConstraints({ | ||
_a.sent(); | ||
setIsOn(true); | ||
setStatus("on"); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }, [isAvailable]); | ||
}); }, [status]); | ||
var off = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -72,3 +74,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
if (!videoTrackRef.current || !isAvailable) | ||
if (!videoTrackRef.current || status !== "on") | ||
return [2 /*return*/]; | ||
@@ -79,4 +81,3 @@ // applyConstraints with torch: false does not work to turn the flashlight off, as a stream's torch stays | ||
videoTrackRef.current = null; | ||
setIsAvailable(null); | ||
setIsOn(false); | ||
setStatus("off"); | ||
return [4 /*yield*/, resetStreamRef.current()]; | ||
@@ -88,7 +89,7 @@ case 1: | ||
}); | ||
}); }, [isAvailable]); | ||
}); }, [status]); | ||
useEffect(function () { | ||
resetStreamRef.current = resetStream; | ||
}, [resetStream]); | ||
return { init: init, isOn: isOn, isAvailable: isAvailable, on: on, off: off }; | ||
return { init: init, status: status, on: on, off: off }; | ||
}; |
@@ -53,13 +53,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { deepCompareObjects } from "./utils"; | ||
function isUseZxingOptionsWithDeviceId(options) { | ||
return options.deviceId !== undefined; | ||
} | ||
export var useZxing = function (options) { | ||
if (options === void 0) { options = {}; } | ||
var _a = options.paused, paused = _a === void 0 ? false : _a, hints = options.hints, timeBetweenDecodingAttempts = options.timeBetweenDecodingAttempts, _b = options.onResult, onResult = _b === void 0 ? function () { } : _b, _c = options.onError, onError = _c === void 0 ? function () { } : _c; | ||
var deviceId = isUseZxingOptionsWithDeviceId(options) | ||
? options.deviceId | ||
: undefined; | ||
var _d = useState(isUseZxingOptionsWithDeviceId(options) ? undefined : options.constraints), constraints = _d[0], setConstraints = _d[1]; | ||
var resultHandlerRef = useRef(onResult); | ||
var _a = options.paused, paused = _a === void 0 ? false : _a, hints = options.hints, timeBetweenDecodingAttempts = options.timeBetweenDecodingAttempts, _b = options.onDecodeResult, onDecodeResult = _b === void 0 ? function () { } : _b, _c = options.onDecodeError, onDecodeError = _c === void 0 ? function () { } : _c, _d = options.onError, onError = _d === void 0 ? function () { } : _d; | ||
var deviceId = "deviceId" in options ? options.deviceId : undefined; | ||
var _e = useState("constraints" in options ? options.constraints : undefined), constraints = _e[0], setConstraints = _e[1]; | ||
var decodeResultHandlerRef = useRef(onDecodeResult); | ||
var decodeErrorHandlerRef = useRef(onDecodeError); | ||
var errorHandlerRef = useRef(onError); | ||
@@ -71,3 +67,3 @@ var ref = useRef(null); | ||
}); | ||
var _e = useTorch({ | ||
var _f = useTorch({ | ||
resetStream: function () { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -85,11 +81,11 @@ return __generator(this, function (_a) { | ||
}); }, | ||
}), torchInit = _e.init, torch = __rest(_e, ["init"]); | ||
}), torchInit = _f.init, torch = __rest(_f, ["init"]); | ||
var decodeCallback = useCallback(function (result, error) { | ||
if (result) | ||
resultHandlerRef.current(result); | ||
decodeResultHandlerRef.current(result); | ||
if (error) | ||
errorHandlerRef.current(error); | ||
decodeErrorHandlerRef.current(error); | ||
}, []); | ||
var startDecoding = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var mediaStream, videoTrack; | ||
var e_1, mediaStream, videoTrack; | ||
return __generator(this, function (_a) { | ||
@@ -102,12 +98,22 @@ switch (_a.label) { | ||
return [2 /*return*/]; | ||
if (!deviceId) return [3 /*break*/, 2]; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 6, , 7]); | ||
if (!deviceId) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, reader.decodeFromVideoDevice(deviceId, ref.current, decodeCallback)]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, reader.decodeFromConstraints(constraints !== null && constraints !== void 0 ? constraints : DEFAULT_CONSTRAINTS, ref.current, decodeCallback)]; | ||
case 3: | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, reader.decodeFromConstraints(constraints !== null && constraints !== void 0 ? constraints : DEFAULT_CONSTRAINTS, ref.current, decodeCallback)]; | ||
case 4: | ||
_a.sent(); | ||
_a.label = 4; | ||
case 4: | ||
_a.label = 5; | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: | ||
e_1 = _a.sent(); | ||
errorHandlerRef.current(e_1); | ||
return [2 /*return*/]; | ||
case 7: | ||
if (!ref.current) | ||
return [2 /*return*/]; | ||
mediaStream = ref.current.srcObject; | ||
@@ -125,5 +131,8 @@ videoTrack = mediaStream.getVideoTracks()[0]; | ||
useEffect(function () { | ||
resultHandlerRef.current = onResult; | ||
}, [onResult]); | ||
decodeResultHandlerRef.current = onDecodeResult; | ||
}, [onDecodeResult]); | ||
useEffect(function () { | ||
decodeErrorHandlerRef.current = onDecodeError; | ||
}, [onDecodeError]); | ||
useEffect(function () { | ||
errorHandlerRef.current = onError; | ||
@@ -130,0 +139,0 @@ }, [onError]); |
@@ -6,4 +6,3 @@ interface UseTorchOptions { | ||
init: (videoTrack: MediaStreamTrack) => void; | ||
isOn: boolean; | ||
isAvailable: boolean | null; | ||
status: "idle" | "on" | "off" | "unavailable"; | ||
on: () => Promise<void>; | ||
@@ -10,0 +9,0 @@ off: () => Promise<void>; |
/// <reference types="react" /> | ||
import { DecodeHintType, Result } from "@zxing/library"; | ||
import { DecodeHintType, Exception, Result } from "@zxing/library"; | ||
export interface UseZxingOptions { | ||
@@ -7,4 +7,5 @@ paused?: boolean; | ||
timeBetweenDecodingAttempts?: number; | ||
onResult?: (result: Result) => void; | ||
onError?: (error: Error) => void; | ||
onDecodeResult?: (result: Result) => void; | ||
onDecodeError?: (error: Exception) => void; | ||
onError?: (error: unknown) => void; | ||
} | ||
@@ -20,4 +21,3 @@ export interface UseZxingOptionsWithConstraints extends UseZxingOptions { | ||
torch: { | ||
isOn: boolean; | ||
isAvailable: boolean | null; | ||
status: "idle" | "on" | "off" | "unavailable"; | ||
on: () => Promise<void>; | ||
@@ -24,0 +24,0 @@ off: () => Promise<void>; |
{ | ||
"name": "react-zxing", | ||
"version": "1.1.4", | ||
"version": "2.0.0", | ||
"description": "Integrate zxing to your React application using a custom hook", | ||
@@ -11,3 +11,3 @@ "license": "MIT", | ||
}, | ||
"homepage": "https://github.com/nomadoda/react-zxing", | ||
"homepage": "https://github.com/adamalfredsson/react-zxing", | ||
"keywords": [ | ||
@@ -24,6 +24,6 @@ "react", | ||
"type": "git", | ||
"url": "https://github.com/nomadoda/react-zxing.git" | ||
"url": "https://github.com/adamalfredsson/react-zxing.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/nomadoda/react-zxing/issues", | ||
"url": "https://github.com/adamalfredsson/react-zxing/issues", | ||
"email": "adam@zodiapps.com" | ||
@@ -67,3 +67,3 @@ }, | ||
"dependencies": { | ||
"@zxing/library": "^0.19.2" | ||
"@zxing/library": "^0.20.0" | ||
}, | ||
@@ -79,5 +79,5 @@ "lint-staged": { | ||
"engines": { | ||
"node": "^16 || ^18", | ||
"node": "^18 || ^20", | ||
"yarn": "^1.22" | ||
} | ||
} |
@@ -27,3 +27,3 @@ <div align="center"> | ||
const { ref } = useZxing({ | ||
onResult(result) { | ||
onDecodeResult(result) { | ||
setResult(result.getText()); | ||
@@ -117,7 +117,7 @@ }, | ||
<tr> | ||
<td>onResult</td> | ||
<td>onDecodeResult</td> | ||
<td>function</td> | ||
<td></td> | ||
<td> | ||
Called when a result is found. The result is an instance of | ||
Called when a decode result is found. The result is an instance of | ||
<a href="https://github.com/zxing-js/library/blob/master/src/core/Result.ts"> | ||
@@ -130,2 +130,13 @@ Result | ||
<tr> | ||
<td>onDecodeError</td> | ||
<td>function</td> | ||
<td></td> | ||
<td> | ||
Called when an decode error is found. The error is an instance of | ||
<a href="https://github.com/zxing-js/library/blob/master/src/core/Exception.ts"> | ||
Exception | ||
</a> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>onError</td> | ||
@@ -135,3 +146,3 @@ <td>function</td> | ||
<td> | ||
Called when an error is found. The error is an instance of Error. | ||
Called when any other error occurs, e.g. when the camera stream cannot be accessed. | ||
</td> | ||
@@ -138,0 +149,0 @@ </tr> |
38161
593
203
+ Added@zxing/library@0.20.0(transitive)
- Removed@zxing/library@0.19.3(transitive)
Updated@zxing/library@^0.20.0