@@ -136,20 +136,25 @@ internal class OperationRepo(
136
136
return waiter.waitForWake()
137
137
}
138
138
139
- // WARNING: Never set to true, until budget rules are added, even for internal use!
139
+ /* *
140
+ * Only used inside this class, adds OperationQueueItem to queue
141
+ * WARNING: Never set flush=true until budget rules are added, even for internal use!
142
+ *
143
+ * @returns true if the OperationQueueItem was added, false if not
144
+ */
140
145
private fun internalEnqueue (
141
146
queueItem : OperationQueueItem ,
142
147
flush : Boolean ,
143
148
addToStore : Boolean ,
144
149
index : Int? = null,
145
- ) {
146
- val hasExisting = queue.any { it.operation.id == queueItem.operation.id }
147
- if (hasExisting) {
148
- Logging .debug(" OperationRepo: internalEnqueue - operation.id: ${queueItem.operation.id} already exists in the queue." )
149
- return
150
- }
151
-
150
+ ): Boolean {
152
151
synchronized(queue) {
152
+ val hasExisting = queue.any { it.operation.id == queueItem.operation.id }
153
+ if (hasExisting) {
154
+ Logging .debug(" OperationRepo: internalEnqueue - operation.id: ${queueItem.operation.id} already exists in the queue." )
155
+ return false
156
+ }
157
+
153
158
if (index != null ) {
154
159
queue.add(index, queueItem)
155
160
} else {
@@ -161,6 +166,7 @@ internal class OperationRepo(
161
166
}
162
167
163
168
waiter.wake(LoopWaiterMessage (flush, 0 ))
169
+ return true
164
170
}
165
171
166
172
/* *
@@ -405,18 +411,25 @@ internal class OperationRepo(
405
411
406
412
/* *
407
413
* Load saved operations from preference service and add them into the queue
414
+ * WARNING: Make sure queue.remove is NEVER called while this method is
415
+ * running, as internalEnqueue will throw IndexOutOfBounds or put things
416
+ * out of order if what was removed was something added by this method.
417
+ * - This never happens now, but is a landmine to be aware of!
408
418
* NOTE: Sometimes the loading might take longer than expected due to I/O reads from disk
409
419
* Any I/O implies executing time will vary greatly.
410
420
*/
411
421
internal fun loadSavedOperations () {
412
422
_operationModelStore .loadOperations()
413
- for (operation in _operationModelStore .list().withIndex()) {
414
- internalEnqueue(
415
- OperationQueueItem (operation.value, bucket = enqueueIntoBucket),
416
- flush = false ,
417
- addToStore = false ,
418
- operation.index,
419
- )
423
+ var successfulIndex = 0
424
+ for (operation in _operationModelStore .list()) {
425
+ val successful =
426
+ internalEnqueue(
427
+ OperationQueueItem (operation, bucket = enqueueIntoBucket),
428
+ flush = false ,
429
+ addToStore = false ,
430
+ index = successfulIndex,
431
+ )
432
+ if (successful) successfulIndex++
420
433
}
421
434
loadedSubscription.fire { it.onOperationRepoLoaded() }
422
435
}
0 commit comments