Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View fmo91's full-sized avatar

Fernando Martín Ortiz fmo91

View GitHub Profile
@fmo91
fmo91 / Publishers+Create.swift
Last active December 29, 2020 13:43
Very simple but handy Publishers extension to create custom AnyPublisher as in RxSwift's Observable<E>.create method
// Copy and Paste this in your project
import Combine
import Foundation
extension Publishers {
struct PublisherObserver<V, E: Swift.Error> {
private let sendValue: (V) -> Void
private let sendCompletion: (Subscribers.Completion<E>) -> Void
init(
@fmo91
fmo91 / EventStream.swift
Created December 23, 2020 18:41
EventStream implementation. It's something similar to Observable<Element> from RxSwift but just as a proof of concept.
import Foundation
public final class EventStream<Element> {
private(set) var handlers: [StreamSubscription: StreamHandler<Element>] = [:]
private var eventsQueue: [Event<Element>] = []
private var isEnabled: Bool = true
private init() {}
public static func create(_ handler: @escaping (StreamHandler<Element>) -> Void) -> EventStream<Element> {
@fmo91
fmo91 / AsyncOperation.swift
Created October 25, 2020 23:47
Basic support for async operations in Swift based on Combine, using generics, protocol oriented programming and type erasure.
//
// AsyncOperation.swift
// SwiftUIPoC
//
// Created by Fernando Martín Ortiz on 25/10/2020.
//
import Combine
protocol AsyncOperation {
import SwiftUI
import Combine
struct CreateToDoScreen: View {
@StateObject var viewModel = CreateToDoViewModel()
var body: some View {
VStack {
TextField("Title", text: $viewModel.title)
Toggle("Completed", isOn: $viewModel.completed)
@fmo91
fmo91 / MutateOperator.swift
Created July 6, 2020 00:15
Value types mutation on copy operator
import Foundation
precedencegroup MutationPrecedence {
associativity: left
}
infix operator +> : MutationPrecedence
func +> <A> (v: A, f: (inout A) -> Void) -> A {
var copy = v
@fmo91
fmo91 / FunctionalNetworkingLayer.swift
Last active July 19, 2020 00:24
Toy Networking Layer with functional operators
import Foundation
import PlaygroundSupport
enum NetworkError: Error {
case generic
}
precedencegroup PipeAssociativity {
associativity: left
}
@fmo91
fmo91 / MinimalPromise.swift
Last active March 9, 2021 07:28
The minimum viable promise in Swift
import Foundation
struct Promise<T> {
typealias ResultType = Result<T, Error>
typealias ResultObserver = (ResultType) -> Void
typealias CreatorFunction = (@escaping ResultObserver) -> Void
private let creatorFunction: CreatorFunction
init(creatorFunction: @escaping CreatorFunction) {
self.creatorFunction = creatorFunction
@fmo91
fmo91 / index.html
Last active April 2, 2020 15:47
test-dumbergist
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<!--
Dumber Gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
// We create a protocol for formatting a name
protocol NameFormatterType {
func format(name: String) -> String
}
// A concrete implementation adds a prefix "Sir" to the given name.
struct SirFormatter: NameFormatterType {
func format(name: String) -> String {
"Sir \(name)"
}
enum Dependencies {
// ...
@propertyWrapper
struct Inject<T> {
private let dependencyName: Name
private let container: Container
var wrappedValue: T {
get { container.resolve(dependencyName) }
}
init(_ dependencyName: Name = .default, on container: Container = .default) {