Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
perf(application): ⚡️ reverted to asynchrocous functions
Browse files Browse the repository at this point in the history
Some of the methods that were changed to synchronous functions were converted back to asynchronous functions for performance reasons.
  • Loading branch information
AndrewDongminYoo committed Apr 4, 2023
1 parent 07a8998 commit 3f626eb
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gbike.segwayblemanager

import android.util.Log
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.WritableMap
Expand Down Expand Up @@ -52,8 +53,8 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
* @see [ReactMethod.isBlockingSynchronousMethod]
* @see [ReactMethod]
*/
override fun getTypedExportedConstants(): MutableMap<String, Any> {
return mutableMapOf(
override val typedExportedConstants: MutableMap<String, Any>
= mutableMapOf(
"supportedEvents" to listOf(
"InitializeResult",
"ConnectResult",
Expand All @@ -67,7 +68,6 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
"IoTInfoResult",
), "moduleName" to NAME,
)
}

/**
* This method is sending device events to the JavaScript side.
Expand All @@ -78,25 +78,31 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
deviceEventEmitter.emit(eventName, params)
}

private fun onSuccess(eventName: String, result: Boolean) {
private fun onSuccess(eventName: String, result: Boolean, promise: Promise?) {
val params = Arguments.createMap().apply {
putBoolean("result", result)
}
Log.d(BLUETOOTH_KIT, "$eventName: $result")
sendEvent(eventName, params)
if (promise !== null) {
promise.resolve(result)
}
}

private fun onFailure(eventName: String, message: String, code: Int) {
private fun onFailure(eventName: String, message: String, code: Int, promise: Promise?) {
val params = Arguments.createMap().apply {
putBoolean("result", false)
putString("message", message)
putInt("code", code)
}
Log.w(BLUETOOTH_KIT, "$eventName: $message")
sendEvent(eventName, params)
if (promise !== null) {
promise.resolve(params)
}
}

private fun onError(eventName: String, error: Exception) {
private fun onError(eventName: String, error: Exception, promise: Promise?) {
val params = Arguments.createMap().apply {
putBoolean("result", false)
putString("errorMessage", error.localizedMessage)
Expand All @@ -105,23 +111,27 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
}
Log.e(BLUETOOTH_KIT, "$eventName: $error")
sendEvent(eventName, params)
if (promise !== null) {
promise.reject(error)
}
}

@ReactMethod
override fun init(
secretKey: String?,
operatorCode: String?,
isDebug: Boolean,
promise: Promise?
) {
val initializeResult = "InitializeResult"
try {
NBIotBle.getInstance().init(secretKey, operatorCode, isDebug)
bluetoothKit = BluetoothKit()
bluetoothKit!!.init(reactContext)
bluetoothKit!!.debugEnabled(isDebug)
onSuccess(initializeResult, true)
onSuccess(initializeResult, true, promise)
} catch (error: Exception) {
onError(initializeResult, error)
onError(initializeResult, error, promise)
}
}

Expand All @@ -130,62 +140,62 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
try {
bluetoothKit!!.connect(deviceMac, deviceKey, iotImei) { state ->
when (state) {
STATE_CONNECTED -> onSuccess(connectResult, true)
STATE_DISCONNECTED -> onFailure(connectResult, STRING_DISCONNECTED, -1001)
else -> onFailure(connectResult, STRING_CONN_FAILURE, -1004)
STATE_CONNECTED -> onSuccess(connectResult, true, null)
STATE_DISCONNECTED -> onFailure(connectResult, STRING_DISCONNECTED, -1001, null)
else -> onFailure(connectResult, STRING_CONN_FAILURE, -1004, null)
}
}
} catch (error: Exception) {
onError(connectResult, error)
onError(connectResult, error, null)
}
}

@ReactMethod
override fun disconnect() {
override fun disconnect(promise: Promise?) {
val disconnectResult = "DisconnectResult"
try {
if (bluetoothKit != null) {
bluetoothKit!!.disConnect()
onSuccess(disconnectResult, true)
onSuccess(disconnectResult, true, promise)
} else {
onSuccess(disconnectResult, false)
onSuccess(disconnectResult, false, promise)
}
} catch (error: Exception) {
onError(disconnectResult, error)
onError(disconnectResult, error, promise)
}
}

@ReactMethod
override fun unLock() {
override fun unLock(promise: Promise?) {
val unlockResult = "UnlockResult"
try {
bluetoothKit!!.unLock(object : OnUnlockListener {
override fun onUnlockSuccess() {
onSuccess(unlockResult, true)
onSuccess(unlockResult, true, promise)
}
override fun onUnlockFail(code: Int, msg: String) {
onFailure(unlockResult, msg, code)
onFailure(unlockResult, msg, code, promise)
}
})
} catch (error: Exception) {
onError(unlockResult, error)
onError(unlockResult, error, promise)
}
}

@ReactMethod
override fun lock() {
override fun lock(promise: Promise?) {
val lockResult = "LockResult"
try {
bluetoothKit!!.lock(object : OnLockListener {
override fun onLockSuccess() {
onSuccess(lockResult, true)
onSuccess(lockResult, true, promise)
}
override fun onLockFail(code: Int, msg: String) {
onFailure(lockResult, msg, code)
onFailure(lockResult, msg, code, promise)
}
})
} catch (error: Exception) {
onError(lockResult, error)
onError(lockResult, error, promise)
}
}

Expand Down Expand Up @@ -213,63 +223,63 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
sendEvent(eventName, params)
}
override fun onQueryVehicleInfoFail(code: Int, msg: String) {
onFailure(eventName, msg, code)
onFailure(eventName, msg, code, null)
}
})
} catch (error: Exception) {
onError(eventName, error)
onError(eventName, error, null)
}
}


@ReactMethod
override fun openBatteryCover() {
override fun openBatteryCover(promise: Promise?) {
val openCoverResult = "OpenCoverResult"
try {
bluetoothKit!!.openBatteryCover(object : OnOpenBatteryCoverListener {
override fun OnOpenBatteryCoverSuccess() {
onSuccess(openCoverResult, true)
onSuccess(openCoverResult, true, promise)
}
override fun OnOpenBatteryCoverFail(code: Int, msg: String) {
onFailure(openCoverResult, msg, code)
onFailure(openCoverResult, msg, code, promise)
}
})
} catch (error: Exception) {
onError(openCoverResult, error)
onError(openCoverResult, error, promise)
}
}

@ReactMethod
override fun openSaddle() {
override fun openSaddle(promise: Promise?) {
val openSaddleResult = "OpenSaddleResult"
try {
bluetoothKit!!.openSaddle(object : OnOpenSaddleListener {
override fun onOpenSaddleSuccess() {
onSuccess(openSaddleResult, true)
onSuccess(openSaddleResult, true, promise)
}
override fun onOpenSaddleFail(code: Int, msg: String) {
onFailure(openSaddleResult, msg, code)
onFailure(openSaddleResult, msg, code, promise)
}
})
} catch (error: Exception) {
onError(openSaddleResult, error)
onError(openSaddleResult, error, promise)
}
}

@ReactMethod
override fun openTailBox() {
override fun openTailBox(promise: Promise?) {
val openTailBoxResult = "OpenTailBoxResult"
try {
bluetoothKit!!.openTailBox(object : OnOpenTailBoxListener {
override fun onOpenTailBoxSuccess() {
onSuccess(openTailBoxResult, true)
onSuccess(openTailBoxResult, true, promise)
}
override fun onOpenTailBoxFail(code: Int, msg: String) {
onFailure(openTailBoxResult, msg, code)
onFailure(openTailBoxResult, msg, code, promise)
}
})
} catch (error: Exception) {
onError(openTailBoxResult, error)
onError(openTailBoxResult, error, promise)
}
}

Expand Down Expand Up @@ -297,7 +307,7 @@ class SegwayBleManagerModule(private val reactContext: ReactApplicationContext)
sendEvent(eventName, params)
}
override fun onQueryIoTInfoFail(code: Int, msg: String) {
onFailure(eventName, msg, code)
onFailure(eventName, msg, code, null)
}
})
}
Expand Down
70 changes: 36 additions & 34 deletions android/src/oldarch/SegwayBleManagerSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,67 @@ import java.util.*

abstract class SegwayBleManagerSpec internal constructor(context: ReactApplicationContext) :
ReactContextBaseJavaModule(context) {
protected abstract fun getTypedExportedConstants(): Map<String, Any>
protected abstract val typedExportedConstants: Map<String, Any>
@DoNotStrip
fun getConstants(): Map<String, Any>? {
val constants = getTypedExportedConstants()
override fun getConstants(): Map<String, Any>? {
val constants = typedExportedConstants
if (ReactBuildConfig.DEBUG || ReactBuildConfig.IS_INTERNAL_BUILD) {
val obligatoryFlowConstants: MutableSet<String> = HashSet<String>(
Arrays.asList<String>(
val obligatoryFlowConstants: MutableSet<String> = HashSet(
Arrays.asList(
"moduleName",
"supportedEvents"
)
)
val optionalFlowConstants: Set<String> = HashSet<String>()
var undeclaredConstants: MutableSet<String> = HashSet<String>(constants.keys)
val optionalFlowConstants: Set<String> = HashSet()
var undeclaredConstants: MutableSet<String> = HashSet(constants.keys)
undeclaredConstants.removeAll(obligatoryFlowConstants)
undeclaredConstants.removeAll(optionalFlowConstants)
if (!undeclaredConstants.isEmpty()) {
throw IllegalStateException(
String.format(
"Native Module Flow doesn't declare constants: %s",
undeclaredConstants
)
check(undeclaredConstants.isEmpty()) {
String.format(
"Native Module Flow doesn't declare constants: %s",
undeclaredConstants
)
}
undeclaredConstants = obligatoryFlowConstants
undeclaredConstants.removeAll(constants.keys)
if (!undeclaredConstants.isEmpty()) {
throw IllegalStateException(
String.format(
"Native Module doesn't fill in constants: %s",
undeclaredConstants
)
check(undeclaredConstants.isEmpty()) {
String.format(
"Native Module doesn't fill in constants: %s",
undeclaredConstants
)
}
}
return constants
}
@ReactMethod
@DoNotStrip
abstract fun init(secretKey: String?, operatorCode: String?, isDebug: Boolean)
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun init(
secretKey: String?,
operatorCode: String?,
isDebug: Boolean,
promise: Promise?
)
@ReactMethod
@DoNotStrip
abstract fun connect(deviceMac: String?, deviceKey: String?, iotImei: String?): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun connect(deviceMac: String?, deviceKey: String?, iotImei: String?)
@ReactMethod
@DoNotStrip
abstract fun disconnect(): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun disconnect(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun unLock(): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun unLock(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun lock(): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun lock(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun openBatteryCover(): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun openBatteryCover(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun openSaddle(): Boolean
@ReactMethod(isBlockingSynchronousMethod = true)
abstract fun openSaddle(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun openTailBox(): Boolean
abstract fun openTailBox(promise: Promise?)
@ReactMethod
@DoNotStrip
abstract fun queryVehicleInformation()
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ PODS:
- React-perflogger (= 0.71.4)
- RNPermissions (3.8.0):
- React-Core
- SegwayBleManager (0.1.0):
- SegwayBleManager (0.1.2):
- NBIoTBleKit (~> 1.1.1)
- React-Core
- SocketRocket (0.6.0)
Expand Down Expand Up @@ -629,7 +629,7 @@ SPEC CHECKSUMS:
React-runtimeexecutor: 8fa50b38df6b992c76537993a2b0553d3b088004
ReactCommon: b49a4b00ca6d181ff74b17c12b2d59ac4add0bde
RNPermissions: afeeeab5f8614fc0eb0dd091537e1c19d2c0476c
SegwayBleManager: dcdacbb9367c9e4d26a3f5753643b1857ee58846
SegwayBleManager: 513c3f5c3ff20e3e6449b06985fb841b3e348916
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
Yoga: 79dd7410de6f8ad73a77c868d3d368843f0c93e0
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
Expand Down
Loading

0 comments on commit 3f626eb

Please sign in to comment.