- Don't restart action if previous task was finished for same location
- Do some user-defined action if location became invalid
This commit is contained in:
@@ -34,7 +34,8 @@ public abstract class LongRunningReadTask<RequestInfo, ResultData> {
|
|||||||
NOT_INITIALIZED,
|
NOT_INITIALIZED,
|
||||||
INITIALIZED,
|
INITIALIZED,
|
||||||
STARTED,
|
STARTED,
|
||||||
FINISHED
|
FINISHED,
|
||||||
|
FINISHED_AND_PROCESSED
|
||||||
}
|
}
|
||||||
|
|
||||||
private ProgressIndicator progressIndicator = null;
|
private ProgressIndicator progressIndicator = null;
|
||||||
@@ -51,19 +52,31 @@ public abstract class LongRunningReadTask<RequestInfo, ResultData> {
|
|||||||
throw new IllegalStateException("Task should be initialized state. Call init() method.");
|
throw new IllegalStateException("Task should be initialized state. Call init() method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cancel previous task if necessary
|
||||||
|
if (previousTask != null && previousTask.currentState == State.STARTED) {
|
||||||
|
if (requestInfo == null || !requestInfo.equals(previousTask.requestInfo)) {
|
||||||
|
previousTask.progressIndicator.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (requestInfo == null) {
|
if (requestInfo == null) {
|
||||||
|
if (previousTask != null && (previousTask.currentState == State.FINISHED_AND_PROCESSED || previousTask.currentState == State.FINISHED)) {
|
||||||
|
previousTask.hideResultOnInvalidLocation();
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previousTask != null && previousTask.currentState == State.STARTED) {
|
|
||||||
if (!requestInfo.equals(previousTask.requestInfo)) {
|
if (previousTask != null) {
|
||||||
// Previous task counting data for outdated request - cancel it.
|
if (previousTask.currentState == State.STARTED) {
|
||||||
previousTask.progressIndicator.cancel();
|
// Start new task only if previous isn't working on similar request
|
||||||
return true;
|
return !requestInfo.equals(previousTask.requestInfo);
|
||||||
}
|
}
|
||||||
else {
|
else if (previousTask.currentState == State.FINISHED_AND_PROCESSED) {
|
||||||
// If previous task is in progress and counting result for similar result don't start new task
|
if (requestInfo.equals(previousTask.requestInfo)) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +91,10 @@ public abstract class LongRunningReadTask<RequestInfo, ResultData> {
|
|||||||
throw new IllegalStateException("Task should be initialized with init() method");
|
throw new IllegalStateException("Task should be initialized with init() method");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (requestInfo == null) {
|
||||||
|
throw new IllegalStateException("Invalid request for task beginning");
|
||||||
|
}
|
||||||
|
|
||||||
currentState = State.STARTED;
|
currentState = State.STARTED;
|
||||||
|
|
||||||
beforeRun();
|
beforeRun();
|
||||||
@@ -123,7 +140,14 @@ public abstract class LongRunningReadTask<RequestInfo, ResultData> {
|
|||||||
ApplicationManager.getApplication().assertIsDispatchThread();
|
ApplicationManager.getApplication().assertIsDispatchThread();
|
||||||
|
|
||||||
currentState = State.FINISHED;
|
currentState = State.FINISHED;
|
||||||
onResultReady(requestInfo, resultData);
|
|
||||||
|
if (resultData != null) {
|
||||||
|
RequestInfo actualInfo = prepareRequestInfo();
|
||||||
|
if (requestInfo.equals(actualInfo)) {
|
||||||
|
currentState = State.FINISHED_AND_PROCESSED;
|
||||||
|
onResultReady(actualInfo, resultData);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,6 +176,11 @@ public abstract class LongRunningReadTask<RequestInfo, ResultData> {
|
|||||||
@Nullable
|
@Nullable
|
||||||
protected abstract RequestInfo prepareRequestInfo();
|
protected abstract RequestInfo prepareRequestInfo();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executed in GUI Thread.
|
||||||
|
*/
|
||||||
|
protected void hideResultOnInvalidLocation() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executed in GUI Thread right before task run. Do nothing by default.
|
* Executed in GUI Thread right before task run. Do nothing by default.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user