Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 71b116a

Browse files
author
Will Anderson
committed
Snapshot current iteration of rollback support (WIP)
1 parent 72e6e09 commit 71b116a

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

CodePush.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
+ (NSString *)getBuildVersion;
2222

2323
+ (void)setRootComponent:(NSString *)rootComponent;
24+
2425
+ (NSString *)getRootComponent;
2526

2627
+ (NSDictionary *)getConfiguration;
@@ -44,4 +45,5 @@
4445
+ (void)applyPackage:(NSDictionary *)updatePackage
4546
error:(NSError **)error;
4647

48+
+ (void)rollbackPackage;
4749
@end

CodePush.m

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ @implementation CodePush
1010
RCT_EXPORT_MODULE()
1111

1212
RCTBridge * _bridge;
13+
NSTimer *_timer;
1314
BOOL usingTestFolder = NO;
1415

1516
@synthesize bridge = _bridge;
@@ -62,7 +63,7 @@ + (NSURL *) getBundleUrl
6263
NSString *packageFolder = [CodePushPackage getCurrentPackageFolderPath:&error];
6364

6465
if (error || !packageFolder) {
65-
[self getNativeBundleURL];
66+
return [self getNativeBundleURL];
6667
}
6768

6869
NSString *packageFile = [packageFolder stringByAppendingPathComponent:@"app.jsbundle"];
@@ -82,6 +83,28 @@ + (void) loadBundle
8283
});
8384
}
8485

86+
+ (void) rollbackPackage:(NSTimer *)timer {
87+
[CodePushPackage rollbackPackage];
88+
[self loadBundle];
89+
}
90+
91+
+ (void) startRollbackTimer:(int)rollbackTimeout
92+
{
93+
double timeoutInSeconds = rollbackTimeout / 1000;
94+
_timer = [NSTimer scheduledTimerWithTimeInterval:timeoutInSeconds
95+
target:self
96+
selector:@selector(rollbackPackage:)
97+
userInfo:nil
98+
repeats:NO];
99+
}
100+
101+
+ (void) cancelRollbackTimer
102+
{
103+
dispatch_async(dispatch_get_main_queue(), ^{
104+
[_timer invalidate];
105+
});
106+
}
107+
85108
RCT_EXPORT_METHOD(setUsingTestFolder:(BOOL) shouldUseTestFolder)
86109
{
87110
usingTestFolder = shouldUseTestFolder;
@@ -118,6 +141,7 @@ + (void) loadBundle
118141
}
119142

120143
RCT_EXPORT_METHOD(applyUpdate:(NSDictionary*)updatePackage
144+
rollbackTimeout:(int)rollbackTimeout
121145
resolver:(RCTPromiseResolveBlock)resolve
122146
rejecter:(RCTPromiseRejectBlock)reject)
123147
{
@@ -132,7 +156,15 @@ + (void) loadBundle
132156
}
133157

134158
[CodePush loadBundle];
159+
160+
if (0 != rollbackTimeout) {
161+
dispatch_async(dispatch_get_main_queue(), ^{
162+
[CodePush startRollbackTimer:rollbackTimeout];
163+
});
164+
165+
}
135166
});
167+
136168
}
137169

138170
RCT_EXPORT_METHOD(getCurrentPackage:(RCTPromiseResolveBlock)resolve
@@ -149,4 +181,12 @@ + (void) loadBundle
149181
});
150182
}
151183

184+
RCT_EXPORT_METHOD(notifyApplicationReady:(RCTPromiseResolveBlock)resolve
185+
rejecter:(RCTPromiseRejectBlock)reject)
186+
{
187+
[CodePush cancelRollbackTimer];
188+
189+
resolve([NSNull null]);
190+
}
191+
152192
@end

CodePushPackage.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ + (NSString *)getCurrentPackageFolderPath:(NSError **)error
7272
return [self getPackageFolderPath:packageHash];
7373
}
7474

75+
+ (NSString *)getCurrentPackageHash:(NSError **)error
76+
{
77+
NSDictionary *info = [self getCurrentPackageInfo:error];
78+
return info[@"currentPackage"];
79+
}
80+
81+
+ (NSString *)getPreviousPackageHash:(NSError **)error
82+
{
83+
NSDictionary *info = [self getCurrentPackageInfo:error];
84+
return info[@"previousPackage"];
85+
}
86+
7587
+ (NSDictionary *)getCurrentPackage:(NSError **)error
7688
{
7789
NSString *folderPath = [CodePushPackage getCurrentPackageFolderPath:error];
@@ -185,10 +197,26 @@ + (void)applyPackage:(NSDictionary *)updatePackage
185197
return;
186198
}
187199

200+
[info setValue:info[@"currentPackage"] forKey:@"previousPackage"];
188201
[info setValue:packageHash forKey:@"currentPackage"];
189202

190203
[self updateCurrentPackageInfo:info
191204
error:error];
192205
}
193206

207+
+ (void)rollbackPackage
208+
{
209+
NSError *error;
210+
NSMutableDictionary *info = [self getCurrentPackageInfo:&error];
211+
212+
if (error) {
213+
return;
214+
}
215+
216+
[info setValue:info[@"previousPackage"] forKey:@"currentPackage"];
217+
[info removeObjectForKey:@"previousPackage"];
218+
219+
[self updateCurrentPackageInfo:info error:&error];
220+
}
221+
194222
@end

0 commit comments

Comments
 (0)