Skip to content

Commit 4e2d6d5

Browse files
committed
Commonize
1 parent 7c2f77d commit 4e2d6d5

File tree

98 files changed

+988
-1554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+988
-1554
lines changed

core/build.gradle.kts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
23

34
plugins {
45
kotlin("multiplatform")
@@ -23,28 +24,28 @@ kotlin {
2324

2425
sourceSets {
2526
commonMain.dependencies {
27+
implementation(project(":external"))
28+
2629
api(libs.coroutines.core)
2730
}
2831

2932
commonTest.dependencies {
30-
implementation(kotlin("test-common"))
31-
implementation(kotlin("test-annotations-common"))
32-
}
33-
34-
jsMain.dependencies {
35-
implementation(project(":external"))
33+
implementation(kotlin("test"))
34+
implementation(libs.coroutines.test)
3635
}
3736

3837
jsTest.dependencies {
3938
implementation(kotlin("test-js"))
4039
}
4140

42-
wasmJsMain.dependencies {
43-
implementation(project(":external"))
44-
}
45-
4641
wasmJsTest.dependencies {
4742
implementation(kotlin("test-wasm-js"))
4843
}
4944
}
5045
}
46+
47+
tasks.withType<KotlinCompilationTask<*>>().configureEach {
48+
compilerOptions {
49+
freeCompilerArgs.add("-Xexpect-actual-classes")
50+
}
51+
}

core/src/wasmJsMain/kotlin/Cursor.kt renamed to core/src/commonMain/kotlin/Cursor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
42
import com.juul.indexeddb.external.IDBCursorWithValue
3+
import com.juul.indexeddb.external.IDBKey
4+
import com.juul.indexeddb.external.JsAny
55
import kotlinx.coroutines.channels.SendChannel
66

77
public open class Cursor internal constructor(
88
internal open val cursor: IDBCursor,
99
private val channel: SendChannel<*>,
1010
) {
11-
public val key: JsAny
11+
public val key: IDBKey
1212
get() = cursor.key
1313

14-
public val primaryKey: JsAny
14+
public val primaryKey: IDBKey
1515
get() = cursor.primaryKey
1616

1717
public fun close() {

core/src/wasmJsMain/kotlin/CursorStart.kt renamed to core/src/commonMain/kotlin/CursorStart.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
42

53
public sealed class CursorStart {

core/src/wasmJsMain/kotlin/Database.kt renamed to core/src/commonMain/kotlin/Database.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBDatabase
42
import com.juul.indexeddb.external.IDBFactory
53
import com.juul.indexeddb.external.IDBTransactionDurability
64
import com.juul.indexeddb.external.IDBTransactionOptions
75
import com.juul.indexeddb.external.IDBVersionChangeEvent
86
import com.juul.indexeddb.external.indexedDB
9-
import kotlinx.browser.window
7+
import com.juul.indexeddb.external.window
8+
import com.juul.indexeddb.selfIndexedDB
109
import kotlinx.coroutines.Dispatchers
1110
import kotlinx.coroutines.withContext
1211

@@ -91,9 +90,7 @@ public class Database internal constructor(
9190

9291
val transaction = Transaction(
9392
ensureDatabase().transaction(
94-
storeNames = ReadonlyArray(
95-
*store.map { it.toJsString() }.toTypedArray(),
96-
),
93+
storeNames = store.toReadonlyArray(),
9794
mode = "readonly",
9895
options = IDBTransactionOptions(durability),
9996
),
@@ -121,9 +118,7 @@ public class Database internal constructor(
121118
val transaction = WriteTransaction(
122119
ensureDatabase()
123120
.transaction(
124-
storeNames = ReadonlyArray(
125-
*store.map { it.toJsString() }.toTypedArray(),
126-
),
121+
storeNames = store.toReadonlyArray(),
127122
mode = "readwrite",
128123
options = IDBTransactionOptions(durability),
129124
),
@@ -145,6 +140,3 @@ public class Database internal constructor(
145140
database = null
146141
}
147142
}
148-
149-
@Suppress("RedundantNullableReturnType")
150-
private val selfIndexedDB: IDBFactory? = js("self.indexedDB || self.webkitIndexedDB")

core/src/wasmJsMain/kotlin/Exceptions.kt renamed to core/src/commonMain/kotlin/Exceptions.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package com.juul.indexeddb
2-
3-
import org.w3c.dom.events.Event
1+
import com.juul.indexeddb.external.Event
42

53
public abstract class EventException(
64
message: String?,

core/src/wasmJsMain/kotlin/Index.kt renamed to core/src/commonMain/kotlin/Index.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
42
import com.juul.indexeddb.external.IDBCursorWithValue
53
import com.juul.indexeddb.external.IDBIndex
4+
import com.juul.indexeddb.external.JsNumber
65
import com.juul.indexeddb.external.ReadonlyArray
76

87
public class Index internal constructor(
@@ -14,10 +13,16 @@ public class Index internal constructor(
1413
override fun requestGetAll(query: Key?): Request<ReadonlyArray<*>> =
1514
Request(index.getAll(query?.toJs()))
1615

17-
override fun requestOpenCursor(query: Key?, direction: Cursor.Direction): Request<IDBCursorWithValue?> =
16+
override fun requestOpenCursor(
17+
query: Key?,
18+
direction: Cursor.Direction,
19+
): Request<IDBCursorWithValue?> =
1820
Request(index.openCursor(query?.toJs(), direction.constant))
1921

20-
override fun requestOpenKeyCursor(query: Key?, direction: Cursor.Direction): Request<IDBCursor?> =
22+
override fun requestOpenKeyCursor(
23+
query: Key?,
24+
direction: Cursor.Direction,
25+
): Request<IDBCursor?> =
2126
Request(index.openKeyCursor(query?.toJs(), direction.constant))
2227

2328
override fun requestCount(query: Key?): Request<JsNumber> =

core/src/commonMain/kotlin/JsArray.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import com.juul.indexeddb.external.JsAny
2+
import com.juul.indexeddb.external.JsArray
3+
import com.juul.indexeddb.external.JsString
4+
import com.juul.indexeddb.external.ReadonlyArray
5+
import com.juul.indexeddb.external.set
6+
import com.juul.indexeddb.external.toJsString
7+
import com.juul.indexeddb.toReadonlyArray
8+
9+
internal fun Array<out String>.toReadonlyArray(): ReadonlyArray<JsString> =
10+
JsArray<JsString>()
11+
.apply {
12+
forEachIndexed { index, s ->
13+
set(index, s.toJsString())
14+
}
15+
}.toReadonlyArray()
16+
17+
internal fun Iterable<String?>.toJsArray(): JsArray<JsString?> =
18+
JsArray<JsString?>().apply {
19+
forEachIndexed { index, s ->
20+
set(index, s?.toJsString())
21+
}
22+
}
23+
24+
internal fun <T : JsAny?> JsArray(vararg values: T): JsArray<T> =
25+
JsArray<T>().apply {
26+
for (i in values.indices) {
27+
set(i, values[i])
28+
}
29+
}

core/src/commonMain/kotlin/Jso.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.juul.indexeddb
2+
3+
import com.juul.indexeddb.external.JsAny
4+
5+
// Copied from:
6+
// https://github.com/JetBrains/kotlin-wrappers/blob/91b2c1568ec6f779af5ec10d89b5e2cbdfe785ff/kotlin-extensions/src/main/kotlin/kotlinx/js/jso.kt
7+
8+
internal expect fun <T : JsAny> jso(): T
9+
internal fun <T : JsAny> jso(block: T.() -> Unit): T = jso<T>().apply(block)

core/src/wasmJsMain/kotlin/Key.kt renamed to core/src/commonMain/kotlin/Key.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBKey
42
import com.juul.indexeddb.external.IDBKeyRange
5-
import org.khronos.webgl.Uint8Array
6-
7-
public external class Date(
8-
value: JsString,
9-
) : JsAny
10-
11-
private fun JsArray<JsAny?>.validateKeyTypes() {
12-
for (i in 0..<length) {
13-
@Suppress("UNCHECKED_CAST")
14-
when (val value = get(i)) {
15-
null, is Uint8Array, is JsString, is Date, is JsNumber, is IDBKeyRange -> continue
16-
is JsArray<*> -> (value as JsArray<JsAny?>).validateKeyTypes()
17-
else -> error("Illegal key: expected string, date, float, binary blob, or array of those types, but got $value.")
18-
}
19-
}
20-
}
3+
import com.juul.indexeddb.external.JsAny
4+
import com.juul.indexeddb.external.JsArray
5+
import com.juul.indexeddb.external.get
6+
import com.juul.indexeddb.external.toJsString
7+
import com.juul.indexeddb.unsafeCast
8+
import com.juul.indexeddb.validateKeyTypes
219

2210
public object AutoIncrement
2311

@@ -46,6 +34,10 @@ public class Key(
4634
internal fun toJs(): IDBKey = (if (values.length == 1) values[0]!! else values).unsafeCast()
4735
}
4836

37+
public expect fun Key(value: String): Key
38+
public expect fun Key(value: Int): Key
39+
public expect fun Key(value: Double): Key
40+
4941
public fun lowerBound(
5042
x: JsAny?,
5143
open: Boolean = false,

core/src/wasmJsMain/kotlin/ObjectStore.kt renamed to core/src/commonMain/kotlin/ObjectStore.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
42
import com.juul.indexeddb.external.IDBCursorWithValue
53
import com.juul.indexeddb.external.IDBObjectStore
4+
import com.juul.indexeddb.external.JsNumber
65
import com.juul.indexeddb.external.ReadonlyArray
76

87
public class ObjectStore internal constructor(

core/src/wasmJsMain/kotlin/OnNextEvent.kt renamed to core/src/commonMain/kotlin/OnNextEvent.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package com.juul.indexeddb
2-
1+
import com.juul.indexeddb.external.Event
2+
import com.juul.indexeddb.external.EventTarget
33
import kotlinx.coroutines.suspendCancellableCoroutine
4-
import org.w3c.dom.events.Event
5-
import org.w3c.dom.events.EventTarget
64
import kotlin.coroutines.resume
75
import kotlin.coroutines.resumeWithException
86

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.juul.indexeddb
2+
3+
import com.juul.indexeddb.external.IDBFactory
4+
import com.juul.indexeddb.external.JsAny
5+
import com.juul.indexeddb.external.JsArray
6+
import com.juul.indexeddb.external.JsByteArray
7+
import com.juul.indexeddb.external.ReadonlyArray
8+
import com.juul.indexeddb.external.toJsByteArray
9+
10+
internal expect val selfIndexedDB: IDBFactory?
11+
12+
internal expect fun <T : JsAny?> JsArray<T>.toReadonlyArray(): ReadonlyArray<T>
13+
14+
internal expect fun <T : JsAny?> JsAny.unsafeCast(): T
15+
16+
internal fun ByteArray.toJsByteArray(): JsByteArray = toTypedArray().toJsByteArray()
17+
18+
internal expect fun JsArray<JsAny?>.validateKeyTypes()

core/src/wasmJsMain/kotlin/Queryable.kt renamed to core/src/commonMain/kotlin/Queryable.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
42
import com.juul.indexeddb.external.IDBCursorWithValue
3+
import com.juul.indexeddb.external.JsNumber
54
import com.juul.indexeddb.external.ReadonlyArray
65

76
public sealed class Queryable {

core/src/commonMain/kotlin/Request.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import com.juul.indexeddb.external.IDBRequest
2+
import com.juul.indexeddb.external.JsAny
3+
4+
internal class Request<T : JsAny?> internal constructor(
5+
internal val request: IDBRequest<T>,
6+
)

core/src/wasmJsMain/kotlin/Transaction.kt renamed to core/src/commonMain/kotlin/Transaction.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package com.juul.indexeddb
2-
1+
import com.juul.indexeddb.external.Event
32
import com.juul.indexeddb.external.IDBCursor
3+
import com.juul.indexeddb.external.IDBIndexOptions
44
import com.juul.indexeddb.external.IDBObjectStoreOptions
55
import com.juul.indexeddb.external.IDBRequest
66
import com.juul.indexeddb.external.IDBTransaction
7+
import com.juul.indexeddb.external.JsAny
78
import com.juul.indexeddb.external.ReadonlyArray
9+
import com.juul.indexeddb.external.toInt
810
import kotlinx.coroutines.channels.SendChannel
911
import kotlinx.coroutines.channels.awaitClose
1012
import kotlinx.coroutines.flow.Flow
1113
import kotlinx.coroutines.flow.callbackFlow
12-
import org.w3c.dom.events.Event
1314

1415
public open class Transaction internal constructor(
1516
internal val transaction: IDBTransaction,
@@ -160,12 +161,12 @@ public open class Transaction internal constructor(
160161
}
161162
}
162163

163-
public suspend fun Queryable.count(query: Key? = null): JsNumber {
164+
public suspend fun Queryable.count(query: Key? = null): Int {
164165
val request = requestCount(query).request
165166
return request.onNextEvent("success", "error") { event ->
166167
when (event.type) {
167168
"error" -> throw ErrorEventException(event)
168-
else -> request.result
169+
else -> request.result.toInt()
169170
}
170171
}
171172
}
@@ -310,15 +311,15 @@ public class VersionChangeTransaction internal constructor(
310311
ensureDatabase()
311312
.createObjectStore(
312313
name = name,
313-
options = IDBObjectStoreOptions(
314-
autoIncrement = false,
315-
keyPath = keyPath.toJs(),
316-
),
314+
options = IDBObjectStoreOptions(keyPath = keyPath.toJs()),
317315
),
318316
)
319317

320318
/** Creates an object-store that uses out-of-line keys with a key-generator. */
321-
public fun Database.createObjectStore(name: String, autoIncrement: AutoIncrement): ObjectStore =
319+
public fun Database.createObjectStore(
320+
name: String,
321+
@Suppress("UNUSED_PARAMETER") autoIncrement: AutoIncrement,
322+
): ObjectStore =
322323
ObjectStore(
323324
ensureDatabase()
324325
.createObjectStore(
@@ -332,7 +333,7 @@ public class VersionChangeTransaction internal constructor(
332333
}
333334

334335
public fun ObjectStore.createIndex(name: String, keyPath: KeyPath, unique: Boolean): Index =
335-
Index(objectStore.createIndex(name, keyPath.toJs(), jso { this.unique = unique }))
336+
Index(objectStore.createIndex(name, keyPath.toJs(), IDBIndexOptions { this.unique = unique }))
336337

337338
public fun ObjectStore.deleteIndex(name: String) {
338339
objectStore.deleteIndex(name)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package com.juul.indexeddb
2-
1+
import com.juul.indexeddb.external.JsAny
2+
import com.juul.indexeddb.jso
3+
import com.juul.indexeddb.unsafeCast
4+
import kotlinx.coroutines.test.runTest
35
import kotlin.test.Test
46
import kotlin.test.assertEquals
57

68
private external interface User : JsAny {
7-
var username: JsString
9+
var username: String
810
}
911

1012
class AutoIncrementKeyObjectStore {
@@ -15,20 +17,19 @@ class AutoIncrementKeyObjectStore {
1517
database.createObjectStore("users", AutoIncrement)
1618
}
1719
}
18-
onCleanup {
19-
database.close()
20-
deleteDatabase("auto-increment-keys")
21-
}
2220

2321
val id = database.writeTransaction("users") {
24-
objectStore("users").add(jso<User> { username = "Username".toJsString() })
22+
objectStore("users").add(jso<User> { username = "Username" })
2523
}
2624

2725
val user = database.transaction("users") {
2826
objectStore("users")
2927
.get(Key(id))
3028
?.unsafeCast<User>()
3129
}
32-
assertEquals("Username", user?.username?.toString())
30+
assertEquals("Username", user?.username)
31+
32+
database.close()
33+
deleteDatabase("auto-increment-keys")
3334
}
3435
}

0 commit comments

Comments
 (0)