Skip to content

Commit 6f0d469

Browse files
refactor(ios): move rejectPromiseWithMessage helper into RNMLKit core (#260)
The rejectPromiseWithMessage Swift helper was duplicated verbatim in the face-detection, object-detection, and image-labeling modules. Move it to RNMLKitCore as a public function so it can be shared across modules (including the upcoming barcode scanning module) instead of being copied into each one. Closes #254 Co-authored-by: Claude <noreply@anthropic.com>
1 parent c08d53b commit 6f0d469

6 files changed

Lines changed: 38 additions & 21 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@infinitered/react-native-mlkit-core": minor
3+
"@infinitered/react-native-mlkit-face-detection": patch
4+
"@infinitered/react-native-mlkit-object-detection": patch
5+
"@infinitered/react-native-mlkit-image-labeling": patch
6+
---
7+
8+
Move the `rejectPromiseWithMessage` Swift helper into RNMLKit core so it can be shared across modules instead of being duplicated in each one.

modules/react-native-mlkit-core/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ var image:InputImage = RNMLKitImage(imagePath, appContext.reactContext!!).image
5050
ImagePickerAsset.
5151
- `appContext` is the AppContext from your expo module -- it's accessible as a property on the Module class.
5252

53+
### rejectPromiseWithMessage
54+
55+
#### Swift
56+
57+
A shared helper for rejecting an Expo `Promise` with a consistent `NSError` shape. Use it from any RNMLKit module to
58+
surface errors back to JavaScript.
59+
60+
```swift
61+
import RNMLKitCore
62+
63+
rejectPromiseWithMessage(promise: promise, message: "Something went wrong", domain: ERROR_DOMAIN)
64+
```
65+
66+
- `promise` is the Expo `Promise` to reject.
67+
- `message` is the localized description attached to the error.
68+
- `domain` is the error domain (e.g. a module-specific error domain constant).
69+
5370
### React Components
5471

5572
#### BoundingBoxView
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import ExpoModulesCore
2+
3+
/// Rejects a promise with a specified message and domain.
4+
///
5+
/// Shared helper used across the React Native MLKit modules to surface
6+
/// errors back to JavaScript with a consistent `NSError` shape.
7+
public func rejectPromiseWithMessage(promise: Promise, message: String, domain: String) {
8+
promise.reject(
9+
NSError(domain: domain, code: 1, userInfo: [NSLocalizedDescriptionKey: message])
10+
)
11+
}

modules/react-native-mlkit-face-detection/ios/RNMLKitFaceDetectionModule.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ import ExpoModulesCore
33
import MLKitFaceDetection
44
import RNMLKitCore
55

6-
// Function to reject a promise with a specified message and domain
7-
func rejectPromiseWithMessage(promise: Promise, message: String, domain: String) {
8-
promise.reject(
9-
NSError(domain: domain, code: 1, userInfo: [NSLocalizedDescriptionKey: message])
10-
)
11-
}
12-
136
let ERROR_DOMAIN: String = "red.infinite.reactnativemlkit.FaceDetectorErrorDomain"
147

158

modules/react-native-mlkit-image-labeling/ios/RNMLKitImageLabelingModule.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ExpoModulesCore
22
import MLKitCommon
33
import MLKitImageLabelingCustom
44
import MLKitVision
5+
import RNMLKitCore
56

67
// Structure to represent the result of image labeling
78
struct RNMLKitImageLabelResult: Record {
@@ -23,13 +24,6 @@ struct RNMLKitImageLabelerOptionsRecord: Record {
2324
var maxResultCount: Int = 10
2425
}
2526

26-
// Function to reject a promise with a specified message and domain
27-
func rejectPromiseWithMessage(promise: Promise, message: String, domain: String) {
28-
promise.reject(
29-
NSError(domain: domain, code: 1, userInfo: [NSLocalizedDescriptionKey: message])
30-
)
31-
}
32-
3327
struct RNMLKitImageLabelerSpec: Record {
3428
@Field
3529
var modelName: String = ""

modules/react-native-mlkit-object-detection/ios/RNMLKitObjectDetectionModule.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import ExpoModulesCore
2-
3-
// Function to reject a promise with a specified message and domain
4-
func rejectPromiseWithMessage(promise: Promise, message: String, domain: String) {
5-
promise.reject(
6-
NSError(domain: domain, code: 1, userInfo: [NSLocalizedDescriptionKey: message])
7-
)
8-
}
2+
import RNMLKitCore
93

104
public struct RNMLKitObjectDetectionModelSpec: Record {
115
public init() {}

0 commit comments

Comments
 (0)