Skip to content

Commit

Permalink
Move update listeners to PartialBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephDuffy committed May 17, 2019
1 parent 326262f commit 158b706
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
5 changes: 0 additions & 5 deletions Source/Partial.swift
Expand Up @@ -11,8 +11,6 @@ public struct Partial<Wrapped>: CustomStringConvertible, CustomDebugStringConver

internal var backingValue: Wrapped? = nil

internal var updateHandlers: [PartialKeyPath<Wrapped>: [(Any?) -> Void]] = [:]

public init(backingValue: Wrapped? = nil) {
self.backingValue = backingValue
}
Expand Down Expand Up @@ -47,7 +45,6 @@ public struct Partial<Wrapped>: CustomStringConvertible, CustomDebugStringConver

public mutating func set<Value>(value: Value, for key: KeyPath<Wrapped, Value>) {
values[key] = value
updateHandlers[key]?.forEach { $0(value as Any) }
}

public mutating func set<Value>(value: Value?, for key: KeyPath<Wrapped, Value?>) {
Expand All @@ -58,12 +55,10 @@ public struct Partial<Wrapped>: CustomStringConvertible, CustomDebugStringConver
a `backingValue` is set and a key is explicitly set to `nil`
*/
values.updateValue(value, forKey: key)
updateHandlers[key]?.forEach { $0(value as Any) }
}

public mutating func removeValue<Value>(for key: KeyPath<Wrapped, Value>) {
values.removeValue(forKey: key)
updateHandlers[key]?.forEach { $0(nil) }
}

public subscript<Value>(key: KeyPath<Wrapped, Value>) -> Value? {
Expand Down
77 changes: 75 additions & 2 deletions Source/PartialBuilder.swift
@@ -1,6 +1,8 @@
public class PartialBuilder<Wrapped> {
public final class PartialBuilder<Wrapped> {

public var partial: Partial<Wrapped>
public private(set) var partial: Partial<Wrapped>

private var updateHandlers: [PartialKeyPath<Wrapped>: [(Any?) -> Void]] = [:]

public init(partial: Partial<Wrapped> = Partial<Wrapped>()) {
self.partial = partial
Expand All @@ -10,6 +12,77 @@ public class PartialBuilder<Wrapped> {
partial = Partial(backingValue: backingValue)
}

public func value<Value>(for key: KeyPath<Wrapped, Value>) throws -> Value {
return try partial.value(for: key)
}

public func value<Value>(for key: KeyPath<Wrapped, Value?>) throws -> Value {
return try partial.value(for: key)
}

public func value<Value>(for key: KeyPath<Wrapped, Value>) throws -> Value where Value: PartialConvertible {
return try partial.value(for: key)
}

public func value<Value>(for key: KeyPath<Wrapped, Value?>) throws -> Value where Value: PartialConvertible {
return try partial.value(for: key)
}

public func partialValue<Value>(for key: KeyPath<Wrapped, Value>) throws -> Partial<Value> where Value: PartialConvertible {
return try partial.partialValue(for: key)
}

public func partialValue<Value>(for key: KeyPath<Wrapped, Value?>) throws -> Partial<Value> where Value: PartialConvertible {
return try partial.partialValue(for: key)
}

public func set<Value>(value: Value, for key: KeyPath<Wrapped, Value>) {
partial.set(value: value, for: key)
updateHandlers[key]?.forEach { $0(value as Any) }
}

public func set<Value>(value: Value?, for key: KeyPath<Wrapped, Value?>) {
partial.set(value: value, for: key)
updateHandlers[key]?.forEach { $0(value as Any) }
}

public func set<Value>(value: Partial<Value>, for key: KeyPath<Wrapped, Value>) where Value: PartialConvertible {
partial.set(value: value, for: key)
updateHandlers[key]?.forEach { $0(value as Any) }
}

public func removeValue<Value>(for key: KeyPath<Wrapped, Value>) {
partial.removeValue(for: key)
updateHandlers[key]?.forEach({ $0(nil) })
}

public subscript<Value>(key: KeyPath<Wrapped, Value>) -> Value? {
get {
return partial[key]
}
set {
partial.removeValue(for: key)
}
}

public subscript<Value>(key: KeyPath<Wrapped, Value?>) -> Value? {
get {
return partial[key]
}
set {
set(value: newValue, for: key)
}
}

public subscript<Value>(key: KeyPath<Wrapped, Value>) -> Partial<Value> where Value: PartialConvertible {
get {
return partial[key]
}
set {
set(value: newValue, for: key)
}
}

}

extension PartialBuilder where Wrapped: PartialConvertible {
Expand Down
2 changes: 0 additions & 2 deletions Source/PartialConvertible.swift
Expand Up @@ -80,12 +80,10 @@ extension Partial {

public mutating func set<Value>(value: Partial<Value>, for key: KeyPath<Wrapped, Value>) where Value: PartialConvertible {
values[key] = value
updateHandlers[key]?.forEach { $0(value as Any) }
}

public mutating func set<Value>(value: Partial<Value>, for key: KeyPath<Wrapped, Value?>) where Value: PartialConvertible {
values[key] = value
updateHandlers[key]?.forEach { $0(value as Any) }
}

public subscript<Value>(key: KeyPath<Wrapped, Value>) -> Partial<Value> where Value: PartialConvertible {
Expand Down

0 comments on commit 158b706

Please sign in to comment.