From 158b7063ca242002312f870f8deea219b6b7f051 Mon Sep 17 00:00:00 2001 From: Joseph Duffy Date: Thu, 16 May 2019 17:00:11 +0100 Subject: [PATCH] Move update listeners to PartialBuilder --- Source/Partial.swift | 5 --- Source/PartialBuilder.swift | 77 ++++++++++++++++++++++++++++++++- Source/PartialConvertible.swift | 2 - 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/Source/Partial.swift b/Source/Partial.swift index f1d96ed3..c9545da1 100644 --- a/Source/Partial.swift +++ b/Source/Partial.swift @@ -11,8 +11,6 @@ public struct Partial: CustomStringConvertible, CustomDebugStringConver internal var backingValue: Wrapped? = nil - internal var updateHandlers: [PartialKeyPath: [(Any?) -> Void]] = [:] - public init(backingValue: Wrapped? = nil) { self.backingValue = backingValue } @@ -47,7 +45,6 @@ public struct Partial: CustomStringConvertible, CustomDebugStringConver public mutating func set(value: Value, for key: KeyPath) { values[key] = value - updateHandlers[key]?.forEach { $0(value as Any) } } public mutating func set(value: Value?, for key: KeyPath) { @@ -58,12 +55,10 @@ public struct Partial: 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(for key: KeyPath) { values.removeValue(forKey: key) - updateHandlers[key]?.forEach { $0(nil) } } public subscript(key: KeyPath) -> Value? { diff --git a/Source/PartialBuilder.swift b/Source/PartialBuilder.swift index 95bcd365..3aefa376 100644 --- a/Source/PartialBuilder.swift +++ b/Source/PartialBuilder.swift @@ -1,6 +1,8 @@ -public class PartialBuilder { +public final class PartialBuilder { - public var partial: Partial + public private(set) var partial: Partial + + private var updateHandlers: [PartialKeyPath: [(Any?) -> Void]] = [:] public init(partial: Partial = Partial()) { self.partial = partial @@ -10,6 +12,77 @@ public class PartialBuilder { partial = Partial(backingValue: backingValue) } + public func value(for key: KeyPath) throws -> Value { + return try partial.value(for: key) + } + + public func value(for key: KeyPath) throws -> Value { + return try partial.value(for: key) + } + + public func value(for key: KeyPath) throws -> Value where Value: PartialConvertible { + return try partial.value(for: key) + } + + public func value(for key: KeyPath) throws -> Value where Value: PartialConvertible { + return try partial.value(for: key) + } + + public func partialValue(for key: KeyPath) throws -> Partial where Value: PartialConvertible { + return try partial.partialValue(for: key) + } + + public func partialValue(for key: KeyPath) throws -> Partial where Value: PartialConvertible { + return try partial.partialValue(for: key) + } + + public func set(value: Value, for key: KeyPath) { + partial.set(value: value, for: key) + updateHandlers[key]?.forEach { $0(value as Any) } + } + + public func set(value: Value?, for key: KeyPath) { + partial.set(value: value, for: key) + updateHandlers[key]?.forEach { $0(value as Any) } + } + + public func set(value: Partial, for key: KeyPath) where Value: PartialConvertible { + partial.set(value: value, for: key) + updateHandlers[key]?.forEach { $0(value as Any) } + } + + public func removeValue(for key: KeyPath) { + partial.removeValue(for: key) + updateHandlers[key]?.forEach({ $0(nil) }) + } + + public subscript(key: KeyPath) -> Value? { + get { + return partial[key] + } + set { + partial.removeValue(for: key) + } + } + + public subscript(key: KeyPath) -> Value? { + get { + return partial[key] + } + set { + set(value: newValue, for: key) + } + } + + public subscript(key: KeyPath) -> Partial where Value: PartialConvertible { + get { + return partial[key] + } + set { + set(value: newValue, for: key) + } + } + } extension PartialBuilder where Wrapped: PartialConvertible { diff --git a/Source/PartialConvertible.swift b/Source/PartialConvertible.swift index 3351e09c..a2500dcf 100644 --- a/Source/PartialConvertible.swift +++ b/Source/PartialConvertible.swift @@ -80,12 +80,10 @@ extension Partial { public mutating func set(value: Partial, for key: KeyPath) where Value: PartialConvertible { values[key] = value - updateHandlers[key]?.forEach { $0(value as Any) } } public mutating func set(value: Partial, for key: KeyPath) where Value: PartialConvertible { values[key] = value - updateHandlers[key]?.forEach { $0(value as Any) } } public subscript(key: KeyPath) -> Partial where Value: PartialConvertible {