-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...ileCaching/src/browserStorageSystemMain/kotlin/fr/frankois944/ktorfilecaching/Database.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@file:Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") | ||
|
||
package fr.frankois944.ktorfilecaching | ||
|
||
internal expect object Database { | ||
suspend fun getItem(key: String): String? | ||
|
||
suspend fun setItem( | ||
key: String, | ||
value: String, | ||
) | ||
|
||
suspend fun removeItem(key: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
55 changes: 55 additions & 0 deletions
55
KtorKMPFileCaching/src/jsBrowserMain/kotlin/fr/frankois944/ktorfilecaching/Database.js.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
@file:Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") | ||
|
||
package fr.frankois944.ktorfilecaching | ||
|
||
import com.juul.indexeddb.Database | ||
import com.juul.indexeddb.Key | ||
import com.juul.indexeddb.KeyPath | ||
import com.juul.indexeddb.openDatabase | ||
import fr.frankois944.ktorfilecaching.database.CacheItem | ||
|
||
internal actual object Database { | ||
private var database: Database? = null | ||
|
||
private const val DATABASE_NAME = "KTorKmpFileCaching" | ||
private const val STORE_NAME = "cacheitems" | ||
|
||
suspend fun getDatabase(): Database { | ||
if (database == null) { | ||
database = | ||
openDatabase(DATABASE_NAME, 1) { database, oldVersion, _ -> | ||
if (oldVersion < 1) { | ||
val store = database.createObjectStore(STORE_NAME, KeyPath("cacheKey")) | ||
store.createIndex("cacheValue", KeyPath("cacheValue"), unique = false) | ||
} | ||
} | ||
} | ||
return database!! | ||
} | ||
|
||
actual suspend fun getItem(key: String): String? = | ||
getDatabase() | ||
.transaction(STORE_NAME) { | ||
objectStore(STORE_NAME).get(Key(key)) as? CacheItem | ||
}?.cacheValue | ||
|
||
actual suspend fun setItem( | ||
key: String, | ||
value: String, | ||
) { | ||
getDatabase().writeTransaction(STORE_NAME) { | ||
objectStore(STORE_NAME).put( | ||
jso<CacheItem> { | ||
cacheKey = key | ||
cacheValue = value | ||
}, | ||
) | ||
} | ||
} | ||
|
||
actual suspend fun removeItem(key: String) { | ||
getDatabase().writeTransaction(STORE_NAME) { | ||
objectStore(STORE_NAME).delete(Key(key)) | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...FileCaching/src/jsBrowserMain/kotlin/fr/frankois944/ktorfilecaching/database/CacheItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fr.frankois944.ktorfilecaching.database | ||
|
||
internal external interface CacheItem { | ||
var cacheKey: String | ||
var cacheValue: String? | ||
} |
7 changes: 7 additions & 0 deletions
7
KtorKMPFileCaching/src/jsBrowserMain/kotlin/fr/frankois944/ktorfilecaching/jso.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package fr.frankois944.ktorfilecaching | ||
|
||
internal inline fun <T : Any> jso(): T = js("({})") | ||
|
||
internal inline fun <T : Any> jso(block: T.() -> Unit): T = jso<T>().apply(block) |
21 changes: 21 additions & 0 deletions
21
KtorKMPFileCaching/src/wasmjsMain/kotlin/fr/frankois944/ktorfilecaching/Database.wasmjs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@file:Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") | ||
|
||
package fr.frankois944.ktorfilecaching | ||
|
||
import kotlinx.browser.window | ||
import org.w3c.dom.set | ||
|
||
internal actual object Database { | ||
actual suspend fun getItem(key: String): String? = window.localStorage.getItem(key) | ||
|
||
actual suspend fun setItem( | ||
key: String, | ||
value: String, | ||
) { | ||
window.localStorage[key] = value | ||
} | ||
|
||
actual suspend fun removeItem(key: String) { | ||
window.localStorage.removeItem(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters