@@ -11,6 +11,7 @@ import org.mozilla.experiments.nimbus.HardcodedNimbusFeatures
1111import org.mozilla.experiments.nimbus.NullVariables
1212import org.mozilla.experiments.nimbus.Variables
1313import java.util.concurrent.locks.ReentrantLock
14+ import kotlin.concurrent.withLock
1415
1516/* *
1617 * `FeatureHolder` is a class that unpacks a JSON object from the Nimbus SDK and transforms it into a useful
@@ -43,7 +44,7 @@ class FeatureHolder<T : FMLFeatureInterface>(
4344 * This can be resolved by setting `FxNimbus.initialize`, and after that by passing in a `Context` object.
4445 */
4546 fun value (): T =
46- lock.runBlock {
47+ lock.withLock {
4748 if (cachedValue != null ) {
4849 cachedValue!!
4950 } else {
@@ -105,7 +106,7 @@ class FeatureHolder<T : FMLFeatureInterface>(
105106 * This is most likely useful during testing only.
106107 */
107108 fun withCachedValue (value : T ? ) {
108- lock.runBlock {
109+ lock.withLock {
109110 cachedValue = value
110111 }
111112 }
@@ -116,7 +117,7 @@ class FeatureHolder<T : FMLFeatureInterface>(
116117 * This is most likely useful during testing and other generated code.
117118 */
118119 fun withInitializer (create : (Variables , SharedPreferences ? ) -> T ) {
119- lock.runBlock {
120+ lock.withLock {
120121 this .create = create
121122 this .cachedValue = null
122123 }
@@ -128,7 +129,7 @@ class FeatureHolder<T : FMLFeatureInterface>(
128129 * This is especially useful at start up and for imported features.
129130 */
130131 fun withSdk (getSdk : () -> FeaturesInterface ? ) {
131- lock.runBlock {
132+ lock.withLock {
132133 this .getSdk = getSdk
133134 this .cachedValue = null
134135 }
@@ -165,19 +166,10 @@ class FeatureHolder<T : FMLFeatureInterface>(
165166 * For example, a background worker might be scheduled to run every 24 hours, but
166167 * under test it would be desirable to run immediately, and only once.
167168 */
168- fun isUnderTest (): Boolean = lock.runBlock {
169- val sdk = getSdk() as ? HardcodedNimbusFeatures ? : return @runBlock false
169+ fun isUnderTest (): Boolean = lock.withLock {
170+ val sdk = getSdk() as ? HardcodedNimbusFeatures ? : return @withLock false
170171 sdk.hasFeature(featureId)
171172 }
172-
173- private fun <T > ReentrantLock.runBlock (block : () -> T ): T {
174- lock.lock()
175- try {
176- return block.invoke()
177- } finally {
178- lock.unlock()
179- }
180- }
181173}
182174
183175class NimbusFeatureException (message : String ) : Exception(message)
0 commit comments