Skip to content

Commit

Permalink
Add option for sync and async rendering in compose (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio authored Dec 3, 2023
1 parent 8fdff23 commit ae2dd0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Setup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fun Project.setup(
configure(KotlinMultiplatform(JavadocJar.Empty()))
}
publishToMavenCentral(SonatypeHost.S01, automaticRelease = true)
signAllPublications()
// signAllPublications()

coordinates("xyz.wingio.syntakts", moduleName, System.getenv("LIBRARY_VERSION") ?: version.toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,44 @@ public fun <C> Syntakts<C>.render(text: String, context: C, builder: AnnotatedSt
/**
* Remember the rendered text, only updates when either [text] or [context] updates
*
* @see [rememberAsyncRendered]
*
* @param text What to parse and render
* @param context Additional information that nodes may need to render
* @return [AnnotatedString] - The final rendered text to be used on a Text component
*/
@Composable
public fun <C> Syntakts<C>.rememberRendered(text: String, context: C): AnnotatedString {
var parsedText by remember { mutableStateOf(AnnotatedString("")) }
val builder = remember { AnnotatedStyledTextBuilder() }

LaunchedEffect(text, context) {
builder.clear()
ClickHandlerStore.clearForBuilder(builder.id)
}

DisposableEffect(Unit) {
onDispose {
ClickHandlerStore.clearForBuilder(builder.id)
}
}

return remember(text, context) {
render(text, context, builder)
}
}

/**
* Remember the rendered text, only updates when either [text] or [context] updates
* ---
* This will do the text parsing in a separate thread and update the text when the rendering finishes
*
* @param text What to parse and render
* @param context Additional information that nodes may need to render
* @return [AnnotatedString] - The final rendered text to be used on a Text component
*/
@Composable
public fun <C> Syntakts<C>.rememberAsyncRendered(text: String, context: C): AnnotatedString {
var parsedText by remember { mutableStateOf(AnnotatedString(text)) }
val builder = remember { AnnotatedStyledTextBuilder() }

LaunchedEffect(text, context) {
Expand Down

0 comments on commit ae2dd0f

Please sign in to comment.