Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.js.ExperimentalWasmJsInterop
import kotlin.js.JsAny
import kotlin.js.Promise
import kotlin.js.asJsException
import kotlin.js.js

/**
* We use [Cache] APIs to cache the successful strings.cvr and other responses.
Expand All @@ -20,6 +21,9 @@ import kotlin.js.asJsException
*
* Cache limits:
* https://developer.mozilla.org/en-US/docs/Web/API/Storage_API/Storage_quotas_and_eviction_criteria#other_web_technologies
*
* The Cache API is available only in secure contexts (HTTPS or localhost).
* In non-secure contexts it will behave as if there are no cached values.
*/
@OptIn(ExperimentalWasmJsInterop::class)
internal object ResourceWebCache {
Expand All @@ -33,7 +37,11 @@ internal object ResourceWebCache {
// A mutex to avoid multiple cache reset
private val resetMutex = Mutex()

private val supportsCacheApi: Boolean by lazy { supportsCacheApi() }

suspend fun load(path: String, onNoCacheHit: suspend (path: String) -> Response): Response {
if (!supportsCacheApi) return onNoCacheHit(path)

if (isNewSession()) {
// There can be many load requests, and there must be 1 reset max. Therefore, using `resetMutex`.
resetMutex.withLock {
Expand Down Expand Up @@ -75,6 +83,11 @@ internal object ResourceWebCache {
}
}


// https://developer.mozilla.org/en-US/docs/Web/API/Window/caches
// Supported only in secure contexts (HTTPS or localhost)
private fun supportsCacheApi(): Boolean = js("Boolean(window.caches)")

// Promise.await is not yet available in webMain: https://github.com/Kotlin/kotlinx.coroutines/issues/4544
// TODO(o.karpovich): get rid of this function, when kotlinx-coroutines provide Promise.await in webMain out of a box
@OptIn(ExperimentalWasmJsInterop::class)
Expand Down