react-native-stallion 2.0.0-alpha.4 → 2.0.0
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.
|
@@ -14,6 +14,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
14
14
|
public class StallionSyncHandler {
|
|
15
15
|
|
|
16
16
|
private static final AtomicBoolean isSyncInProgress = new AtomicBoolean(false);
|
|
17
|
+
private static final AtomicBoolean isDownloadInProgress = new AtomicBoolean(false);
|
|
17
18
|
|
|
18
19
|
public static void sync() {
|
|
19
20
|
// Ensure only one sync job runs at a time
|
|
@@ -99,41 +100,51 @@ public class StallionSyncHandler {
|
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
public static void downloadNewRelease(String newReleaseHash, String newReleaseUrl) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
103
|
+
// Ensure only one download job runs at a time
|
|
104
|
+
if (!isDownloadInProgress.compareAndSet(false, true)) {
|
|
105
|
+
return; // Exit if another job is already running
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
StallionStateManager stateManager = StallionStateManager.getInstance();
|
|
109
|
+
String downloadPath = stateManager.getStallionConfig().getFilesDirectory()
|
|
110
|
+
+ StallionConfigConstants.PROD_DIRECTORY
|
|
111
|
+
+ StallionConfigConstants.TEMP_FOLDER_SLOT;
|
|
112
|
+
String projectId = stateManager.getStallionConfig().getProjectId();
|
|
113
|
+
|
|
114
|
+
emitDownloadStarted(newReleaseHash);
|
|
115
|
+
|
|
116
|
+
StallionFileDownloader.downloadBundle(
|
|
117
|
+
newReleaseUrl + "?projectId=" + projectId,
|
|
118
|
+
downloadPath,
|
|
119
|
+
new StallionDownloadCallback() {
|
|
120
|
+
@Override
|
|
121
|
+
public void onReject(String prefix, String error) {
|
|
122
|
+
isDownloadInProgress.set(false);
|
|
123
|
+
emitDownloadError(newReleaseHash, prefix + error);
|
|
124
|
+
}
|
|
118
125
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
+
@Override
|
|
127
|
+
public void onSuccess(String successPayload) {
|
|
128
|
+
isDownloadInProgress.set(false);
|
|
129
|
+
stateManager.stallionMeta.setCurrentProdSlot(StallionMetaConstants.SlotStates.NEW_SLOT);
|
|
130
|
+
stateManager.stallionMeta.setProdTempHash(newReleaseHash);
|
|
131
|
+
String currentProdNewHash = stateManager.stallionMeta.getProdNewHash();
|
|
132
|
+
if(currentProdNewHash != null && !currentProdNewHash.isEmpty()) {
|
|
133
|
+
StallionSlotManager.stabilizeProd();
|
|
134
|
+
}
|
|
135
|
+
stateManager.syncStallionMeta();
|
|
136
|
+
emitDownloadSuccess(newReleaseHash);
|
|
126
137
|
}
|
|
127
|
-
stateManager.syncStallionMeta();
|
|
128
|
-
emitDownloadSuccess(newReleaseHash);
|
|
129
|
-
}
|
|
130
138
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
139
|
+
@Override
|
|
140
|
+
public void onProgress(double downloadFraction) {
|
|
141
|
+
// Optional: Handle progress updates
|
|
142
|
+
}
|
|
134
143
|
}
|
|
135
|
-
|
|
136
|
-
)
|
|
144
|
+
);
|
|
145
|
+
} catch (Exception ignored) {
|
|
146
|
+
isDownloadInProgress.set(false);
|
|
147
|
+
}
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
private static void emitSyncError(Exception e) {
|
|
@@ -10,17 +10,16 @@ import Foundation
|
|
|
10
10
|
class StallionSyncHandler {
|
|
11
11
|
|
|
12
12
|
private static var isSyncInProgress = false
|
|
13
|
+
private static var isDownloadInProgress = false
|
|
13
14
|
private static let syncQueue = DispatchQueue(label: "com.stallion.syncQueue")
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
static func sync() {
|
|
16
17
|
syncQueue.async {
|
|
17
18
|
guard !isSyncInProgress else { return }
|
|
18
19
|
isSyncInProgress = true
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
DispatchQueue.global().async {
|
|
22
|
-
defer { isSyncInProgress = false }
|
|
23
|
-
|
|
24
23
|
do {
|
|
25
24
|
// Fetch StallionStateManager and StallionConfig
|
|
26
25
|
let stateManager = StallionStateManager.sharedInstance()
|
|
@@ -44,6 +43,7 @@ class StallionSyncHandler {
|
|
|
44
43
|
makeApiCall(payload: requestPayload, appVersion: appVersion)
|
|
45
44
|
|
|
46
45
|
} catch {
|
|
46
|
+
defer { isSyncInProgress = false }
|
|
47
47
|
emitSyncError(error)
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -69,17 +69,20 @@ class StallionSyncHandler {
|
|
|
69
69
|
let jsonData = try JSONSerialization.data(withJSONObject: payload, options: [])
|
|
70
70
|
request.httpBody = jsonData
|
|
71
71
|
} catch {
|
|
72
|
+
defer { isSyncInProgress = false }
|
|
72
73
|
emitSyncError(error)
|
|
73
74
|
return
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
|
77
78
|
if let error = error {
|
|
79
|
+
defer { isSyncInProgress = false }
|
|
78
80
|
emitSyncError(error)
|
|
79
81
|
return
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
|
|
85
|
+
defer { isSyncInProgress = false }
|
|
83
86
|
let responseError = NSError(domain: "Invalid response from server", code: -2)
|
|
84
87
|
emitSyncError(responseError)
|
|
85
88
|
return
|
|
@@ -88,12 +91,15 @@ class StallionSyncHandler {
|
|
|
88
91
|
// Parse the JSON response
|
|
89
92
|
do {
|
|
90
93
|
if let releaseMeta = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
|
|
94
|
+
defer { isSyncInProgress = false }
|
|
91
95
|
processReleaseMeta(releaseMeta, appVersion: appVersion)
|
|
92
96
|
} else {
|
|
97
|
+
defer { isSyncInProgress = false }
|
|
93
98
|
let parsingError = NSError(domain: "Invalid JSON format", code: -3)
|
|
94
99
|
emitSyncError(parsingError)
|
|
95
100
|
}
|
|
96
101
|
} catch {
|
|
102
|
+
defer { isSyncInProgress = false }
|
|
97
103
|
emitSyncError(error)
|
|
98
104
|
}
|
|
99
105
|
}
|
|
@@ -145,6 +151,11 @@ class StallionSyncHandler {
|
|
|
145
151
|
static func downloadNewRelease(newReleaseHash: String, newReleaseUrl: String) {
|
|
146
152
|
guard let stateManager = StallionStateManager.sharedInstance(),
|
|
147
153
|
let config = stateManager.stallionConfig else { return }
|
|
154
|
+
|
|
155
|
+
syncQueue.async {
|
|
156
|
+
guard !isDownloadInProgress else { return }
|
|
157
|
+
isDownloadInProgress = true
|
|
158
|
+
}
|
|
148
159
|
|
|
149
160
|
let downloadPath = config.filesDirectory + "/" + StallionConstants.PROD_DIRECTORY + "/" + StallionConstants.TEMP_FOLDER_SLOT
|
|
150
161
|
let projectId = config.projectId ?? ""
|
|
@@ -156,6 +167,7 @@ class StallionSyncHandler {
|
|
|
156
167
|
StallionFileDownloader().downloadBundle(url: fromUrl, downloadDirectory: downloadPath, onProgress: { progress in
|
|
157
168
|
// Handle progress updates if necessary
|
|
158
169
|
}, resolve: { _ in
|
|
170
|
+
defer { isDownloadInProgress = false }
|
|
159
171
|
stateManager.stallionMeta?.currentProdSlot = SlotStates.newSlot
|
|
160
172
|
stateManager.stallionMeta?.prodTempHash = newReleaseHash
|
|
161
173
|
if let currentProdNewHash = stateManager.stallionMeta?.prodNewHash,
|
|
@@ -165,6 +177,7 @@ class StallionSyncHandler {
|
|
|
165
177
|
stateManager.syncStallionMeta()
|
|
166
178
|
emitDownloadSuccess(releaseHash: newReleaseHash)
|
|
167
179
|
}, reject: { code, prefix, error in
|
|
180
|
+
defer { isDownloadInProgress = false }
|
|
168
181
|
emitDownloadError(
|
|
169
182
|
releaseHash: newReleaseHash,
|
|
170
183
|
error: "\(String(describing: prefix))\(String(describing: error))"
|