From dde5385232abddc8a85d6e9a939549b71dd9130e Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 23 Jun 2022 01:41:36 +0200 Subject: [PATCH] feat: yield the patch result --- .../kotlin/app/revanced/patcher/Patcher.kt | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/Patcher.kt b/src/main/kotlin/app/revanced/patcher/Patcher.kt index 48930d77..c62ead1f 100644 --- a/src/main/kotlin/app/revanced/patcher/Patcher.kt +++ b/src/main/kotlin/app/revanced/patcher/Patcher.kt @@ -302,29 +302,23 @@ class Patcher(private val options: PatcherOptions) { /** * Apply patches loaded into the patcher. * @param stopOnError If true, the patches will stop on the first error. - * @return A map of [PatchResultSuccess]. If the [Patch] was successfully applied, - * [PatchResultSuccess] will always be returned to the wrapping Result object. - * If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object. + * @return A pair of the name of the [Patch] and its [PatchResult]. */ - fun applyPatches( - stopOnError: Boolean = false, callback: (Class>, Boolean) -> Unit = { _, _ -> } - ): Map> { + fun applyPatches(stopOnError: Boolean = false) = sequence { logger.trace("Applying all patches") val appliedPatches = mutableListOf() - return buildMap { - for (patch in data.patches) { - val result = applyPatch(patch, appliedPatches) + for (patch in data.patches) { + val patchResult = applyPatch(patch, appliedPatches) - this[patch.patchName] = if (result.isSuccess()) { - Result.success(result.success()!!) - } else { - Result.failure(result.error()!!) - } - - callback(patch, result.isSuccess()) - if (stopOnError && result.isError()) break + val result = if (patchResult.isSuccess()) { + Result.success(patchResult.success()!!) + } else { + Result.failure(patchResult.error()!!) } + + yield(patch.name to result) + if (stopOnError && patchResult.isError()) break } } }