Skip to content

Library adding lifetime API to use with resource destructors

License

Notifications You must be signed in to change notification settings

exerro/lifetimes-kt

Repository files navigation

lifetimes-kt

JitPack badge

A lifetime API to help with resource lifetimes and cleanup.

A lifetime represents a time span for which it is "alive". Destructors can be attached to a lifetime to be called when the lifetime ends.

withLifetime {
    onLifetimeEnded { println("I was called!") }
    println("I run first.")
    //> I run first.
}
//> I was called!

Using Kotlin contexts, this approach can be streamlined and composed:

context (Lifetime)
fun doLivingThing() {
    onLifetimeEnded { println("I was called!") }
}

withLifetime {
    doLivingThing()
    println("I run first.")
    //> I run first.
}
//> I was called!

Installation

Check out the releases, or using a build system...

Gradle (build.gradle.kts)

repositories {
    // ...
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("me.exerro:lifetimes-kt:1.2.1")
}

Maven

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

<dependency>
  <groupId>me.exerro</groupId>
  <artifactId>lifetimes-kt</artifactId>
  <version>1.2.1</version>
</dependency>

Getting started

The documentation is heavily linked with "see also"s, so take a look at the docs for Lifetime.

A useful wrapper is withLifetime, which introduces a lifetime spanning a block as shown above. When more control is required, use Lifetime.createDetached to explicitly manage when the lifetime ends.

Sometimes, constant lifetimes might be wanted, for which there exists Lifetime.createNeverEnding and Lifetime.createAlreadyEnded.

When there are already lifetimes available, using Lifetime.createChildOf can build a lifetime which relates to some parents.

About

Library adding lifetime API to use with resource destructors

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages