Skip to content

Commit

Permalink
Get rid of @ThreadLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
alshan committed Nov 4, 2024
1 parent 6d95b8b commit 47f82e5
Showing 1 changed file with 4 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@

package org.jetbrains.letsPlot.core.plot.base.render.svg

import org.jetbrains.letsPlot.commons.intern.concurrent.AtomicInteger
import org.jetbrains.letsPlot.commons.intern.random.RandomString.randomString
import kotlin.native.concurrent.ThreadLocal

// In Kotlin Native objects a frozen by default. Annotate with `ThreadLocal` to unfreeze.
// See: https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md
// Required mutations:
// - `suffixGen`
@ThreadLocal
object SvgUID {
private var suffixGen: () -> Any = { randomString(6) }

// For tests only.
val index = AtomicInteger(-1)
fun setUpForTest() {
val incrementalId = IncrementalId()
suffixGen = { incrementalId.next() }
suffixGen = { "clip-${index.incrementAndGet()}" }
}

fun get(prefix: String): String {
Expand All @@ -30,9 +26,4 @@ object SvgUID {
}
return "$prefix${suffixGen()}"
}

private class IncrementalId {
private var nextIndex = 0
fun next() = ("clip-${nextIndex++}")
}
}

0 comments on commit 47f82e5

Please sign in to comment.