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

Commit 6b7b311

Browse files
author
Will Anderson
committed
Merge pull request #5 from Microsoft/update-api
Update api to match Cordova implementation
2 parents d73d24a + 655c9f7 commit 6b7b311

File tree

10 files changed

+457
-175
lines changed

10 files changed

+457
-175
lines changed

CodePush.h

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

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

2627
+ (NSDictionary *)getConfiguration;
2728

29+
@end
30+
31+
@interface CodePushPackage : NSObject
32+
33+
+ (NSString *)getCurrentPackageFolderPath:(NSError **)error;
34+
35+
+ (NSString *)getPackageFolderPath:(NSString *)packageHash;
36+
37+
+ (NSDictionary *)getCurrentPackage:(NSError **)error;
38+
39+
+ (NSDictionary *)getPackage:(NSString *)packageHash
40+
error:(NSError **)error;
41+
42+
+ (void)downloadPackage:(NSDictionary *)updatePackage
43+
error:(NSError **)error;
44+
45+
+ (void)applyPackage:(NSDictionary *)updatePackage
46+
error:(NSError **)error;
47+
48+
+ (void)rollbackPackage;
2849
@end

CodePush.ios.js

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
'use strict';
77

8+
var extend = require("extend");
89
var NativeCodePush = require('react-native').NativeModules.CodePush;
910
var requestFetchAdapter = require("./request-fetch-adapter.js");
10-
var semver = require('semver');
1111
var Sdk = require("code-push/script/acquisition-sdk").AcquisitionManager;
12-
var sdk;
13-
var config;
12+
var packageMixins = require("./package-mixins")(NativeCodePush);
1413

1514
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
1615
function setUpTestDependencies(testSdk, testConfiguration, testNativeBridge){
@@ -19,62 +18,80 @@ function setUpTestDependencies(testSdk, testConfiguration, testNativeBridge){
1918
if (testNativeBridge) NativeCodePush = testNativeBridge;
2019
}
2120

22-
function getConfiguration(callback) {
23-
if (config) {
24-
setImmediate(function() {
25-
callback(/*error=*/ null, config);
26-
});
27-
} else {
28-
NativeCodePush.getConfiguration(function(err, configuration) {
29-
if (err) callback(err);
30-
config = configuration;
31-
callback(/*error=*/ null, config);
32-
});
21+
var getConfiguration = (() => {
22+
var config;
23+
return function getConfiguration() {
24+
if (config) {
25+
return Promise.resolve(config);
26+
} else {
27+
return NativeCodePush.getConfiguration()
28+
.then((configuration) => {
29+
if (!config) config = configuration;
30+
return config;
31+
});
32+
}
3333
}
34-
}
34+
})();
3535

36-
function getSdk(callback) {
37-
if (sdk) {
38-
setImmediate(function() {
39-
callback(/*error=*/ null, sdk);
40-
});
41-
} else {
42-
getConfiguration(function(err, configuration) {
43-
sdk = new Sdk(requestFetchAdapter, configuration);
44-
callback(/*error=*/ null, sdk);
45-
});
36+
var getSdk = (() => {
37+
var sdk;
38+
return function getSdk() {
39+
if (sdk) {
40+
return Promise.resolve(sdk);
41+
} else {
42+
return getConfiguration()
43+
.then((configuration) => {
44+
sdk = new Sdk(requestFetchAdapter, configuration);
45+
return sdk;
46+
});
47+
}
4648
}
47-
}
49+
})();
50+
51+
function checkForUpdate() {
52+
var config;
53+
var sdk;
54+
return getConfiguration()
55+
.then((configResult) => {
56+
config = configResult;
57+
return getSdk();
58+
})
59+
.then((sdkResult) => {
60+
sdk = sdkResult;
61+
return getCurrentPackage();
62+
})
63+
.then((localPackage) => {
64+
var queryPackage = {appVersion: config.appVersion};
65+
if (localPackage && localPackage.appVersion === config.appVersion) {
66+
queryPackage = localPackage;
67+
}
4868

49-
function queryUpdate(callback) {
50-
getConfiguration(function(err, configuration) {
51-
if (err) callback(err);
52-
getSdk(function(err, sdk) {
53-
if (err) callback(err);
54-
NativeCodePush.getLocalPackage(function(err, localPackage) {
55-
var queryPackage = {appVersion: configuration.appVersion};
56-
if (!err && localPackage && localPackage.appVersion === configuration.appVersion) {
57-
queryPackage = localPackage;
58-
} else if (err) {
59-
console.log(err);
60-
}
61-
62-
sdk.queryUpdateWithCurrentPackage(queryPackage, callback);
69+
return new Promise((resolve, reject) => {
70+
sdk.queryUpdateWithCurrentPackage(queryPackage, (err, update) => {
71+
if (err) return reject(err);
72+
if (update) {
73+
resolve(extend({}, update, packageMixins.remote));
74+
} else {
75+
resolve(update);
76+
}
77+
});
6378
});
6479
});
65-
});
6680
}
6781

68-
function installUpdate(update) {
69-
// Use the downloaded package info. Native code will save the package info
70-
// so that the client knows what the current package version is.
71-
NativeCodePush.installUpdate(update, JSON.stringify(update), (err) => console.log(err));
82+
function getCurrentPackage() {
83+
return NativeCodePush.getCurrentPackage();
84+
}
85+
86+
function notifyApplicationReady() {
87+
return NativeCodePush.notifyApplicationReady();
7288
}
7389

7490
var CodePush = {
7591
getConfiguration: getConfiguration,
76-
queryUpdate: queryUpdate,
77-
installUpdate: installUpdate,
92+
checkForUpdate: checkForUpdate,
93+
getCurrentPackage: getCurrentPackage,
94+
notifyApplicationReady: notifyApplicationReady,
7895
setUpTestDependencies: setUpTestDependencies
7996
};
8097

0 commit comments

Comments
 (0)