Skip to content

Commit 06f5cb2

Browse files
authored
Merge pull request #451 from Microsoft/allow-no-opts-decorator
Allow passing no opts to decorator
2 parents 88d325d + d8bdd63 commit 06f5cb2

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

CodePush.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -402,26 +402,26 @@ async function syncInternal(options = {}, syncStatusChangeCallback, downloadProg
402402
let CodePush;
403403

404404
function codePushify(options = {}) {
405-
return (RootComponent) => {
406-
let React;
407-
let ReactNative = require("react-native");
405+
let React;
406+
let ReactNative = require("react-native");
408407

409-
try { React = require("react"); } catch (e) { }
408+
try { React = require("react"); } catch (e) { }
409+
if (!React) {
410+
try { React = ReactNative.React; } catch (e) { }
410411
if (!React) {
411-
try { React = ReactNative.React; } catch (e) { }
412-
if (!React) {
413-
throw new Error("Unable to find the 'React' module.");
414-
}
412+
throw new Error("Unable to find the 'React' module.");
415413
}
414+
}
416415

417-
if (!React.Component) {
418-
throw new Error(
416+
if (!React.Component) {
417+
throw new Error(
419418
`Unable to find the "Component" class, please either:
420419
1. Upgrade to a newer version of React Native that supports it, or
421420
2. Call the codePush.sync API in your component instead of using the @codePush decorator`
422-
);
423-
}
421+
);
422+
}
424423

424+
var decorator = (RootComponent) => {
425425
return class CodePushComponent extends React.Component {
426426
componentDidMount() {
427427
if (options.checkFrequency === CodePush.CheckFrequency.MANUAL) {
@@ -444,6 +444,13 @@ function codePushify(options = {}) {
444444
}
445445
}
446446
}
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+
}
447454
}
448455

449456
// If the "NativeCodePush" variable isn't defined, then

README.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ In order to accommodate as many developer preferences as possible, the CodePush
102102
```
103103
react-native link react-native-code-push
104104
```
105-
105+
106106
If your app uses a version of React Native that is lower than v0.27, run the following:
107107
```
108108
rnpm link react-native-code-push
109-
```
109+
```
110110
111111
*Note: If you don't already have RNPM installed, you can do so by simply running `npm i -g rnpm` and then executing the above command. If you already have RNPM installed, make sure you have v1.9.0+ in order to benefit from this one step install.*
112112
@@ -220,14 +220,14 @@ In order to accommodate as many developer preferences as possible, the CodePush
220220
```
221221
react-native link react-native-code-push
222222
```
223-
223+
224224
If your app uses a version of React Native that is lower than v0.27, run the following:
225225
```
226226
rnpm link react-native-code-push
227-
```
228-
227+
```
228+
229229
*Note: If you don't already have RNPM installed, you can do so by simply running `npm i -g rnpm` and then executing the above command.*
230-
230+
231231
2. If you're using RNPM >=1.6.0, you will be prompted for the deployment key you'd like to use. If you don't already have it, you can retreive this value by running `code-push deployment ls <appName> -k`, or you can choose to ignore it (by simply hitting `<ENTER>`) and add it in later. To get started, we would recommend just using your `Staging` deployment key, so that you can test out the CodePush end-to-end.
232232
233233
3. (Only needed in v1.8.0+ of the plugin) In your `android/app/build.gradle` file, add the `codepush.gradle` file as an additional build task definition underneath `react.gradle`:
@@ -426,13 +426,11 @@ The simplest way to do this is to "CodePush-ify" your app's root component. To d
426426

427427
```javascript
428428
import codePush from "react-native-code-push";
429-
430-
let codePushOptions;
431-
429+
432430
class MyApp extends Component {
433431
}
434-
435-
MyApp = codePush(codePushOptions)(MyApp);
432+
433+
MyApp = codePush(MyApp);
436434
```
437435

438436
* **Option 2: Use the [ES7 decorator](https://github.com/wycats/javascript-decorators) syntax:**
@@ -442,9 +440,7 @@ The simplest way to do this is to "CodePush-ify" your app's root component. To d
442440
```javascript
443441
import codePush from "react-native-code-push";
444442
445-
let codePushOptions;
446-
447-
@codePush(codePushOptions)
443+
@codePush
448444
class MyApp extends Component {
449445
}
450446
```
@@ -722,9 +718,13 @@ When you require `react-native-code-push`, the module object provides the follow
722718
#### codePush
723719
724720
```javascript
725-
codePush(options: CodePushOptions)(rootComponent: React.Component): React.Component; // Wrapper function
721+
// Wrapper function
722+
codePush(rootComponent: React.Component): React.Component;
723+
codePush(options: CodePushOptions)(rootComponent: React.Component): React.Component;
726724

727-
@codePush(options: CodePushOptions) // Decorator; Requires ES7 support
725+
// Decorator; Requires ES7 support
726+
@codePush
727+
@codePush(options: CodePushOptions)
728728
```
729729
730730
Used to wrap a React component inside a "higher order" React component that knows how to synchronize your app's JavaScript bundle and image assets when it is mounted. Internally, the higher-order component calls [`sync`](#codepushsync) inside its `componentDidMount` lifecycle handle, which in turns performs an update check, downloads the update if it exists and installs the update for you.
@@ -738,15 +738,15 @@ This decorator provides support for letting you customize its behaviour to easil
738738
// sync with the server, without ever
739739
// interrupting the end user
740740
class MyApp extends Component {}
741-
codePush()(MyApp);
741+
MyApp = codePush(MyApp);
742742
```
743743
744744
2. **Silent sync everytime the app resumes**. Same as 1, except we check for updates, or apply an update if one exists every time the app returns to the foreground after being "backgrounded".
745745
746746
```javascript
747747
// Sync for updates everytime the app resumes.
748748
class MyApp extends Component {}
749-
codePush({ checkFrequency: codePush.CheckFrequency.ON_APP_RESUME, installMode: codePush.InstallMode.ON_NEXT_RESUME })(MyApp);
749+
MyApp = codePush({ checkFrequency: codePush.CheckFrequency.ON_APP_RESUME, installMode: codePush.InstallMode.ON_NEXT_RESUME })(MyApp);
750750
```
751751
752752
3. **Interactive**. When an update is available, prompt the end user for permission before downloading it, and then immediately apply the update. If an update was released using the `mandatory` flag, the end user would still be notified about the update, but they wouldn't have the choice to ignore it.
@@ -756,7 +756,7 @@ This decorator provides support for letting you customize its behaviour to easil
756756
// about each update, and displays it to them
757757
// immediately after downloading it
758758
class MyApp extends Component {}
759-
codePush({ updateDialog: true, installMode: codePush.InstallMode.IMMEDIATE })(MyApp);
759+
MyApp = codePush({ updateDialog: true, installMode: codePush.InstallMode.IMMEDIATE })(MyApp);
760760
```
761761
762762
4. **Log/display progress**. While the app is syncing with the server for updates, make use of the `codePushStatusDidChange` and/or `codePushDownloadDidProgress` event hooks to log down the different stages of this process, or even display a progress bar to the user.
@@ -789,7 +789,7 @@ This decorator provides support for letting you customize its behaviour to easil
789789
console.log(progess.receivedBytes + " of " + progress.totalBytes + " received.");
790790
}
791791
}
792-
codePush()(MyApp);
792+
MyApp = codePush(MyApp);
793793
```
794794
795795
##### CodePushOptions

0 commit comments

Comments
 (0)