2021-07-30 10:27:45 +02:00
|
|
|
/* global _setGlobalConsole */
|
|
|
|
|
2021-05-06 14:11:55 +02:00
|
|
|
import { DependencyList, useCallback } from 'react';
|
|
|
|
import type { Frame } from 'src/Frame';
|
|
|
|
|
2021-05-27 11:08:57 +02:00
|
|
|
type FrameProcessor = (frame: Frame) => void;
|
|
|
|
|
2021-07-30 10:27:45 +02:00
|
|
|
const capturableConsole = console;
|
|
|
|
|
2021-05-06 14:11:55 +02:00
|
|
|
/**
|
2021-06-21 22:42:46 +02:00
|
|
|
* Returns a memoized Frame Processor function wich you can pass to the `<Camera>`. (See ["Frame Processors"](https://mrousavy.github.io/react-native-vision-camera/docs/guides/frame-processors))
|
2021-05-06 14:11:55 +02:00
|
|
|
*
|
2021-05-27 11:08:57 +02:00
|
|
|
* Make sure to add the `'worklet'` directive to the top of the Frame Processor function, otherwise it will not get compiled into a worklet.
|
2021-05-06 14:11:55 +02:00
|
|
|
*
|
|
|
|
* @param frameProcessor The Frame Processor
|
|
|
|
* @param dependencies The React dependencies which will be copied into the VisionCamera JS-Runtime.
|
|
|
|
* @returns The memoized Frame Processor.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const frameProcessor = useFrameProcessor((frame) => {
|
|
|
|
* 'worklet'
|
|
|
|
* const qrCodes = scanQRCodes(frame)
|
2021-07-30 10:27:45 +02:00
|
|
|
* console.log(`QR Codes: ${qrCodes}`)
|
2021-05-06 14:11:55 +02:00
|
|
|
* }, [])
|
|
|
|
* ```
|
|
|
|
*/
|
2021-07-30 10:27:45 +02:00
|
|
|
export function useFrameProcessor(frameProcessor: FrameProcessor, dependencies: DependencyList): FrameProcessor {
|
|
|
|
return useCallback((frame: Frame) => {
|
|
|
|
'worklet';
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
if (global.didSetConsole == null || global.didSetConsole === false) {
|
|
|
|
const console = {
|
|
|
|
// @ts-expect-error __callAsync is injected by native REA
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
debug: capturableConsole.debug.__callAsync,
|
|
|
|
// @ts-expect-error __callAsync is injected by native REA
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
log: capturableConsole.log.__callAsync,
|
|
|
|
// @ts-expect-error __callAsync is injected by native REA
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
warn: capturableConsole.warn.__callAsync,
|
|
|
|
// @ts-expect-error __callAsync is injected by native REA
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
error: capturableConsole.error.__callAsync,
|
|
|
|
// @ts-expect-error __callAsync is injected by native REA
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
info: capturableConsole.info.__callAsync,
|
|
|
|
};
|
|
|
|
// @ts-expect-error _setGlobalConsole is set by RuntimeDecorator::decorateRuntime
|
|
|
|
_setGlobalConsole(console);
|
|
|
|
// @ts-expect-error
|
|
|
|
global.didSetConsole = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
frameProcessor(frame);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, dependencies);
|
|
|
|
}
|