Skip to content

Commit 35dbe8e

Browse files
committed
checkCallbackFnOrThrow
1 parent 18af862 commit 35dbe8e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/readers/BrowserCodeReader.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,23 @@ export class BrowserCodeReader {
512512
});
513513
}
514514

515+
/**
516+
* Checks if the `callbackFn` is defined, otherwise throws.
517+
*/
518+
private static checkCallbackFnOrThrow(callbackFn: DecodeContinuouslyCallback) {
519+
if (!callbackFn) {
520+
throw new ArgumentException('`callbackFn` is a required parameter, you cannot capture results without it.');
521+
}
522+
}
523+
524+
/**
525+
* Standard method to dispose a media stream object.
526+
*/
527+
private static disposeMediaStream(stream: MediaStream) {
528+
stream.getVideoTracks().forEach((x) => x.stop());
529+
stream = undefined;
530+
}
531+
515532
/**
516533
* BrowserCodeReader specific configuration options.
517534
*/
@@ -614,6 +631,8 @@ export class BrowserCodeReader {
614631
callbackFn: DecodeContinuouslyCallback,
615632
): Promise<IScannerControls> {
616633

634+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
635+
617636
const stream = await navigator.mediaDevices.getUserMedia(constraints);
618637

619638
try {
@@ -639,6 +658,8 @@ export class BrowserCodeReader {
639658
callbackFn: DecodeContinuouslyCallback,
640659
): Promise<IScannerControls> {
641660

661+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
662+
642663
const timeout = this.options.tryPlayVideoTimeout;
643664

644665
const video = await BrowserCodeReader.attachStreamToVideo(stream, preview, timeout);
@@ -743,6 +764,8 @@ export class BrowserCodeReader {
743764
callbackFn: DecodeContinuouslyCallback,
744765
): Promise<IScannerControls> {
745766

767+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
768+
746769
let videoConstraints: MediaTrackConstraints;
747770

748771
if (!deviceId) {
@@ -764,6 +787,8 @@ export class BrowserCodeReader {
764787
callbackFn: DecodeContinuouslyCallback,
765788
): Promise<IScannerControls> {
766789

790+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
791+
767792
if (!source) {
768793
throw new ArgumentException('A video element must be provided.');
769794
}
@@ -788,6 +813,8 @@ export class BrowserCodeReader {
788813
callbackFn: DecodeContinuouslyCallback,
789814
): Promise<IScannerControls> {
790815

816+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
817+
791818
if (!url) {
792819
throw new ArgumentException('An URL must be provided.');
793820
}
@@ -984,6 +1011,8 @@ export class BrowserCodeReader {
9841011
finalizeCallback?: (error?: Error) => void,
9851012
): IScannerControls {
9861013

1014+
BrowserCodeReader.checkCallbackFnOrThrow(callbackFn);
1015+
9871016
/**
9881017
* The HTML canvas element, used to draw the video or image's frame for decoding.
9891018
*/

0 commit comments

Comments
 (0)