Skip to content

Instantly share code, notes, and snippets.

View fluidsonic's full-sized avatar

Marc Knaup fluidsonic

View GitHub Profile
import java.util.concurrent.*
import kotlin.reflect.*
import kotlinx.coroutines.*
class SynchronousEventEmitter(
private val onError: (error: Throwable, event: Event) -> Unit,
) : EventEmitter, EventSource {
private val subscriptions = CopyOnWriteArraySet<Subscription<*>>()
@fluidsonic
fluidsonic / webpack.js
Created February 10, 2021 12:05
Kotlin/JS TeserPlugin settings for optimization
// …
new TerserPlugin({
terserOptions: {
// ecma: "2016", // doesn't work with some libraries
compress: {
keep_fargs: false,
negate_iife: false,
passes: 5, // 4 was highest number observed to date that results in even smaller code
pure_funcs(node) {
// noinspection JSUnresolvedVariable
;+function () {
// noinspection JSUnresolvedFunction,NpmUsedModulesInstalled
const TerserPlugin = require('terser-webpack-plugin')
// noinspection JSUnresolvedVariable
if (config.mode === 'production') {
// noinspection JSUnresolvedVariable,SpellCheckingInspection,JSUnusedGlobalSymbols
config.optimization = {
minimize: true,
minimizer: [
import java.time.*
import kotlin.time.*
import kotlin.time.Duration
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
private object Done
private object TimerExpired
@fluidsonic
fluidsonic / StringBuilder.kt
Created November 24, 2020 23:28
Array.push/join-based StringBuilder for Kotlin/JS
public external interface StringBuilder
public inline fun StringBuilder.append(value: Any) {
append("$value")
}
public inline fun StringBuilder.append(string: String) {
if (string != "")
@fluidsonic
fluidsonic / Example.kt
Created November 4, 2020 03:10
Resource/asset hashing using Webpack in Kotlin React project
@JsName("require")
external fun asset(path: String): String
// in a React builder
img(src = asset("image.png")) {}
/* Output
Collecting 2 elements…
upstream -> hot
emit: 0
resumed
collect: 0
Expensive work on 0 (isPaused = false)…
Expensive work on 0 (isPaused = false)…
Expensive work on 0 (isPaused = false)…
@fluidsonic
fluidsonic / CacheFlow.kt
Last active October 14, 2020 17:12
Creates a cache that uses "child" MutableStateFlows that won't get a value emitted for its own changes in order to avoid cycles.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
/* Output:
cache collected: initial
flow1 (collector 1) collected: initial
flow1 (collector 2) collected: initial
flow2 (collector 1) collected: initial
@fluidsonic
fluidsonic / KtorClientBenchmark.kt
Created October 12, 2020 14:35
Testing how fast Ktor client engines are in comparison
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.engine.apache.*
import io.ktor.client.engine.cio.*
import io.ktor.client.engine.jetty.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
@fluidsonic
fluidsonic / CIO.kt
Created October 12, 2020 10:46
CIO fails under high load
import io.ktor.application.*
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*