Skip to content

Commit 1c2bf82

Browse files
committed
prevent adding executionInterval & postCreateDelay
After we wait for the opRepoPostCreateDelay time call to waiter.wake() would cause waitForNewOperationAndExecutionInterval() to wake up as we expect, however it was then waiting for the full time on opRepoExecutionInterval as well. To solve this we simply subtract the time we already waited from opRepoExecutionInterval. With the current values this means we fully skip opRepoExecutionInterval so we only wait opRepoPostCreateDelay.
1 parent fb10758 commit 1c2bf82

File tree

1 file changed

+14
-13
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl

1 file changed

+14
-13
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/core/internal/operations/impl/OperationRepo.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ internal class OperationRepo(
3838
}
3939
}
4040

41+
internal class LoopWaiterMessage(
42+
val force: Boolean,
43+
val previousWaitedTime: Long = 0,
44+
)
45+
4146
private val executorsMap: Map<String, IOperationExecutor>
4247
private val queue = mutableListOf<OperationQueueItem>()
43-
private val waiter = WaiterWithValue<Boolean>()
48+
private val waiter = WaiterWithValue<LoopWaiterMessage>()
4449
private var paused = false
4550
private var coroutineScope = CoroutineScope(newSingleThreadContext(name = "OpRepo"))
4651

@@ -124,7 +129,7 @@ internal class OperationRepo(
124129
}
125130
}
126131

127-
waiter.wake(flush)
132+
waiter.wake(LoopWaiterMessage(flush, 0))
128133
}
129134

130135
/**
@@ -161,16 +166,16 @@ internal class OperationRepo(
161166
*/
162167
private suspend fun waitForNewOperationAndExecutionInterval() {
163168
// 1. Wait for an operation to be enqueued
164-
var force = waiter.waitForWake()
169+
var wakeMessage = waiter.waitForWake()
165170

166171
// 2. Wait at least the time defined in opRepoExecutionInterval
167172
// so operations can be grouped, unless one of them used
168173
// flush=true (AKA force)
169174
var lastTime = _time.currentTimeMillis
170-
var remainingTime = _configModelStore.model.opRepoExecutionInterval
171-
while (!force && remainingTime > 0) {
175+
var remainingTime = _configModelStore.model.opRepoExecutionInterval - wakeMessage.previousWaitedTime
176+
while (!wakeMessage.force && remainingTime > 0) {
172177
withTimeoutOrNull(remainingTime) {
173-
force = waiter.waitForWake()
178+
wakeMessage = waiter.waitForWake()
174179
}
175180
remainingTime -= _time.currentTimeMillis - lastTime
176181
lastTime = _time.currentTimeMillis
@@ -198,14 +203,10 @@ internal class OperationRepo(
198203
}
199204
response.idTranslations.values.forEach { _newRecordState.add(it) }
200205
coroutineScope.launch {
201-
delay(_configModelStore.model.opRepoPostCreateDelay)
206+
val waitTime = _configModelStore.model.opRepoPostCreateDelay
207+
delay(waitTime)
202208
synchronized(queue) {
203-
// NOTE: Even if the queue is not empty we may wake
204-
// when not needed, as those operations may not have
205-
// depended on these ids. This however should be very
206-
// rare and the side-effect is only a bit less
207-
// batching.
208-
if (queue.isNotEmpty()) waiter.wake(false)
209+
if (queue.isNotEmpty()) waiter.wake(LoopWaiterMessage(false, waitTime))
209210
}
210211
}
211212
}

0 commit comments

Comments
 (0)