@@ -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 ) ;
@@ -401,12 +401,58 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
401
401
402
402
let CodePush ;
403
403
404
+ function codePushify ( options = { } ) {
405
+ return ( RootComponent ) => {
406
+ let React ;
407
+ let ReactNative = require ( "react-native" ) ;
408
+
409
+ try { React = require ( "react" ) ; } catch ( e ) { }
410
+ if ( ! React ) {
411
+ try { React = ReactNative . React ; } catch ( e ) { }
412
+ if ( ! React ) {
413
+ throw new Error ( "Unable to find the 'React' module." ) ;
414
+ }
415
+ }
416
+
417
+ if ( ! React . Component ) {
418
+ throw new Error (
419
+ `Unable to find the "Component" class, please either:
420
+ 1. Upgrade to a newer version of React Native that supports it, or
421
+ 2. Call the codePush.sync API in your component instead of using the @codePush decorator`
422
+ ) ;
423
+ }
424
+
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
+
404
449
// If the "NativeCodePush" variable isn't defined, then
405
450
// the app didn't properly install the native module,
406
451
// and therefore, it doesn't make sense initializing
407
452
// the JS interface when it wouldn't work anyways.
408
453
if ( NativeCodePush ) {
409
- CodePush = {
454
+ CodePush = codePushify ;
455
+ Object . assign ( CodePush , {
410
456
AcquisitionSdk : Sdk ,
411
457
checkForUpdate,
412
458
getConfiguration,
@@ -436,6 +482,11 @@ if (NativeCodePush) {
436
482
DOWNLOADING_PACKAGE : 7 ,
437
483
INSTALLING_UPDATE : 8
438
484
} ,
485
+ CheckFrequency : {
486
+ ON_APP_START : 0 ,
487
+ ON_APP_RESUME : 1 ,
488
+ MANUAL : 2
489
+ } ,
439
490
UpdateState : {
440
491
RUNNING : NativeCodePush . codePushUpdateStateRunning ,
441
492
PENDING : NativeCodePush . codePushUpdateStatePending ,
@@ -455,7 +506,7 @@ if (NativeCodePush) {
455
506
optionalUpdateMessage : "An update is available. Would you like to install it?" ,
456
507
title : "Update available"
457
508
}
458
- } ;
509
+ } ) ;
459
510
} else {
460
511
log ( "The CodePush module doesn't appear to be properly installed. Please double-check that everything is setup correctly." ) ;
461
512
}
0 commit comments