Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
name: Release ${{ matrix.module }} to Sonatype
strategy:
matrix:
module: [kstore, kstore-file, kstore-storage]
module: [kstore, kstore-file, kstore-storage, kstore-test]
if: ${{ github.event_name != 'pull_request' }}
runs-on: macos-latest
needs:
Expand Down
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ kstore-file = { module = "io.github.xxfast:kstore-file", version.ref = "kstore"

# for jsBrowser and wasmJsBrowser
kstore-storage = { module = "io.github.xxfast:kstore-storage", version.ref = "kstore" }

# for testing
kstore-test = { module = "io.github.xxfast:kstore-test", version.ref = "kstore" }
```

Depending on your target platforms, you will need to add platform configurations [here](https://xxfast.github.io/KStore/installation.html)
Expand Down
1 change: 1 addition & 0 deletions docs/topics/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kstore = "x.x.x"
kstore = { module = "io.github.xxfast:kstore", version.ref = "kstore" }
kstore-file = { module = "io.github.xxfast:kstore-file", version.ref = "kstore" }
kstore-storage = { module = "io.github.xxfast:kstore-storage", version.ref = "kstore" }
kstore-test = { module = "io.github.xxfast:kstore-test", version.ref = "kstore" }
```

### Targeting Android, iOS and/or Desktop
Expand Down
1 change: 1 addition & 0 deletions kstore-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
57 changes: 57 additions & 0 deletions kstore-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask

plugins {
kotlin("multiplatform")
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.15.1"
}

kotlin {
explicitApi()

js(IR) {
browser()
}

wasmJs {
binaries.executable()
browser()
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":kstore"))
implementation(libs.kotlinx.serialization.json)
}
}

val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(libs.kotlinx.coroutines.test)
}
}
}
}

//
// TODO: https://youtrack.jetbrains.com/issue/KT-63014/Running-tests-with-wasmJs-in-1.9.20-requires-Chrome-Canary#focus=Comments-27-8321383.0-0
// The following is required to support the wasmJs target.
//
// Node.js Canary is set to 21.0.0-v8-canary20231019bd785be450
// as that is the last version to ship Windows binaries too.
//

rootProject.extensions.configure<NodeJsRootExtension> {
nodeVersion = "21.0.0-v8-canary20231019bd785be450"
nodeDownloadBaseUrl = "https://nodejs.org/download/v8-canary"
}

rootProject.tasks.withType<KotlinNpmInstallTask>().configureEach {
val flag = "--ignore-engines"

if (!args.contains(flag)) {
args.add(flag)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.xxfast.kstore.test

import io.github.xxfast.kstore.Codec
import io.github.xxfast.kstore.DefaultJson
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.StringFormat
import kotlinx.serialization.serializer


/**
* A codec that stores data in memory.
*/
public inline fun <reified T : @Serializable Any> InMemoryCodec(
format: StringFormat = DefaultJson,
): InMemoryCodec<T> = InMemoryCodec(format, format.serializersModule.serializer())

/**
* A codec that stores data in memory.
*/
public class InMemoryCodec<T : @Serializable Any>(
private val format: StringFormat,
private val serializer: KSerializer<T>
) : Codec<T> {

private var storedData: String? = null

override suspend fun encode(value: T?) {
storedData = value?.let { format.encodeToString(serializer, it) }
}

override suspend fun decode(): T? = storedData?.let {
format.decodeFromString(serializer, it)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.xxfast.kstore.test

import io.github.xxfast.kstore.DefaultJson
import io.github.xxfast.kstore.KStore
import kotlinx.serialization.Serializable
import kotlinx.serialization.StringFormat

/**
* Creates a store with [InMemoryCodec]
*
* @param default returns this value if the record is not found. defaults to null
* @param enableCache maintain a cache. If set to false, it always reads from storage
* @param format Serializer to use. defaults to [DefaultJson]
*
* @return store that contains a value of type [T]
*/
public inline fun <reified T : @Serializable Any> testStoreOf(
default: T? = null,
enableCache: Boolean = true,
format: StringFormat = DefaultJson,
): KStore<T> = KStore(
default = default,
enableCache = enableCache,
codec = InMemoryCodec(format)
)
1 change: 1 addition & 0 deletions kstore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,5 @@ rootProject.tasks.withType<KotlinNpmInstallTask>().configureEach {
dependencies {
kover(project(":kstore-file"))
kover(project(":kstore-storage"))
kover(project(":kstore-test"))
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ rootProject.name = "KStore"
include(":kstore")
include(":kstore-file")
include(":kstore-storage")
include(":kstore-test")