Add an Immutable Plugin protocol (#4)
* Add ab Immutable Plugin protocol * Add missing immutable keyPath * Clean up usage and make test for immutable plugin
This commit is contained in:
parent
fc8c7bf385
commit
2663e6f541
|
@ -0,0 +1,24 @@
|
||||||
|
public protocol ImmutablePlugin: Plugin where Output == Void {
|
||||||
|
/// The input value that the plugin will handle.
|
||||||
|
associatedtype Value
|
||||||
|
|
||||||
|
/// Handles the input value.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The input value that the plugin will handle.
|
||||||
|
/// - Throws: Any errors that occur during handling of the input value.
|
||||||
|
func handle(value: Value) async throws
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ImmutablePlugin where Value == Input {
|
||||||
|
/// A keypath that provides access to the `immutable` property of the `Source`.
|
||||||
|
public var keyPath: WritableKeyPath<Source, ()> {
|
||||||
|
get { \.immutable }
|
||||||
|
set { }
|
||||||
|
}
|
||||||
|
|
||||||
|
public func handle(value: Value, output: inout ()) async throws {
|
||||||
|
try await handle(value: value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -86,4 +86,42 @@ final class PluginTests: XCTestCase {
|
||||||
let token = try XCTUnwrap(urlRequest.request.allHTTPHeaderFields)["auth"]
|
let token = try XCTUnwrap(urlRequest.request.allHTTPHeaderFields)["auth"]
|
||||||
XCTAssertEqual(token, "token")
|
XCTAssertEqual(token, "token")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testImmutablePlugin() async throws {
|
||||||
|
class MockService: Pluginable {
|
||||||
|
var plugins: [any Plugin] = []
|
||||||
|
}
|
||||||
|
|
||||||
|
class CountPlugin: ImmutablePlugin {
|
||||||
|
typealias Source = MockService
|
||||||
|
|
||||||
|
static let shared = CountPlugin()
|
||||||
|
|
||||||
|
var count: Int = 0
|
||||||
|
|
||||||
|
private init() {}
|
||||||
|
|
||||||
|
func handle(value: Void) async throws {
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let service = MockService()
|
||||||
|
|
||||||
|
XCTAssertEqual(service.pluginTypes, [])
|
||||||
|
|
||||||
|
service.register(
|
||||||
|
plugin: CountPlugin.shared
|
||||||
|
)
|
||||||
|
|
||||||
|
XCTAssertEqual(service.pluginTypes, ["(input: (), output: ())"])
|
||||||
|
|
||||||
|
try await service.handle(value: "Woot")
|
||||||
|
|
||||||
|
XCTAssertEqual(CountPlugin.shared.count, 0)
|
||||||
|
|
||||||
|
try await service.handle()
|
||||||
|
|
||||||
|
XCTAssertEqual(CountPlugin.shared.count, 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue