From 64dd140c1b3222d4b0447066c9cab1f3e430cba4 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 11 Oct 2016 12:34:58 -0700 Subject: [PATCH 1/5] Cancel network requests from the correct queue Summary: Fix suggested by sooth-sayer (https://github.com/facebook/react-native/pull/10280) Reviewed By: mmmulani Differential Revision: D4001618 fbshipit-source-id: cc28d19d02a29b62d2bdbddcd30f94b1c1bcfd76 --- Libraries/Image/RCTImageLoader.m | 17 +++++++++++++---- Libraries/Network/RCTNetworkTask.m | 10 ++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 7c6685a0704..a460f712a04 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -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); diff --git a/Libraries/Network/RCTNetworkTask.m b/Libraries/Network/RCTNetworkTask.m index 3438143b6da..1a177fca060 100644 --- a/Libraries/Network/RCTNetworkTask.m +++ b/Libraries/Network/RCTNetworkTask.m @@ -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:)]) { From 44206a949f8c3ada7577a0ea93c26485ede1411d Mon Sep 17 00:00:00 2001 From: James Ide Date: Wed, 12 Oct 2016 22:31:37 -0700 Subject: [PATCH 2/5] [0.36.0-rc.1] Bump version numbers --- React.podspec | 2 +- ReactAndroid/gradle.properties | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/React.podspec b/React.podspec index 58c99796c46..90434f1d0b8 100644 --- a/React.podspec +++ b/React.podspec @@ -4,7 +4,7 @@ package = JSON.parse(File.read(File.join(__dir__, 'package.json'))) Pod::Spec.new do |s| s.name = "React" - s.version = "0.36.0-rc.0" + s.version = "0.36.0-rc.1" s.summary = package['description'] s.description = <<-DESC React Native apps are built using the React JS diff --git a/ReactAndroid/gradle.properties b/ReactAndroid/gradle.properties index 020a0e37aa6..95f99131547 100644 --- a/ReactAndroid/gradle.properties +++ b/ReactAndroid/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=0.36.0-rc.0 +VERSION_NAME=0.36.0-rc.1 GROUP=com.facebook.react POM_NAME=ReactNative diff --git a/package.json b/package.json index c161af23ed1..bb31825cdd2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native", - "version": "0.36.0-rc.0", + "version": "0.36.0-rc.1", "description": "A framework for building native apps using React", "license": "BSD-3-Clause", "repository": { From 60b2b5175d0448be33ff493d1ba30210e4c9ca79 Mon Sep 17 00:00:00 2001 From: Felix Oghina Date: Wed, 12 Oct 2016 06:18:35 -0700 Subject: [PATCH 3/5] add headless js guide Summary: **Test plan (required)** Run the website, check everything renders. Closes https://github.com/facebook/react-native/pull/10325 Differential Revision: D4008427 Pulled By: bestander fbshipit-source-id: 2ba78d33efab2bf2267a806bfc8d3b0ec50f54f5 --- docs/HeadlessJSAndroid.md | 57 +++++++++++++++++++++++++++++++++ docs/NativeComponentsAndroid.md | 2 +- docs/RunningOnDeviceAndroid.md | 2 +- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 docs/HeadlessJSAndroid.md diff --git a/docs/HeadlessJSAndroid.md b/docs/HeadlessJSAndroid.md new file mode 100644 index 00000000000..11257844365 --- /dev/null +++ b/docs/HeadlessJSAndroid.md @@ -0,0 +1,57 @@ +--- +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](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)), 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()`. \ No newline at end of file diff --git a/docs/NativeComponentsAndroid.md b/docs/NativeComponentsAndroid.md index 03d1cb66f06..7311713e0cb 100644 --- a/docs/NativeComponentsAndroid.md +++ b/docs/NativeComponentsAndroid.md @@ -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 --- diff --git a/docs/RunningOnDeviceAndroid.md b/docs/RunningOnDeviceAndroid.md index b0af260e4e1..c1eaedcfe45 100644 --- a/docs/RunningOnDeviceAndroid.md +++ b/docs/RunningOnDeviceAndroid.md @@ -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 From c2b60dd02980dfdee118cb8d4d2e64232395c12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Tue, 18 Oct 2016 14:33:05 -0700 Subject: [PATCH 4/5] Headless JS - fix markdown typo. Summary: Closes https://github.com/facebook/react-native/pull/10448 Differential Revision: D4039297 Pulled By: lacker fbshipit-source-id: 21a398e3d35b2aa3d34cd35c865d36cf5a9e4b25 --- docs/HeadlessJSAndroid.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/HeadlessJSAndroid.md b/docs/HeadlessJSAndroid.md index 11257844365..4fc6f4be3d7 100644 --- a/docs/HeadlessJSAndroid.md +++ b/docs/HeadlessJSAndroid.md @@ -49,9 +49,9 @@ public class MyTaskService extends FbHeadlessJsTaskService { } ``` -Now, whenever you [start your service](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)), e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down. +Now, whenever you [start your service](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent), 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()`. \ No newline at end of file +* If you start your service from a `BroadcastReceiver`, make sure to call `HeadlessJsTaskService.acquireWakelockNow()` before returning from `onReceive()`. From 8491e08755c468c3b3e1256b7f352cb44140d189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Wed, 19 Oct 2016 15:17:50 -0700 Subject: [PATCH 5/5] Undo markdown typo fix. Summary: See #10448. Confirmed link is rendered correctly. cc foghina Closes https://github.com/facebook/react-native/pull/10468 Differential Revision: D4047432 Pulled By: lacker fbshipit-source-id: dfa1427d6dcd7b2e5e66ce276cae1ed10778c4ff --- docs/HeadlessJSAndroid.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/HeadlessJSAndroid.md b/docs/HeadlessJSAndroid.md index 4fc6f4be3d7..e7a79f9a36f 100644 --- a/docs/HeadlessJSAndroid.md +++ b/docs/HeadlessJSAndroid.md @@ -49,9 +49,11 @@ public class MyTaskService extends FbHeadlessJsTaskService { } ``` -Now, whenever you [start your service](https://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent), e.g. as a periodic task or in response to some system event / broadcast, JS will spin up, run your task, then spin down. +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)