Skip to content

Commit 9260783

Browse files
committed
feat(FunctionalClosures): API improvements
- Added `Configurator` support for FunctionalHandler and FunctionalDataSource - Tests cleanup
1 parent c5e9a8e commit 9260783

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Sources/FunctionalClosures/FunctionalDataSource.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public struct FunctionalDataSource<Input, Output> {
2525

2626
public var wrappedValue: Container = .init()
2727

28-
public var projectedValue: ((Input) -> Output)? { wrappedValue.action }
28+
public var projectedValue: ((Input) -> Output)? {
29+
get { wrappedValue.action }
30+
set { wrappedValue.action = newValue }
31+
}
2932

3033
public func callAsFunction(_ input: Input) -> Output? {
3134
projectedValue?(input)

Sources/FunctionalClosures/FunctionalHandler.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public struct FunctionalHandler<Input> {
5656

5757
public var wrappedValue: Container = .init()
5858

59-
public var projectedValue: ((Input) -> Void)? { wrappedValue.action }
59+
public var projectedValue: ((Input) -> Void)? {
60+
get { wrappedValue.action }
61+
set { wrappedValue.action = newValue }
62+
}
6063

6164
public func callAsFunction(_ input: Input) {
6265
projectedValue?(input)

Tests/DeclarativeConfigurationTests/FunctionalClosuresTests.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import XCTest
33

44
final class FunctionalClosuresTests: XCTestCase {
55
func testHandler() {
6-
class Object {
6+
class Object: NSObject {
77
@FunctionalDataSource<(Int, Int), Int>
88
var sum = .init { $0 + $1 } // You can specify default handler
99

1010
@FunctionalHandler<Int>
1111
var handleSumResult // or leave it nil
1212

13-
func sum(_ a: Int, _ b: Int) -> Int? {
14-
let result = $sum?((a, b))
13+
func sumOf(_ a: Int, _ b: Int) -> Int? {
14+
let result = _sum((a, b))
1515
if let result = result {
1616
_handleSumResult(result)
1717
}
@@ -20,24 +20,30 @@ final class FunctionalClosuresTests: XCTestCase {
2020
}
2121

2222
let object = Object()
23-
let expectation = XCTestExpectation()
2423
let a = 10
2524
let b = 20
2625
let c = 30
26+
var storageForHandler: Int? = nil
2727

2828
object.handleSumResult { int in
29+
storageForHandler = int
2930
XCTAssertEqual(int, c)
3031
}
3132

32-
XCTAssertEqual(object.sum(a, b), c)
33-
34-
object.handleSumResult(action: nil)
3533
// object._handleSumResult(0) // private
34+
object.$handleSumResult!(c)
35+
XCTAssertEqual(storageForHandler, c)
36+
storageForHandler = nil
3637

37-
XCTAssertEqual(object.$sum!((1,1)), 2)
38+
XCTAssertEqual(object.sumOf(a, b), c)
39+
XCTAssertEqual(storageForHandler, c)
40+
storageForHandler = nil
3841

39-
XCTAssertEqual(object.sum(a, b), c)
42+
// object._sum(a, b) // private
43+
XCTAssertEqual(object.$sum!((a,b)), c)
44+
XCTAssertEqual(storageForHandler, nil)
4045

41-
XCTAssertTrue(expectation.expectedFulfillmentCount == 1)
46+
object.handleSumResult(action: nil)
47+
XCTAssertEqual(storageForHandler, nil)
4248
}
4349
}

0 commit comments

Comments
 (0)