Skip to content

Commit

Permalink
fix build, remove expect object usages
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek-12 committed May 1, 2024
1 parent e077bd9 commit 6874417
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ class StoreExceptionsTest : FreeSpec({
}
"given store that throws on subscribe" - {
val store = testStore {
recover {
println("recover from $it")
null
}
install {
recover {
println("recover from $it")
null
}
onSubscribe { _ ->
throw e
}
onSubscribe { _ -> throw e }
}
}
"then exceptions in subscriber scope do not cancel the pipeline" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
package pro.respawn.flowmvi.savedstate.platform

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.InputStream
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

@PublishedApi
internal actual object FileAccess {

private fun File.outputStreamOrEmpty() = run {
parentFile?.mkdirs()
createNewFile()
outputStream()
}
private fun File.outputStreamOrEmpty() = run {
parentFile?.mkdirs()
createNewFile()
outputStream()
}

private fun InputStream.readOrNull() = bufferedReader().use { it.readText() }.takeIf { it.isNotBlank() }
private fun InputStream.readOrNull() = bufferedReader().use { it.readText() }.takeIf { it.isNotBlank() }

actual suspend fun writeCompressed(data: String?, path: String) = withContext(Dispatchers.IO) {
val file = File(path)
if (data == null) {
file.delete()
return@withContext
}
file.outputStreamOrEmpty().let(::GZIPOutputStream).bufferedWriter().use { it.write(data) }
internal actual suspend fun writeCompressed(data: String?, path: String) {
val file = File(path)
if (data == null) {
file.delete()
return
}
return file.outputStreamOrEmpty().let(::GZIPOutputStream).bufferedWriter().use { it.write(data) }
}

actual suspend fun readCompressed(path: String): String? = withContext(Dispatchers.IO) {
val file = File(path)
if (!file.exists()) return@withContext null
file.inputStream().let(::GZIPInputStream).readOrNull()
}
internal actual suspend fun readCompressed(path: String): String? {
val file = File(path)
if (!file.exists()) return null
return file.inputStream().let(::GZIPInputStream).readOrNull()
}

actual suspend fun write(data: String?, path: String) = withContext(Dispatchers.IO) {
val file = File(path)
if (data == null) {
file.delete()
return@withContext
}
file.outputStreamOrEmpty().bufferedWriter().use { it.write(data) }
internal actual suspend fun write(data: String?, path: String) {
val file = File(path)
if (data == null) {
file.delete()
return
}
return file.outputStreamOrEmpty().bufferedWriter().use { it.write(data) }
}

actual suspend fun read(path: String): String? = withContext(Dispatchers.IO) {
val file = File(path)
if (!file.exists()) return@withContext null
file.inputStream().readOrNull()
}
internal actual suspend fun read(path: String): String? {
val file = File(path)
if (!file.exists()) return null
return file.inputStream().readOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import pro.respawn.flowmvi.savedstate.api.Saver
import pro.respawn.flowmvi.savedstate.api.ThrowRecover
import pro.respawn.flowmvi.savedstate.platform.FileAccess
import pro.respawn.flowmvi.savedstate.platform.read
import pro.respawn.flowmvi.savedstate.platform.readCompressed
import pro.respawn.flowmvi.savedstate.platform.write
import pro.respawn.flowmvi.savedstate.platform.writeCompressed

/**
* A [Saver] implementation that saves the given state to a file in a specified [path]
Expand Down Expand Up @@ -56,8 +59,8 @@ public fun FileSaver(
): Saver<String> = DefaultFileSaver(
path = path,
recover = recover,
write = FileAccess::write,
read = FileAccess::read,
write = ::write,
read = ::read,
)

/**
Expand All @@ -79,6 +82,6 @@ public fun CompressedFileSaver(
): Saver<String> = DefaultFileSaver(
path = path,
recover = recover,
write = FileAccess::writeCompressed,
read = FileAccess::readCompressed,
write = ::writeCompressed,
read = ::readCompressed,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package pro.respawn.flowmvi.savedstate.platform

@PublishedApi
internal expect object FileAccess {

suspend fun writeCompressed(data: String?, path: String)
suspend fun readCompressed(path: String): String?
suspend fun write(data: String?, path: String)
suspend fun read(path: String): String?
}
internal expect suspend fun writeCompressed(data: String?, path: String)

internal expect suspend fun readCompressed(path: String): String?

internal expect suspend fun write(data: String?, path: String)

internal expect suspend fun read(path: String): String?
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package pro.respawn.flowmvi.savedstate.platform

import kotlinx.browser.localStorage

@PublishedApi
internal actual object FileAccess {
internal actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)

actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)
internal actual suspend fun readCompressed(path: String): String? = read(path)

actual suspend fun readCompressed(path: String): String? = read(path)
internal actual suspend fun write(
data: String?, path: String

Check notice on line 10 in savedstate/src/jsMain/kotlin/pro/respawn/flowmvi/savedstate/platform/FileAccess.js.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

savedstate/src/jsMain/kotlin/pro/respawn/flowmvi/savedstate/platform/FileAccess.js.kt#L10

Parameter should start on a newline
) = if (data != null) localStorage.setItem(path, data) else localStorage.removeItem(path)

actual suspend fun write(data: String?, path: String) =
if (data != null) localStorage.setItem(path, data) else localStorage.removeItem(path)

actual suspend fun read(path: String): String? = localStorage.getItem(path)
}
internal actual suspend fun read(path: String): String? = localStorage.getItem(path)
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
package pro.respawn.flowmvi.savedstate.platform

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.InputStream
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

@PublishedApi
internal actual object FileAccess {

private fun File.outputStreamOrEmpty() = run {
parentFile?.mkdirs()
createNewFile()
outputStream()
}
private fun File.outputStreamOrEmpty() = run {
parentFile?.mkdirs()
createNewFile()
outputStream()
}

private fun InputStream.readOrNull() = bufferedReader().use { it.readText() }.takeIf { it.isNotBlank() }
private fun InputStream.readOrNull() = bufferedReader().use { it.readText() }.takeIf { it.isNotBlank() }

actual suspend fun writeCompressed(data: String?, path: String) = withContext(Dispatchers.IO) {
val file = File(path)
if (data == null) {
file.delete()
return@withContext
}
file.outputStreamOrEmpty().let(::GZIPOutputStream).bufferedWriter().use { it.write(data) }
internal actual suspend fun writeCompressed(data: String?, path: String) {
val file = File(path)
if (data == null) {
file.delete()
return
}
return file.outputStreamOrEmpty().let(::GZIPOutputStream).bufferedWriter().use { it.write(data) }
}

actual suspend fun readCompressed(path: String): String? = withContext(Dispatchers.IO) {
val file = File(path)
if (!file.exists()) return@withContext null
file.inputStream().let(::GZIPInputStream).readOrNull()
}
internal actual suspend fun readCompressed(path: String): String? {
val file = File(path)
if (!file.exists()) return null
return file.inputStream().let(::GZIPInputStream).readOrNull()
}

actual suspend fun write(data: String?, path: String) = withContext(Dispatchers.IO) {
val file = File(path)
if (data == null) {
file.delete()
return@withContext
}
file.outputStreamOrEmpty().bufferedWriter().use { it.write(data) }
internal actual suspend fun write(data: String?, path: String) {
val file = File(path)
if (data == null) {
file.delete()
return
}
return file.outputStreamOrEmpty().bufferedWriter().use { it.write(data) }
}

actual suspend fun read(path: String): String? = withContext(Dispatchers.IO) {
val file = File(path)
if (!file.exists()) return@withContext null
file.inputStream().readOrNull()
}
internal actual suspend fun read(path: String): String? {
val file = File(path)
if (!file.exists()) return null
return file.inputStream().readOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@ import kotlinx.io.files.SystemFileSystem
import kotlinx.io.readString
import kotlinx.io.writeString

@PublishedApi
internal actual object FileAccess {
internal actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)

actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)
internal actual suspend fun readCompressed(path: String): String? = read(path)

actual suspend fun readCompressed(path: String): String? = read(path)

@OptIn(ExperimentalStdlibApi::class)
actual suspend fun write(data: String?, path: String) {
SystemFileSystem.run {
val file = Path(path)
if (data == null) {
delete(file, false)
return@run
}
sink(file).buffered().use { it.writeString(data) }
@OptIn(ExperimentalStdlibApi::class)
internal actual suspend fun write(data: String?, path: String) {
SystemFileSystem.run {
val file = Path(path)
if (data == null) {
delete(file, false)
return@run
}
sink(file).buffered().use { it.writeString(data) }
}

@OptIn(ExperimentalStdlibApi::class)
actual suspend fun read(path: String): String? = SystemFileSystem
.source(Path(path))
.buffered()
.use { it.readString() }
.takeIf { it.isNotBlank() }
}

@OptIn(ExperimentalStdlibApi::class)
internal actual suspend fun read(path: String): String? = SystemFileSystem
.source(Path(path))
.buffered()
.use { it.readString() }
.takeIf { it.isNotBlank() }
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package pro.respawn.flowmvi.savedstate.dsl
import kotlinx.io.files.Path
import pro.respawn.flowmvi.savedstate.api.Saver
import pro.respawn.flowmvi.savedstate.api.ThrowRecover
import pro.respawn.flowmvi.savedstate.platform.FileAccess
import pro.respawn.flowmvi.savedstate.platform.read
import pro.respawn.flowmvi.savedstate.platform.readCompressed
import pro.respawn.flowmvi.savedstate.platform.write
import pro.respawn.flowmvi.savedstate.platform.writeCompressed

/**
* A [Saver] implementation that saves the given state to a file in a specified [dir] and [fileName].
Expand Down Expand Up @@ -46,8 +49,8 @@ public fun FileSaver(
dir = dir,
fileName = fileName,
recover = recover,
write = { data, path -> FileAccess.write(data, path.name) },
read = { path -> FileAccess.read(path.name) },
write = { data, path -> write(data, path.name) },
read = { path -> read(path.name) },
)

/**
Expand All @@ -71,6 +74,6 @@ public fun CompressedFileSaver(
dir = dir,
fileName = fileName,
recover = recover,
write = { data, path -> FileAccess.writeCompressed(data, path.name) },
read = { path -> FileAccess.readCompressed(path.name) },
write = { data, path -> writeCompressed(data, path.name) },
read = { path -> readCompressed(path.name) },
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package pro.respawn.flowmvi.savedstate.platform

import kotlinx.browser.localStorage

@PublishedApi
internal actual object FileAccess {
internal actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)

actual suspend fun writeCompressed(data: String?, path: String) = write(data, path)
internal actual suspend fun readCompressed(path: String): String? = read(path)

actual suspend fun readCompressed(path: String): String? = read(path)
internal actual suspend fun write(
data: String?, path: String

Check notice on line 10 in savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/platform/FileAccess.wasmJs.kt

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/platform/FileAccess.wasmJs.kt#L10

Parameter should start on a newline
) = if (data != null) localStorage.setItem(path, data) else localStorage.removeItem(path)

actual suspend fun write(data: String?, path: String) =
if (data != null) localStorage.setItem(path, data) else localStorage.removeItem(path)

actual suspend fun read(path: String): String? = localStorage.getItem(path)
}
internal actual suspend fun read(path: String): String? = localStorage.getItem(path)
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ public class PluginTestScope<S : MVIState, I : MVIIntent, A : MVIAction> private
override fun equals(other: Any?): Boolean = ctx.plugin == other
override fun hashCode(): Int = ctx.plugin.hashCode()
override fun toString(): String = "PluginTestScope(plugin=${ctx.plugin.name})"
public val state: S by ctx::state
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:OptIn(DelicateStoreApi::class)

package pro.respawn.flowmvi.test.plugin

import kotlinx.atomicfu.atomic
Expand All @@ -17,7 +19,7 @@ internal class TestPipelineContext<S : MVIState, I : MVIIntent, A : MVIAction> @

override val coroutineContext by config::coroutineContext

var state: S by atomic(config.initial)
override var state: S by atomic(config.initial)
private set

@DelicateStoreApi
Expand Down

0 comments on commit 6874417

Please sign in to comment.