Skip to content

Instantly share code, notes, and snippets.

View patka817's full-sized avatar

Patrik Karlsson patka817

View GitHub Profile
@patka817
patka817 / PublisherExtension.swift
Created May 4, 2022 08:49
An extension that won't create reference-cycles when using assign(to:on:) or sink(receiveValue:)
extension Publisher where Failure == Never {
func weakAssign<T: AnyObject>(
to keyPath: ReferenceWritableKeyPath<T, Output>,
on object: T
) -> AnyCancellable {
sink { [weak object] value in
object?[keyPath: keyPath] = value
}
}
@patka817
patka817 / debounce-throttle.swift
Created April 14, 2022 16:45 — forked from simme/debounce-throttle.swift
Swift 3 debounce & throttle
//
// debounce-throttle.swift
//
// Created by Simon Ljungberg on 19/12/16.
// License: MIT
//
import Foundation
extension TimeInterval {
@patka817
patka817 / ReentrantLock.swift
Created October 1, 2020 20:14
Wrapper class with a simple API around a recursive pthread_mutex_t
final class ReentrantLock {
private var _lock: pthread_mutex_t
init() {
var attr = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
self._lock = pthread_mutex_t()
pthread_mutex_init(&self._lock, &attr)
@patka817
patka817 / OptionalAssignment.swift
Created March 2, 2020 11:11
Operator for optional assignment
infix operator ?= : AssignmentPrecedence
public func ?= <Value>(lhs: inout Value, rhs: Value?) {
if let rhs = rhs {
lhs = rhs
}
}