Skip to content

Commit 63256ad

Browse files
committed
Redesigning the ComposableName API
1 parent 067b7c8 commit 63256ad

File tree

2 files changed

+18
-54
lines changed
  • compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler
  • runtime/src/commonMain/kotlin/land/sungbin/composeinvestigator/runtime

2 files changed

+18
-54
lines changed

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/names.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ public object ComposeNames {
2929
public val currentComposer: FqName = ANDROIDX_COMPOSE_RUNTIME_FQN.child(identifier("currentComposer"))
3030

3131
public val state: FqName = ANDROIDX_COMPOSE_RUNTIME_FQN.child(identifier("State"))
32-
public val stateObject: FqName = ANDROIDX_COMPOSE_RUNTIME_FQN.child(identifier("snapshots.StateObject"))
32+
public val stateObject: FqName = ANDROIDX_COMPOSE_RUNTIME_FQN.child(identifier("snapshots")).child(identifier("StateObject"))
3333
}
3434

3535
public object InvestigatorNames {
3636
public val composableScope: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("ComposableScope"))
3737
public val noInvestigation: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("NoInvestigation"))
3838

3939
public val composeInvestigator: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("ComposeInvestigator"))
40-
public val composeInvestigatorCurrentComposablenName: FqName = composeInvestigator.child(identifier("currentComposableName"))
40+
public val composeInvestigatorSetComposableName: FqName = composeInvestigator.child(identifier("setComposableName"))
41+
public val composeInvestigatorGetComposableName: FqName = composeInvestigator.child(identifier("getComposableName"))
4142
public val composeInvestigatorRegisterStateObject: FqName = composeInvestigator.child(identifier("registerStateObject"))
4243
public val composeInvestigatorComputeInvalidationReason: FqName = composeInvestigator.child(identifier("computeInvalidationReason"))
4344
public val composeInvestigatorLogger: FqName = composeInvestigator.child(identifier("Companion")).child(identifier("Logger"))
@@ -49,7 +50,6 @@ public object InvestigatorNames {
4950
public val invalidationResult: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("InvalidationResult"))
5051
public val invalidationResultSkipped: FqName = invalidationResult.child(identifier("Skipped"))
5152

52-
public val composableName: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("ComposableName"))
5353
public val composableInformation: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("ComposableInformation"))
5454
public val valueArgument: FqName = COMPOSE_INVESTIGATOR_RUNTIME_FQN.child(identifier("ValueArgument"))
5555

runtime/src/commonMain/kotlin/land/sungbin/composeinvestigator/runtime/ComposeInvestigator.kt

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@ package land.sungbin.composeinvestigator.runtime
44

55
import androidx.annotation.VisibleForTesting
66
import androidx.compose.runtime.Immutable
7+
import androidx.compose.runtime.NoLiveLiterals
78
import androidx.compose.runtime.Stable
89
import androidx.compose.runtime.snapshots.StateObject
910

1011
/** Returns the [ComposeInvestigator] assigned to the current file. */
1112
public val currentComposeInvestigator: ComposeInvestigator
1213
@[Stable JvmSynthetic] get() = intrinsicImplementedError()
1314

14-
/**
15-
* Returns the name of the current Composable.
16-
* See [ComposeInvestigator.currentComposableName].
17-
*
18-
* You can get [name] directly from the property delegation.
19-
* See [ComposableName.getValue].
20-
*/
21-
@MustBeDocumented
22-
@Target() // We use an annotation class to prevent LiveLiteral transform from the Compose compiler.
23-
@Retention(AnnotationRetention.SOURCE)
24-
public annotation class ComposableName(public val name: String)
25-
26-
/**
27-
* `val name: String by ComposableName("MyComposable") // result: "MyComposable"`
28-
*
29-
* @see ComposableName
30-
*/
31-
@Suppress("unused")
32-
@Stable public operator fun ComposableName.getValue(thisRef: Any?, property: Any?): String = name
33-
3415
/**
3516
* Classes that hold data from ComposeInvestigator.
3617
*
@@ -51,39 +32,22 @@ public annotation class ComposableName(public val name: String)
5132
@Immutable public class ComposeInvestigator @ComposeInvestigatorCompilerApi public constructor() {
5233
private val stateObjectMap: MutableMap<StateObject, String> = mutableMapOf()
5334
private val argumentMap: MutableMap<Int, List<ValueArgument>> = mutableMapOf()
35+
private val nameMap: MutableMap<Int, String> = mutableMapOf()
5436

55-
/**
56-
* Returns the name of the current Composable, or you can define your own Composable name to use
57-
* for ComposeInvestigator.
58-
*
59-
* ```
60-
* @Composable fun MyComposable() {
61-
* val table = currentComposableInvalidationTracer
62-
*
63-
* val originalName = table.currentComposableName.name
64-
* assertEquals(originalName, "MyComposable") // Ok
65-
*
66-
* table.currentComposableName = ComposableName("AwesomeComposable")
67-
* val newName = table.currentComposableName.name
68-
* assertEquals(newName, "AwesomeComposable") // Ok
69-
* }
70-
* ```
71-
*
72-
* This is used as the value of [name][ComposableInformation.simpleName] in [ComposableInformation].
73-
*
74-
* If you call this getter from a Composable configured as an anonymous function, it will always
75-
* be named 'anonymous.' Therefore, in this case, we recommend you specify your Composable name.
76-
*/
77-
public var currentComposableName: ComposableName
78-
@[Stable ComposableScope JvmSynthetic] get() {
79-
intrinsicImplementedError()
80-
}
81-
@[ComposableScope JvmSynthetic] set(@Suppress("unused") name) {
82-
intrinsicImplementedError()
83-
}
37+
@NoLiveLiterals @ComposableScope
38+
@Stable public fun setComposableName(name: String, compoundKey: Int = intrinsicImplementedError()) {
39+
nameMap[compoundKey] = name
40+
}
41+
42+
@NoLiveLiterals @ComposableScope
43+
@Stable public fun getComposableName(
44+
compoundKey: Int = intrinsicImplementedError(),
45+
default: String = intrinsicImplementedError(),
46+
): String = nameMap[compoundKey] ?: default
8447

8548
@VisibleForTesting public fun reset() {
8649
argumentMap.clear()
50+
nameMap.clear()
8751
}
8852

8953
/**
@@ -105,10 +69,10 @@ public annotation class ComposableName(public val name: String)
10569
return stateObjectMap[value]
10670
}
10771

108-
/** @suppress ComposeInvestigator compiler-only API */
109-
@ComposeInvestigatorCompilerApi
11072
// FIXME Storing the StateObject itself can potentially create a memory leak.
11173
// Consider storing immutable primitives like hashCode instead of the object.
74+
/** @suppress ComposeInvestigator compiler-only API */
75+
@ComposeInvestigatorCompilerApi
11276
public fun <T : Any> registerStateObject(value: T, name: String): T {
11377
if (value !is StateObject) return value
11478
return value.apply { stateObjectMap[this] = name }

0 commit comments

Comments
 (0)