Skip to content

Commit e4c74e9

Browse files
committed
Commonize
1 parent 7c2f77d commit e4c74e9

File tree

102 files changed

+1318
-1763
lines changed

Some content is hidden

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

102 files changed

+1318
-1763
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ a [`WriteTransaction`] sharing that store.
7777

7878
```kotlin
7979
val bill = database.transaction("customers") {
80-
objectStore("customers").get(Key("444-44-4444")) as Customer
80+
objectStore("customers").get(IDBKey("444-44-4444")) as Customer
8181
}
8282
assertEquals("Bill", bill.name)
8383
```

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+
api(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: 8 additions & 8 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() {
@@ -26,12 +26,12 @@ public open class Cursor internal constructor(
2626
cursor.advance(count)
2727
}
2828

29-
public fun `continue`(key: Key) {
30-
cursor.`continue`(key.toJs())
29+
public fun `continue`(key: IDBKey) {
30+
cursor.`continue`(key)
3131
}
3232

33-
public fun continuePrimaryKey(key: Key, primaryKey: Key) {
34-
cursor.continuePrimaryKey(key.toJs(), primaryKey.toJs())
33+
public fun continuePrimaryKey(key: IDBKey, primaryKey: IDBKey) {
34+
cursor.continuePrimaryKey(key, primaryKey)
3535
}
3636

3737
public enum class Direction(
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.juul.indexeddb
2-
31
import com.juul.indexeddb.external.IDBCursor
2+
import com.juul.indexeddb.external.IDBKey
43

54
public sealed class CursorStart {
65

@@ -15,19 +14,19 @@ public sealed class CursorStart {
1514
}
1615

1716
public data class Continue(
18-
val key: Key,
17+
val key: IDBKey,
1918
) : CursorStart() {
2019
override fun apply(cursor: IDBCursor) {
21-
cursor.`continue`(key.toJs())
20+
cursor.`continue`(key)
2221
}
2322
}
2423

2524
public data class ContinuePrimaryKey(
26-
val key: Key,
27-
val primaryKey: Key,
25+
val key: IDBKey,
26+
val primaryKey: IDBKey,
2827
) : CursorStart() {
2928
override fun apply(cursor: IDBCursor) {
30-
cursor.continuePrimaryKey(key.toJs(), primaryKey.toJs())
29+
cursor.continuePrimaryKey(key, primaryKey)
3130
}
3231
}
3332
}

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

Lines changed: 39 additions & 32 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

@@ -81,23 +80,28 @@ public class Database internal constructor(
8180
* - `suspend` functions composed entirely of other legal functions
8281
*/
8382
public suspend fun <T> transaction(
84-
vararg store: String,
83+
store: String,
84+
vararg moreStores: String,
8585
durability: IDBTransactionDurability = IDBTransactionDurability.Default,
8686
action: suspend Transaction.() -> T,
8787
): T = withContext(Dispatchers.Unconfined) {
88-
check(store.isNotEmpty()) {
89-
"At least one store needs to be passed to transaction"
90-
}
88+
val transaction = when {
89+
moreStores.isEmpty() -> Transaction(
90+
ensureDatabase().transaction(
91+
storeName = store,
92+
mode = "readonly",
93+
options = IDBTransactionOptions(durability),
94+
),
95+
)
9196

92-
val transaction = Transaction(
93-
ensureDatabase().transaction(
94-
storeNames = ReadonlyArray(
95-
*store.map { it.toJsString() }.toTypedArray(),
97+
else -> Transaction(
98+
ensureDatabase().transaction(
99+
storeNames = toReadonlyArray(store, *moreStores),
100+
mode = "readonly",
101+
options = IDBTransactionOptions(durability),
96102
),
97-
mode = "readonly",
98-
options = IDBTransactionOptions(durability),
99-
),
100-
)
103+
)
104+
}
101105
val result = transaction.action()
102106
transaction.awaitCompletion()
103107
result
@@ -110,28 +114,34 @@ public class Database internal constructor(
110114
* - `suspend` functions composed entirely of other legal functions
111115
*/
112116
public suspend fun <T> writeTransaction(
113-
vararg store: String,
117+
store: String,
118+
vararg moreStores: String,
114119
durability: IDBTransactionDurability = IDBTransactionDurability.Default,
115120
action: suspend WriteTransaction.() -> T,
116121
): T = withContext(Dispatchers.Unconfined) {
117-
check(store.isNotEmpty()) {
118-
"At least one store needs to be passed to writeTransaction"
119-
}
122+
val transaction = when {
123+
moreStores.isEmpty() -> WriteTransaction(
124+
ensureDatabase()
125+
.transaction(
126+
storeName = store,
127+
mode = "readwrite",
128+
options = IDBTransactionOptions(durability),
129+
),
130+
)
120131

121-
val transaction = WriteTransaction(
122-
ensureDatabase()
123-
.transaction(
124-
storeNames = ReadonlyArray(
125-
*store.map { it.toJsString() }.toTypedArray(),
132+
else -> WriteTransaction(
133+
ensureDatabase()
134+
.transaction(
135+
storeNames = toReadonlyArray(store, *moreStores),
136+
mode = "readwrite",
137+
options = IDBTransactionOptions(durability),
126138
),
127-
mode = "readwrite",
128-
options = IDBTransactionOptions(durability),
129-
),
130-
)
139+
)
140+
}
131141

132142
with(transaction) {
133143
// Force overlapping transactions to not call `action` until prior transactions complete.
134-
objectStore(store.first())
144+
objectStore(store)
135145
.openKeyCursor(autoContinue = false)
136146
.collect { it.close() }
137147
}
@@ -145,6 +155,3 @@ public class Database internal constructor(
145155
database = null
146156
}
147157
}
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/commonMain/kotlin/Index.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import com.juul.indexeddb.external.IDBCursor
2+
import com.juul.indexeddb.external.IDBCursorWithValue
3+
import com.juul.indexeddb.external.IDBIndex
4+
import com.juul.indexeddb.external.IDBKey
5+
import com.juul.indexeddb.external.JsNumber
6+
import com.juul.indexeddb.external.ReadonlyArray
7+
8+
public class Index internal constructor(
9+
internal val index: IDBIndex,
10+
) : Queryable() {
11+
override fun requestGet(key: IDBKey): Request<*> =
12+
Request(index.get(key))
13+
14+
override fun requestGetAll(query: IDBKey?): Request<ReadonlyArray<*>> =
15+
Request(index.getAll(query))
16+
17+
override fun requestOpenCursor(
18+
query: IDBKey?,
19+
direction: Cursor.Direction,
20+
): Request<IDBCursorWithValue?> =
21+
Request(index.openCursor(query, direction.constant))
22+
23+
override fun requestOpenKeyCursor(
24+
query: IDBKey?,
25+
direction: Cursor.Direction,
26+
): Request<IDBCursor?> =
27+
Request(index.openKeyCursor(query, direction.constant))
28+
29+
override fun requestCount(query: IDBKey?): Request<JsNumber> =
30+
Request(index.count(query))
31+
}

core/src/commonMain/kotlin/JsArray.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 toReadonlyArray(value: String, vararg moreValues: String): ReadonlyArray<JsString> =
10+
JsArray<JsString>()
11+
.apply {
12+
set(0, value.toJsString())
13+
moreValues.forEachIndexed { index, s ->
14+
set(index + 1, s.toJsString())
15+
}
16+
}.toReadonlyArray()
17+
18+
internal fun Iterable<String?>.toJsArray(): JsArray<JsString?> =
19+
JsArray<JsString?>().apply {
20+
forEachIndexed { index, s ->
21+
set(index, s?.toJsString())
22+
}
23+
}
24+
25+
internal fun <T : JsAny?> JsArray(vararg values: T): JsArray<T> =
26+
JsArray<T>().apply {
27+
for (i in values.indices) {
28+
set(i, values[i])
29+
}
30+
}

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/commonMain/kotlin/Key.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import com.juul.indexeddb.external.IDBKey
2+
import com.juul.indexeddb.external.IDBKeyRange
3+
import com.juul.indexeddb.external.JsAny
4+
import com.juul.indexeddb.external.toJsString
5+
6+
public object AutoIncrement
7+
8+
public class KeyPath private constructor(
9+
private val paths: List<String?>,
10+
) {
11+
init {
12+
require(paths.isNotEmpty()) { "A key path must have at least one member." }
13+
}
14+
15+
public constructor(path: String?, vararg morePaths: String?) : this(listOf(path, *morePaths))
16+
17+
internal fun toJs(): JsAny? = if (paths.size == 1) paths[0]?.toJsString() else paths.toJsArray()
18+
}
19+
20+
public fun lowerBound(
21+
x: JsAny?,
22+
open: Boolean = false,
23+
): IDBKey = IDBKey(IDBKeyRange.lowerBound(x, open))
24+
25+
public fun upperBound(
26+
y: JsAny?,
27+
open: Boolean = false,
28+
): IDBKey = IDBKey(IDBKeyRange.upperBound(y, open))
29+
30+
public fun bound(
31+
x: JsAny?,
32+
y: JsAny?,
33+
lowerOpen: Boolean = false,
34+
upperOpen: Boolean = false,
35+
): IDBKey = IDBKey(IDBKeyRange.bound(x, y, lowerOpen, upperOpen))
36+
37+
public fun only(
38+
z: JsAny?,
39+
): IDBKey = IDBKey(IDBKeyRange.only(z))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import com.juul.indexeddb.external.IDBCursor
2+
import com.juul.indexeddb.external.IDBCursorWithValue
3+
import com.juul.indexeddb.external.IDBKey
4+
import com.juul.indexeddb.external.IDBObjectStore
5+
import com.juul.indexeddb.external.JsNumber
6+
import com.juul.indexeddb.external.ReadonlyArray
7+
8+
public class ObjectStore internal constructor(
9+
internal val objectStore: IDBObjectStore,
10+
) : Queryable() {
11+
override fun requestGet(key: IDBKey): Request<*> =
12+
Request(objectStore.get(key))
13+
14+
override fun requestGetAll(query: IDBKey?): Request<ReadonlyArray<*>> =
15+
Request(objectStore.getAll(query))
16+
17+
override fun requestOpenCursor(query: IDBKey?, direction: Cursor.Direction): Request<IDBCursorWithValue?> =
18+
Request(objectStore.openCursor(query, direction.constant))
19+
20+
override fun requestOpenKeyCursor(query: IDBKey?, direction: Cursor.Direction): Request<IDBCursor?> =
21+
Request(objectStore.openKeyCursor(query, direction.constant))
22+
23+
override fun requestCount(query: IDBKey?): Request<JsNumber> =
24+
Request(objectStore.count(query))
25+
}

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

0 commit comments

Comments
 (0)