Skip to content

Commit

Permalink
Hooks update (#47)
Browse files Browse the repository at this point in the history
* ClientReady hooks rework

* Fix autoPoll init in case of fetching

* Update .gitignore

* Added ktor-client dependency to nativeRestMain sourceSet.

* ktlint fixes

* Fixup tests

* Update ConfigService.kt

* Update Entry to handle cachedString

* Code clean based on review

* update jsMain dependency

* Prepare 4.1.0 release

* Deprecated original addOnClientReady method

* Klint and test fix

* testHooks test cover deprecated method

---------

Co-authored-by: Peter Csajtai <[email protected]>
  • Loading branch information
novalisdenahi and z4kn4fein authored Nov 21, 2024
1 parent eccaffd commit c1ecdc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
30 changes: 26 additions & 4 deletions src/commonMain/kotlin/com/configcat/Hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,31 @@ import kotlinx.atomicfu.locks.withLock
*/
public class Hooks {
private val isClientReady = atomic(false)
private val clientCacheState = atomic<ClientCacheState>(ClientCacheState.NO_FLAG_DATA)
private val onClientReady: MutableList<(ClientCacheState) -> Unit> = mutableListOf()
private val clientCacheState = atomic(ClientCacheState.NO_FLAG_DATA)
private val onClientReady: MutableList<() -> Unit> = mutableListOf()
private val onClientReadyWithState: MutableList<(ClientCacheState) -> Unit> = mutableListOf()
private val onConfigChanged: MutableList<(Map<String, Setting>) -> Unit> = mutableListOf()
private val onFlagEvaluated: MutableList<(EvaluationDetails) -> Unit> = mutableListOf()
private val onError: MutableList<(String) -> Unit> = mutableListOf()
private val lock: ReentrantLock = reentrantLock()

/**
* This event is sent when the SDK reaches the ready state.
* If the SDK is configured with lazy load or manual polling it's considered ready right after instantiation.
* If it's using auto polling, the ready state is reached when the SDK has a valid config.json loaded into
* memory either from cache or from HTTP. If the config couldn't be loaded neither from cache nor
* from HTTP the `onClientReady` event fires when the auto polling's `maxInitWaitTimeInSeconds` is reached.
*/
@Deprecated(
message = "Use the new addOnClientReady(handler: (ClientCacheState) -> Unit) method.",
level = DeprecationLevel.WARNING,
)
public fun addOnClientReady(handler: () -> Unit) {
lock.withLock {
onClientReady.add(handler)
}
}

/**
* This event is sent when the SDK reaches the ready state.
* If the SDK is configured with lazy load or manual polling it's considered ready right after instantiation.
Expand All @@ -30,7 +48,7 @@ public class Hooks {
if (isClientReady.value) {
handler(clientCacheState.value)
} else {
onClientReady.add(handler)
onClientReadyWithState.add(handler)
}
}
}
Expand Down Expand Up @@ -68,9 +86,12 @@ public class Hooks {
lock.withLock {
this.isClientReady.value = true
this.clientCacheState.value = clientCacheState
for (method in onClientReady) {
for (method in onClientReadyWithState) {
method(clientCacheState)
}
for (method in onClientReady) {
method()
}
}
}

Expand Down Expand Up @@ -101,6 +122,7 @@ public class Hooks {
internal fun clear() {
lock.withLock {
onClientReady.clear()
onClientReadyWithState.clear()
onConfigChanged.clear()
onFlagEvaluated.clear()
onError.clear()
Expand Down
16 changes: 9 additions & 7 deletions src/commonTest/kotlin/com/configcat/ConfigCatClientTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -825,17 +825,17 @@ class ConfigCatClientTests {
status = HttpStatusCode.OK,
)
}
var ready = false
var ready: ClientCacheState? = null
ConfigCatClient(TestUtils.randomSdkKey()) {
httpEngine = mockEngine
pollingMode = autoPoll { pollingInterval = 2.seconds }
offline = true
hooks.addOnClientReady { ready = true }
hooks.addOnClientReady { clientCacheState-> ready = clientCacheState }
}

assertEquals(0, mockEngine.requestHistory.size)
TestUtils.awaitUntil {
ready
ready != null
}
}

Expand Down Expand Up @@ -917,14 +917,16 @@ class ConfigCatClientTests {
}
var error = ""
var changed = false
var ready: ClientCacheState? = null
var ready = false
var readyWithClientState: ClientCacheState? = null

val client =
ConfigCatClient(TestUtils.randomSdkKey()) {
httpEngine = mockEngine
pollingMode = manualPoll()
hooks.addOnConfigChanged { changed = true }
hooks.addOnClientReady { clientReadyState -> ready = clientReadyState }
hooks.addOnClientReady { clientReadyState -> readyWithClientState = clientReadyState }
hooks.addOnClientReady { -> ready = true}
hooks.addOnError { err -> error = err }
}

Expand All @@ -942,8 +944,8 @@ class ConfigCatClientTests {
)
assertTrue(changed)


assertEquals(ClientCacheState.NO_FLAG_DATA, ready)
assertTrue(ready)
assertEquals(ClientCacheState.NO_FLAG_DATA, readyWithClientState)
}

@Test
Expand Down

0 comments on commit c1ecdc5

Please sign in to comment.