From c5b355f49d0bd5c54f5335b70766ed1309ecdd95 Mon Sep 17 00:00:00 2001 From: Jieyi Date: Mon, 3 Dec 2018 18:15:25 +0900 Subject: [PATCH] Feature: added the coroutine into shaver library. --- .../com/devrapid/kotlinknifer/Coroutine.kt | 16 ------- .../com/devrapid/kotlinshaver/Coroutine.kt | 42 +++++++++++++++++++ 2 files changed, 42 insertions(+), 16 deletions(-) delete mode 100644 kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Coroutine.kt create mode 100644 kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Coroutine.kt diff --git a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Coroutine.kt b/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Coroutine.kt deleted file mode 100644 index 3961f90..0000000 --- a/kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Coroutine.kt +++ /dev/null @@ -1,16 +0,0 @@ -@file:Suppress("NOTHING_TO_INLINE") - -package com.devrapid.kotlinknifer - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.Default -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch - -inline fun ui(noinline block: suspend CoroutineScope.() -> Unit) = GlobalScope.launch(Main, block = block) - -inline fun bkg(noinline block: suspend CoroutineScope.() -> Unit) = GlobalScope.launch(Default, block = block) - -inline fun io(noinline block: suspend CoroutineScope.() -> Unit) = GlobalScope.launch(IO, block = block) diff --git a/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Coroutine.kt b/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Coroutine.kt new file mode 100644 index 0000000..92c0aa3 --- /dev/null +++ b/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Coroutine.kt @@ -0,0 +1,42 @@ +@file:Suppress("NOTHING_TO_INLINE") + +package com.devrapid.kotlinshaver + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Dispatchers.Default +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.async +import kotlinx.coroutines.launch +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext + +// --------------------- launch + +inline fun gLaunch( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + noinline block: suspend CoroutineScope.() -> Unit +) = GlobalScope.launch(context, start, block) + +inline fun ui(noinline block: suspend CoroutineScope.() -> Unit) = gLaunch(Main, block = block) + +inline fun bkg(noinline block: suspend CoroutineScope.() -> Unit) = gLaunch(Default, block = block) + +inline fun io(noinline block: suspend CoroutineScope.() -> Unit) = gLaunch(IO, block = block) + +// ---------------------- async + +inline fun gAsync( + context: CoroutineContext = EmptyCoroutineContext, + start: CoroutineStart = CoroutineStart.DEFAULT, + noinline block: suspend CoroutineScope.() -> T +) = GlobalScope.async(context, start, block) + +inline fun aUI(noinline block: suspend CoroutineScope.() -> T) = gAsync(Main, block = block) + +inline fun aBKG(noinline block: suspend CoroutineScope.() -> T) = gAsync(Default, block = block) + +inline fun aIO(noinline block: suspend CoroutineScope.() -> T) = gAsync(IO, block = block)