kohesive / injekt

Dependency Injection for Kotlin
MIT License
235 stars 19 forks source link

addSingletonFactory is broken #7

Closed nsk-mironov closed 9 years ago

nsk-mironov commented 9 years ago
public class Singleton(val count: Int) {
  init {
    println("not really a singleton $count")
  }
}

public class Magic() {
  public val first: Singleton by Delegates.injectValue()
  public val second: Singleton by Delegates.injectValue()
  public val third: Singleton by Delegates.injectValue()
}

public object Hello : InjektMain() {
  override fun InjektRegistrar.registerInjectables() {
    val counter = AtomicInteger()

    addSingletonFactory {
      Singleton(counter.incrementAndGet())
    }
  }

  platformStatic public fun main(args: Array<String>) {
    val test = Magic()

    println("first ${test.first.count}")
    println("second ${test.second.count}")
    println("third ${test.third.count}")
  }
}

Output:

not really a singleton 1
not really a singleton 2
not really a singleton 3
first 1
second 1
third 1
apatrida commented 9 years ago

Thanks, problem with the concurrentGetOrPut taken from Kara is broken, quick fix coming.

apatrida commented 9 years ago

Fixed in v 1.1.1 - releasing to maven central.

I will also release 1.1.1-JDK8 which uses a safe concurrentGetOrPut. And the registry based on Guava coming soon is also safe.