Skip to content

Instantly share code, notes, and snippets.

View Nillerr's full-sized avatar

Nicklas Jensen Nillerr

View GitHub Profile
$url = "http://csgolounge.com/match?m=3071";
$id = str_replace("http://csgolounge.com/match?m=", "", $url);
@Nillerr
Nillerr / UIView+MainThread.swift
Last active February 26, 2022 05:46
Replaces a call to *addSubview* with an implementation that ensures it is always invoked on the main thread.
import UIKit
/**
Swizzles `addSubview` of `UIView`, replacing it with an implementatio that ensures it is always
invoked on the main thread.
Call this method in your application entry point, e.g. the `UIApplicationDelegate`.
*/
func swizzleAddSubview() {
let clazz = UIView.self
@Nillerr
Nillerr / copy-linked-frameworks.sh
Last active October 3, 2018 06:16
Automatically copies linked Carthage frameworks, without explicitly specifying them in a Run Script build phase. The script works by inspecting the executable build output using otool to create a list of dependencies, and passes them on to the `carthage copy-frameworks` script.
#!/bin/sh
frameworks=$(otool -L $BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH | pcregrep -o1 "@rpath/(.*?\.framework)")
index=0
for framework in $frameworks; do
if [ -e "$SRCROOT/Carthage/Build/iOS/$framework" ]; then
export "SCRIPT_INPUT_FILE_$index"="$SRCROOT/Carthage/Build/iOS/$framework"
export "SCRIPT_OUTPUT_FILE_$index"="$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/$framework"
let index=index+1
@Nillerr
Nillerr / KeyValueEntriesPipe.ts
Last active August 13, 2020 08:44
Allows the use of keyof MyType type declarations in methods accepting the keys of the entries when transformed using this pipe in an *ngFor loop.
/**
* Transforms Object into an array of key value pairs.
*/
@Pipe({ name: 'entries' })
export class KeyValueEntriesPipe implements PipeTransform {
transform<T extends { [p in keyof T]: T[keyof T] }>(input: T): { key: keyof T, value: T[keyof T] }[] {
const entries: { key: keyof T, value: T[keyof T] }[] = [];
for (const key in input) {
if (input.hasOwnProperty(key)) {
entries.push({ key, value: input[key] });
@Nillerr
Nillerr / AnyFlow.kt
Last active May 25, 2021 17:12
AnyFlow<T> with collect
class AnyFlow<T>(source: Flow<T>): Flow<T> by source {
fun collect(onEach: (T) -> Unit, onCompletion: (cause: Throwable?) -> Unit): Cancellable {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
scope.launch {
try {
collect {
onEach(it)
}
@Nillerr
Nillerr / AnyFlow.kt
Created May 20, 2021 19:21
AnyFlow<T>
class AnyFlow<T>(source: Flow<T>): Flow<T> by source
interface Cancellable {
fun cancel()
}
interface TaskService {
val tasks: Flow<List<Task>>
fun tasks(onEach: (List<Task>) -> Unit, onCompletion: (Throwable?) -> Unit): Cancellable =
tasks.collect(onEach, onCompletion)
}
fun <T> Flow<T>.collect(onEach: (T) -> Unit, onCompletion: (cause: Throwable?) -> Unit): Cancellable {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
scope.launch {
try {
collect {
onEach(it)
}
onCompletion(null)
import Combine
import Foundation
import shared
typealias OnEach<Output> = (Output) -> Void
typealias OnCompletion<Failure> = (Failure?) -> Void
typealias OnCollect<Output, Failure> = (@escaping OnEach<Output>, @escaping OnCompletion<Failure>) -> shared.Cancellable
/**