Skip to content

Instantly share code, notes, and snippets.

@Tetraquark
Tetraquark / EventBusKotlinNativeTest.kt
Created August 11, 2021 18:59
Kotlin Native test of Kotlin mulitplatform EventBus
import ru.tetraquark.kotlin.playground.shared.eventbus.EventBus
import kotlin.native.concurrent.TransferMode
import kotlin.native.concurrent.Worker
import kotlin.native.concurrent.freeze
class RefHolder<T>(ref: T?) {
var ref: T? = ref
get() {
val tmp = field
ref = null
@Tetraquark
Tetraquark / EventBus.kt
Created August 11, 2021 18:55
Kotlin multiplatform simple EventBus
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.getAndUpdate
import kotlin.reflect.KClass
typealias Observer = (Any) -> Unit
object EventBus {
private val observers = atomic(mutableMapOf<KClass<out Any>, List<Observer>>())
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=API_ACCESS_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"to": "CLIENT_PUSH_TOKEN",
"notification": {
"body": "Push text message",
"title": "Push title"
},
"data": {
{
"to": "CLIENT_PUSH_TOKEN",
"notification": {
"body": "Push text message",
"title": "Push title"
},
"data": {
"some_important_id": "1234"
}
}
@Tetraquark
Tetraquark / SendChannelInterface.kt
Created February 2, 2020 10:35
What is channel? SendChannelInterface
public suspend fun send(element: E)
public fun offer(element: E): Boolean
@Tetraquark
Tetraquark / SendChannelClose.kt
Created February 2, 2020 10:35
What is channel? SendChannelClose
public fun close(cause: Throwable? = null): Boolean
@Tetraquark
Tetraquark / ReceiveChannelIterate.kt
Created February 2, 2020 10:34
What is channel? ReceiveChannelIterate
for (item in channel) {
// do something with item
}
@Tetraquark
Tetraquark / ReceiveChannelInterface.kt
Created February 2, 2020 10:34
What is channel? ReceiveChannelInterface
public suspend fun receive(): E
public fun poll(): E?
@Tetraquark
Tetraquark / ReceiveChannelCancel.kt
Created February 2, 2020 10:33
What is channel? ReceiveChannelCancel
public fun cancel(cause: CancellationException? = null)
@Tetraquark
Tetraquark / ProduceChannelExmpl.kt
Created February 2, 2020 10:33
What is channel? ProduceChannelExmpl
import kotlinx.coroutines.channels.produce
val receiveChannel = produce(context = Dispatchers.Default) {
for (i in 0..10) {
send(i)
}
}