react-native-ota-hot-update 2.2.53 → 2.2.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,18 +13,29 @@ import com.rnhotupdate.Common.VERSION
13
13
  import com.rnhotupdate.Common.PREVIOUS_VERSION
14
14
  import com.rnhotupdate.Common.METADATA
15
15
  import com.rnhotupdate.SharedPrefs
16
+ import kotlinx.coroutines.CoroutineScope
17
+ import kotlinx.coroutines.Dispatchers
18
+ import kotlinx.coroutines.SupervisorJob
19
+ import kotlinx.coroutines.cancel
20
+ import kotlinx.coroutines.launch
21
+ import kotlinx.coroutines.withContext
16
22
  import java.io.File
17
23
 
18
24
  class OtaHotUpdateModule internal constructor(context: ReactApplicationContext) :
19
25
  OtaHotUpdateSpec(context) {
20
26
  private val utils: Utils = Utils(context)
27
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
28
+
21
29
  override fun getName(): String {
22
30
  return NAME
23
31
  }
24
32
 
33
+ override fun invalidate() {
34
+ super.invalidate()
35
+ scope.cancel()
36
+ }
25
37
 
26
- @ReactMethod
27
- override fun setupBundlePath(path: String?, extension: String?, promise: Promise) {
38
+ private fun processBundleFile(path: String?, extension: String?): Boolean {
28
39
  if (path != null) {
29
40
  val file = File(path)
30
41
  if (file.exists() && file.isFile) {
@@ -34,7 +45,7 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
34
45
  utils.deleteOldBundleIfneeded(null)
35
46
  val sharedPrefs = SharedPrefs(reactApplicationContext)
36
47
  val oldPath = sharedPrefs.getString(PATH)
37
- if (!oldPath.equals("")) {
48
+ if (!oldPath.isNullOrEmpty()) {
38
49
  sharedPrefs.putString(PREVIOUS_PATH, oldPath)
39
50
  }
40
51
  sharedPrefs.putString(PATH, fileUnzip)
@@ -42,16 +53,31 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
42
53
  CURRENT_VERSION_CODE,
43
54
  reactApplicationContext.getVersionCode()
44
55
  )
45
- promise.resolve(true)
56
+ return true
46
57
  } else {
47
58
  file.delete()
48
- promise.reject("E_UNZIP_FAIL", "File unzip failed or path is invalid file: $file")
59
+ throw Exception("File unzip failed or path is invalid: $file")
49
60
  }
50
61
  } else {
51
- promise.reject("E_INVALID_PATH", "File not exist: $file")
62
+ throw Exception("File not exist: $file")
52
63
  }
53
64
  } else {
54
- promise.reject("E_INVALID_PATH", "File not exist: $path")
65
+ throw Exception("Invalid path: $path")
66
+ }
67
+ }
68
+ @ReactMethod
69
+ override fun setupBundlePath(path: String?, extension: String?, promise: Promise) {
70
+ scope.launch {
71
+ try {
72
+ val result = processBundleFile(path, extension)
73
+ withContext(Dispatchers.Main) {
74
+ promise.resolve(result)
75
+ }
76
+ } catch (e: Exception) {
77
+ withContext(Dispatchers.Main) {
78
+ promise.reject("SET_ERROR", e)
79
+ }
80
+ }
55
81
  }
56
82
  }
57
83
 
@@ -87,7 +113,7 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
87
113
 
88
114
  val currentVersion = sharedPrefs.getString(VERSION)
89
115
  if (currentVersion != "" && currentVersion != version) {
90
- sharedPrefs.putString(PREVIOUS_VERSION, currentVersion)
116
+ sharedPrefs.putString(PREVIOUS_VERSION, currentVersion)
91
117
  }
92
118
 
93
119
  sharedPrefs.putString(VERSION, version)
@@ -99,9 +125,9 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
99
125
  val sharedPrefs = SharedPrefs(reactApplicationContext)
100
126
  val metadata = sharedPrefs.getString(METADATA)
101
127
  if (metadata != "") {
102
- promise.resolve(metadata);
128
+ promise.resolve(metadata);
103
129
  } else {
104
- promise.resolve(null);
130
+ promise.resolve(null);
105
131
  }
106
132
  }
107
133
 
@@ -137,10 +163,10 @@ class OtaHotUpdateModule internal constructor(context: ReactApplicationContext)
137
163
  sharedPrefs.putString(PREVIOUS_PATH, "")
138
164
 
139
165
  if (previousVersion != "") {
140
- sharedPrefs.putString(VERSION, previousVersion)
141
- sharedPrefs.putString(PREVIOUS_VERSION, "")
166
+ sharedPrefs.putString(VERSION, previousVersion)
167
+ sharedPrefs.putString(PREVIOUS_VERSION, "")
142
168
  } else {
143
- sharedPrefs.putString(VERSION, "")
169
+ sharedPrefs.putString(VERSION, "")
144
170
  }
145
171
 
146
172
  promise.resolve(true)
@@ -28,15 +28,19 @@ class Utils internal constructor(private val context: Context) {
28
28
  return directory.delete()
29
29
  }
30
30
  fun deleteOldBundleIfneeded(pathKey: String?): Boolean {
31
- val pathName = if (pathKey != null) pathKey else PREVIOUS_PATH
31
+ val pathName = pathKey ?: PREVIOUS_PATH
32
32
  val sharedPrefs = SharedPrefs(context)
33
- val path = sharedPrefs.getString(pathName)
34
- if (!path.isNullOrEmpty()) {
35
- val file = File(path)
36
- if (file.exists() && file.isFile) {
37
- val isDeleted = deleteDirectory(file.parentFile)
38
- sharedPrefs.putString(pathName, "")
39
- return isDeleted
33
+ val bundlePath = sharedPrefs.getString(pathName)
34
+
35
+ if (!bundlePath.isNullOrEmpty()) {
36
+ val bundleFile = File(bundlePath)
37
+ if (bundleFile.exists() && bundleFile.isFile) {
38
+ val outputFolder = bundleFile.parentFile
39
+ if (outputFolder != null && outputFolder.exists() && outputFolder.isDirectory) {
40
+ val isDeleted = deleteDirectory(outputFolder)
41
+ sharedPrefs.putString(pathName, "")
42
+ return isDeleted
43
+ }
40
44
  }
41
45
  }
42
46
  return false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ota-hot-update",
3
- "version": "2.2.53",
3
+ "version": "2.2.61",
4
4
  "description": "Hot update for react native",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",