Skip to content

Instantly share code, notes, and snippets.

View willasrari's full-sized avatar

Will Asrari willasrari

View GitHub Profile
@woodycatliu
woodycatliu / BindingValueSubject.swift
Last active July 2, 2023 11:05
BindingValueSubject provides the ability to callback and update the source's new value.
//
// BindingValueSubject.swift
//
//
// Created by Woody Liu on 2023/7/2.
//
import Combine
@frozen public struct BindingValue<Output>: Publisher {
@daniyalyousuf07
daniyalyousuf07 / CommentViewModel.swift
Created February 24, 2023 09:20
A demo of dependency injection - loose coupling - change details without affecting abstraction.
@MainActor
class CommentViewModel: ObservableObject {
let serviceHandler: CommentViewServiceDelegate
let databaseHandler: CommentsDelegate
let mainDispatchQueue: DispatchQueueType
init(serviceHandler: CommentViewServiceDelegate = CommentViewService(),
databaseHandler: CommentsDelegate = DatabaseHandler(),
mainDispatchQueue: DispatchQueueType = DispatchQueue.main) {
@kbazzani
kbazzani / Swift Dependency Injection - Property.swift
Last active March 2, 2023 12:52
A swift dependency injection example using a variable property and generics
import Foundation
protocol SomeProtocol<Thing>
{
associatedtype Thing
func doSomething()
}
struct SomeStruct<Thing>:SomeProtocol
@chunkyguy
chunkyguy / State+Binding.swift
Created December 27, 2022 20:40
Poor man's State and Binding without SwiftUI
@dynamicMemberLookup
class Variable<T> {
var value: T {
get { sub.value }
set { sub.value = newValue }
}
var stream: AnyPublisher<T, Never> {
return sub.eraseToAnyPublisher()
}
@arashkashi
arashkashi / injection.swift
Created November 21, 2022 15:46
a light weight injection code.
// MARK: Demontration of Usage of Injection system
// 1. First step: make the key object, the current value type is the one you are trying to add to injection system.
private struct BaseClassKey: InjectionKey {
static var currentValue: BaseClass = RealSubClass()
}
// 2. How to inject the currect value
func test() {
var dataController = TestDataStructure()
protocol Resolver {
func resolve<T>(_ type: T.Type) -> T
}
class MainResolver: Resolver {
static let shared = MainResolver()
private var dependencies: [Any] = []
@ts95
ts95 / Injected.swift
Created September 26, 2022 10:03
Swift basic singleton-based dependency injection
import Foundation
@propertyWrapper
struct Injected<T> {
private let keyPath: WritableKeyPath<InjectedValues, T>
var wrappedValue: T {
get { InjectedValues[keyPath] }
set { InjectedValues[keyPath] = newValue }
}
@dcarmo-tribalscale
dcarmo-tribalscale / DependencyContainerV1.swift
Last active March 3, 2023 22:24
Dependency Injection Blog
// 1
public protocol DIContainer {
func register<DependencyType>(type: DependencyType.Type, dependency: DependencyType)
func resolve<DependencyType>(type: DependencyType.Type) -> DependencyType
}
// 2
public class DependencyContainer {
public static let shared: DIContainer = DependencyContainer()
private var registeredDependencies: [String: Any] = [:]
class HardService: ServiceImplementationProtocol {
static var kind: ServiceContainerKind { .temporary }
unowned let serviceLocator: ServiceLocator
private let someString: String
private let someService: ServiceImplementationProtocol
required init(serviceLocator: ServiceLocator) { fatalError() }
@marcpalmer
marcpalmer / ReceiveAsyncResultsOn.swift
Last active August 29, 2022 02:30
A Combine operator that works like receive(on:) but only hops on the scheduler after the initial subscribe process completes
import Foundation
import Combine
/// Usage example:
///
/// ```
/// source
/// .receiveAsyncResults(on: DispatchQueue.main)
/// .sink {
/// print("Value: \($0)")