Skip to content

Commit 44e50a6

Browse files
committed
Add Collection overloads in persistent timeout methods
Also fixes type-safe timeout methods giving a nested lists
1 parent e84703b commit 44e50a6

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

src/main/kotlin/io/github/freya022/botcommands/api/components/builder/ITimeoutableComponent.kt

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import io.github.freya022.botcommands.internal.utils.javaMethodInternal
1717
import io.github.freya022.botcommands.internal.utils.throwArgument
1818
import kotlinx.coroutines.runBlocking
1919
import net.dv8tion.jda.api.entities.User
20+
import java.time.Duration as JavaDuration
2021
import java.util.concurrent.TimeUnit
2122
import javax.annotation.CheckReturnValue
2223
import kotlin.reflect.*
2324
import kotlin.time.*
24-
import java.time.Duration as JavaDuration
2525

2626
/**
2727
* Allows components to have timeouts.
@@ -126,6 +126,44 @@ interface ITimeoutableComponent<T : ITimeoutableComponent<T>> {
126126
* @see ITimeoutableComponent
127127
*/
128128
interface IPersistentTimeoutableComponent<T : IPersistentTimeoutableComponent<T>> : ITimeoutableComponent<T> {
129+
/**
130+
* Sets the timeout on this component, invalidating the component on expiration,
131+
* and running the timeout handler with the given name and its arguments.
132+
*
133+
* **Note:** Components inside groups cannot have timeouts.
134+
*
135+
* ### Timeout cancellation
136+
* The timeout will be canceled once a component has been deleted,
137+
* including if the component was set to a [single use][IUniqueComponent.singleUse].
138+
*
139+
* ### Component deletion
140+
* - If the component is a group, then all of its owned components will also be deleted.
141+
* - If the component is inside a group, then all the group's components will also be deleted.
142+
*
143+
* ### Timeout data
144+
* The data passed is [serialized][TimeoutParameterResolver.serialize]
145+
* and later [deserialized][TimeoutParameterResolver.resolveSuspend] using their resolver.
146+
*
147+
* Each passed object needs to correspond to a parameter of the function (in the declaration order, excluding non-data parameters).
148+
*
149+
* For objects supported by default (see [ParameterResolver]) and by other [TimeoutParameterResolver]s,
150+
* you must annotate your parameter with [@TimeoutData][TimeoutData].
151+
*
152+
* For serializable objects, you can instead use [@SerializableTimeoutData][SerializableTimeoutData].
153+
*
154+
* @param timeout The value of the timeout
155+
* @param timeoutUnit The unit of the timeout
156+
* @param handlerName The name of the handler to run when the component expires,
157+
* defined by either [@ComponentTimeoutHandler][ComponentTimeoutHandler] or [@GroupTimeoutHandler][GroupTimeoutHandler] depending on the type
158+
* @param data The data to pass to the timeout handler
159+
*
160+
* @see ComponentTimeoutHandler @ComponentTimeoutHandler
161+
* @see GroupTimeoutHandler @GroupTimeoutHandler
162+
*/
163+
@CheckReturnValue
164+
fun timeout(timeout: Long, timeoutUnit: TimeUnit, handlerName: String, data: List<Any?>): T =
165+
timeout(timeout.toDuration(timeoutUnit.toDurationUnit()), handlerName, data)
166+
129167
/**
130168
* Sets the timeout on this component, invalidating the component on expiration,
131169
* and running the timeout handler with the given name and its arguments.
@@ -164,6 +202,43 @@ interface IPersistentTimeoutableComponent<T : IPersistentTimeoutableComponent<T>
164202
fun timeout(timeout: Long, timeoutUnit: TimeUnit, handlerName: String, vararg data: Any?): T =
165203
timeout(timeout.toDuration(timeoutUnit.toDurationUnit()), handlerName, *data)
166204

205+
/**
206+
* Sets the timeout on this component, invalidating the component on expiration,
207+
* and running the timeout handler with the given name and its arguments.
208+
*
209+
* **Note:** Components inside groups cannot have timeouts.
210+
*
211+
* ### Timeout cancellation
212+
* The timeout will be canceled once a component has been deleted,
213+
* including if the component was set to a [single use][IUniqueComponent.singleUse].
214+
*
215+
* ### Component deletion
216+
* - If the component is a group, then all of its owned components will also be deleted.
217+
* - If the component is inside a group, then all the group's components will also be deleted.
218+
*
219+
* ### Timeout data
220+
* The data passed is [serialized][TimeoutParameterResolver.serialize]
221+
* and later [deserialized][TimeoutParameterResolver.resolveSuspend] using their resolver.
222+
*
223+
* Each passed object needs to correspond to a parameter of the function (in the declaration order, excluding non-data parameters).
224+
*
225+
* For objects supported by default (see [ParameterResolver]) and by other [TimeoutParameterResolver]s,
226+
* you must annotate your parameter with [@TimeoutData][TimeoutData].
227+
*
228+
* For serializable objects, you can instead use [@SerializableTimeoutData][SerializableTimeoutData].
229+
*
230+
* @param timeout The duration of the timeout
231+
* @param handlerName The name of the handler to run when the component expires,
232+
* defined by either [@ComponentTimeoutHandler][ComponentTimeoutHandler] or [@GroupTimeoutHandler][GroupTimeoutHandler] depending on the type
233+
* @param data The data to pass to the timeout handler
234+
*
235+
* @see ComponentTimeoutHandler @ComponentTimeoutHandler
236+
* @see GroupTimeoutHandler @GroupTimeoutHandler
237+
*/
238+
@CheckReturnValue
239+
fun timeout(timeout: JavaDuration, handlerName: String, data: List<Any?>): T =
240+
timeout(timeout.toKotlinDuration(), handlerName, data)
241+
167242
/**
168243
* Sets the timeout on this component, invalidating the component on expiration,
169244
* and running the timeout handler with the given name and its arguments.
@@ -199,7 +274,43 @@ interface IPersistentTimeoutableComponent<T : IPersistentTimeoutableComponent<T>
199274
*/
200275
@CheckReturnValue
201276
fun timeout(timeout: JavaDuration, handlerName: String, vararg data: Any?): T =
202-
timeout(timeout.toKotlinDuration(), handlerName, *data)
277+
timeout(timeout.toKotlinDuration(), handlerName, data.toList())
278+
279+
/**
280+
* Sets the timeout on this component, invalidating the component on expiration,
281+
* and running the timeout handler with the given name and its arguments.
282+
*
283+
* **Note:** Components inside groups cannot have timeouts.
284+
*
285+
* ### Timeout cancellation
286+
* The timeout will be canceled once a component has been deleted,
287+
* including if the component was set to a [single use][IUniqueComponent.singleUse].
288+
*
289+
* ### Component deletion
290+
* - If the component is a group, then all of its owned components will also be deleted.
291+
* - If the component is inside a group, then all the group's components will also be deleted.
292+
*
293+
* ### Timeout data
294+
* The data passed is [serialized][TimeoutParameterResolver.serialize]
295+
* and later [deserialized][TimeoutParameterResolver.resolveSuspend] using their resolver.
296+
*
297+
* Each passed object needs to correspond to a parameter of the function (in the declaration order, excluding non-data parameters).
298+
*
299+
* For objects supported by default (see [ParameterResolver]) and by other [TimeoutParameterResolver]s,
300+
* you must annotate your parameter with [@TimeoutData][TimeoutData].
301+
*
302+
* For serializable objects, you can instead use [@SerializableTimeoutData][SerializableTimeoutData].
303+
*
304+
* @param timeout The duration of the timeout
305+
* @param handlerName The name of the handler to run when the component expires,
306+
* defined by either [@ComponentTimeoutHandler][ComponentTimeoutHandler] or [@GroupTimeoutHandler][GroupTimeoutHandler] depending on the type
307+
* @param data The data to pass to the timeout handler
308+
*
309+
* @see ComponentTimeoutHandler @ComponentTimeoutHandler
310+
* @see GroupTimeoutHandler @GroupTimeoutHandler
311+
*/
312+
@JvmSynthetic
313+
fun timeout(timeout: Duration, handlerName: String, data: List<Any?>): T
203314

204315
/**
205316
* Sets the timeout on this component, invalidating the component on expiration,
@@ -235,7 +346,8 @@ interface IPersistentTimeoutableComponent<T : IPersistentTimeoutableComponent<T>
235346
* @see GroupTimeoutHandler @GroupTimeoutHandler
236347
*/
237348
@JvmSynthetic
238-
fun timeout(timeout: Duration, handlerName: String, vararg data: Any?): T
349+
fun timeout(timeout: Duration, handlerName: String, vararg data: Any?): T =
350+
timeout(timeout, handlerName, data.toList())
239351
}
240352

241353
/**
@@ -986,7 +1098,7 @@ fun <C : IPersistentTimeoutableComponent<C>, E : ITimeoutData, T1, T2, T3, T4, T
9861098
}
9871099

9881100
private fun <C : IPersistentTimeoutableComponent<C>> C.timeoutWithBoundCallable(duration: Duration, func: KFunction<*>, data: List<Any?>): C {
989-
return timeout(duration, findHandlerName(func), *data.toTypedArray())
1101+
return timeout(duration, findHandlerName(func), data)
9901102
}
9911103

9921104
/**

src/main/kotlin/io/github/freya022/botcommands/internal/components/builder/mixin/impl/PersistentTimeoutableComponentImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ internal class PersistentTimeoutableComponentImpl<T : IPersistentTimeoutableComp
4141
this.timeout = null
4242
}
4343

44-
override fun timeout(timeout: Duration, handlerName: String, vararg data: Any?): T = applyInstance {
44+
override fun timeout(timeout: Duration, handlerName: String, data: List<Any?>): T = applyInstance {
4545
Checks.checkFinite(timeout, "timeout")
4646
Checks.checkFitInt(timeout, "timeout")
4747

4848
this.timeoutDuration = timeout
49-
this.timeout = PersistentTimeout.create(context, componentType, handlerName, data.toList())
49+
this.timeout = PersistentTimeout.create(context, componentType, handlerName, data)
5050
}
5151
}

0 commit comments

Comments
 (0)