From 61d6c1037e7eab693eca30388a697081537c8453 Mon Sep 17 00:00:00 2001 From: Nek-12 Date: Wed, 17 Apr 2024 01:04:07 +0300 Subject: [PATCH] save data to localstorage on wasm --- .../features/savedstate/SavedStateContainer.kt | 15 ++++++--------- .../flowmvi/sample/platform/FileManager.kt | 2 +- .../flowmvi/sample/platform/BrowserFileManager.kt | 2 +- .../flowmvi/savedstate/dsl/Compress.wasmJs.kt | 5 +++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/features/savedstate/SavedStateContainer.kt b/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/features/savedstate/SavedStateContainer.kt index f9473613..79993451 100644 --- a/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/features/savedstate/SavedStateContainer.kt +++ b/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/features/savedstate/SavedStateContainer.kt @@ -27,15 +27,12 @@ internal class SavedStateContainer( // can also be injected, defined here for illustration purposes // see "StoreConfiguration" for injection setup - // Saved state is not supported on wasm yet - fileManager.cacheDir("state")?.let { - serializeState( - dir = it, - json = json, - serializer = DisplayingInput.serializer(), - recover = NullRecover, - ) - } + serializeState( + dir = fileManager.cacheDir(name!!), + json = json, + serializer = DisplayingInput.serializer(), + recover = NullRecover, + ) reduce { intent -> when (intent) { diff --git a/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/platform/FileManager.kt b/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/platform/FileManager.kt index b441f842..49b5651b 100644 --- a/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/platform/FileManager.kt +++ b/sample/src/commonMain/kotlin/pro/respawn/flowmvi/sample/platform/FileManager.kt @@ -1,5 +1,5 @@ package pro.respawn.flowmvi.sample.platform interface FileManager { - fun cacheDir(relative: String): String? + fun cacheDir(relative: String): String } diff --git a/sample/src/wasmJsMain/kotlin/pro/respawn/flowmvi/sample/platform/BrowserFileManager.kt b/sample/src/wasmJsMain/kotlin/pro/respawn/flowmvi/sample/platform/BrowserFileManager.kt index c970f57f..fde9ac76 100644 --- a/sample/src/wasmJsMain/kotlin/pro/respawn/flowmvi/sample/platform/BrowserFileManager.kt +++ b/sample/src/wasmJsMain/kotlin/pro/respawn/flowmvi/sample/platform/BrowserFileManager.kt @@ -2,5 +2,5 @@ package pro.respawn.flowmvi.sample.platform internal class BrowserFileManager : FileManager { - override fun cacheDir(relative: String): String? = null + override fun cacheDir(relative: String): String = "cache/$relative" } diff --git a/savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/dsl/Compress.wasmJs.kt b/savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/dsl/Compress.wasmJs.kt index 1c08d21e..cf31648b 100644 --- a/savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/dsl/Compress.wasmJs.kt +++ b/savedstate/src/wasmJsMain/kotlin/pro/respawn/flowmvi/savedstate/dsl/Compress.wasmJs.kt @@ -1,7 +1,8 @@ package pro.respawn.flowmvi.savedstate.dsl +import kotlinx.browser.localStorage import kotlinx.io.files.Path -internal actual suspend fun writeCompressed(data: String, to: Path) = write(data, to) +internal actual suspend fun writeCompressed(data: String, to: Path) = localStorage.setItem(to.name, data) -internal actual suspend fun readCompressed(from: Path): String? = read(from) +internal actual suspend fun readCompressed(from: Path): String? = localStorage.getItem(from.name)