1+ package org.phoenixframework
2+
3+ import java.util.Timer
4+ import java.util.concurrent.ScheduledExecutorService
5+ import java.util.concurrent.ScheduledFuture
6+ import java.util.concurrent.ScheduledThreadPoolExecutor
7+ import java.util.concurrent.TimeUnit
8+ import kotlin.concurrent.schedule
9+
10+ // Copyright (c) 2019 Daniel Rees <daniel.rees18@gmail.com>
11+ //
12+ // Permission is hereby granted, free of charge, to any person obtaining a copy
13+ // of this software and associated documentation files (the "Software"), to deal
14+ // in the Software without restriction, including without limitation the rights
15+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+ // copies of the Software, and to permit persons to whom the Software is
17+ // furnished to do so, subject to the following conditions:
18+ //
19+ // The above copyright notice and this permission notice shall be included in
20+ // all copies or substantial portions of the Software.
21+ //
22+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+ // THE SOFTWARE.
29+
30+ /* *
31+ * A Timer class that schedules a callback to be called in the future. Can be configured
32+ * to use a custom retry pattern, such as exponential backoff.
33+ */
34+ class TimeoutTimer (
35+ private val scheduledExecutorService : ScheduledExecutorService ,
36+ private val callback : () -> Unit ,
37+ private val timerCalculation : (tries: Int ) -> Long
38+ ) {
39+
40+ /* * How many tries the Timer has attempted */
41+ private var tries: Int = 0
42+
43+ /* * The task that has been scheduled to be executed */
44+ private var futureTask: ScheduledFuture <* >? = null
45+
46+ /* *
47+ * Resets the Timer, clearing the number of current tries and stops
48+ * any scheduled timeouts.
49+ */
50+ fun reset () {
51+ this .tries = 0
52+ this .clearTimer()
53+ }
54+
55+ /* * Cancels any previous timeouts and scheduled a new one */
56+ fun scheduleTimeout () {
57+ this .clearTimer()
58+
59+ // Schedule a task to be performed after the calculated timeout in milliseconds
60+ val timeout = timerCalculation(tries + 1 )
61+ this .futureTask = scheduledExecutorService.schedule({
62+ this .tries + = 1
63+ this .callback.invoke()
64+ }, timeout, TimeUnit .MILLISECONDS )
65+ }
66+
67+ // ------------------------------------------------------------------------------
68+ // Private
69+ // ------------------------------------------------------------------------------
70+ private fun clearTimer () {
71+ // Cancel the task from completing, allowing it to fi
72+ this .futureTask?.cancel(true )
73+ this .futureTask = null
74+ }
75+ }
0 commit comments