@@ -136,20 +136,25 @@ internal class OperationRepo(
136136 return waiter.waitForWake()
137137 }
138138
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+ */
140145 private fun internalEnqueue (
141146 queueItem : OperationQueueItem ,
142147 flush : Boolean ,
143148 addToStore : Boolean ,
144149 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 {
152151 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+
153158 if (index != null ) {
154159 queue.add(index, queueItem)
155160 } else {
@@ -161,6 +166,7 @@ internal class OperationRepo(
161166 }
162167
163168 waiter.wake(LoopWaiterMessage (flush, 0 ))
169+ return true
164170 }
165171
166172 /* *
@@ -405,18 +411,25 @@ internal class OperationRepo(
405411
406412 /* *
407413 * 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!
408418 * NOTE: Sometimes the loading might take longer than expected due to I/O reads from disk
409419 * Any I/O implies executing time will vary greatly.
410420 */
411421 internal fun loadSavedOperations () {
412422 _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++
420433 }
421434 loadedSubscription.fire { it.onOperationRepoLoaded() }
422435 }
0 commit comments