@@ -10,6 +10,7 @@ var NativeCodePush = require('react-native').NativeModules.CodePush;
10
10
var requestFetchAdapter = require ( "./request-fetch-adapter.js" ) ;
11
11
var Sdk = require ( "code-push/script/acquisition-sdk" ) . AcquisitionManager ;
12
12
var packageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
13
+ var { AlertIOS } = require ( "react-native" ) ;
13
14
14
15
// This function is only used for tests. Replaces the default SDK, configuration and native bridge
15
16
function setUpTestDependencies ( providedTestSdk , providedTestConfig , testNativeBridge ) {
@@ -95,12 +96,100 @@ function notifyApplicationReady() {
95
96
return NativeCodePush . notifyApplicationReady ( ) ;
96
97
}
97
98
99
+ /**
100
+ * The sync method provides a simple, one-line experience for
101
+ * incorporating the check, download and application of an update.
102
+ *
103
+ * It simply composes the existing API methods together and adds additional
104
+ * support for respecting mandatory updates, ignoring previously failed
105
+ * releases, and displaying a standard confirmation UI to the end-user
106
+ * when an update is available.
107
+ */
108
+ function sync ( options = { } ) {
109
+ var syncOptions = {
110
+ descriptionPrefix : " Description: " ,
111
+ appendReleaseDescription : false ,
112
+
113
+ ignoreFailedUpdates : true ,
114
+
115
+ mandatoryContinueButtonLabel : "Continue" ,
116
+ mandatoryUpdateMessage : "An update is available that must be installed." ,
117
+
118
+ optionalIgnoreButtonLabel : "Ignore" ,
119
+ optionalInstallButtonLabel : "Install" ,
120
+ optionalUpdateMessage : "An update is available. Would you like to install it?" ,
121
+
122
+ rollbackTimeout : 0 ,
123
+
124
+ updateTitle : "Update available" ,
125
+
126
+ ...options
127
+ } ;
128
+
129
+ return new Promise ( ( resolve , reject ) => {
130
+ checkForUpdate ( )
131
+ . then ( ( remotePackage ) => {
132
+ if ( ! remotePackage || ( remotePackage . failedApply && syncOptions . ignoreFailedUpdates ) ) {
133
+ resolve ( CodePush . SyncStatus . UP_TO_DATE ) ;
134
+ }
135
+ else {
136
+ var message = null ;
137
+ var dialogButtons = [
138
+ {
139
+ text : null ,
140
+ onPress : ( ) => {
141
+ remotePackage . download ( )
142
+ . then ( ( localPackage ) => {
143
+ resolve ( CodePush . SyncStatus . UPDATE_APPLIED )
144
+ return localPackage . apply ( syncOptions . rollbackTimeout ) ;
145
+ } )
146
+ . catch ( reject )
147
+ . done ( ) ;
148
+ }
149
+ }
150
+ ] ;
151
+
152
+ if ( remotePackage . isMandatory ) {
153
+ message = syncOptions . mandatoryUpdateMessage ;
154
+ dialogButtons [ 0 ] . text = syncOptions . mandatoryContinueButtonLabel ;
155
+ } else {
156
+ message = syncOptions . optionalUpdateMessage ;
157
+ dialogButtons [ 0 ] . text = syncOptions . optionalInstallButtonLabel ;
158
+
159
+ // Since this is an optional update, add another button
160
+ // to allow the end-user to ignore it
161
+ dialogButtons . push ( {
162
+ text : syncOptions . optionalIgnoreButtonLabel ,
163
+ onPress : ( ) => resolve ( CodePush . SyncStatus . UPDATE_IGNORED )
164
+ } ) ;
165
+ }
166
+
167
+ // If the update has a description, and the developer
168
+ // explicitly chose to display it, then set that as the message
169
+ if ( syncOptions . appendReleaseDescription && remotePackage . description ) {
170
+ message += `${ syncOptions . descriptionPrefix } ${ remotePackage . description } ` ;
171
+ }
172
+
173
+ AlertIOS . alert ( syncOptions . updateTitle , message , dialogButtons ) ;
174
+ }
175
+ } )
176
+ . catch ( reject )
177
+ . done ( ) ;
178
+ } ) ;
179
+ } ;
180
+
98
181
var CodePush = {
99
182
getConfiguration : getConfiguration ,
100
183
checkForUpdate : checkForUpdate ,
101
184
getCurrentPackage : getCurrentPackage ,
102
185
notifyApplicationReady : notifyApplicationReady ,
103
- setUpTestDependencies : setUpTestDependencies
186
+ setUpTestDependencies : setUpTestDependencies ,
187
+ sync : sync ,
188
+ SyncStatus : {
189
+ UP_TO_DATE : 0 , // The running app is up-to-date
190
+ UPDATE_IGNORED : 1 , // The app had an optional update and the end-user chose to ignore it
191
+ UPDATE_APPLIED : 2 // The app had an optional/mandatory update that was successfully downloaded and is about to be applied
192
+ }
104
193
} ;
105
194
106
- module . exports = CodePush ;
195
+ module . exports = CodePush ;
0 commit comments