Skip to content

Commit

Permalink
sync flags
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed May 3, 2024
1 parent 8c055ee commit 87f9554
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PostHogFake : PostHogInterface {
properties: Map<String, Any>?,
userProperties: Map<String, Any>?,
userPropertiesSetOnce: Map<String, Any>?,
groups: Map<String, Any>?,
groupProperties: Map<String, Any>?,
) {
this.event = event
Expand All @@ -43,6 +44,7 @@ public class PostHogFake : PostHogInterface {
key: String,
defaultValue: Boolean,
distinctId: String?,
groups: Map<String, Any>?,
): Boolean {
return false
}
Expand All @@ -51,6 +53,7 @@ public class PostHogFake : PostHogInterface {
key: String,
defaultValue: Any?,
distinctId: String?,
groups: Map<String, Any>?,
): Any? {
return null
}
Expand All @@ -59,6 +62,7 @@ public class PostHogFake : PostHogInterface {
key: String,
defaultValue: Any?,
distinctId: String?,
groups: Map<String, Any>?,
): Any? {
return null
}
Expand Down
7 changes: 4 additions & 3 deletions posthog-samples/posthog-console-sample/src/main/java/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public fun main() {
}
PostHog.setup(config)

PostHog.capture("Hello World!", distinctId = "123")
// PostHog.capture("Hello World!", distinctId = "123")
PostHog.isFeatureEnabled("myFlag", defaultValue = false, distinctId = "123")

// PostHog.flush()
PostHog.flush()
//
// PostHog.close()
PostHog.close()

while (Thread.activeCount() > 1) {
println("threads still active")
Expand Down
32 changes: 16 additions & 16 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ public final class com/posthog/PostHog : com/posthog/PostHogInterface {
public static final field Companion Lcom/posthog/PostHog$Companion;
public synthetic fun <init> (Ljava/util/concurrent/ExecutorService;Ljava/util/concurrent/ExecutorService;Ljava/util/concurrent/ExecutorService;Ljava/util/concurrent/ExecutorService;ZLkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun alias (Ljava/lang/String;Ljava/lang/String;)V
public fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public fun close ()V
public fun debug (Z)V
public fun distinctId ()Ljava/lang/String;
public fun endSession ()V
public fun flush ()V
public fun getConfig ()Lcom/posthog/PostHogConfig;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public fun group (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)V
public fun identify (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
public fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;)Z
public fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;Ljava/util/Map;)Z
public fun isOptOut ()Z
public fun isSessionActive ()Z
public fun optIn ()V
Expand All @@ -29,18 +29,18 @@ public final class com/posthog/PostHog : com/posthog/PostHogInterface {

public final class com/posthog/PostHog$Companion : com/posthog/PostHogInterface {
public fun alias (Ljava/lang/String;Ljava/lang/String;)V
public fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public fun close ()V
public fun debug (Z)V
public fun distinctId ()Ljava/lang/String;
public fun endSession ()V
public fun flush ()V
public fun getConfig ()Lcom/posthog/PostHogConfig;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public fun group (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)V
public fun identify (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
public fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;)Z
public fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;Ljava/util/Map;)Z
public fun isOptOut ()Z
public fun isSessionActive ()Z
public fun optIn ()V
Expand Down Expand Up @@ -175,18 +175,18 @@ public final class com/posthog/PostHogIntegration$DefaultImpls {

public abstract interface class com/posthog/PostHogInterface {
public abstract fun alias (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public abstract fun capture (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
public abstract fun close ()V
public abstract fun debug (Z)V
public abstract fun distinctId ()Ljava/lang/String;
public abstract fun endSession ()V
public abstract fun flush ()V
public abstract fun getConfig ()Lcom/posthog/PostHogConfig;
public abstract fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public abstract fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
public abstract fun getFeatureFlag (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public abstract fun getFeatureFlagPayload (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;
public abstract fun group (Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)V
public abstract fun identify (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;)V
public abstract fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;)Z
public abstract fun isFeatureEnabled (Ljava/lang/String;ZLjava/lang/String;Ljava/util/Map;)Z
public abstract fun isOptOut ()Z
public abstract fun isSessionActive ()Z
public abstract fun optIn ()V
Expand All @@ -202,13 +202,13 @@ public abstract interface class com/posthog/PostHogInterface {

public final class com/posthog/PostHogInterface$DefaultImpls {
public static synthetic fun alias$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/String;ILjava/lang/Object;)V
public static synthetic fun capture$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V
public static synthetic fun capture$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V
public static synthetic fun debug$default (Lcom/posthog/PostHogInterface;ZILjava/lang/Object;)V
public static synthetic fun getFeatureFlag$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getFeatureFlagPayload$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getFeatureFlag$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun getFeatureFlagPayload$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/util/Map;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun group$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;ILjava/lang/Object;)V
public static synthetic fun identify$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ILjava/lang/Object;)V
public static synthetic fun isFeatureEnabled$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;ZLjava/lang/String;ILjava/lang/Object;)Z
public static synthetic fun isFeatureEnabled$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;ZLjava/lang/String;Ljava/util/Map;ILjava/lang/Object;)Z
public static synthetic fun reloadFeatureFlags$default (Lcom/posthog/PostHogInterface;Lcom/posthog/PostHogOnFeatureFlags;ILjava/lang/Object;)V
public static synthetic fun screen$default (Lcom/posthog/PostHogInterface;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;ILjava/lang/Object;)V
}
Expand Down
85 changes: 74 additions & 11 deletions posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.posthog
import com.posthog.internal.PostHogApi
import com.posthog.internal.PostHogApiEndpoint
import com.posthog.internal.PostHogFeatureFlags
import com.posthog.internal.PostHogFeatureFlagsInterface
import com.posthog.internal.PostHogFeatureFlagsSync
import com.posthog.internal.PostHogMemoryPreferences
import com.posthog.internal.PostHogPreferences
import com.posthog.internal.PostHogPreferences.Companion.ALL_INTERNAL_KEYS
Expand Down Expand Up @@ -59,7 +61,7 @@ public class PostHog private constructor(

private var config: PostHogConfig? = null

private var featureFlags: PostHogFeatureFlags? = null
private var featureFlags: PostHogFeatureFlagsInterface? = null
private var queue: PostHogQueue? = null
private var replayQueue: PostHogQueue? = null
private var memoryPreferences = PostHogMemoryPreferences()
Expand Down Expand Up @@ -92,9 +94,14 @@ public class PostHog private constructor(
} else {
replayExecutor.shutdownSafely()
cachedEventsExecutor.shutdownSafely()
featureFlagsExecutor.shutdownSafely()
}

val featureFlags = PostHogFeatureFlags(config, api, featureFlagsExecutor)
val featureFlags: PostHogFeatureFlagsInterface =
if (isClientSDK) {
PostHogFeatureFlags(config, api, featureFlagsExecutor)
} else {
PostHogFeatureFlagsSync(config, api)
}

// no need to lock optOut here since the setup is locked already
val optOut =
Expand Down Expand Up @@ -262,9 +269,11 @@ public class PostHog private constructor(

private fun buildProperties(
distinctId: String,
anonymousId: String?,
properties: Map<String, Any>?,
userProperties: Map<String, Any>?,
userPropertiesSetOnce: Map<String, Any>?,
groups: Map<String, Any>?,
groupProperties: Map<String, Any>?,
appendSharedProps: Boolean = true,
appendGroups: Boolean = true,
Expand All @@ -289,7 +298,7 @@ public class PostHog private constructor(
}

if (config?.sendFeatureFlagEvent == true) {
featureFlags?.getFeatureFlags()?.let {
featureFlags?.getFeatureFlags(distinctId, anonymousId = anonymousId, groups = groups)?.let {
if (it.isNotEmpty()) {
val keys = mutableListOf<String>()
for (entry in it.entries) {
Expand Down Expand Up @@ -378,6 +387,7 @@ public class PostHog private constructor(
properties: Map<String, Any>?,
userProperties: Map<String, Any>?,
userPropertiesSetOnce: Map<String, Any>?,
groups: Map<String, Any>?,
groupProperties: Map<String, Any>?,
) {
try {
Expand Down Expand Up @@ -411,9 +421,11 @@ public class PostHog private constructor(
val mergedProperties =
buildProperties(
newDistinctId,
anonymousId = this.anonymousId,
properties = properties,
userProperties = userProperties,
userPropertiesSetOnce = userPropertiesSetOnce,
groups = groups,
groupProperties = groupProperties,
// only append shared props if not a snapshot event
appendSharedProps = !snapshotEvent,
Expand Down Expand Up @@ -615,7 +627,7 @@ public class PostHog private constructor(
capture(GROUP_IDENTIFY, distinctId = newDistinctId, properties = props)

// only because of testing in isolation, this flag is always enabled
if (reloadFeatureFlags && reloadFeatureFlagsIfNewGroup) {
if (reloadFeatureFlags && reloadFeatureFlagsIfNewGroup && isClientSDK()) {
loadFeatureFlagsRequest(null)
}
}
Expand Down Expand Up @@ -646,23 +658,54 @@ public class PostHog private constructor(
key: String,
defaultValue: Boolean,
distinctId: String?,
groups: Map<String, Any>?,
): Boolean {
if (!isEnabled()) {
return defaultValue
}
return featureFlags?.isFeatureEnabled(key, defaultValue) ?: defaultValue

val newDistinctId = distinctId ?: this.distinctId

if (newDistinctId.isBlank()) {
config?.logger?.log("isFeatureEnabled call not allowed, distinctId is invalid: $newDistinctId.")
return defaultValue
}

return featureFlags?.isFeatureEnabled(
key,
defaultValue,
distinctId = newDistinctId,
anonymousId = this.anonymousId,
groups = groups,
) ?: defaultValue
}

// TODO: only_evaluate_locally
public override fun getFeatureFlag(
key: String,
defaultValue: Any?,
distinctId: String?,
groups: Map<String, Any>?,
): Any? {
if (!isEnabled()) {
return defaultValue
}
val value = featureFlags?.getFeatureFlag(key, defaultValue) ?: defaultValue

val newDistinctId = distinctId ?: this.distinctId

if (newDistinctId.isBlank()) {
config?.logger?.log("getFeatureFlag call not allowed, distinctId is invalid: $newDistinctId.")
return defaultValue
}

val value =
featureFlags?.getFeatureFlag(
key,
defaultValue,
distinctId = newDistinctId,
anonymousId = this.anonymousId,
groups = groups,
) ?: defaultValue

var shouldSendFeatureFlagEvent = true
synchronized(featureFlagsCalledLock) {
Expand Down Expand Up @@ -691,11 +734,26 @@ public class PostHog private constructor(
key: String,
defaultValue: Any?,
distinctId: String?,
groups: Map<String, Any>?,
): Any? {
if (!isEnabled()) {
return defaultValue
}
return featureFlags?.getFeatureFlagPayload(key, defaultValue) ?: defaultValue

val newDistinctId = distinctId ?: this.distinctId

if (newDistinctId.isBlank()) {
config?.logger?.log("getFeatureFlagPayload call not allowed, distinctId is invalid: $newDistinctId.")
return defaultValue
}

return featureFlags?.getFeatureFlagPayload(
key,
defaultValue,
distinctId = newDistinctId,
anonymousId = this.anonymousId,
groups = groups,
) ?: defaultValue
}

public override fun flush() {
Expand Down Expand Up @@ -855,6 +913,7 @@ public class PostHog private constructor(
properties: Map<String, Any>?,
userProperties: Map<String, Any>?,
userPropertiesSetOnce: Map<String, Any>?,
groups: Map<String, Any>?,
groupProperties: Map<String, Any>?,
) {
shared.capture(
Expand All @@ -863,6 +922,7 @@ public class PostHog private constructor(
properties = properties,
userProperties = userProperties,
userPropertiesSetOnce = userPropertiesSetOnce,
groups = groups,
groupProperties = groupProperties,
)
}
Expand All @@ -887,19 +947,22 @@ public class PostHog private constructor(
key: String,
defaultValue: Boolean,
distinctId: String?,
): Boolean = shared.isFeatureEnabled(key, defaultValue = defaultValue, distinctId = distinctId)
groups: Map<String, Any>?,
): Boolean = shared.isFeatureEnabled(key, defaultValue = defaultValue, distinctId = distinctId, groups = groups)

public override fun getFeatureFlag(
key: String,
defaultValue: Any?,
distinctId: String?,
): Any? = shared.getFeatureFlag(key, defaultValue = defaultValue, distinctId = distinctId)
groups: Map<String, Any>?,
): Any? = shared.getFeatureFlag(key, defaultValue = defaultValue, distinctId = distinctId, groups = groups)

public override fun getFeatureFlagPayload(
key: String,
defaultValue: Any?,
distinctId: String?,
): Any? = shared.getFeatureFlagPayload(key, defaultValue = defaultValue, distinctId = distinctId)
groups: Map<String, Any>?,
): Any? = shared.getFeatureFlagPayload(key, defaultValue = defaultValue, distinctId = distinctId, groups = groups)

public override fun flush() {
shared.flush()
Expand Down
4 changes: 4 additions & 0 deletions posthog/src/main/java/com/posthog/PostHogInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface PostHogInterface {
properties: Map<String, Any>? = null,
userProperties: Map<String, Any>? = null,
userPropertiesSetOnce: Map<String, Any>? = null,
groups: Map<String, Any>? = null,
groupProperties: Map<String, Any>? = null,
)

Expand Down Expand Up @@ -61,6 +62,7 @@ public interface PostHogInterface {
key: String,
defaultValue: Boolean = false,
distinctId: String? = null,
groups: Map<String, Any>? = null,
): Boolean

/**
Expand All @@ -73,6 +75,7 @@ public interface PostHogInterface {
key: String,
defaultValue: Any? = null,
distinctId: String? = null,
groups: Map<String, Any>? = null,
): Any?

/**
Expand All @@ -85,6 +88,7 @@ public interface PostHogInterface {
key: String,
defaultValue: Any? = null,
distinctId: String? = null,
groups: Map<String, Any>? = null,
): Any?

/**
Expand Down
Loading

0 comments on commit 87f9554

Please sign in to comment.