-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAction.swift
80 lines (69 loc) · 1.93 KB
/
Action.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
70
71
72
73
74
75
76
77
78
79
80
import Combine
public enum EitherAction<AsyncAction, SyncAction> {
public typealias Async = AsyncAction
public typealias Sync = SyncAction
public typealias Publisher = AnyPublisher<Self, Never>
case async([Async])
case sync([Sync])
}
public extension EitherAction {
var asyncActions: [Async]? {
switch self {
case let .async(action):
return action
case .sync:
return nil
}
}
var syncActions: [Sync]? {
switch self {
case let .sync(action):
return action
case .async:
return nil
}
}
var caseName: String {
switch self {
case .async:
return "async"
case .sync:
return "sync"
}
}
var allActions: [Any] {
switch self {
case let .async(actions):
return actions
case let .sync(actions):
return actions
}
}
}
public extension EitherAction {
func map<NewAsyncAction>(async transform: (Async) -> NewAsyncAction) -> EitherAction<NewAsyncAction, SyncAction> {
map(async: transform, sync: { $0 })
}
func map<NewSyncAction>(sync transform: (Sync) -> NewSyncAction) -> EitherAction<AsyncAction, NewSyncAction> {
map(async: { $0 }, sync: transform)
}
func map<NewAsyncAction, NewSyncAction>(
async asyncTransform: (Async) -> NewAsyncAction,
sync syncTransform: (Sync) -> NewSyncAction
) -> EitherAction<NewAsyncAction, NewSyncAction> {
switch self {
case let .async(actions):
return .async(actions.map(asyncTransform))
case let .sync(actions):
return .sync(actions.map(syncTransform))
}
}
}
public extension EitherAction {
static func sync(_ actions: Sync...) -> Self {
.sync(actions)
}
static func async(_ actions: Async...) -> Self {
.async(actions)
}
}