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
import Foundation | |
func getData1() async throws -> Int { | |
try await Task.sleep(nanoseconds: 2_000_000_000) | |
return 1 | |
} | |
func getData2() async throws -> Int { | |
try await Task.sleep(nanoseconds: 2_000_000_000) | |
return 2 |
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
package com.hoc081098.kotlin_playground | |
import kotlinx.coroutines.CancellationException | |
import kotlinx.coroutines.cancelAndJoin | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.runBlocking | |
import kotlinx.coroutines.suspendCancellableCoroutine | |
import java.util.concurrent.Callable | |
import java.util.concurrent.Executors |
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
private class DemoError: Error { } | |
private final class GetDataRequest: @unchecked Sendable { | |
private let lock = NSLock() | |
private var item: DispatchWorkItem? | |
private var onCancel: (@Sendable () -> Void)? | |
func start( | |
block: @Sendable @escaping (Result<Int, Error>) -> Void, | |
onCancel: @Sendable @escaping () -> Void |
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
@HiltAndroidApp | |
class MyApp : Application(), ImageLoaderFactory { | |
@Inject | |
internal lateinit var activityTracker: TopResumedActivityTracker | |
override fun onCreate() { | |
super.onCreate() | |
registerActivityLifecycleCallbacks(activityTracker) | |
} | |
} |
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
import androidx.annotation.MainThread | |
import com.st.android.showcase.ui.extension.debugCheckImmediateMainDispatcher | |
import java.io.Closeable | |
import javax.inject.Inject | |
import kotlinx.coroutines.channels.Channel | |
import kotlinx.coroutines.channels.onFailure | |
import kotlinx.coroutines.channels.onSuccess | |
import kotlinx.coroutines.flow.Flow | |
import kotlinx.coroutines.flow.receiveAsFlow |
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
import 'package:rxdart/rxdart.dart'; | |
import 'dart:async'; | |
Stream<int> dataStream() => | |
Stream.periodic(const Duration(milliseconds: 500), (i) => i); | |
void main() async { | |
final trigger = PublishSubject<void>(); | |
final sub = dataStream().bufferUntil(trigger).listen(print); |
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
extension BufferUntilStreamExtension<T> on Stream<T> { | |
Stream<T> bufferUntil(Stream<void> trigger) { | |
final controller = StreamController<T>(sync: true); | |
final bag = CompositeSubscription(); | |
// send source events to buffer controller. | |
final bufferController = StreamController<T>(sync: true); | |
listen( | |
bufferController.add, | |
onError: bufferController.addError, |
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
// | |
// Demo.swift | |
// PhDownloader Example | |
// | |
// Created by Hoc Nguyen T. on 12/10/22. | |
// | |
import Foundation | |
import RxSwift | |
import RxRelay |
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
import 'package:rxdart_ext/rxdart_ext.dart'; | |
import 'package:tuple/tuple.dart'; | |
Future<List<int>> getList() async { | |
print('getList: start'); | |
await delay(500); | |
print('getList: end'); | |
return List.generate(10, identity); | |
} |
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
// CONSUMING FLOWS SAFELY IN JETPACK COMPOSE | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.platform.LocalLifecycleOwner | |
import androidx.hilt.navigation.compose.hiltViewModel | |
import androidx.lifecycle.* | |
import androidx.lifecycle.compose.* | |
import androidx.lifecycle.repeatOnLifecycle | |
import kotlinx.collections.immutable.* |