Skip to content

Commit 925ee7c

Browse files
committed
Write a flow-typed wrapper around the native module. Update example and README.md to use this wrapper.
1 parent 7b2dcea commit 925ee7c

File tree

6 files changed

+125
-20
lines changed

6 files changed

+125
-20
lines changed

.flowconfig

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[ignore]
2+
; We fork some components by platform
3+
.*/*[.]android.js
4+
.*/example/.*
5+
6+
; Ignore "BUCK" generated dirs
7+
<PROJECT_ROOT>/\.buckd/
8+
9+
; Ignore unexpected extra "@providesModule"
10+
.*/node_modules/.*/node_modules/fbjs/.*
11+
12+
; Ignore duplicate module providers
13+
; For RN Apps installed via npm, "Libraries" folder is inside
14+
; "node_modules/react-native" but in the source repo it is in the root
15+
.*/Libraries/react-native/React.js
16+
17+
; Ignore polyfills
18+
.*/Libraries/polyfills/.*
19+
20+
; Ignore metro
21+
.*/node_modules/metro/.*
22+
23+
[include]
24+
25+
[libs]
26+
node_modules/react-native/Libraries/react-native/react-native-interface.js
27+
node_modules/react-native/flow/
28+
node_modules/react-native/flow-github/
29+
30+
[options]
31+
emoji=true
32+
33+
module.system=haste
34+
35+
munge_underscores=true
36+
37+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
38+
39+
module.file_ext=.js
40+
module.file_ext=.jsx
41+
module.file_ext=.json
42+
module.file_ext=.native.js
43+
44+
suppress_type=$FlowIssue
45+
suppress_type=$FlowFixMe
46+
suppress_type=$FlowFixMeProps
47+
suppress_type=$FlowFixMeState
48+
49+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
50+
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
51+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
52+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError
53+
54+
[version]
55+
^0.68.0

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ or A/B testing logic.
2222
* Explanations of how to set it up on the native side. In the meanwhile have
2323
a look at AppDelegate.m for iOS and MainActivity.java / MainApplication.java
2424
for Android.
25-
* Don't export the NativeModule directly, but have a simple JS wrapper around
26-
it, with Flow annotations and API docs.
27-
* Our reloading mechanism on Android does not seem to work...
25+
2826

2927
## Getting started
3028

@@ -67,29 +65,34 @@ or
6765

6866
## Usage
6967
```javascript
70-
import RNDynamicBundle from 'react-native-dynamic-bundle';
68+
import {
69+
setActiveBundle,
70+
registerBundle,
71+
unregisterBundle,
72+
reloadBundle
73+
} from 'react-native-dynamic-bundle';
7174

7275
/* Register a bundle in the documents directory of the app. This could be
7376
* pre-packaged in your app, downloaded over http, etc. Paths are relative
7477
* to your documents directory.
7578
*/
76-
RNDynamicBundle.registerBundle('a_b_test', 'bundles/a_b_test.bundle');
79+
registerBundle('a_b_test', 'bundles/a_b_test.bundle');
7780

7881
/* Set the active bundle to a_b_test. This means that on the next load
7982
* this bundle will be loaded instead of the default.
8083
*/
81-
RNDynamicBundle.setActiveBundle('a_b_test');
84+
setActiveBundle('a_b_test');
8285

8386
/* Unregister a bundle once you're done with it. Note that you will have to
8487
* remove the file yourself.
8588
*/
86-
RNDynamicBundle.unregisterBundle('a_b_test');
89+
unregisterBundle('a_b_test');
8790

8891
/* In some circumstances (e.g. the user consents to an update) we want to
8992
* force a bundle reload instead of waiting until the next app restart.
9093
* Note that this will have to result in the destruction of the current
9194
* RCTBridge and its recreation with the new bundle URL. It is therefore
9295
* recommended to sync data and let actions complete before calling this.
9396
*/
94-
RNDynamicBundle.reloadBundle();
97+
reloadBundle();
9598
```

example/App.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* Sample React Native App
3-
* https://github.com/facebook/react-native
2+
* Sample RNDynamicBundle app
3+
* https://github.com/mauritsd/react-native-dynamic-bundle
44
* @flow
55
*/
66

@@ -12,7 +12,12 @@ import {
1212
Button,
1313
TextInput
1414
} from 'react-native';
15-
import RNDynamicBundle from 'react-native-dynamic-bundle';
15+
import {
16+
setActiveBundle,
17+
registerBundle,
18+
unregisterBundle,
19+
reloadBundle
20+
} from 'react-native-dynamic-bundle';
1621
import RNFS from 'react-native-fs';
1722

1823
type Props = {};
@@ -25,10 +30,6 @@ export default class App extends Component<Props> {
2530
}
2631
}
2732

28-
componentWillMount = () => {
29-
30-
}
31-
3233
render() {
3334
return (
3435
<View style={styles.container}>
@@ -52,9 +53,9 @@ export default class App extends Component<Props> {
5253
});
5354
const result = await promise;
5455

55-
RNDynamicBundle.registerBundle('test', 'test.bundle');
56-
RNDynamicBundle.setActiveBundle('test');
57-
RNDynamicBundle.reloadBundle();
56+
registerBundle('test', 'test.bundle');
57+
setActiveBundle('test');
58+
reloadBundle();
5859
}
5960
}
6061

index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1+
/**
2+
* react-native-dynamic-bundle
3+
* @flow
4+
*/
15

6+
// $FlowExpectedError: library code, so RN is not in node_modules
27
import { NativeModules } from 'react-native';
38

49
const { RNDynamicBundle } = NativeModules;
510

6-
export default RNDynamicBundle;
11+
/**
12+
* Set the active Javascript bundle to the bundle with the given bundle ID in
13+
* the registry. This will cause the given bundle to be loaded on the next app
14+
* startup or invocation of reloadBundle().
15+
*/
16+
export function setActiveBundle(bundleId: string) {
17+
RNDynamicBundle.setActiveBundle(bundleId);
18+
}
19+
20+
/**
21+
* Register a Javascript bundle in the bundle registry. The path to the bundle
22+
* should be relative to the documents directory on iOS and to the internal app
23+
* storage directory on Android, i.e. the directory returned by getFilesDir().
24+
*/
25+
export function registerBundle(bundleId: string, relativePath: string) {
26+
RNDynamicBundle.registerBundle(bundleId, relativePath);
27+
}
28+
29+
/**
30+
* Unregister a bundle from the bundle registry.
31+
*/
32+
export function unregisterBundle(bundleId: string) {
33+
RNDynamicBundle.unregisterBundle(bundleId);
34+
}
35+
36+
/**
37+
* Reload the bundle that is used by the app immediately. This can be used to
38+
* apply a new bundle that was set by setActiveBundle() immediately.
39+
*/
40+
export function reloadBundle() {
41+
RNDynamicBundle.reloadBundle();
42+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
"peerDependencies": {
2626
"react-native": "*"
2727
},
28-
"nativePackage": true
28+
"nativePackage": true,
29+
"devDependencies": {
30+
"flow-bin": "^0.68.0"
31+
}
2932
}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
flow-bin@^0.68.0:
6+
version "0.68.0"
7+
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.68.0.tgz#86c2d14857d306eb2e85e274f2eebf543564f623"

0 commit comments

Comments
 (0)