Browse Source

refactor: prevent hard-coding of byteCount of resource flow

pull/4500/head
Chanjung Kim 9 months ago
parent
commit
a7e86a79f0
  1. 10
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceReader.kt
  2. 7
      gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

10
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ResourceReader.kt

@ -33,10 +33,16 @@ expect suspend fun readResourceBytes(path: String): ByteArray
*
* @return A flow that emits the content of the file as byte sub-arrays.
*
* @throws IllegalArgumentException - when [byteCount] is not positive.
* @throws IllegalArgumentException When [byteCount] is not positive.
*/
@InternalResourceApi
expect fun getResourceAsFlow(path: String, byteCount: Int = 8192): Flow<ByteArray>
expect fun getResourceAsFlow(path: String, byteCount: Int = DEFAULT_RESOURCE_CHUNK_SIZE): Flow<ByteArray>
/**
* The default size of byte array chunks emitted by flows built with [getResourceAsFlow].
*/
@InternalResourceApi
const val DEFAULT_RESOURCE_CHUNK_SIZE: Int = 8192
internal interface ResourceReader {
suspend fun read(path: String): ByteArray

7
gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/resources/ResourcesSpec.kt

@ -153,6 +153,9 @@ internal fun getResFileSpecs(
.build()
)
val defaultResourceChunkSize = MemberName(
"org.jetbrains.compose.resources", "DEFAULT_RESOURCE_CHUNK_SIZE"
)
val getResourceAsFlow = MemberName("org.jetbrains.compose.resources", "getResourceAsFlow")
resObject.addFunction(
FunSpec.builder("getAsFlow")
@ -172,13 +175,13 @@ internal fun getResFileSpecs(
@return A flow that emits the content of the file as byte sub-arrays.
@throws IllegalArgumentException - when [byteCount] is not positive.
@throws IllegalArgumentException When [byteCount] is not positive.
""".trimIndent()
)
.addParameter("path", String::class)
.addParameter(
ParameterSpec.builder("byteCount", Int::class)
.defaultValue("%L", 8192)
.defaultValue("%M", defaultResourceChunkSize)
.build()
)
.returns(

Loading…
Cancel
Save