-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrite Logger (#29) * Remove dependency on timber * Update logger * Reorder throwabl * Fix lint * Update readme * Blank target * Create Zip (#30) * Finish zips with tests * Finalize * Update changelog * Add log hooks * Open most logging functions * Remap kpref items (#31) * Update readme * Generate files and prepare release * Kpref -
- Loading branch information
Showing
25 changed files
with
338 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ca.allanwang.kau.kotlin | ||
|
||
import org.jetbrains.anko.doAsync | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
/** | ||
* Created by Allan Wang on 2017-08-06. | ||
* | ||
* Collection of zip methods that aim to replicate | ||
* <a href="http://reactivex.io/documentation/operators/zip.html">Reactive Zips</a> | ||
* For unit returning functions | ||
* | ||
* Typically, the functions will execute asynchronously and call their given callbacks when finished. | ||
* Once all callbacks are called, the final onFinish callback will be executed. | ||
* | ||
* There is also a helper zipper to wrap synchronous functions with Anko's doAsync to achieve the same results | ||
* | ||
* Note that not wrapping synchronous functions will render these methods useless, | ||
* as you can simply define an inline callback after all functions are finished | ||
*/ | ||
|
||
/** | ||
* Callback which will only execute the first time | ||
*/ | ||
open class ZipCallbackBase { | ||
var completed: Boolean = false | ||
|
||
inline operator fun invoke(callback: () -> Unit) { | ||
if (completed) return | ||
completed = true | ||
callback() | ||
} | ||
} | ||
|
||
class ZipCallback<T>(val onReceived: (T) -> Unit) : ZipCallbackBase() { | ||
operator fun invoke(result: T) = invoke { onReceived(result) } | ||
} | ||
|
||
class ZipEmptyCallback(val onReceived: () -> Unit) : ZipCallbackBase() { | ||
operator fun invoke() = invoke(onReceived) | ||
} | ||
|
||
/** | ||
* Given a default result, a series of tasks, and a finished callback, | ||
* this method will run all tasks and wait until all tasks emit a response | ||
* The response will then be sent back to the callback | ||
* | ||
* ALl tasks must invoke the task callback for [onFinished] to execute | ||
*/ | ||
inline fun <reified T> Collection<(ZipCallback<T>) -> Unit>.zip( | ||
defaultResult: T, crossinline onFinished: (results: Array<T>) -> Unit | ||
) { | ||
val result = Array(size) { defaultResult } | ||
val countDown = AtomicInteger(size) | ||
forEachIndexed { index, asyncFun -> | ||
asyncFun(ZipCallback<T> { | ||
result[index] = it | ||
if (countDown.decrementAndGet() <= 0) | ||
onFinished(result) | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Simplified zip method with no finished callback arguments | ||
*/ | ||
inline fun Collection<(ZipEmptyCallback) -> Unit>.zip(crossinline onFinished: () -> Unit) { | ||
val countDown = AtomicInteger(size) | ||
forEach { asyncFun -> | ||
asyncFun(ZipEmptyCallback { | ||
if (countDown.decrementAndGet() <= 0) | ||
onFinished() | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Converts a collection of synchronous tasks to asynchronous tasks with a common callback | ||
*/ | ||
inline fun Collection<() -> Unit>.zipAsync(crossinline onFinished: () -> Unit) { | ||
map { synchronousFun -> | ||
{ | ||
callback: ZipEmptyCallback -> | ||
doAsync { | ||
synchronousFun() | ||
callback() | ||
}; Unit | ||
} | ||
}.zip(onFinished) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.