Skip to content

Instantly share code, notes, and snippets.

View hoc081098's full-sized avatar
✝️
Glory to God

Petrus Nguyễn Thái Học hoc081098

✝️
Glory to God
View GitHub Profile
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
@hoc081098
hoc081098 / suspendCancellableCoroutine.kt
Last active March 11, 2023 15:10
Demo how to use suspendCancellableCoroutine to convert the callback style function to suspend function
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
@hoc081098
hoc081098 / swift_async_await_withTaskCancellationHandler.swift
Last active March 11, 2023 15:10
Demo how to use withTaskCancellationHandler and withCheckedThrowingContinuation to convert the callback style function to async/await function
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
@HiltAndroidApp
class MyApp : Application(), ImageLoaderFactory {
@Inject
internal lateinit var activityTracker: TopResumedActivityTracker
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(activityTracker)
}
}
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
@hoc081098
hoc081098 / bufferUntil_example.dart
Created December 17, 2022 11:24
bufferUntil_example.dart
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);
@hoc081098
hoc081098 / bufferUntil.dart
Created December 17, 2022 11:18
bufferUntil
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,
//
// Demo.swift
// PhDownloader Example
//
// Created by Hoc Nguyen T. on 12/10/22.
//
import Foundation
import RxSwift
import RxRelay
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);
}
@hoc081098
hoc081098 / ConsumingFlowsSafelyInJetpackCompose.kt
Created November 28, 2022 04:17
Consuming #Coroutines #Flows safely in #JetpackCompose 📚.
// 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.*