5
5
6
6
'use strict' ;
7
7
8
+ var extend = require ( "extend" ) ;
8
9
var NativeCodePush = require ( 'react-native' ) . NativeModules . CodePush ;
9
10
var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
10
- var semver = require ( 'semver' ) ;
11
11
var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
12
- var sdk ;
13
- var config ;
12
+ var packageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
14
13
15
14
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
16
15
function setUpTestDependencies ( testSdk , testConfiguration , testNativeBridge ) {
@@ -19,62 +18,80 @@ function setUpTestDependencies(testSdk, testConfiguration, testNativeBridge){
19
18
if ( testNativeBridge ) NativeCodePush = testNativeBridge ;
20
19
}
21
20
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
+ }
33
33
}
34
- }
34
+ } ) ( ) ;
35
35
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
+ }
46
48
}
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
+ }
48
68
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
+ } ) ;
63
78
} ) ;
64
79
} ) ;
65
- } ) ;
66
80
}
67
81
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 ( ) ;
72
88
}
73
89
74
90
var CodePush = {
75
91
getConfiguration : getConfiguration ,
76
- queryUpdate : queryUpdate ,
77
- installUpdate : installUpdate ,
92
+ checkForUpdate : checkForUpdate ,
93
+ getCurrentPackage : getCurrentPackage ,
94
+ notifyApplicationReady : notifyApplicationReady ,
78
95
setUpTestDependencies : setUpTestDependencies
79
96
} ;
80
97
0 commit comments