Skip to content

Commit

Permalink
fix: Use type imports if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Apr 15, 2024
1 parent 9a83756 commit cbee439
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 43 deletions.
1 change: 1 addition & 0 deletions package/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
],
'@typescript-eslint/no-non-null-assertion': 'error',
'@typescript-eslint/no-unnecessary-condition': 'error',
'@typescript-eslint/consistent-type-imports': 'warn',

// react hooks
'react-hooks/exhaustive-deps': [
Expand Down
8 changes: 4 additions & 4 deletions package/src/Camera.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react'
import { requireNativeComponent, NativeSyntheticEvent, findNodeHandle, NativeMethods, View, StyleSheet } from 'react-native'
import { requireNativeComponent, findNodeHandle, View, StyleSheet } from 'react-native'
import type { CameraDevice } from './CameraDevice'
import type { ErrorWithCause } from './CameraError'
import { CameraCaptureError, CameraRuntimeError, tryParseNativeCameraError, isErrorWithCause } from './CameraError'
import type { ErrorWithCause, CameraCaptureError } from './CameraError'
import { CameraRuntimeError, tryParseNativeCameraError, isErrorWithCause } from './CameraError'
import type { CameraProps, OnShutterEvent } from './CameraProps'
import { CameraModule } from './NativeCameraModule'
import type { PhotoFile, TakePhotoOptions } from './PhotoFile'
import type { Point } from './Point'
import type { RecordVideoOptions, VideoFile } from './VideoFile'
import { VisionCameraProxy } from './FrameProcessorPlugins'
import { CameraDevices } from './CameraDevices'
import type { EmitterSubscription } from 'react-native'
import type { EmitterSubscription, NativeSyntheticEvent, NativeMethods } from 'react-native'
import type { Code, CodeScanner, CodeScannerFrame } from './CodeScanner'
import type { TakeSnapshotOptions } from './Snapshot'
import { SkiaCameraCanvas } from './skia/SkiaCameraCanvas'
Expand Down
2 changes: 1 addition & 1 deletion package/src/CameraDevice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Orientation } from './Orientation'
import type { Orientation } from './Orientation'

/**
* Represents the camera device position.
Expand Down
2 changes: 1 addition & 1 deletion package/src/CameraDevices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeModules, NativeEventEmitter } from 'react-native'
import { CameraDevice } from './CameraDevice'
import type { CameraDevice } from './CameraDevice'

const CameraDevicesManager = NativeModules.CameraDevices as {
getConstants: () => {
Expand Down
2 changes: 1 addition & 1 deletion package/src/CodeScanner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Point } from './Point'
import type { Point } from './Point'

/**
* The type of the code to scan.
Expand Down
2 changes: 1 addition & 1 deletion package/src/Frame.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Orientation } from './Orientation'
import { PixelFormat } from './PixelFormat'
import type { PixelFormat } from './PixelFormat'

/**
* A single frame, as seen by the camera. This is backed by a C++ HostObject wrapping the native GPU buffer.
Expand Down
2 changes: 1 addition & 1 deletion package/src/PhotoFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Orientation } from './Orientation'
import type { Orientation } from './Orientation'
import type { TemporaryFile } from './TemporaryFile'

export interface TakePhotoOptions {
Expand Down
2 changes: 1 addition & 1 deletion package/src/Snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PhotoFile } from './PhotoFile'
import type { PhotoFile } from './PhotoFile'

export interface TakeSnapshotOptions {
/**
Expand Down
2 changes: 1 addition & 1 deletion package/src/devices/Templates.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dimensions } from 'react-native'
import { FormatFilter } from './getCameraFormat'
import type { FormatFilter } from './getCameraFormat'

type PredefinedTemplates =
| 'Video'
Expand Down
2 changes: 1 addition & 1 deletion package/src/devices/getCameraDevice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CameraDevice, CameraPosition, PhysicalCameraDeviceType } from '../CameraDevice'
import type { CameraDevice, CameraPosition, PhysicalCameraDeviceType } from '../CameraDevice'

export interface DeviceFilter {
/**
Expand Down
5 changes: 3 additions & 2 deletions package/src/expo-plugin/withAndroidMLKitVisionModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigPlugin, withGradleProperties } from '@expo/config-plugins'
import { ConfigProps } from './@types'
import type { ConfigPlugin } from '@expo/config-plugins'
import { withGradleProperties } from '@expo/config-plugins'
import type { ConfigProps } from './@types'

/**
* Set the `VisionCamera_enableCodeScanner` value in the static `gradle.properties` file.
Expand Down
3 changes: 2 additions & 1 deletion package/src/expo-plugin/withDisableFrameProcessorsAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigPlugin, withGradleProperties } from '@expo/config-plugins'
import type { ConfigPlugin } from '@expo/config-plugins'
import { withGradleProperties } from '@expo/config-plugins'

/**
* Set the `VisionCamera_disableFrameProcessors` value in the static `gradle.properties` file.
Expand Down
3 changes: 2 additions & 1 deletion package/src/expo-plugin/withDisableFrameProcessorsIOS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigPlugin, withPodfileProperties } from '@expo/config-plugins'
import type { ConfigPlugin } from '@expo/config-plugins'
import { withPodfileProperties } from '@expo/config-plugins'

/**
* Set the `disableFrameProcessors` inside of the XcodeProject.
Expand Down
5 changes: 3 additions & 2 deletions package/src/expo-plugin/withVisionCamera.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { withPlugins, AndroidConfig, ConfigPlugin, createRunOncePlugin } from '@expo/config-plugins'
import type { ConfigPlugin } from '@expo/config-plugins'
import { withPlugins, AndroidConfig, createRunOncePlugin } from '@expo/config-plugins'
import { withDisableFrameProcessorsAndroid } from './withDisableFrameProcessorsAndroid'
import { withDisableFrameProcessorsIOS } from './withDisableFrameProcessorsIOS'
import { withAndroidMLKitVisionModel } from './withAndroidMLKitVisionModel'
import { ConfigProps } from './@types'
import type { ConfigProps } from './@types'
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const pkg = require('../../../package.json')

Expand Down
5 changes: 3 additions & 2 deletions package/src/hooks/useCameraDevice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react'
import { CameraDevice, CameraPosition } from '../CameraDevice'
import { getCameraDevice, DeviceFilter } from '../devices/getCameraDevice'
import type { CameraDevice, CameraPosition } from '../CameraDevice'
import type { DeviceFilter } from '../devices/getCameraDevice'
import { getCameraDevice } from '../devices/getCameraDevice'
import { useCameraDevices } from './useCameraDevices'

/**
Expand Down
5 changes: 3 additions & 2 deletions package/src/hooks/useCameraFormat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react'
import { CameraDevice, CameraDeviceFormat } from '../CameraDevice'
import { FormatFilter, getCameraFormat } from '../devices/getCameraFormat'
import type { CameraDevice, CameraDeviceFormat } from '../CameraDevice'
import type { FormatFilter } from '../devices/getCameraFormat'
import { getCameraFormat } from '../devices/getCameraFormat'

/**
* Get the best matching Camera format for the given device that satisfies your requirements using a sorting filter. By default, formats are sorted by highest to lowest resolution.
Expand Down
3 changes: 2 additions & 1 deletion package/src/hooks/useCameraPermission.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Camera, CameraPermissionRequestResult, CameraPermissionStatus } from '../Camera'
import type { CameraPermissionRequestResult, CameraPermissionStatus } from '../Camera'
import { Camera } from '../Camera'
import { AppState } from 'react-native'

interface PermissionState {
Expand Down
2 changes: 1 addition & 1 deletion package/src/hooks/useCodeScanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useMemo, useRef } from 'react'
import { Code, CodeScanner, CodeScannerFrame } from '../CodeScanner'
import type { Code, CodeScanner, CodeScannerFrame } from '../CodeScanner'

export function useCodeScanner(codeScanner: CodeScanner): CodeScanner {
const { onCodeScanned, ...codeScannerOptions } = codeScanner
Expand Down
5 changes: 3 additions & 2 deletions package/src/hooks/useFrameProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { DependencyList, useMemo } from 'react'
import type { DependencyList } from 'react'
import { useMemo } from 'react'
import { wrapFrameProcessorWithRefCounting } from '../FrameProcessorPlugins'
import type { ReadonlyFrameProcessor } from '../CameraProps'
import { Frame } from '../Frame'
import type { Frame } from '../Frame'

/**
* Create a new Frame Processor function which you can pass to the `<Camera>`.
Expand Down
2 changes: 1 addition & 1 deletion package/src/skia/SkiaCameraCanvas.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useState } from 'react'
import { LayoutChangeEvent, ViewProps } from 'react-native'
import type { LayoutChangeEvent, ViewProps } from 'react-native'
import type { CameraProps } from '../CameraProps'
import type { ISharedValue } from 'react-native-worklets-core'
import { ReanimatedProxy } from '../dependencies/ReanimatedProxy'
Expand Down
22 changes: 6 additions & 16 deletions package/src/skia/useSkiaFrameProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Frame, FrameInternal } from '../Frame'
import { DependencyList, useEffect, useMemo } from 'react'
import { Orientation } from '../Orientation'
import type { Frame, FrameInternal } from '../Frame'
import type { DependencyList } from 'react'
import { useEffect, useMemo } from 'react'
import type { Orientation } from '../Orientation'
import { wrapFrameProcessorWithRefCounting } from '../FrameProcessorPlugins'
import { DrawableFrameProcessor } from '../CameraProps'
import type { DrawableFrameProcessor } from '../CameraProps'
import type { ISharedValue } from 'react-native-worklets-core'
import { WorkletsProxy } from '../dependencies/WorkletsProxy'
import type { SkCanvas, SkPaint, SkImage, SkSurface } from '@shopify/react-native-skia'
import { SkiaProxy } from '../dependencies/SkiaProxy'
import { Platform } from 'react-native'

/**
* Represents a Camera Frame that can be directly drawn to using Skia.
Expand Down Expand Up @@ -66,8 +66,6 @@ function getPortraitSize(frame: Frame): { width: number; height: number } {
}
}

const NEEDS_CPU_COPY = Platform.OS === 'ios'

/**
* Create a new Frame Processor function which you can pass to the `<Camera>`.
* (See ["Frame Processors"](https://react-native-vision-camera.com/docs/guides/frame-processors))
Expand Down Expand Up @@ -109,15 +107,7 @@ export function createSkiaFrameProcessor(

// Convert Frame to SkImage/Texture
const platformBuffer = (frame as FrameInternal).getPlatformBuffer()
let image = Skia.Image.MakeImageFromPlatformBuffer(platformBuffer.pointer)
if (NEEDS_CPU_COPY) {
// On some backends (e.g. Metal/iOS) we cannot use a Texture from this Thread
// and render it on the UI thread later. In this case, we need to make an explicit
// CPU copy of the Texture, otherwise it will flicker and fail to render.
const copy = image.makeNonTextureImage()
image.dispose()
image = copy
}
const image = Skia.Image.MakeImageFromPlatformBuffer(platformBuffer.pointer)

return new Proxy(frame as DrawableFrame, {
get: (_, property: keyof DrawableFrame) => {
Expand Down

0 comments on commit cbee439

Please sign in to comment.