mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
fix notification task timeout crashing
Reviewed By: hedgerwang Differential Revision: D4088570 fbshipit-source-id: e2a217564d9325815e396daafbef2b7f06e84b33
This commit is contained in:
committed by
Facebook Github Bot
parent
285786ad30
commit
3580de541d
@@ -93,12 +93,12 @@ public class HeadlessJsTaskContext {
|
|||||||
" while in foreground, but this is not allowed.");
|
" while in foreground, but this is not allowed.");
|
||||||
}
|
}
|
||||||
final int taskId = mLastTaskId.incrementAndGet();
|
final int taskId = mLastTaskId.incrementAndGet();
|
||||||
|
mActiveTasks.add(taskId);
|
||||||
reactContext.getJSModule(AppRegistry.class)
|
reactContext.getJSModule(AppRegistry.class)
|
||||||
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
|
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
|
||||||
if (taskConfig.getTimeout() > 0) {
|
if (taskConfig.getTimeout() > 0) {
|
||||||
scheduleTaskTimeout(taskId, taskConfig.getTimeout());
|
scheduleTaskTimeout(taskId, taskConfig.getTimeout());
|
||||||
}
|
}
|
||||||
mActiveTasks.add(taskId);
|
|
||||||
for (HeadlessJsTaskEventListener listener : mHeadlessJsTaskEventListeners) {
|
for (HeadlessJsTaskEventListener listener : mHeadlessJsTaskEventListeners) {
|
||||||
listener.onHeadlessJsTaskStart(taskId);
|
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
|
* 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}.
|
* @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) {
|
private void scheduleTaskTimeout(final int taskId, long timeout) {
|
||||||
Runnable runnable = new Runnable() {
|
Runnable runnable = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
package com.facebook.react.modules.core;
|
package com.facebook.react.modules.core;
|
||||||
|
|
||||||
|
import com.facebook.common.logging.FLog;
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
@@ -37,6 +38,13 @@ public class HeadlessJsTaskSupportModule extends ReactContextBaseJavaModule {
|
|||||||
public void notifyTaskFinished(int taskId) {
|
public void notifyTaskFinished(int taskId) {
|
||||||
HeadlessJsTaskContext headlessJsTaskContext =
|
HeadlessJsTaskContext headlessJsTaskContext =
|
||||||
HeadlessJsTaskContext.getInstance(getReactApplicationContext());
|
HeadlessJsTaskContext.getInstance(getReactApplicationContext());
|
||||||
headlessJsTaskContext.finishTask(taskId);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user