@@ -15,6 +15,7 @@ @implementation CodePush {
15
15
16
16
static BOOL testConfigurationFlag = NO;
17
17
18
+ // These keys represent the names we use to store data in NSUserDefaults
18
19
static NSString *const FailedUpdatesKey = @" CODE_PUSH_FAILED_UPDATES" ;
19
20
static NSString *const PendingUpdateKey = @" CODE_PUSH_PENDING_UPDATE" ;
20
21
@@ -23,7 +24,10 @@ @implementation CodePush {
23
24
static NSString *const PendingUpdateHashKey = @" hash" ;
24
25
static NSString *const PendingUpdateIsLoadingKey = @" isLoading" ;
25
26
26
- id saveTestModule = nil ;
27
+ // These keys are used to inspect/augment the metadata
28
+ // that is associated with an update's package.
29
+ static NSString *const PackageHashKey = @" packageHash" ;
30
+ static NSString *const PackageIsPendingKey = @" isPending" ;
27
31
28
32
@synthesize bridge = _bridge;
29
33
@@ -176,6 +180,25 @@ - (BOOL)isFailedHash:(NSString*)packageHash
176
180
return (failedUpdates != nil && [failedUpdates containsObject: packageHash]);
177
181
}
178
182
183
+ /*
184
+ * This method checks to see whether a specific package hash
185
+ * represents a downloaded and installed update, that hasn't
186
+ * been applied yet via an app restart.
187
+ */
188
+ - (BOOL )isPendingUpdate : (NSString *)packageHash
189
+ {
190
+ NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults ];
191
+ NSDictionary *pendingUpdate = [preferences objectForKey: PendingUpdateKey];
192
+
193
+ // If there is a pending update, whose hash is equal to the one
194
+ // specified, and its "state" isn't loading, then we consider it "pending".
195
+ BOOL updateIsPending = pendingUpdate &&
196
+ [pendingUpdate[PendingUpdateIsLoadingKey] boolValue ] == NO &&
197
+ [pendingUpdate[PendingUpdateHashKey] isEqualToString: packageHash];
198
+
199
+ return updateIsPending;
200
+ }
201
+
179
202
/*
180
203
* This method updates the React Native bridge's bundle URL
181
204
* to point at the latest CodePush update, and then restarts
@@ -195,7 +218,6 @@ - (void)loadBundle
195
218
_bridge.bundleURL = url;
196
219
}
197
220
198
- saveTestModule = _bridge.modules [@" RCTTestModule" ];
199
221
[_bridge reload ];
200
222
});
201
223
}
@@ -296,7 +318,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
296
318
// The download completed
297
319
doneCallback: ^{
298
320
NSError *err;
299
- NSDictionary *newPackage = [CodePushPackage getPackage: updatePackage[@" packageHash " ] error: &err];
321
+ NSDictionary *newPackage = [CodePushPackage getPackage: updatePackage[PackageHashKey ] error: &err];
300
322
301
323
if (err) {
302
324
return reject (err);
@@ -330,12 +352,18 @@ - (void)savePendingUpdate:(NSString *)packageHash
330
352
{
331
353
dispatch_async (dispatch_get_main_queue (), ^{
332
354
NSError *error;
333
- NSDictionary *package = [CodePushPackage getCurrentPackage: &error];
355
+ NSMutableDictionary *package = [[CodePushPackage getCurrentPackage: &error] mutableCopy ];
356
+
334
357
if (error) {
335
358
reject (error);
336
- } else {
337
- resolve (package);
338
359
}
360
+
361
+ // Add the "isPending" virtual property to the package at this point, so that
362
+ // the script-side doesn't need to immediately call back into native to populate it.
363
+ BOOL isPendingUpdate = [self isPendingUpdate: [package objectForKey: PackageHashKey]];
364
+ [package setObject: @(isPendingUpdate) forKey: PackageIsPendingKey];
365
+
366
+ resolve (package);
339
367
});
340
368
}
341
369
@@ -355,7 +383,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
355
383
if (error) {
356
384
reject (error);
357
385
} else {
358
- [self savePendingUpdate: updatePackage[@" packageHash " ]
386
+ [self savePendingUpdate: updatePackage[PackageHashKey ]
359
387
isLoading: NO ];
360
388
361
389
if (installMode == CodePushInstallModeOnNextResume) {
@@ -378,7 +406,7 @@ - (void)savePendingUpdate:(NSString *)packageHash
378
406
379
407
/*
380
408
* This method isn't publicly exposed via the "react-native-code-push"
381
- * module, and is only used internally to populate the RemotePackage.failedApply property.
409
+ * module, and is only used internally to populate the RemotePackage.failedInstall property.
382
410
*/
383
411
RCT_EXPORT_METHOD (isFailedUpdate:(NSString *)packageHash
384
412
resolve:(RCTPromiseResolveBlock)resolve
0 commit comments