-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
67 additions
and
168 deletions.
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
library/src/main/java/renetik/android/event/common/CSHasDestruct+.kt
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
package renetik.android.event.common | ||
|
||
fun CSHasDestruct.destruct() = onDestruct() | ||
import renetik.android.core.java.util.concurrent.background | ||
import renetik.android.event.registration.task.CSBackground | ||
import renetik.android.event.registration.task.CSBackground.executor | ||
|
||
fun CSHasDestruct.destruct() = onDestruct() |
24 changes: 19 additions & 5 deletions
24
library/src/main/java/renetik/android/event/registration/CSHasRegistrations+Background.kt
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 |
---|---|---|
@@ -1,12 +1,26 @@ | ||
package renetik.android.event.registration | ||
|
||
import androidx.annotation.AnyThread | ||
import androidx.annotation.WorkerThread | ||
import renetik.android.core.lang.Func | ||
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration | ||
import renetik.android.event.registration.task.CSBackground.background | ||
|
||
//TODO: Can CSHasDestruct.background bew used instead without memory leak reports ? | ||
@AnyThread | ||
inline fun CSHasRegistrations.registerBackground( | ||
@WorkerThread crossinline function: Func, | ||
): CSRegistration = register(background { | ||
function() | ||
cancel(it) | ||
}) | ||
@WorkerThread crossinline function: Func | ||
) = registerBackground(after = 0, function) | ||
|
||
//TODO: Can CSHasDestruct.background bew used instead without memory leak reports ? | ||
@AnyThread | ||
inline fun CSHasRegistrations.registerBackground( | ||
after: Int, @WorkerThread crossinline function: () -> Unit, | ||
): CSRegistration { | ||
lateinit var registration: CSRegistration | ||
registration = register(background(after) { | ||
function() | ||
cancel(registration) | ||
}) | ||
return CSRegistration { if (!registration.isCanceled) cancel(registration) } | ||
} |
51 changes: 2 additions & 49 deletions
51
library/src/main/java/renetik/android/event/registration/CSHasRegistrations+MainHandler.kt
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 |
---|---|---|
@@ -1,60 +1,13 @@ | ||
package renetik.android.event.registration | ||
|
||
import androidx.annotation.AnyThread | ||
import renetik.android.core.lang.CSHandler.mainHandler | ||
import renetik.android.core.lang.CSHandler.main | ||
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration | ||
import renetik.android.event.registration.task.CSBackground.background | ||
|
||
//@AnyThread | ||
//fun CSHasRegistrations.registerLater( | ||
// after: Int = 0, function: () -> Unit, | ||
//): CSRegistration { | ||
// lateinit var registration: CSRegistration | ||
// registration = register(later(if (after < 10) 10 else after) { | ||
// cancel(registration) | ||
// function() | ||
// }) | ||
// return CSRegistration { if (!registration.isCanceled) cancel(registration) } | ||
//} | ||
|
||
//@Deprecated("Can we use instead later extension on HasDestroy ?") | ||
//@AnyThread | ||
//fun CSHasRegistrations.registerLater( | ||
// function: () -> Unit, | ||
//): CSRegistration { | ||
// lateinit var registration: CSRegistration | ||
// registration = register(later(10) { | ||
// cancel(registration) | ||
// function() | ||
// }) | ||
// return CSRegistration { if (!registration.isCanceled) cancel(registration) } | ||
//} | ||
|
||
@AnyThread | ||
fun CSHasRegistrations.registerLaterEach( | ||
after: Int, period: Int = after, function: () -> Unit, | ||
): CSRegistration { | ||
val registration = register(mainHandler.laterEach(after, period, function)) | ||
val registration = register(main.laterEach(after, period, function)) | ||
return CSRegistration { cancel(registration) } | ||
} | ||
|
||
//@AnyThread | ||
//fun <T : CSHasRegistrations> T.registerOnMain( | ||
// function: (T).() -> Unit, | ||
//): CSRegistration? = if (currentThread.isMain) { | ||
// function() | ||
// null | ||
//} else registerLater { function(this) } | ||
|
||
@AnyThread | ||
fun CSHasRegistrations.registerBackground( | ||
delay: Int, function: () -> Unit, | ||
): CSRegistration { | ||
lateinit var registration: CSRegistration | ||
registration = register(background(if (delay < 10) 10 else delay) { | ||
function() | ||
cancel(registration) | ||
}) | ||
return CSRegistration { if (!registration.isCanceled) cancel(registration) } | ||
} | ||
|
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
31 changes: 10 additions & 21 deletions
31
library/src/main/java/renetik/android/event/util/CSLater.kt
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 |
---|---|---|
@@ -1,46 +1,35 @@ | ||
package renetik.android.event.util | ||
|
||
import renetik.android.core.java.lang.isThreadMain | ||
import renetik.android.core.lang.CSHandler.mainHandler | ||
import renetik.android.core.lang.CSHandler.postOnMain | ||
import renetik.android.core.kotlin.primitives.min | ||
import renetik.android.core.lang.CSHandler.main | ||
import renetik.android.core.lang.send | ||
import renetik.android.event.common.CSHasDestruct | ||
import renetik.android.event.registration.CSHasRegistrations | ||
import renetik.android.event.registration.CSRegistration | ||
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration | ||
import renetik.android.event.registration.cancel | ||
import renetik.android.event.registration.later | ||
import renetik.android.event.registration.laterEach | ||
import renetik.android.event.registration.register | ||
|
||
object CSLater { | ||
inline fun later( | ||
after: Int, crossinline function: () -> Unit | ||
): CSRegistration = mainHandler.later(after) { function() } | ||
|
||
inline fun later( | ||
crossinline function: () -> Unit | ||
): CSRegistration = later(5, function) | ||
|
||
// TODO: Move to CSHasDestruct+MainHandler | ||
inline fun CSHasDestruct.later(crossinline function: () -> Unit) = | ||
postOnMain { if (!isDestructed) function() } | ||
main.send { if (!isDestructed) function() } | ||
|
||
// TODO: Move to CSHasDestruct+MainHandler | ||
inline fun CSHasDestruct.onMain(crossinline function: () -> Unit) = | ||
if (isThreadMain) function() | ||
else postOnMain { if (!isDestructed) function() } | ||
else main.send { if (!isDestructed) function() } | ||
|
||
// = CSLater.later(after) { if (!isDestructed) function() } | ||
// TODO: Move to CSHasRegistrations+MainHandler | ||
inline fun CSHasRegistrations.later( | ||
after: Int, crossinline function: () -> Unit | ||
): CSRegistration { | ||
lateinit var registration: CSRegistration | ||
registration = register(mainHandler.later(if (after < 10) 10 else after) { | ||
cancel(registration) | ||
function() | ||
registration = register(main.later(after.min(10)) { | ||
cancel(registration); function() | ||
}) | ||
return CSRegistration { if (!registration.isCanceled) cancel(registration) } | ||
} | ||
|
||
inline fun laterEach( | ||
after: Int, period: Int = after, crossinline function: () -> Unit | ||
): CSRegistration = mainHandler.laterEach(after, period, function) | ||
} |
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