-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBsDiffPatchModule.kt
More file actions
52 lines (46 loc) · 1.62 KB
/
BsDiffPatchModule.kt
File metadata and controls
52 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.jimmydaddy.bsdiffpatch
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule
@ReactModule(name = BsDiffPatchNative.NAME)
class BsDiffPatchModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String = BsDiffPatchNative.NAME
@ReactMethod
fun patch(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
execute(promise) {
BsDiffPatchNative.patch(
requireArgument(oldFile, "oldFile"),
requireArgument(newFile, "newFile"),
requireArgument(patchFile, "patchFile")
)
}
}
@ReactMethod
fun diff(oldFile: String?, newFile: String?, patchFile: String?, promise: Promise) {
execute(promise) {
BsDiffPatchNative.diff(
requireArgument(oldFile, "oldFile"),
requireArgument(newFile, "newFile"),
requireArgument(patchFile, "patchFile")
)
}
}
private fun requireArgument(value: String?, fieldName: String): String {
if (value.isNullOrEmpty()) {
throw BsDiffPatchException("EINVAL", "$fieldName can not be null or empty")
}
return value
}
private fun execute(promise: Promise, block: () -> Int) {
try {
promise.resolve(block())
} catch (error: BsDiffPatchException) {
promise.reject(error.code, error.message, error)
} catch (error: Exception) {
promise.reject("EUNSPECIFIED", error.message, error)
}
}
}