|
| 1 | +package com.juul.indexeddb |
| 2 | + |
| 3 | +import com.juul.indexeddb.external.IDBDatabase |
| 4 | +import com.juul.indexeddb.external.IDBFactory |
| 5 | +import com.juul.indexeddb.external.IDBTransactionDurability |
| 6 | +import com.juul.indexeddb.external.IDBTransactionOptions |
| 7 | +import com.juul.indexeddb.external.IDBVersionChangeEvent |
| 8 | +import com.juul.indexeddb.external.indexedDB |
| 9 | +import kotlinx.browser.window |
| 10 | +import kotlinx.coroutines.Dispatchers |
| 11 | +import kotlinx.coroutines.withContext |
| 12 | + |
| 13 | +/** |
| 14 | + * Inside the [initialize] block, you must not call any `suspend` functions except for: |
| 15 | + * - those provided by this library and scoped on [Transaction] (and its subclasses) |
| 16 | + * - flow operations on the flows returns by [Transaction.openCursor] and [Transaction.openKeyCursor] |
| 17 | + * - `suspend` functions composed entirely of other legal functions |
| 18 | + */ |
| 19 | +public suspend fun openDatabase( |
| 20 | + name: String, |
| 21 | + version: Int, |
| 22 | + initialize: suspend VersionChangeTransaction.( |
| 23 | + database: Database, |
| 24 | + oldVersion: Int, |
| 25 | + newVersion: Int, |
| 26 | + ) -> Unit, |
| 27 | +): Database = withContext(Dispatchers.Unconfined) { |
| 28 | + val indexedDB: IDBFactory? = selfIndexedDB |
| 29 | + val factory = checkNotNull(indexedDB) { "Your browser doesn't support IndexedDB." } |
| 30 | + val request = factory.open(name, version) |
| 31 | + val versionChangeEvent = request.onNextEvent("success", "upgradeneeded", "error", "blocked") { event -> |
| 32 | + when (event.type) { |
| 33 | + "upgradeneeded" -> event as IDBVersionChangeEvent |
| 34 | + "error" -> throw ErrorEventException(event) |
| 35 | + "blocked" -> throw OpenBlockedException(name, event) |
| 36 | + else -> null |
| 37 | + } |
| 38 | + } |
| 39 | + Database(request.result).also { database -> |
| 40 | + if (versionChangeEvent != null) { |
| 41 | + val transaction = VersionChangeTransaction(checkNotNull(request.transaction)) |
| 42 | + transaction.initialize( |
| 43 | + database, |
| 44 | + versionChangeEvent.oldVersion, |
| 45 | + versionChangeEvent.newVersion, |
| 46 | + ) |
| 47 | + transaction.awaitCompletion() |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +public suspend fun deleteDatabase(name: String) { |
| 53 | + val factory = checkNotNull(window.indexedDB) { "Your browser doesn't support IndexedDB." } |
| 54 | + val request = factory.deleteDatabase(name) |
| 55 | + request.onNextEvent("success", "error", "blocked") { event -> |
| 56 | + when (event.type) { |
| 57 | + "error", "blocked" -> throw ErrorEventException(event) |
| 58 | + else -> null |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +public class Database internal constructor( |
| 64 | + database: IDBDatabase, |
| 65 | +) { |
| 66 | + private var database: IDBDatabase? = database |
| 67 | + |
| 68 | + init { |
| 69 | + // listen for database structure changes (e.g., upgradeneeded while DB is open or deleteDatabase) |
| 70 | + database.addEventListener("versionchange") { close() } |
| 71 | + // listen for force close, e.g., browser profile on a USB drive that's ejected or db deleted through dev tools |
| 72 | + database.addEventListener("close") { close() } |
| 73 | + } |
| 74 | + |
| 75 | + internal fun ensureDatabase(): IDBDatabase = checkNotNull(database) { "database is closed" } |
| 76 | + |
| 77 | + /** |
| 78 | + * Inside the [action] block, you must not call any `suspend` functions except for: |
| 79 | + * - those provided by this library and scoped on [Transaction] (and its subclasses) |
| 80 | + * - flow operations on the flows returns by [Transaction.openCursor] and [Transaction.openKeyCursor] |
| 81 | + * - `suspend` functions composed entirely of other legal functions |
| 82 | + */ |
| 83 | + public suspend fun <T> transaction( |
| 84 | + vararg store: String, |
| 85 | + durability: IDBTransactionDurability = IDBTransactionDurability.Default, |
| 86 | + action: suspend Transaction.() -> T, |
| 87 | + ): T = withContext(Dispatchers.Unconfined) { |
| 88 | + check(store.isNotEmpty()) { |
| 89 | + "At least one store needs to be passed to transaction" |
| 90 | + } |
| 91 | + |
| 92 | + val transaction = Transaction( |
| 93 | + ensureDatabase().transaction( |
| 94 | + storeNames = ReadonlyArray( |
| 95 | + *store.map { it.toJsString() }.toTypedArray(), |
| 96 | + ), |
| 97 | + mode = "readonly", |
| 98 | + options = IDBTransactionOptions(durability), |
| 99 | + ), |
| 100 | + ) |
| 101 | + val result = transaction.action() |
| 102 | + transaction.awaitCompletion() |
| 103 | + result |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Inside the [action] block, you must not call any `suspend` functions except for: |
| 108 | + * - those provided by this library and scoped on [Transaction] (and its subclasses) |
| 109 | + * - flow operations on the flows returns by [Transaction.openCursor] and [Transaction.openKeyCursor] |
| 110 | + * - `suspend` functions composed entirely of other legal functions |
| 111 | + */ |
| 112 | + public suspend fun <T> writeTransaction( |
| 113 | + vararg store: String, |
| 114 | + durability: IDBTransactionDurability = IDBTransactionDurability.Default, |
| 115 | + action: suspend WriteTransaction.() -> T, |
| 116 | + ): T = withContext(Dispatchers.Unconfined) { |
| 117 | + check(store.isNotEmpty()) { |
| 118 | + "At least one store needs to be passed to writeTransaction" |
| 119 | + } |
| 120 | + |
| 121 | + val transaction = WriteTransaction( |
| 122 | + ensureDatabase() |
| 123 | + .transaction( |
| 124 | + storeNames = ReadonlyArray( |
| 125 | + *store.map { it.toJsString() }.toTypedArray(), |
| 126 | + ), |
| 127 | + mode = "readwrite", |
| 128 | + options = IDBTransactionOptions(durability), |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + with(transaction) { |
| 133 | + // Force overlapping transactions to not call `action` until prior transactions complete. |
| 134 | + objectStore(store.first()) |
| 135 | + .openKeyCursor(autoContinue = false) |
| 136 | + .collect { it.close() } |
| 137 | + } |
| 138 | + val result = transaction.action() |
| 139 | + transaction.awaitCompletion() |
| 140 | + result |
| 141 | + } |
| 142 | + |
| 143 | + public fun close() { |
| 144 | + database?.close() |
| 145 | + database = null |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +@Suppress("RedundantNullableReturnType") |
| 150 | +private val selfIndexedDB: IDBFactory? = js("self.indexedDB || self.webkitIndexedDB") |
0 commit comments