This file contains 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 | |
// MARK: - (1) Original app blob scenario: | |
/// We have the app blob with original code. It contains a builder to build a UI component, which contains a UIView: | |
/// _Note: We use enums just for namespaces._ | |
enum AppBlob { | |
/// Some class that needs to be run on Main thread: | |
struct UIView { | |
init() { |
This file contains 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 | |
// MARK: - (1) Original app blob scenario: | |
/// We have the app blob with original code: | |
/// _Note: We use enums just for namespaces._ | |
enum AppBlob { | |
/// Some sigleton (global state): | |
static var someSingleton: String? | |
/// Some view: |
This file contains 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
/// Traffic consumer account. | |
struct TrafficAccount { | |
// MARK: - Properties (State) | |
private let balance = Property<Double>(value: 0) // remaining money | |
private let traffic = Property<Double>(value: 0) // traffic consumed | |
private let sharedManager = SharedManager() | |
// MARK: - Queries | |
public var currentBalance: Double { |
This file contains 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
/// Traffic consumer account. | |
final class TrafficAccount { | |
// MARK: - Properties (State) | |
private var balance: Double = 0 // remaining money | |
private var traffic: Double = 0 // traffic consumed | |
// MARK: - Queries | |
public var currentBalance: Double { balance } | |
public var currentTraffic: Double { traffic } | |
public var summary: (balance: Double, traffic: Double) { (balance: balance, traffic: traffic) } |
This file contains 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
public final class SharedManager { | |
private var registry = Registry() | |
private let mutex = NSLock() | |
internal func internalBorrow(_ props: [AnyObject], accessBlock: () -> Void) { | |
let active = activateBorrowingFor(props) | |
defer { | |
deactivateBorrowing(active) | |
active.revoke() | |
} | |
accessBlock() |
This file contains 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
final class Borrowing { | |
private let props: [AnyObject] | |
private let revokeCond = NSCondition() | |
private var isRevoked = false | |
init(_ props: [AnyObject]) { | |
self.props = props | |
} | |
func wait() { | |
revokeCond.lock() | |
defer { revokeCond.unlock() } |
This file contains 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
extension SharedManager { | |
public func borrow<A>(_ a: Property<A>, | |
accessBlock: (inout Accessor<A>) -> Void) | |
{ | |
var _a = Accessor(a) | |
internalBorrow([a]) { | |
accessBlock(&_a) | |
} | |
} | |
public func borrow<A, B>(_ a: Property<A>, |
This file contains 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
public struct Accessor<T> { | |
private let prop: Property<T> | |
internal init(_ p: Property<T>) { | |
self.prop = p | |
} | |
public var value: T { | |
get { prop.value } | |
set { prop.value = newValue } | |
} | |
} |
This file contains 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
public final class Property<T> { | |
internal var value: T | |
public init(value: T) { | |
self.value = value | |
} | |
} |
This file contains 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
class Foo { | |
let sharedCounter = Shared(value: 0) | |
let sharedName = Shared(value: "") | |
let sm = SharedManager() | |
func reset() { | |
sm.borrow(sharedCounter, sharedName) { sharedCounter, sharedName in | |
sharedCounter.value = 0 | |
sharedName.value = "" |
NewerOlder