Skip to content

Instantly share code, notes, and snippets.

View viztea's full-sized avatar
😴
sleeping

viztea

😴
sleeping
View GitHub Profile
@viztea
viztea / AsyncLimiter2.ts
Created September 21, 2021 12:55
V2 of my async rate-limiter
export class AsyncLimiter extends TypedEmitter<{ limited: (wait: number) => void; }> {
private queue: Callback[] = [];
private tokens: number;
private executing = false;
private locked = false;
private last = 0;
constructor(readonly defaultTokens: number, readonly waitTime: number) {
super();
this.tokens = +defaultTokens;
@viztea
viztea / limit.ts
Created September 21, 2021 12:52
decorators for creating rate-limited methods (uses AsyncLimiter.ts)
const metadata = Symbol.for("rate-limiter");
const limiters = new Map<Fn | string | number, AsyncLimiter>();
type Fn = (...args: any[]) => void;
export function limit(): MethodDecorator
export function limit(id: string | number): MethodDecorator
export function limit(tokens: number, wait?: number): MethodDecorator
export function limit(totalOrLimiter?: number | string, wait?: number): MethodDecorator {
// @ts-ignore
export class AsyncLimiter extends TypedEmitter<{ limited: (duration: number) => void; }> {
private tokens: number;
private lock?: Promise<void>;
constructor(readonly defaultTokens: number, readonly ratelimitDuration: number) {
super();
this.tokens = +defaultTokens;
}
@viztea
viztea / StringDB.kt
Last active August 29, 2021 09:22
A key/value database that uses a single string
import java.util.concurrent.atomic.AtomicReference
class StringDB {
/**
* The key/value pairs in this database.
*/
val entries: Map<String, Any>
get() = db.lines()
.filter { it.isNotBlank() }
.map { it.split("=") }
@viztea
viztea / LoggerFactory.ts
Created August 27, 2021 04:02
winston logger factory
import { gray, magenta } from "colors/safe"
import { join } from "path";
import { format as fmt, loggers, transports as tps } from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
export namespace LoggerFactory {
const transports = [
new DailyRotateFile({
filename: "combined-%DATE%.log",
dirname: join(process.cwd(), "logs")
import java.util.*
import java.util.concurrent.ThreadFactory
import java.util.concurrent.atomic.AtomicInteger
fun ThreadFactory(name: String, daemon: Boolean = false): ThreadFactory {
val group = System.getSecurityManager()
?.threadGroup
?: Thread.currentThread().threadGroup
return object : ThreadFactory {
@viztea
viztea / Config.kt
Last active October 4, 2021 03:00
Kotlin JSON config object
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import java.nio.file.Paths
import kotlin.io.path.readText
object Config {
val json = Json {}
@viztea
viztea / Eval.kt
Last active February 21, 2022 20:46
Some stuff for inspecting values returned from an eval command
val padding = " "
fun <V : Any> inspectValue(value: V?, prop: KProperty<*>? = null): String {
return when (value) {
null -> "null"
is String -> "\"$value\""
is Number, is Boolean -> "$value"
is Pair<*, *> -> "${prop?.returnType?.name ?: value::class.name}(${inspectValue(value.first as Any, value::first)}, ${inspectValue(value.first as Any, value::second)})"
is Collection<*> -> "${prop?.returnType?.name ?: value::class.name}(${value.size})"
else -> {
val valueStr = value.toString()
❗️ Closed issue #18 in melike2d/lavalink
πŸ—£ Commented on #18 in melike2d/lavalink
❗️ Closed issue #156 in MenuDocs/erela.js
❗️ Closed issue #157 in MenuDocs/erela.js
❗️ Opened issue #157 in MenuDocs/erela.js
Kotlin 19 mins β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ 100.0%