suspend fun load()

in components/resources/library/src/webMain/kotlin/org/jetbrains/compose/resources/ResourceWebCache.web.kt [42:71]


    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 {
                // Checking isNewSession() again in case it was just changed by another load request.
                // I avoid wrapping withLock in if (isNewSession()) check to avoid unnecessary locking on every load request
                if (isNewSession()) {
                    sessionStarted = true
                    resetCache()
                }
            }
        }

        val mutex = mutexes.getOrPut(path) { Mutex() }

        return mutex.withLock {
            val cache = window.caches.open(CACHE_NAME).await()
            val response = (cache.match(path, CacheQueryOptions()) as Promise<Response?>).await()

            response?.clone() ?: onNoCacheHit(path).also {
                if (it.ok) {
                    cache.put(path, it.clone()).await()
                }
            }
        }.also {
            mutexes.remove(path)
        }
    }