- How to add this library to your project
- Getting started
- Features
- Key-association callbacks
- Integrations
<dependency>
<groupId>io.github.edmondantes</groupId>
<artifactId>simple-kotlin-callbacks</artifactId>
<version>0.1.1</version>
</dependency>
implementation("io.github.edmondantes:simple-kotlin-callbacks:0.1.1")
implementation "io.github.edmondantes:simple-kotlin-callbacks:0.1.1"
val callbackManager = DefaultCallbackManager<String>()
callbackManager.add(SimpleCallback { context ->
if (context == null) {
println("Callback was cancelled")
} else {
println(context.data)
}
})
// ... \\
callbackManager.invoke("Hello world")
You can set priority for callbacks. Example:
callbackManager.add(callback1, priority = 1)
callbackManager.add(callback2, priority = 2)
In example before callback callback1
will be called before callback2
For enable this future, you should create DefaultCallbackManager
with first parameter SchedulerExecutorService
.
Example:
val callbackManager = DefaultCallbackManager<String>(
Executors.newScheduledThreadPool(1)
)
After that you can call method add
with additional parameter timeout
. Example:
callbackManager.add(SimpleCallback {
// ... \\
}, timeout = 1000)
After timeout callback will be cancelled.
For default all callback will not be deleted after call. For removing callback after call, please call method remove
in callback context. Please pay attention if you're using class SimpleCallback
before removing callback will be called
and callback context will be null
.
SimpleCallback { context ->
// ... \\
contex.remove()
}
If a callback want to ignore all another callback which have less priority, you can use method ignoreNextStage
in
callback context. Example:
val callback1 = SimpleCallback { context ->
// ... \\
context.ignoreNextStage()
}
callbackManager.add(callback1, 1)
callbackManager.add(callback2, 2)
In example before manager will call only callback callback1
, because this callback requires ignoring callbacks which
have less priority
If you want to get callback only for special key, you can use class DefaultKeyCallbackManager
. Example:
val callbackManager = DefaultKeyCallbackManager<String, String>()
callbackManager.add("key", callback1)
callbackManager.add("anotherKey", callback2)
// ... \\
callbackManager.invoke("key", "data")
In example before manager will call only callback callback1
, because callback2
associate with another key.
This type of managers support all features described before
In integrations you can use only these futures: priority, timeout for waiting result
If you want to get Future
from callback manager you can call method future
. Returned future supports canceling
feature. Example:
val future = callbackManager.future(priority = -1)
try {
future.get(5, TimeUnit.MILLISECONDS)
} catch (e: TimeoutException) {
future.cancel(true)
}
You can use wait
for wait callback's result in coroutine. If callback will be cancelled this method
throw CancellationException
. Example:
coroutineScope {
val result = callbackManager.wait(priority = 1, timeout = 2000)
}