fix notification task timeout crashing

Reviewed By: hedgerwang

Differential Revision: D4088570

fbshipit-source-id: e2a217564d9325815e396daafbef2b7f06e84b33
This commit is contained in:
Emily Janzer 2016-10-27 17:21:38 -07:00 committed by Facebook Github Bot
parent 285786ad30
commit 3580de541d
2 changed files with 19 additions and 3 deletions

View File

@ -93,12 +93,12 @@ public class HeadlessJsTaskContext {
" while in foreground, but this is not allowed.");
}
final int taskId = mLastTaskId.incrementAndGet();
mActiveTasks.add(taskId);
reactContext.getJSModule(AppRegistry.class)
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
if (taskConfig.getTimeout() > 0) {
scheduleTaskTimeout(taskId, taskConfig.getTimeout());
}
mActiveTasks.add(taskId);
for (HeadlessJsTaskEventListener listener : mHeadlessJsTaskEventListeners) {
listener.onHeadlessJsTaskStart(taskId);
}
@ -107,7 +107,7 @@ public class HeadlessJsTaskContext {
/**
* Finish a JS task. Doesn't actually stop the task on the JS side, only removes it from the list
* of active tasks and notifies listeners.
* of active tasks and notifies listeners. A task can only be finished once.
*
* @param taskId the unique id returned by {@link #startTask}.
*/
@ -130,6 +130,14 @@ public class HeadlessJsTaskContext {
});
}
/**
* Check if a given task is currently running. A task is stopped if either {@link #finishTask} is
* called or it times out.
*/
public synchronized boolean isTaskRunning(final int taskId) {
return mActiveTasks.contains(taskId);
}
private void scheduleTaskTimeout(final int taskId, long timeout) {
Runnable runnable = new Runnable() {
@Override

View File

@ -9,6 +9,7 @@
package com.facebook.react.modules.core;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
@ -37,6 +38,13 @@ public class HeadlessJsTaskSupportModule extends ReactContextBaseJavaModule {
public void notifyTaskFinished(int taskId) {
HeadlessJsTaskContext headlessJsTaskContext =
HeadlessJsTaskContext.getInstance(getReactApplicationContext());
if (headlessJsTaskContext.isTaskRunning(taskId)) {
headlessJsTaskContext.finishTask(taskId);
} else {
FLog.w(
HeadlessJsTaskSupportModule.class,
"Tried to finish non-active task with id %d. Did it time out?",
taskId);
}
}
}