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

commonMain.internal.actions.WriteExternalContextAction.kt Maven / Gradle / Ivy

There is a newer version: 0.23.0
Show newest version
package io.kform.internal.actions

import io.kform.AbsolutePath
import io.kform.FormManager
import io.kform.internal.AccessExternalContext
import io.kform.internal.Action
import io.kform.internal.ActionAccessType

/**
 * Action that writes the external context with name [externalContextName] available to validations.
 */
internal abstract class WriteExternalContextAction(formManager: FormManager) :
    FormManagerAction(formManager) {
    abstract val externalContextName: String

    override val accesses = listOf(AccessExternalContext(ActionAccessType.Write))
    override val accessedPaths: List = emptyList()

    override fun overridesConflictingAction(action: Action<*>): Boolean =
        action is WriteExternalContextAction

    // FIXME: Cannot add line below due to the map not being thread-safe
    // && action.externalContextName == externalContextName

    /**
     * Invalidate all validations depending on the external context as well as remove external
     * issues depending on it.
     */
    protected suspend fun invalidateDependenciesAndRemoveDependingExternalIssues() {
        // Invalidate depending validations
        for ((path, validation, validationIndex) in
            formManager.externalContextDependendencies[externalContextName] ?: emptyList()) {
            formManager.scheduleAction(
                InvalidateValidationAction(formManager, path, validation, validationIndex)
            )
        }

        // Remove depending external issues
        val dependingExternalIssues =
            formManager.externalIssuesDependencies
                .getAndRemoveExternalIssuesDependentOnExternalContext(externalContextName)
        if (dependingExternalIssues.isNotEmpty()) {
            formManager.scheduleAction(
                RemoveDependingExternalIssuesAction(formManager, dependingExternalIssues)
            )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy