-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathEvent.swift
69 lines (61 loc) · 1.88 KB
/
Event.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Event.swift
// CombineExt
//
// Created by Shai Mishali on 13/03/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//
#if canImport(Combine)
/// Represents a Combine Event
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public enum Event<Output, Failure: Swift.Error> {
case value(Output)
case failure(Failure)
case finished
}
// MARK: - Codable Conformance
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Event: Codable where Output: Codable, Failure: Codable {}
// MARK: - Equatable Conformance
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Event: Equatable where Output: Equatable, Failure: Equatable {
static public func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.finished, .finished):
return true
case let (.failure(err1), .failure(err2)):
return err1 == err2
case let (.value(val1), .value(val2)):
return val1 == val2
default:
return false
}
}
}
// MARK: - Friendly Output
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Event: CustomStringConvertible {
public var description: String {
switch self {
case .value(let val):
return "value(\(val))"
case .failure(let err):
return "failure(\(err))"
case .finished:
return "finished"
}
}
}
// MARK: - Event Convertible
/// A protocol representing `Event` convertible types
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public protocol EventConvertible {
associatedtype Output
associatedtype Failure: Swift.Error
var event: Event<Output, Failure> { get }
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Event: EventConvertible {
public var event: Event<Output, Failure> { self }
}
#endif