Skip to content

Commit

Permalink
Feature: added the coroutine into shaver library.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Dec 3, 2018
1 parent b4f393d commit c5b355f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
16 changes: 0 additions & 16 deletions kotlinknifer/src/main/java/com/devrapid/kotlinknifer/Coroutine.kt

This file was deleted.

42 changes: 42 additions & 0 deletions kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Coroutine.kt
Original file line number Diff line number Diff line change
@@ -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 <T> gAsync(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
noinline block: suspend CoroutineScope.() -> T
) = GlobalScope.async(context, start, block)

inline fun <T> aUI(noinline block: suspend CoroutineScope.() -> T) = gAsync(Main, block = block)

inline fun <T> aBKG(noinline block: suspend CoroutineScope.() -> T) = gAsync(Default, block = block)

inline fun <T> aIO(noinline block: suspend CoroutineScope.() -> T) = gAsync(IO, block = block)

0 comments on commit c5b355f

Please sign in to comment.