This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Effect { | |
associatedtype Action | |
static var none: Self { get } | |
static func batch(_ effects: [Self]) -> Self | |
// ! What should we return? Effect has associated types. Effect<B>, Self<B> are not possible. | |
// Feels like a usecase for type-erasure but didnt get me anywhere (+ I feel like a type-erased AnyEffect defeats the purpose of tagless) | |
//func map<B>(_ transform: @escaping (Action) -> B) -> ??? | |
// this works but is too general, we want to keep OtherEffect fixed to the same implementation as Self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ReactiveSwift | |
import enum Result.Result | |
import Alamofire | |
typealias Never = NoError | |
//This file alone wont build, its just for reading ;) | |
struct Tagged<Tag, RawValue> { | |
var rawValue: RawValue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
private func get(keyPaths: [AnyKeyPath], from object: Any) -> Any { | |
return keyPaths.reduce(object) { acc, kp in acc[keyPath: kp] } | |
} | |
extension Collection where Element: Equatable { //might not work for unordered collections | |
func hasPrefix(_ prefix: Self) -> Bool { | |
guard let firstFromPrefix = prefix.first else { return true } | |
guard let firstFromSelf = self.first else { return false } | |
return firstFromPrefix == firstFromSelf && dropFirst().hasPrefix(prefix.dropFirst()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Functor { | |
associatedtype Tag | |
associatedtype E //Element or _Element clashes on Dictionary :( | |
} | |
struct Functoring<F: Functor> { | |
let _map: (F, (F.E) -> Any) -> Any | |
func map<FB: Functor>(_ functor: F, transform: (F.E) -> FB.E) -> FB where F.Tag == FB.Tag { | |
return _map(functor, transform) as! FB | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension Binding { | |
init(global keyPath: KeyPath<Bindings, Binding>) { | |
self = bindings[keyPath: keyPath] | |
} | |
} | |
struct Bindings { | |
@Binding var anotherOne: Bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
struct OnScroll: ViewModifier { | |
@Binding var offset: CGFloat | |
//we can have a version with a closure instead of the binding, but that triggers an infinite loop if content depends on the same Store | |
// var onOffset: (CGFloat) -> () | |
func body(content: Content) -> some View { | |
return VStack { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
@propertyWrapper struct MyObjectBinding<BindableObjectType: BindableObject> : DynamicViewProperty where BindableObjectType.PublisherType == PassthroughSubject<(), Never> { | |
init(initialValue: BindableObjectType) { | |
delegateValue = Wrapper(bindableObject: initialValue) | |
} | |
@dynamicMemberLookup struct Wrapper { | |
var bindableObject: BindableObjectType |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
final class Store<State>: BindableObject { | |
let didChange = PassthroughSubject<(), Never>() | |
init(state: State) { | |
self.state = state | |
} | |
var state: State { | |
didSet { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WIP PoC of passing animated Binding transactions from a View to the Store. | |
// This works, but is probably a dead end since it will be better to have the Store decide on animations and impose them top-down | |
// I still havent played with interactive gestures tho, so this could come in handy someday | |
import SwiftUI | |
import Combine | |
final class P { | |
let store: Store<ExampleApp.State, ExampleApp.Action> = ObjectBinding(initialValue: ExampleApp.theStore) | |
func dispatch(_ action: ExampleApp.Action, transaction: Transaction? = nil) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I wrote this and then decided its an overengineered piece of bullshit code. Just deleting it makes me sad so let it forever take up space on Github | |
// a property that flips back to its default value when it is read | |
// TODO: does this have proper value semantics with the class inside? And do we even care? | |
@propertyWrapper struct ReadOnce<Value> { | |
init(initialValue: Value) { | |
defaultValue = initialValue | |
box = ValueBox(value: initialValue) | |
} | |
private let defaultValue: Value |
OlderNewer