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> =
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(

0 commit comments

Comments
 (0)