75 lines
1.7 KiB
Swift
75 lines
1.7 KiB
Swift
//
|
|
// Reactive.swift
|
|
// RxSwift
|
|
//
|
|
// Created by Yury Korolev on 5/2/16.
|
|
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
|
|
//
|
|
|
|
/**
|
|
Use `Reactive` proxy as customization point for constrained protocol extensions.
|
|
|
|
General pattern would be:
|
|
|
|
// 1. Extend Reactive protocol with constrain on Base
|
|
// Read as: Reactive Extension where Base is a SomeType
|
|
extension Reactive where Base: SomeType {
|
|
// 2. Put any specific reactive extension for SomeType here
|
|
}
|
|
|
|
With this approach we can have more specialized methods and properties using
|
|
`Base` and not just specialized on common base type.
|
|
|
|
*/
|
|
|
|
public struct Reactive<Base> {
|
|
/// Base object to extend.
|
|
public let base: Base
|
|
|
|
/// Creates extensions with base object.
|
|
///
|
|
/// - parameter base: Base object.
|
|
public init(_ base: Base) {
|
|
self.base = base
|
|
}
|
|
}
|
|
|
|
/// A type that has reactive extensions.
|
|
public protocol ReactiveCompatible {
|
|
/// Extended type
|
|
associatedtype CompatibleType
|
|
|
|
/// Reactive extensions.
|
|
static var rx: Reactive<CompatibleType>.Type { get set }
|
|
|
|
/// Reactive extensions.
|
|
var rx: Reactive<CompatibleType> { get set }
|
|
}
|
|
|
|
extension ReactiveCompatible {
|
|
/// Reactive extensions.
|
|
public static var rx: Reactive<Self>.Type {
|
|
get {
|
|
return Reactive<Self>.self
|
|
}
|
|
set {
|
|
// this enables using Reactive to "mutate" base type
|
|
}
|
|
}
|
|
|
|
/// Reactive extensions.
|
|
public var rx: Reactive<Self> {
|
|
get {
|
|
return Reactive(self)
|
|
}
|
|
set {
|
|
// this enables using Reactive to "mutate" base object
|
|
}
|
|
}
|
|
}
|
|
|
|
import class Foundation.NSObject
|
|
|
|
/// Extend NSObject with `rx` proxy.
|
|
extension NSObject: ReactiveCompatible { }
|