feat: Add console logging support for Frame Processors (#297)

* Try to log to console via runOnJS

* Call `console.log`

* Create custom `VisionCameraScheduler`

* Fix scheduler call

* Call with this

* Fix console setting

* Move J---- to `java-bindings`

* c++ style

* Android: 1/2 Create custom Scheduler

* Android: 2/2 Use custom Scheduler

* Don't use `runOnJS`, use `__callAsync` directly
This commit is contained in:
Marc Rousavy
2021-07-30 10:27:45 +02:00
committed by GitHub
parent 123d0d9e9c
commit 0f7ee51333
31 changed files with 281 additions and 58 deletions

View File

@@ -1,8 +1,12 @@
/* global _setGlobalConsole */
import { DependencyList, useCallback } from 'react';
import type { Frame } from 'src/Frame';
type FrameProcessor = (frame: Frame) => void;
const capturableConsole = console;
/**
* 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))
*
@@ -16,8 +20,40 @@ type FrameProcessor = (frame: Frame) => void;
* const frameProcessor = useFrameProcessor((frame) => {
* 'worklet'
* const qrCodes = scanQRCodes(frame)
* _log(`QR Codes: ${qrCodes}`)
* console.log(`QR Codes: ${qrCodes}`)
* }, [])
* ```
*/
export const useFrameProcessor: (frameProcessor: FrameProcessor, dependencies: DependencyList) => FrameProcessor = useCallback;
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);
}