Skip to content

Commit 741ac36

Browse files
committed
Merge branch '0.36-stable' of github.com:facebook/react-native into 0.36-stable
2 parents 486f3b0 + 8491e08 commit 741ac36

8 files changed

Lines changed: 87 additions & 9 deletions

File tree

Libraries/Image/RCTImageLoader.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
444444

445445
// Download image
446446
__weak __typeof(self) weakSelf = self;
447-
RCTNetworkTask *task = [networking networkTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
447+
__block RCTNetworkTask *task =
448+
[networking networkTaskWithRequest:request
449+
completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
448450
__typeof(self) strongSelf = weakSelf;
449451
if (!strongSelf) {
450452
return;
@@ -488,8 +490,15 @@ - (RCTImageLoaderCancellationBlock)_loadURLRequest:(NSURLRequest *)request
488490
}
489491

490492
return ^{
491-
[task cancel];
492-
[weakSelf dequeueTasks];
493+
__typeof(self) strongSelf = weakSelf;
494+
if (!strongSelf || !task) {
495+
return;
496+
}
497+
dispatch_async(strongSelf->_URLRequestQueue, ^{
498+
[task cancel];
499+
task = nil;
500+
});
501+
[strongSelf dequeueTasks];
493502
};
494503
}
495504

@@ -505,7 +514,7 @@ - (RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLRequest *)image
505514
__block volatile uint32_t cancelled = 0;
506515
__block dispatch_block_t cancelLoad = nil;
507516
dispatch_block_t cancellationBlock = ^{
508-
if (cancelLoad) {
517+
if (cancelLoad && !cancelled) {
509518
cancelLoad();
510519
}
511520
OSAtomicOr32Barrier(1, &cancelled);

Libraries/Network/RCTNetworkTask.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ - (void)invalidate
5353
_incrementalDataBlock = nil;
5454
_responseBlock = nil;
5555
_uploadProgressBlock = nil;
56+
_requestToken = nil;
5657
}
5758

5859
- (void)dispatchCallback:(dispatch_block_t)callback
@@ -66,6 +67,11 @@ - (void)dispatchCallback:(dispatch_block_t)callback
6667

6768
- (void)start
6869
{
70+
if (_status != RCTNetworkTaskPending) {
71+
RCTLogError(@"RCTNetworkTask was already started or completed");
72+
return;
73+
}
74+
6975
if (_requestToken == nil) {
7076
id token = [_handler sendRequest:_request withDelegate:self];
7177
if ([self validateRequestToken:token]) {
@@ -77,6 +83,10 @@ - (void)start
7783

7884
- (void)cancel
7985
{
86+
if (_status == RCTNetworkTaskFinished) {
87+
return;
88+
}
89+
8090
_status = RCTNetworkTaskFinished;
8191
id token = _requestToken;
8292
if (token && [_handler respondsToSelector:@selector(cancelRequest:)]) {

React.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
44

55
Pod::Spec.new do |s|
66
s.name = "React"
7-
s.version = "0.36.0-rc.0"
7+
s.version = "0.36.0-rc.1"
88
s.summary = package['description']
99
s.description = <<-DESC
1010
React Native apps are built using the React JS

ReactAndroid/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION_NAME=0.36.0-rc.0
1+
VERSION_NAME=0.36.0-rc.1
22
GROUP=com.facebook.react
33

44
POM_NAME=ReactNative

docs/HeadlessJSAndroid.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: headless-js-android
3+
title: Headless JS
4+
layout: docs
5+
category: Guides (Android)
6+
permalink: docs/headless-js-android.html
7+
next: running-on-device-android
8+
previous: native-components-android
9+
---
10+
11+
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.
12+
13+
## The JS API
14+
15+
A task is a simple async function that you register on `AppRegistry`, similar to registering React applications:
16+
17+
```js
18+
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
19+
```
20+
21+
Then, in `SomeTaskName.js`:
22+
23+
```js
24+
module.exports = async (taskData) => {
25+
// do stuff
26+
}
27+
```
28+
29+
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).
30+
31+
## The Java API
32+
33+
Yes, this does still require some native code, but it's pretty thin. You need to extend `HeadlessJsTaskService` and override `getTaskConfig`, e.g.:
34+
35+
```java
36+
public class MyTaskService extends FbHeadlessJsTaskService {
37+
38+
@Override
39+
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
40+
Bundle extras = intent.getExtras();
41+
if (extras != null) {
42+
return new HeadlessJsTaskConfig(
43+
"SomeTaskName",
44+
Arguments.fromBundle(extras),
45+
5000);
46+
}
47+
return null;
48+
}
49+
}
50+
```
51+
52+
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.
53+
54+
## Caveats
55+
56+
* 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.
57+
* If you start your service from a `BroadcastReceiver`, make sure to call `HeadlessJsTaskService.acquireWakelockNow()` before returning from `onReceive()`.
58+
59+
[0]: https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

docs/NativeComponentsAndroid.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Native UI Components
44
layout: docs
55
category: Guides (Android)
66
permalink: docs/native-components-android.html
7-
next: running-on-device-android
7+
next: headless-js-android
88
previous: native-modules-android
99
---
1010

docs/RunningOnDeviceAndroid.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ layout: docs
55
category: Guides (Android)
66
permalink: docs/running-on-device-android.html
77
next: signed-apk-android
8-
props: native-components-android
8+
previous: headless-js-android
99
---
1010

1111
## Prerequisite: USB Debugging

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native",
3-
"version": "0.36.0-rc.0",
3+
"version": "0.36.0-rc.1",
44
"description": "A framework for building native apps using React",
55
"license": "BSD-3-Clause",
66
"repository": {

0 commit comments

Comments
 (0)