mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Compare commits
11
Commits
main
...
0.36-stable
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
486c507dd0 | ||
|
|
7aa5858ea6 | ||
|
|
ec9f38c540 | ||
|
|
741ac367d8 | ||
|
|
486f3b035f | ||
|
|
8491e08755 | ||
|
|
c2b60dd029 | ||
|
|
60b2b5175d | ||
|
|
44206a949f | ||
|
|
64dd140c1b | ||
|
|
295867607b |
@@ -11,20 +11,41 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const invariant = require('fbjs/lib/invariant');
|
||||
const NativeEventEmitter = require('NativeEventEmitter');
|
||||
const KeyboardObserver = require('NativeModules').KeyboardObserver;
|
||||
const dismissKeyboard = require('dismissKeyboard');
|
||||
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
|
||||
|
||||
type KeyboardEventName =
|
||||
| 'keyboardWillShow'
|
||||
| 'keyboardDidShow'
|
||||
| 'keyboardWillHide'
|
||||
| 'keyboardDidHide'
|
||||
| 'keyboardWillChangeFrame'
|
||||
| 'keyboardDidChangeFrame';
|
||||
|
||||
type KeyboardEventData = {
|
||||
endCoordinates: {
|
||||
width: number,
|
||||
height: number,
|
||||
screenX: number,
|
||||
screenY: number,
|
||||
},
|
||||
};
|
||||
|
||||
type KeyboardEventListener = (e: KeyboardEventData) => void;
|
||||
|
||||
// The following object exists for documentation purposes
|
||||
// Actual work happens in
|
||||
// https://github.com/facebook/react-native/blob/master/Libraries/EventEmitter/NativeEventEmitter.js
|
||||
|
||||
/**
|
||||
* `Keyboard` component to control keyboard events.
|
||||
* `Keyboard` module to control keyboard events.
|
||||
*
|
||||
* ### Usage
|
||||
*
|
||||
* The Keyboard component allows you to listen for native events and react to them, as
|
||||
* The Keyboard module allows you to listen for native events and react to them, as
|
||||
* well as make changes to the keyboard, like dismissing it.
|
||||
*
|
||||
*```
|
||||
@@ -60,16 +81,17 @@ const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
|
||||
* }
|
||||
*```
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
let Keyboard = {
|
||||
/**
|
||||
* The `addListener` function connects a JavaScript function to an identified native
|
||||
* keyboard notification event.
|
||||
*
|
||||
* This function then returns the reference to the listener.
|
||||
*
|
||||
* @param {string} nativeEvent The `nativeEvent` is the string that identifies the event you're listening for. This
|
||||
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for. This
|
||||
*can be any of the following:
|
||||
*
|
||||
* - `keyboardWillShow`
|
||||
* - `keyboardDidShow`
|
||||
* - `keyboardWillHide`
|
||||
@@ -77,10 +99,20 @@ module.exports = {
|
||||
* - `keyboardWillChangeFrame`
|
||||
* - `keyboardDidChangeFrame`
|
||||
*
|
||||
* @param {function} jsFunction function to be called when the event fires.
|
||||
* @param {function} callback function to be called when the event fires.
|
||||
*/
|
||||
addListener (nativeEvent: string, jsFunction: Function) {
|
||||
return KeyboardEventEmitter.addListener(nativeEvent, jsFunction);
|
||||
addListener(eventName: KeyboardEventName, callback: KeyboardEventListener) {
|
||||
invariant(false, 'Dummy method used for documentation');
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes a specific listener.
|
||||
*
|
||||
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for.
|
||||
* @param {function} callback function to be called when the event fires.
|
||||
*/
|
||||
removeListener(eventName: KeyboardEventName, callback: Function) {
|
||||
invariant(false, 'Dummy method used for documentation');
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -88,24 +120,21 @@ module.exports = {
|
||||
*
|
||||
* @param {string} eventType The native event string listeners are watching which will be removed.
|
||||
*/
|
||||
removeAllListeners (eventType: string) {
|
||||
KeyboardEventEmitter.removeAllListeners(eventType);
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes a specific subscription.
|
||||
*
|
||||
* @param {EmitterSubscription} subscription The subscription emitter to be removed.
|
||||
*/
|
||||
removeSubscription (subscription: Object) {
|
||||
KeyboardEventEmitter.removeSubscription(subscription);
|
||||
removeAllListeners(eventName: KeyboardEventName) {
|
||||
invariant(false, 'Dummy method used for documentation');
|
||||
},
|
||||
|
||||
/**
|
||||
* Dismisses the active keyboard and removes focus.
|
||||
*/
|
||||
dismiss () {
|
||||
dismissKeyboard();
|
||||
dismiss() {
|
||||
invariant(false, 'Dummy method used for documentation');
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Throw away the dummy object and reassign it to original module
|
||||
Keyboard = KeyboardEventEmitter;
|
||||
Keyboard.dismiss = dismissKeyboard;
|
||||
|
||||
module.exports = Keyboard;
|
||||
|
||||
|
||||
@@ -444,7 +444,9 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
|
||||
|
||||
// Download image
|
||||
__weak __typeof(self) weakSelf = self;
|
||||
RCTNetworkTask *task = [networking networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
|
||||
__block RCTNetworkTask *task =
|
||||
[networking networkTaskWithRequest:request
|
||||
completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
|
||||
__typeof(self) strongSelf = weakSelf;
|
||||
if (!strongSelf) {
|
||||
return;
|
||||
@@ -488,8 +490,15 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
|
||||
}
|
||||
|
||||
return ^{
|
||||
[task cancel];
|
||||
[weakSelf dequeueTasks];
|
||||
__typeof(self) strongSelf = weakSelf;
|
||||
if (!strongSelf || !task) {
|
||||
return;
|
||||
}
|
||||
dispatch_async(strongSelf->_URLRequestQueue, ^{
|
||||
[task cancel];
|
||||
task = nil;
|
||||
});
|
||||
[strongSelf dequeueTasks];
|
||||
};
|
||||
}
|
||||
|
||||
@@ -505,7 +514,7 @@ static UIImage *RCTResizeImageIfNeeded(UIImage *image,
|
||||
__block volatile uint32_t cancelled = 0;
|
||||
__block dispatch_block_t cancelLoad = nil;
|
||||
dispatch_block_t cancellationBlock = ^{
|
||||
if (cancelLoad) {
|
||||
if (cancelLoad && !cancelled) {
|
||||
cancelLoad();
|
||||
}
|
||||
OSAtomicOr32Barrier(1, &cancelled);
|
||||
|
||||
@@ -53,6 +53,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
_incrementalDataBlock = nil;
|
||||
_responseBlock = nil;
|
||||
_uploadProgressBlock = nil;
|
||||
_requestToken = nil;
|
||||
}
|
||||
|
||||
- (void)dispatchCallback:(dispatch_block_t)callback
|
||||
@@ -66,6 +67,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (void)start
|
||||
{
|
||||
if (_status != RCTNetworkTaskPending) {
|
||||
RCTLogError(@"RCTNetworkTask was already started or completed");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_requestToken == nil) {
|
||||
id token = [_handler sendRequest:_request withDelegate:self];
|
||||
if ([self validateRequestToken:token]) {
|
||||
@@ -77,6 +83,10 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
||||
|
||||
- (void)cancel
|
||||
{
|
||||
if (_status == RCTNetworkTaskFinished) {
|
||||
return;
|
||||
}
|
||||
|
||||
_status = RCTNetworkTaskFinished;
|
||||
id token = _requestToken;
|
||||
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
||||
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "React"
|
||||
s.version = package['version']
|
||||
s.version = "0.36.1"
|
||||
s.summary = package['description']
|
||||
s.description = <<-DESC
|
||||
React Native apps are built using the React JS
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
VERSION_NAME=1000.0.0-master
|
||||
VERSION_NAME=0.36.1
|
||||
GROUP=com.facebook.react
|
||||
|
||||
POM_NAME=ReactNative
|
||||
|
||||
+15
-5
@@ -31,6 +31,7 @@ import com.facebook.react.modules.core.PermissionListener;
|
||||
@ReactModule(name = "PermissionsAndroid")
|
||||
public class PermissionsModule extends ReactContextBaseJavaModule implements PermissionListener {
|
||||
|
||||
private static final String ERROR_INVALID_ACTIVITY = "E_INVALID_ACTIVITY";
|
||||
private final SparseArray<Callback> mCallbacks;
|
||||
private int mRequestCode = 0;
|
||||
|
||||
@@ -73,7 +74,11 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per
|
||||
promise.resolve(false);
|
||||
return;
|
||||
}
|
||||
promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission));
|
||||
try {
|
||||
promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission));
|
||||
} catch (IllegalStateException e) {
|
||||
promise.reject(ERROR_INVALID_ACTIVITY, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,7 +100,10 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per
|
||||
return;
|
||||
}
|
||||
|
||||
mCallbacks.put(
|
||||
try {
|
||||
PermissionAwareActivity activity = getPermissionAwareActivity();
|
||||
|
||||
mCallbacks.put(
|
||||
mRequestCode, new Callback() {
|
||||
@Override
|
||||
public void invoke(Object... args) {
|
||||
@@ -103,9 +111,11 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per
|
||||
}
|
||||
});
|
||||
|
||||
PermissionAwareActivity activity = getPermissionAwareActivity();
|
||||
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
|
||||
mRequestCode++;
|
||||
activity.requestPermissions(new String[]{permission}, mRequestCode, this);
|
||||
mRequestCode++;
|
||||
} catch (IllegalStateException e) {
|
||||
promise.reject(ERROR_INVALID_ACTIVITY, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
id: headless-js-android
|
||||
title: Headless JS
|
||||
layout: docs
|
||||
category: Guides (Android)
|
||||
permalink: docs/headless-js-android.html
|
||||
next: running-on-device-android
|
||||
previous: native-components-android
|
||||
---
|
||||
|
||||
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.
|
||||
|
||||
## The JS API
|
||||
|
||||
A task is a simple async function that you register on `AppRegistry`, similar to registering React applications:
|
||||
|
||||
```js
|
||||
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
|
||||
```
|
||||
|
||||
Then, in `SomeTaskName.js`:
|
||||
|
||||
```js
|
||||
module.exports = async (taskData) => {
|
||||
// do stuff
|
||||
}
|
||||
```
|
||||
|
||||
You can do anything in your task as long as it doesn't touch UI: network requests, timers and so on. Once your task completes (i.e. the promise is resolved), React Native will go into "paused" mode (unless there are other tasks running, or there is a foreground app).
|
||||
|
||||
## The Java API
|
||||
|
||||
Yes, this does still require some native code, but it's pretty thin. You need to extend `HeadlessJsTaskService` and override `getTaskConfig`, e.g.:
|
||||
|
||||
```java
|
||||
public class MyTaskService extends FbHeadlessJsTaskService {
|
||||
|
||||
@Override
|
||||
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
|
||||
Bundle extras = intent.getExtras();
|
||||
if (extras != null) {
|
||||
return new HeadlessJsTaskConfig(
|
||||
"SomeTaskName",
|
||||
Arguments.fromBundle(extras),
|
||||
5000);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now, whenever you [start your service][0], e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down.
|
||||
|
||||
## Caveats
|
||||
|
||||
* By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. There is a way around this.
|
||||
* If you start your service from a `BroadcastReceiver`, make sure to call `HeadlessJsTaskService.acquireWakelockNow()` before returning from `onReceive()`.
|
||||
|
||||
[0]: https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)
|
||||
@@ -4,7 +4,7 @@ title: Native UI Components
|
||||
layout: docs
|
||||
category: Guides (Android)
|
||||
permalink: docs/native-components-android.html
|
||||
next: running-on-device-android
|
||||
next: headless-js-android
|
||||
previous: native-modules-android
|
||||
---
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ layout: docs
|
||||
category: Guides (Android)
|
||||
permalink: docs/running-on-device-android.html
|
||||
next: signed-apk-android
|
||||
props: native-components-android
|
||||
previous: headless-js-android
|
||||
---
|
||||
|
||||
## Prerequisite: USB Debugging
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "react-native",
|
||||
"version": "1000.0.0",
|
||||
"version": "0.36.1",
|
||||
"description": "A framework for building native apps using React",
|
||||
"license": "BSD-3-Clause",
|
||||
"repository": {
|
||||
@@ -222,4 +222,4 @@
|
||||
"shelljs": "0.6.0",
|
||||
"sinon": "^2.0.0-pre.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user