Skip to content

EdmonDantes/simple-kotlin-callbacks

Repository files navigation

Simple kotlin callbacks

Maven Central GitHub Lines of code


Lightweight callback implementation, written in Pure Kotlin.

Table of Contents

  1. How to add this library to your project
  2. Getting started
  3. Features
    1. Priority
    2. Timeout for waiting result
    3. Reusing callback
    4. Stage managing
  4. Key-association callbacks
  5. Integrations
    1. Future from java.util.concurrent
    2. Coroutines

How add this library to your project

Maven

<dependency>
    <groupId>io.github.edmondantes</groupId>
    <artifactId>simple-kotlin-callbacks</artifactId>
    <version>0.1.1</version>
</dependency>

Gradle (kotlin)

implementation("io.github.edmondantes:simple-kotlin-callbacks:0.1.1")

Gradle (groove)

implementation "io.github.edmondantes:simple-kotlin-callbacks:0.1.1"

Getting started

val callbackManager = DefaultCallbackManager<String>()
callbackManager.add(SimpleCallback { context ->
    if (context == null) {
        println("Callback was cancelled")
    } else {
        println(context.data)
    }
})

// ... \\

callbackManager.invoke("Hello world")

Features

Priority

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

Timeout for waiting result

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.

Reusing callback

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()
}

Stage managing

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

Key-association callbacks

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

Integrations

In integrations you can use only these futures: priority, timeout for waiting result

Future from java.util.concurrent

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)
}

Coroutines

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)
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages