@@ -63,7 +63,7 @@ UpdateTask getOrCreateUpdateTask(UpdateTaskProducer producer) {
63
63
UpdateTaskImpl impl = new UpdateTaskImpl ();
64
64
sequentialExecutor .execute (
65
65
() -> {
66
- if (! isOngoing ( cachedUpdateTaskImpl )) {
66
+ if (cachedUpdateTaskImpl == null || cachedUpdateTaskImpl . isComplete ( )) {
67
67
cachedUpdateTaskImpl = producer .produce ();
68
68
}
69
69
impl .shadow (cachedUpdateTaskImpl );
@@ -72,50 +72,59 @@ UpdateTask getOrCreateUpdateTask(UpdateTaskProducer producer) {
72
72
}
73
73
74
74
/**
75
- * Sets the exception on the cached {@link UpdateTaskImpl}, if there is one and it is not
75
+ * Sets the progress on the cached {@link UpdateTaskImpl}, if there is one and it is not
76
76
* completed.
77
+ *
78
+ * <p>The task must be created using {@link #getOrCreateUpdateTask} before calling this method.
79
+ *
80
+ * @return A task that resolves when the change is applied, or fails with {@link
81
+ * IllegalStateException} if the task was not created yet or was already completed.
77
82
*/
78
83
Task <Void > setProgress (UpdateProgress progress ) {
79
- TaskCompletionSource <Void > taskCompletionSource = new TaskCompletionSource <>();
80
- sequentialExecutor .execute (
81
- () -> {
82
- if (isOngoing (cachedUpdateTaskImpl )) {
83
- cachedUpdateTaskImpl .updateProgress (progress );
84
- }
85
- });
86
- return taskCompletionSource .getTask ();
84
+ return executeIfOngoing (() -> cachedUpdateTaskImpl .updateProgress (progress ));
87
85
}
88
86
89
87
/**
90
88
* Sets the result on the cached {@link UpdateTaskImpl}, if there is one and it is not completed.
89
+ *
90
+ * <p>The task must be created using {@link #getOrCreateUpdateTask} before calling this method.
91
+ *
92
+ * @return A task that resolves when the change is applied, or fails with {@link
93
+ * IllegalStateException} if the task was not created yet or was already completed.
91
94
*/
92
95
Task <Void > setResult () {
93
- TaskCompletionSource <Void > taskCompletionSource = new TaskCompletionSource <>();
94
- sequentialExecutor .execute (
95
- () -> {
96
- if (isOngoing (cachedUpdateTaskImpl )) {
97
- cachedUpdateTaskImpl .setResult ();
98
- }
99
- });
100
- return taskCompletionSource .getTask ();
96
+ return executeIfOngoing (() -> cachedUpdateTaskImpl .setResult ());
101
97
}
102
98
103
99
/**
104
100
* Sets the exception on the cached {@link UpdateTaskImpl}, if there is one and it is not
105
101
* completed.
102
+ *
103
+ * <p>The task must be created using {@link #getOrCreateUpdateTask} before calling this method.
104
+ *
105
+ * @return A task that resolves when the change is applied, or fails with {@link
106
+ * IllegalStateException} if the task was not created yet or was already completed.
106
107
*/
107
108
Task <Void > setException (FirebaseAppDistributionException e ) {
109
+ return executeIfOngoing (() -> cachedUpdateTaskImpl .setException (e ));
110
+ }
111
+
112
+ private Task <Void > executeIfOngoing (Runnable r ) {
108
113
TaskCompletionSource <Void > taskCompletionSource = new TaskCompletionSource <>();
109
114
sequentialExecutor .execute (
110
115
() -> {
111
- if (isOngoing (cachedUpdateTaskImpl )) {
112
- cachedUpdateTaskImpl .setException (e );
116
+ if (cachedUpdateTaskImpl == null ) {
117
+ taskCompletionSource .setException (
118
+ new IllegalStateException (
119
+ "Tried to set exception before calling getOrCreateUpdateTask()" ));
120
+ } else if (cachedUpdateTaskImpl .isComplete ()) {
121
+ taskCompletionSource .setException (
122
+ new IllegalStateException ("Tried to set exception on a completed task" ));
123
+ } else {
124
+ r .run ();
125
+ taskCompletionSource .setResult (null );
113
126
}
114
127
});
115
128
return taskCompletionSource .getTask ();
116
129
}
117
-
118
- private static boolean isOngoing (UpdateTask updateTask ) {
119
- return updateTask != null && !updateTask .isComplete ();
120
- }
121
130
}
0 commit comments