Skip to content

Commit cc01a26

Browse files
authored
Merge pull request #401 from PlayerData/safer-fetch
fix(android): Safer null checks while retriving upgrade
2 parents c3fe326 + 640810e commit cc01a26

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

react-native-mcu-manager/android/src/main/java/uk/co/playerdata/reactnativemcumanager/ReactNativeMcuManagerModule.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,36 @@ class ReactNativeMcuManagerModule : Module() {
8787
}
8888

8989
AsyncFunction("runUpgrade") { id: String, promise: Promise ->
90-
if (!upgrades.contains(id)) {
90+
val upgrade = upgrades[id]
91+
92+
if (upgrade == null) {
9193
promise.reject(CodedException("UPGRADE_ID_MISSING", "Upgrade ID $id not present", null))
94+
return@AsyncFunction
9295
}
9396

94-
upgrades[id]!!.startUpgrade(promise)
97+
upgrade.startUpgrade(promise)
9598
}
9699

97100
AsyncFunction("cancelUpgrade") { id: String, promise: Promise ->
98-
if (!upgrades.contains(id)) {
99-
Log.w(TAG, "Can't cancel update ID ($id} not present")
101+
val upgrade = upgrades[id]
102+
103+
if (upgrade == null) {
104+
promise.reject(CodedException("UPGRADE_ID_MISSING", "Upgrade ID $id not present", null))
100105
return@AsyncFunction
101106
}
102107

103-
upgrades[id]!!.cancel()
108+
upgrade.startUpgrade(promise)
104109
}
105110

106111
Function("destroyUpgrade") { id: String ->
107-
if (!upgrades.contains(id)) {
112+
val upgrade = upgrades[id]
113+
114+
if (upgrade == null) {
108115
Log.w(TAG, "Can't destroy update ID ($id} not present")
109116
return@Function
110117
}
111118

112-
upgrades[id]!!.cancel()
119+
upgrade.cancel()
113120
upgrades.remove(id)
114121
}
115122
}

0 commit comments

Comments
 (0)