Skip to content

Commit

Permalink
Expose transaction durability, fix write transaction exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
cedrickcooke committed Dec 21, 2023
1 parent 967f243 commit 8eed554
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
22 changes: 18 additions & 4 deletions core/src/jsMain/kotlin/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ public class Database internal constructor(internal val database: IDBDatabase) {
*/
public suspend fun <T> transaction(
vararg store: String,
durability: Durability = Durability.Default,
action: suspend Transaction.() -> T,
): T = withContext(Dispatchers.Unconfined) {
@Suppress("UNCHECKED_CAST") // What a silly cast. Apparently `vararg` creates `Array<out String>` instead of `Array<String>`
val transaction = Transaction(database.transaction(store as Array<String>, "readonly"))
val transaction = Transaction(
database.transaction(arrayOf(*store), "readonly", transactionOptions(durability)),
)
val result = transaction.action()
transaction.awaitCompletion()
result
Expand All @@ -81,10 +83,18 @@ public class Database internal constructor(internal val database: IDBDatabase) {
*/
public suspend fun <T> writeTransaction(
vararg store: String,
durability: Durability = Durability.Default,
action: suspend WriteTransaction.() -> T,
): T = withContext(Dispatchers.Unconfined) {
@Suppress("UNCHECKED_CAST") // What a silly cast. Apparently `vararg` creates `Array<out String>` instead of `Array<String>`
val transaction = WriteTransaction(database.transaction(store as Array<String>, "readwrite"))
val transaction = WriteTransaction(
database.transaction(arrayOf(*store), "readwrite", transactionOptions(durability)),
)
with(transaction) {
// Force overlapping transactions to not call `action` until prior transactions complete.
objectStore(store.first())
.openCursor(autoContinue = false)
.collect { it.close() }
}
val result = transaction.action()
transaction.awaitCompletion()
result
Expand All @@ -94,3 +104,7 @@ public class Database internal constructor(internal val database: IDBDatabase) {
database.close()
}
}

private fun transactionOptions(durability: Durability): dynamic = jso {
this.durability = durability.jsValue
}
10 changes: 10 additions & 0 deletions core/src/jsMain/kotlin/Durability.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.juul.indexeddb

/** https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction/durability */
public enum class Durability(
internal val jsValue: String
) {
Default("default"),
Strict("strict"),
Relaxed("relaxed");
}
2 changes: 1 addition & 1 deletion external/src/jsMain/kotlin/IDBDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public external class IDBDatabase : EventTarget {
public fun createObjectStore(name: String): IDBObjectStore
public fun createObjectStore(name: String, options: dynamic): IDBObjectStore
public fun deleteObjectStore(name: String)
public fun transaction(storeNames: Array<String>, mode: String): IDBTransaction
public fun transaction(storeNames: Array<String>, mode: String, options: dynamic): IDBTransaction
}

0 comments on commit 8eed554

Please sign in to comment.