All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.me.aartikov.replica.keyed.BulkOperations.kt Maven / Gradle / Ivy

package me.aartikov.replica.keyed

import me.aartikov.replica.common.InvalidationMode
import me.aartikov.replica.common.ReplicaTag

/**
 * Cancels network requests in all child replicas.
 */
suspend fun  KeyedPhysicalReplica.cancelAll() {
    onEachReplica {
        cancel()
    }
}

/**
 * Makes all child replicas stale.
 *
 * @param mode specifies how replicas behave after invalidation. See: [InvalidationMode].
 */
suspend fun  KeyedPhysicalReplica.invalidateAll(
    mode: InvalidationMode = InvalidationMode.RefreshIfHasObservers
) {
    onEachReplica {
        invalidate(mode)
    }
}

/**
 * Cancels network requests in child replicas with the matching tags.
 */
suspend fun  KeyedPhysicalReplica.cancelByTags(
    predicate: (Set) -> Boolean
) {
    if (predicate(tags)) {
        cancelAll()
        return
    }

    onEachReplica {
        if (predicate(tags)) {
            cancel()
        }
    }
}

/**
 * Cancels network requests and clears data in child replicas with the matching tags.
 */
suspend fun  KeyedPhysicalReplica.clearByTags(
    predicate: (Set) -> Boolean
) {
    if (predicate(tags)) {
        clearAll()
        return
    }

    onEachReplica {
        if (predicate(tags)) {
            clear()
        }
    }
}

/**
 * Makes child replicas with the matching tags stale.
 *
 * @param mode specifies how replicas behave after invalidation. See: [InvalidationMode].
 */
suspend fun  KeyedPhysicalReplica.invalidateByTags(
    mode: InvalidationMode = InvalidationMode.RefreshIfHasObservers,
    predicate: (Set) -> Boolean
) {
    if (predicate(tags)) {
        invalidateAll(mode)
        return
    }

    onEachReplica {
        if (predicate(tags)) {
            invalidate(mode)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy