Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,69 @@ open class Analytics protected constructor(
return anonymousId()
}

/**
* Subscribes to UserInfo state changes.
*
* The handler is called immediately with the current UserInfo, then again whenever
* the user's identity, traits, or referrer changes. The subscription remains active
* for the lifetime of the Analytics instance unless explicitly unsubscribed.
*
* - Parameter handler: A closure called on the main queue with updated UserInfo.
*
* - Returns: A subscription ID that can be passed to `unsubscribe(_:)` to stop
* receiving updates. If you don't need to unsubscribe, you can ignore the return value.
*
* - Note: Multiple calls create multiple independent subscriptions.
*
* ## Example
* ```kotlin
* // Subscribe for the lifetime of Analytics
* analytics.subscribeToUserInfo { userInfo ->
* print("User: ${userInfo.userId ?: userInfo.anonymousId}")
* }
*
* // Subscribe with manual cleanup
* val subscriptionId = analytics.subscribeToUserInfo { userInfo ->
* // ... handle update
* }
* // Later, when you're done...
* analytics.unsubscribe(subscriptionId)
* ```
*
*/
suspend fun subscribeToUserInfo(handler: (UserInfo) -> Unit) =
store.subscribe(
this,
UserInfo::class,
initialState = true,
handler = handler,
queue = fileIODispatcher
)

/**
* Unsubscribes from state updates.
*
* Stops receiving updates for the subscription associated with the given ID.
* After calling this, the handler will no longer be invoked for state changes.
*
* - Parameter id: The subscription ID returned from a previous subscribe call.
*
* - Note: Unsubscribing an already-unsubscribed or invalid ID is a no-op.
*
* ## Example
* ```kotlin
* val id = analytics.subscribeToUserInfo { userInfo ->
* print("User changed: ${userInfo.userId ?: "anonymous"}")
* }
*
* // Later, stop listening
* analytics.unsubscribe(id)
* ```
*/
suspend fun unsubscribe(id: Int) {
store.unsubscribe(id)
}

/**
* Retrieve the version of this library in use.
* - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ data class Settings(
var edgeFunction: JsonObject = emptyJsonObject,
var middlewareSettings: JsonObject = emptyJsonObject,
var metrics: JsonObject = emptyJsonObject,
var consentSettings: JsonObject = emptyJsonObject
var consentSettings: JsonObject = emptyJsonObject,
var autoInstrumentation: JsonObject = emptyJsonObject
) {
inline fun <reified T : Any> destinationSettings(
name: String,
Expand Down
Loading