Skip to content

Instantly share code, notes, and snippets.

final class Registry {
private var borrowings: [Borrowing] = []
func copyWithAdded(_ borrowing: Borrowing) -> Registry {
let new = Registry()
new.borrowings = borrowings + [borrowing]
return new
}
func copyWithRemoved(_ borrowing: Borrowing) -> Registry {
let new = Registry()
new.borrowings = borrowings.filter { $0 !== borrowing }
public protocol Borrowable {
var oid: ObjectIdentifier { get }
}
extension Shared: Borrowable {
public var oid: ObjectIdentifier { ObjectIdentifier(self) }
}
@serhiybutz
serhiybutz / console.txt
Created July 23, 2020 21:26
Combine: zip, 01
p1: receive subscription: ([1, 2])
p2: receive subscription: (["foo"])
zip: receive subscription: (Zip)
zip: request unlimited
p1: request unlimited
p1: receive value: (1)
p1: receive value: (2)
p1: receive finished
p2: request unlimited
p2: receive value: (foo)
@serhiybutz
serhiybutz / code.swift
Last active January 25, 2023 19:48
Combine: withLatestFrom, 04
import Combine
extension Publishers {
public struct WithLatestFrom<Upstream: Publisher, Other: Publisher>:
Publisher where Upstream.Failure == Other.Failure
{
// MARK: - Types
public typealias Output = (Upstream.Output, Other.Output)
public typealias Failure = Upstream.Failure
@serhiybutz
serhiybutz / code.swift
Last active August 19, 2020 02:20
Combine: withLatestFrom, 05
func setupAdvPipeline() {
let searchStream = searchBar.searchTextField.textPublisher
.prepend(searchBar.text!)
.eraseToAnyPublisher()
let sparsedSearchStream = searchStream
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
let beginRefreshingStream = refreshControl
@serhiybutz
serhiybutz / code.swift
Created July 21, 2020 17:22
Combine: withLatestFrom, 02
var cancellables = Set<AnyCancellable>()
func setupPipelines() {
let searchStream = searchBar.searchTextField.textPublisher
.prepend(searchBar.text!)
.eraseToAnyPublisher()
let sparsedSearchStream = searchStream
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
@serhiybutz
serhiybutz / code.swift
Created July 21, 2020 17:14
Combine: withLatestFrom, 03
import Combine
import Foundation
extension Publishers {
public struct WithLatestFrom<Upstream: Publisher, Other: Publisher>: Publisher where Upstream.Failure == Other.Failure {
// MARK: - Types
public typealias Output = (Upstream.Output, Other.Output)
public typealias Failure = Upstream.Failure
@serhiybutz
serhiybutz / code.swift
Created July 21, 2020 16:37
Combine: withLatestFrom, 02
var cancellables = Set<AnyCancellable>()
func setupStreams() {
let searchStream = searchBar.searchTextField.textPublisher
.prepend(searchBar.text!)
.eraseToAnyPublisher()
let sparsedSearchStream = searchStream
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
@serhiybutz
serhiybutz / code.swift
Created July 21, 2020 16:15
Combine: withLatestFrom, 02
var cancellables = Set<AnyCancellable>()
func setupStreams() {
let searchStream = searchBar.searchTextField.textStream
.prepend(searchBar.text!)
.eraseToAnyPublisher()
let sparsedSearchStream = searchStream
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.eraseToAnyPublisher()
@serhiybutz
serhiybutz / code.swift
Created July 21, 2020 16:11
Combine: withLatestFrom, 01
import UIKit
import Combine
extension UITextField {
var textStream: AnyPublisher<String?, Never> {
NotificationCenter.default
.publisher(
for: UITextField.textDidChangeNotification,
object: self)
.map(\.object)