Skip to content

Instantly share code, notes, and snippets.

View darrarski's full-sized avatar
🍏

Dariusz Rybicki darrarski

🍏
View GitHub Profile
@darrarski
darrarski / AsyncSerialQueue.swift
Last active May 6, 2025 11:44
Swift Serial Queue for async/await operations
import Foundation
/// Serial queue that executes added operations asynchronosly, one at a time.
///
/// Example usage:
/// ```swift
/// let queue = AsyncSerialQueue()
/// for number in 1...100_000 {
/// queue.addOperation {
/// let task = Task { print(number) }
import ComposableArchitecture
import Foundation
@Reducer
public struct AsyncSequenceReducer<Element, Failure>: Sendable
where Element: Sendable,
Element: Equatable,
Failure: Sendable,
Failure: Error
{
import ConcurrencyExtras
import struct Foundation.UUID
@dynamicMemberLookup
final class LockIsolatedValueStream<Value>: Sendable where Value: Sendable {
init(_ value: Value) {
self._lock = LockIsolated(value)
}
var value: Value { _lock.value }
import ComposableArchitecture
import Foundation
@Reducer
struct AsyncStreamReducer<Element>
where Element: Sendable,
Element: Equatable
{
struct State: Equatable {
let id = UUID()
#!/usr/bin/env bash
# Measure time
# Usage: stopwatch [start|stop|print]
function stopwatch {
case "$1" in
start)
# start new stopwatch
STOPWATCH+=($(date -u +%s))
;;
@darrarski
darrarski / Gravatar.swift
Created August 31, 2023 14:10
Swift wrapper for Gravatar JSON API
import CryptoKit
import Dependencies
import Foundation
import XCTestDynamicOverlay
public struct GravatarJSON: Equatable, Sendable, Codable {
public init(entry: [Entry]) {
self.entry = entry
}
@darrarski
darrarski / ReducerPrinter+SwiftLog.swift
Created August 9, 2023 13:56
ComposableArchitecture + SwiftLog integration
import ComposableArchitecture
import Logging
extension _ReducerPrinter {
/// Logs info about received actions and state changes to swift-log's Logger with provided label.
///
/// Example usage:
/// ```
/// let store = Store(initialState: AppFeature.State()) {
/// AppFeature()._printChanges(.swiftLog(label: "tca"))
@darrarski
darrarski / ExampleApp.swift
Created March 13, 2023 23:47
TCA prerelease 1.0 NavigationLinkStore returningLastNonNilValue issue
import ComposableArchitecture
import SwiftUI
struct Parent: Reducer {
struct State: Equatable {
@PresentationState var child: Child.State?
}
enum Action: Equatable {
case presentChildButtonTapped
@darrarski
darrarski / AsyncThrowingStreamTestReducer.swift
Last active March 5, 2023 10:56
AsyncStream and AsyncThrowingStream testing utility
/// MIT License
///
/// Copyright (c) 2023 Dariusz Rybicki Darrarski
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
@darrarski
darrarski / CurrentValueAsyncSequence.swift
Created January 15, 2023 01:18
[swift-concurrency] Async sequence with current value (similar to Combine's CurrentValueSubject).
import Foundation
@dynamicMemberLookup
public actor CurrentValueAsyncSequence<Value>: AsyncSequence where Value: Sendable {
public typealias Element = Value
public init(_ value: Value) {
self.value = value
}