@@ -4,33 +4,14 @@ package land.sungbin.composeinvestigator.runtime
4
4
5
5
import androidx.annotation.VisibleForTesting
6
6
import androidx.compose.runtime.Immutable
7
+ import androidx.compose.runtime.NoLiveLiterals
7
8
import androidx.compose.runtime.Stable
8
9
import androidx.compose.runtime.snapshots.StateObject
9
10
10
11
/* * Returns the [ComposeInvestigator] assigned to the current file. */
11
12
public val currentComposeInvestigator: ComposeInvestigator
12
13
@[Stable JvmSynthetic ] get() = intrinsicImplementedError()
13
14
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
-
34
15
/* *
35
16
* Classes that hold data from ComposeInvestigator.
36
17
*
@@ -51,39 +32,22 @@ public annotation class ComposableName(public val name: String)
51
32
@Immutable public class ComposeInvestigator @ComposeInvestigatorCompilerApi public constructor() {
52
33
private val stateObjectMap: MutableMap <StateObject , String > = mutableMapOf ()
53
34
private val argumentMap: MutableMap <Int , List <ValueArgument >> = mutableMapOf ()
35
+ private val nameMap: MutableMap <Int , String > = mutableMapOf ()
54
36
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
84
47
85
48
@VisibleForTesting public fun reset () {
86
49
argumentMap.clear()
50
+ nameMap.clear()
87
51
}
88
52
89
53
/* *
@@ -105,10 +69,10 @@ public annotation class ComposableName(public val name: String)
105
69
return stateObjectMap[value]
106
70
}
107
71
108
- /* * @suppress ComposeInvestigator compiler-only API */
109
- @ComposeInvestigatorCompilerApi
110
72
// FIXME Storing the StateObject itself can potentially create a memory leak.
111
73
// Consider storing immutable primitives like hashCode instead of the object.
74
+ /* * @suppress ComposeInvestigator compiler-only API */
75
+ @ComposeInvestigatorCompilerApi
112
76
public fun <T : Any > registerStateObject (value : T , name : String ): T {
113
77
if (value !is StateObject ) return value
114
78
return value.apply { stateObjectMap[this ] = name }
0 commit comments