Skip to content

Commit 64db5d4

Browse files
committed
feat: API improvements
1 parent be6ee99 commit 64db5d4

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

Sources/FunctionalClosures/DataSource.swift

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,41 @@
22
///
33
/// Provides a public API to set internal closure-based datasource with a functional API
44
@propertyWrapper
5-
public struct DataSource<Input, Output> {
5+
public class DataSource<Input, Output> {
6+
public struct Container {
7+
internal var action: ((Input) -> Output)
8+
9+
public init(handler: @escaping (Input) -> Output) {
10+
self.action = handler
11+
}
12+
13+
public mutating func callAsFunction(perform action: @escaping (Input) -> Output) {
14+
self.action = action
15+
}
16+
}
17+
18+
public init(wrappedValue: Container) {
19+
self.wrappedValue = wrappedValue
20+
}
21+
22+
public var wrappedValue: Container
23+
24+
public var projectedValue: (Input) -> Output {
25+
get { wrappedValue.action }
26+
set { wrappedValue.action = newValue }
27+
}
28+
29+
public func callAsFunction(_ input: Input) -> Output? {
30+
projectedValue(input)
31+
}
32+
}
33+
34+
35+
/// A wrapper for clusure-based interaction between objects
36+
///
37+
/// Provides a public API to set internal closure-based datasource with a functional API
38+
@propertyWrapper
39+
public class OptionalDataSource<Input, Output> {
640
public struct Container {
741
internal var action: ((Input) -> Output)?
842

@@ -12,7 +46,7 @@ public struct DataSource<Input, Output> {
1246
self.action = handler
1347
}
1448

15-
public mutating func callAsFunction(action: ((Input) -> Output)?) {
49+
public mutating func callAsFunction(perform action: ((Input) -> Output)?) {
1650
self.action = action
1751
}
1852
}

Sources/FunctionalClosures/Handler.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
///
44
/// Provides a public API to set internal closure-based hanlder or delegate with a functional API
55
@propertyWrapper
6-
public struct Handler<Input> {
6+
public class Handler<Input> {
77
public struct Container {
88
internal var action: ((Input) -> Void)?
99

@@ -23,7 +23,7 @@ public struct Handler<Input> {
2323
Feel free to discuss the API here \
2424
https://github.com/MakeupStudio/swift-declarative-configuration/issues/1
2525
""")
26-
public mutating func callAsFunction(_ behaviour: Behaviour, action: ((Input) -> Void)?) {
26+
public mutating func callAsFunction(_ behaviour: Behaviour, perform action: ((Input) -> Void)?) {
2727
switch behaviour {
2828
case .resetting:
2929
self.action = action
@@ -73,7 +73,7 @@ public struct Handler<Input> {
7373
///
7474
/// Provides a public API to set internal closure-based hanlder or delegate with a functional API
7575
@propertyWrapper
76-
public struct Handler2<T0, T1> {
76+
public class Handler2<T0, T1> {
7777
public struct Container {
7878
internal var action: ((T0, T1) -> Void)?
7979

@@ -93,7 +93,7 @@ public struct Handler2<T0, T1> {
9393
Feel free to discuss the API here \
9494
https://github.com/MakeupStudio/swift-declarative-configuration/issues/1
9595
""")
96-
public mutating func callAsFunction(_ behaviour: Behaviour, action: ((T0, T1) -> Void)?) {
96+
public mutating func callAsFunction(_ behaviour: Behaviour, perform action: ((T0, T1) -> Void)?) {
9797
switch behaviour {
9898
case .resetting:
9999
self.action = action
@@ -141,7 +141,7 @@ public struct Handler2<T0, T1> {
141141
///
142142
/// Provides a public API to set internal closure-based hanlder or delegate with a functional API
143143
@propertyWrapper
144-
public struct Handler3<T0, T1, T2> {
144+
public class Handler3<T0, T1, T2> {
145145
public struct Container {
146146
internal var action: ((T0, T1, T2) -> Void)?
147147

@@ -161,7 +161,7 @@ public struct Handler3<T0, T1, T2> {
161161
Feel free to discuss the API here \
162162
https://github.com/MakeupStudio/swift-declarative-configuration/issues/1
163163
""")
164-
public mutating func callAsFunction(_ behaviour: Behaviour, action: ((T0, T1, T2) -> Void)?) {
164+
public mutating func callAsFunction(_ behaviour: Behaviour, perform action: ((T0, T1, T2) -> Void)?) {
165165
switch behaviour {
166166
case .resetting:
167167
self.action = action
@@ -209,7 +209,7 @@ public struct Handler3<T0, T1, T2> {
209209
///
210210
/// Provides a public API to set internal closure-based hanlder or delegate with a functional API
211211
@propertyWrapper
212-
public struct Handler4<T0, T1, T2, T3> {
212+
public class Handler4<T0, T1, T2, T3> {
213213
public struct Container {
214214
internal var action: ((T0, T1, T2, T3) -> Void)?
215215

@@ -229,7 +229,7 @@ public struct Handler4<T0, T1, T2, T3> {
229229
Feel free to discuss the API here \
230230
https://github.com/MakeupStudio/swift-declarative-configuration/issues/1
231231
""")
232-
public mutating func callAsFunction(_ behaviour: Behaviour, action: ((T0, T1, T2, T3) -> Void)?) {
232+
public mutating func callAsFunction(_ behaviour: Behaviour, perform action: ((T0, T1, T2, T3) -> Void)?) {
233233
switch behaviour {
234234
case .resetting:
235235
self.action = action
@@ -277,7 +277,7 @@ public struct Handler4<T0, T1, T2, T3> {
277277
///
278278
/// Provides a public API to set internal closure-based hanlder or delegate with a functional API
279279
@propertyWrapper
280-
public struct Handler5<T0, T1, T2, T3, T4> {
280+
public class Handler5<T0, T1, T2, T3, T4> {
281281
public struct Container {
282282
internal var action: ((T0, T1, T2, T3, T4) -> Void)?
283283

@@ -297,7 +297,7 @@ public struct Handler5<T0, T1, T2, T3, T4> {
297297
Feel free to discuss the API here \
298298
https://github.com/MakeupStudio/swift-declarative-configuration/issues/1
299299
""")
300-
public mutating func callAsFunction(_ behaviour: Behaviour, action: ((T0, T1, T2, T3, T4) -> Void)?) {
300+
public mutating func callAsFunction(_ behaviour: Behaviour, perform action: ((T0, T1, T2, T3, T4) -> Void)?) {
301301
switch behaviour {
302302
case .resetting:
303303
self.action = action

Tests/DeclarativeConfigurationTests/FunctionalClosuresTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class FunctionalClosuresTests: XCTestCase {
4141
storageForHandler = nil
4242

4343
// object._sum(a, b) // private
44-
XCTAssertEqual(object.$sum!((a,b)), c)
44+
XCTAssertEqual(object.$sum((a,b)), c)
4545
XCTAssertEqual(storageForHandler, nil)
4646

4747
object.handleSumResult(action: nil)

0 commit comments

Comments
 (0)