Skip to content

Commit

Permalink
Improve runtime (#44)
Browse files Browse the repository at this point in the history
- `currentComposableInvalidationTracker`
  - Resolves #40 
  - `ComposableInvalidationListener`
  - `currentComposableName`
  - `currentComposableKeyName`
  - `registerListener`, `unregisterListener`
  - `callListeners`
  - `computeInvalidationReason`
- Improved code structure
- Rewrite some tests
  • Loading branch information
jisungbin authored Nov 27, 2023
1 parent efaf34f commit d22158e
Show file tree
Hide file tree
Showing 54 changed files with 1,505 additions and 1,157 deletions.
5 changes: 3 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ root = true
charset = utf-8
indent_size = 2
indent_style = space
max_line_length = 170
insert_final_newline = true
trim_trailing_whitespace = true
ktlint_standard_filename = disabled
ktlint_standard_import-ordering = disabled
ktlint_standard_annotation = disabled
ktlint_standard_wrapping = disabled
ktlint_standard_import-ordering = disabled
ktlint_standard_max-line-length = disabled
ktlint_standard_argument-list-wrapping = disabled
ktlint_standard_parameter-list-wrapping = disabled
ktlint_standard_multiline-if-else = disabled

[*.{kt,kts}]
Expand Down
14 changes: 12 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ plugins {

buildscript {
repositories {
google()
google {
content {
includeGroupByRegex(".*google.*")
includeGroupByRegex(".*android.*")
}
}
mavenCentral()
}

Expand All @@ -29,7 +34,12 @@ buildscript {

subprojects {
repositories {
google()
google {
content {
includeGroupByRegex(".*google.*")
includeGroupByRegex(".*android.*")
}
}
mavenCentral()
}

Expand Down
14 changes: 8 additions & 6 deletions compiler-integration-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@file:Suppress("UnstableApiUsage")


plugins {
id("com.android.library")
kotlin("android")
Expand All @@ -15,7 +14,8 @@ android {
}

sourceSets {
getByName("test").java.srcDir("src/main/kotlin")
getByName("main").java.srcDir("src/main/kotlin")
getByName("test").java.srcDir("src/test/kotlin")
}

compileOptions {
Expand All @@ -40,6 +40,7 @@ android {

composeOptions {
kotlinCompilerExtensionVersion = libs.versions.compose.core.get()
useLiveLiterals = true
}
}

Expand All @@ -55,11 +56,12 @@ kotlin {
}

dependencies {
testImplementation(projects.runtime)
testImplementation(projects.compiler)
implementation(projects.runtime)
implementation(libs.compose.material)
implementation(libs.test.kotest.assertion)

testImplementation(projects.compiler)
testImplementation(libs.compose.compiler)
testImplementation(libs.compose.material)

testImplementation(libs.kotlin.compiler.embedded)
testImplementation(libs.test.kotlin.compilation)
Expand All @@ -68,7 +70,7 @@ dependencies {
}

testImplementation(libs.test.mockk)
testImplementation(libs.test.kotest)
testImplementation(libs.test.kotest.junit5)
testImplementation(libs.test.robolectric) {
because("https://stackoverflow.com/a/64287388/14299073")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.logger

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue

@Composable
fun InvalidationProcessedParameterChangedRoot() {
var count by remember { mutableIntStateOf(0) }
Button(onClick = { count = 1 }) {}
InvalidationProcessedParameterChangedChild(count)
}

@Composable
private fun InvalidationProcessedParameterChangedChild(count: Int) {
Text(text = "$count")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.logger

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentRecomposeScope
import land.sungbin.composeinvestigator.runtime.ComposableName
import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracker

@Composable
fun InvalidationSkippedRoot() {
val recomposeScope = currentRecomposeScope

Button(onClick = { recomposeScope.invalidate() }) {}
InvalidationSkippedChild()
}

@Composable
private fun InvalidationSkippedChild() {
Text(text = "Child")
}

@Composable
fun InvalidationSkippedRoot_CustomName() {
currentComposableInvalidationTracker.currentComposableName = ComposableName("InvalidationSkippedRoot_custom_name")
val recomposeScope = currentRecomposeScope

Button(onClick = { recomposeScope.invalidate() }) {}
InvalidationSkippedChild_CustomName()
}

@Composable
private fun InvalidationSkippedChild_CustomName() {
currentComposableInvalidationTracker.currentComposableName = ComposableName("InvalidationSkippedChild_custom_name")
Text(text = "Child")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.logger

import land.sungbin.composeinvestigator.runtime.AffectedComposable
import land.sungbin.composeinvestigator.runtime.ComposableInvalidationLogger
import land.sungbin.composeinvestigator.runtime.ComposableInvalidationType

val invalidationLog = mutableMapOf<AffectedComposable, MutableList<ComposableInvalidationType>>()

fun findInvalidationLog(composableName: String): List<ComposableInvalidationType> =
invalidationLog.filterKeys { composable -> composable.name == composableName }.values.flatten()

@Suppress("unused")
@ComposableInvalidationLogger
fun invalidationLogger(composable: AffectedComposable, type: ComposableInvalidationType) {
invalidationLog.getOrPut(composable, ::mutableListOf).add(type)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.table.callback

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentRecomposeScope
import land.sungbin.composeinvestigator.runtime.AffectedComposable
import land.sungbin.composeinvestigator.runtime.ComposableInvalidationType
import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracker

val invalidationListensViaManualRegister = mutableMapOf<AffectedComposable, MutableList<ComposableInvalidationType>>()

fun findInvalidationListensViaManualRegister(composableName: String): List<ComposableInvalidationType> =
invalidationListensViaManualRegister.filterKeys { composable -> composable.name == composableName }.values.flatten()

@Composable
fun RegisterListener_InvalidationSkippedRoot() {
val recomposeScope = currentRecomposeScope
val tracker = currentComposableInvalidationTracker

tracker.registerListener(keyName = tracker.currentComposableKeyName) { composable, type ->
invalidationListensViaManualRegister.getOrPut(composable, ::mutableListOf).add(type)
}

Button(onClick = { recomposeScope.invalidate() }) {}
RegisterListener_InvalidationSkippedChild()
}

@Composable
private fun RegisterListener_InvalidationSkippedChild() {
val tracker = currentComposableInvalidationTracker

tracker.registerListener(keyName = tracker.currentComposableKeyName) { composable, type ->
invalidationListensViaManualRegister.getOrPut(composable, ::mutableListOf).add(type)
}

Text(text = "Child")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.table.invalidationtracktablecall

import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeSameInstanceAs
import land.sungbin.composeinvestigator.runtime.ComposableName
import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracker
import land.sungbin.composeinvestigator.runtime.getValue

val table1 = currentComposableInvalidationTracker

fun table1() {
table1 shouldBeSameInstanceAs currentComposableInvalidationTracker
}

fun currentComposableName1() {
val prevComposableName by table1.currentComposableName
prevComposableName shouldBe "currentComposableName1"

table1.currentComposableName = ComposableName("ChangedComposableName1")
table1.currentComposableName.name shouldBe "ChangedComposableName1"
}

fun currentComposableKeyName1() {
table1.currentComposableKeyName shouldBe "fun-currentComposableKeyName1()Unit/pkg-land.sungbin.composeinvestigator.compiler.test.source.table.invalidationtracktablecall/file-TableCallFile1.kt"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Designed and developed by Ji Sungbin 2023.
*
* Licensed under the MIT.
* Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE
*/

package land.sungbin.composeinvestigator.compiler.test.source.table.invalidationtracktablecall

import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeSameInstanceAs
import land.sungbin.composeinvestigator.runtime.ComposableName
import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracker
import land.sungbin.composeinvestigator.runtime.getValue

val table2 = currentComposableInvalidationTracker

fun table2() {
table2 shouldBeSameInstanceAs currentComposableInvalidationTracker
}

fun currentComposableName2() {
val prevComposableName by table2.currentComposableName
prevComposableName shouldBe "currentComposableName2"

table2.currentComposableName = ComposableName("ChangedComposableName2")
table2.currentComposableName.name shouldBe "ChangedComposableName2"
}

fun currentComposableKeyName2() {
table2.currentComposableKeyName shouldBe "fun-currentComposableKeyName2()Unit/pkg-land.sungbin.composeinvestigator.compiler.test.source.table.invalidationtracktablecall/file-TableCallFile2.kt"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package land.sungbin.composeinvestigator.compiler.test

import androidx.compose.compiler.plugins.kotlin.ComposeCommandLineProcessor
import androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
Expand Down Expand Up @@ -61,7 +62,7 @@ fun IrBaseTest.kotlinCompilation(
enableVerboseLogging: Boolean = true,
@Suppress("DEPRECATION") additionalVisitor: ComponentRegistrar? = null,
vararg sourceFiles: SourceFile,
) = KotlinCompilation().apply {
): JvmCompilationResult = KotlinCompilation().apply {
this.workingDir = workingDir
sources = sourceFiles.asList()
jvmTarget = JvmTarget.JVM_17.toString()
Expand All @@ -82,4 +83,4 @@ fun IrBaseTest.kotlinCompilation(
commandLineProcessors = mutableListOf<CommandLineProcessor>(ComposeInvestigatorCommandLineProcessor()).also { processors ->
if (enableComposeCompiler) processors.add(0, ComposeCommandLineProcessor())
}
}
}.compile()
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package land.sungbin.composeinvestigator.compiler.test

import androidx.compose.compiler.plugins.kotlin.ComposeCommandLineProcessor
import androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
Expand All @@ -29,10 +30,10 @@ class IrDumpingTest {
compile(source("SourceForIrDump.kt"))
}

private fun compile(vararg sourceFiles: SourceFile) =
private fun compile(vararg sourceFiles: SourceFile): JvmCompilationResult =
prepareCompilation(*sourceFiles).compile()

private fun prepareCompilation(vararg sourceFiles: SourceFile) =
private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation =
KotlinCompilation().apply {
workingDir = tempDir.root
sources = sourceFiles.asList()
Expand All @@ -45,11 +46,11 @@ class IrDumpingTest {
optionName = ComposeInvestigatorCommandLineProcessor.OPTION_VERBOSE.optionName,
optionValue = "true",
),
PluginOption(
pluginId = ComposeCommandLineProcessor.PLUGIN_ID,
optionName = ComposeCommandLineProcessor.LIVE_LITERALS_V2_ENABLED_OPTION.optionName,
optionValue = "true",
),
// PluginOption(
// pluginId = ComposeCommandLineProcessor.PLUGIN_ID,
// optionName = ComposeCommandLineProcessor.LIVE_LITERALS_V2_ENABLED_OPTION.optionName,
// optionValue = "true",
// ),
)
@Suppress("DEPRECATION")
componentRegistrars = listOf(ComposePluginRegistrar(), ComposeInvestigatorPluginRegistrar())
Expand Down
Loading

0 comments on commit d22158e

Please sign in to comment.