@@ -3,7 +3,7 @@ import { Alert } from "./AlertAdapter";
3
3
import requestFetchAdapter from "./request-fetch-adapter" ;
4
4
import { AppState , Platform } from "react-native" ;
5
5
import RestartManager from "./RestartManager" ;
6
- import log from ' ./logging' ;
6
+ import log from " ./logging" ;
7
7
8
8
let NativeCodePush = require ( "react-native" ) . NativeModules . CodePush ;
9
9
const PackageMixins = require ( "./package-mixins" ) ( NativeCodePush ) ;
@@ -75,7 +75,7 @@ async function checkForUpdate(deploymentKey = null) {
75
75
localPackage && ( update . packageHash === localPackage . packageHash ) ||
76
76
( ! localPackage || localPackage . _isDebugOnly ) && config . packageHash === update . packageHash ) {
77
77
if ( update && update . updateAppVersion ) {
78
- log ( "An update is available but it is targeting a newer binary version than you are currently running ." ) ;
78
+ log ( "An update is available but it is not targeting the binary version of your app ." ) ;
79
79
}
80
80
81
81
return null ;
@@ -401,12 +401,65 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
401
401
402
402
let CodePush ;
403
403
404
+ function codePushify ( options = { } ) {
405
+ let React ;
406
+ let ReactNative = require ( "react-native" ) ;
407
+
408
+ try { React = require ( "react" ) ; } catch ( e ) { }
409
+ if ( ! React ) {
410
+ try { React = ReactNative . React ; } catch ( e ) { }
411
+ if ( ! React ) {
412
+ throw new Error ( "Unable to find the 'React' module." ) ;
413
+ }
414
+ }
415
+
416
+ if ( ! React . Component ) {
417
+ throw new Error (
418
+ `Unable to find the "Component" class, please either:
419
+ 1. Upgrade to a newer version of React Native that supports it, or
420
+ 2. Call the codePush.sync API in your component instead of using the @codePush decorator`
421
+ ) ;
422
+ }
423
+
424
+ var decorator = ( RootComponent ) => {
425
+ return class CodePushComponent extends React . Component {
426
+ componentDidMount ( ) {
427
+ if ( options . checkFrequency === CodePush . CheckFrequency . MANUAL ) {
428
+ CodePush . notifyAppReady ( ) ;
429
+ } else {
430
+ let rootComponentInstance = this . refs . rootComponent ;
431
+ let syncStatusCallback = rootComponentInstance && rootComponentInstance . codePushStatusDidChange ;
432
+ let downloadProgressCallback = rootComponentInstance && rootComponentInstance . codePushDownloadDidProgress ;
433
+ CodePush . sync ( options , syncStatusCallback , downloadProgressCallback ) ;
434
+ if ( options . checkFrequency === CodePush . CheckFrequency . ON_APP_RESUME ) {
435
+ ReactNative . AppState . addEventListener ( "change" , ( newState ) => {
436
+ newState === "active" && CodePush . sync ( options , syncStatusCallback , downloadProgressCallback ) ;
437
+ } ) ;
438
+ }
439
+ }
440
+ }
441
+
442
+ render ( ) {
443
+ return < RootComponent { ...this . props } ref = { "rootComponent" } />
444
+ }
445
+ }
446
+ }
447
+
448
+ if ( typeof options === "function" ) {
449
+ // Infer that the root component was directly passed to us.
450
+ return decorator ( options ) ;
451
+ } else {
452
+ return decorator ;
453
+ }
454
+ }
455
+
404
456
// If the "NativeCodePush" variable isn't defined, then
405
457
// the app didn't properly install the native module,
406
458
// and therefore, it doesn't make sense initializing
407
459
// the JS interface when it wouldn't work anyways.
408
460
if ( NativeCodePush ) {
409
- CodePush = {
461
+ CodePush = codePushify ;
462
+ Object . assign ( CodePush , {
410
463
AcquisitionSdk : Sdk ,
411
464
checkForUpdate,
412
465
getConfiguration,
@@ -436,6 +489,11 @@ if (NativeCodePush) {
436
489
DOWNLOADING_PACKAGE : 7 ,
437
490
INSTALLING_UPDATE : 8
438
491
} ,
492
+ CheckFrequency : {
493
+ ON_APP_START : 0 ,
494
+ ON_APP_RESUME : 1 ,
495
+ MANUAL : 2
496
+ } ,
439
497
UpdateState : {
440
498
RUNNING : NativeCodePush . codePushUpdateStateRunning ,
441
499
PENDING : NativeCodePush . codePushUpdateStatePending ,
@@ -455,7 +513,7 @@ if (NativeCodePush) {
455
513
optionalUpdateMessage : "An update is available. Would you like to install it?" ,
456
514
title : "Update available"
457
515
}
458
- } ;
516
+ } ) ;
459
517
} else {
460
518
log ( "The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly." ) ;
461
519
}
0 commit comments