-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathStatsPeriodAsyncOperation.swift
93 lines (78 loc) · 2.97 KB
/
StatsPeriodAsyncOperation.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
81
82
83
84
85
86
87
88
89
90
91
92
93
import WordPressShared
final class StatsPeriodAsyncOperation<TimeStatsType: StatsTimeIntervalData>: AsyncOperation, @unchecked Sendable {
typealias StatsPeriodCompletion = (TimeStatsType?, Error?) -> Void
private weak var service: StatsServiceRemoteV2?
private let period: StatsPeriodUnit
private let unit: StatsPeriodUnit?
private let date: Date
private let limit: Int
private var completion: StatsPeriodCompletion
init(
service: StatsServiceRemoteV2,
for period: StatsPeriodUnit,
unit: StatsPeriodUnit? = nil,
date: Date,
limit: Int = 10,
completion: @escaping StatsPeriodCompletion
) {
self.service = service
self.period = period
self.unit = unit
self.date = date
self.limit = limit
self.completion = completion
}
override func main() {
service?.getData(for: period, unit: unit, endingOn: date, limit: limit) { [unowned self] (type: TimeStatsType?, error: Error?) in
if self.isCancelled {
self.state = .isFinished
return
}
self.completion(type, error)
}
}
}
final class StatsPublishedPostsAsyncOperation: AsyncOperation, @unchecked Sendable {
typealias StatsPeriodCompletion = (StatsPublishedPostsTimeIntervalData?, Error?) -> Void
private weak var service: StatsServiceRemoteV2?
private let period: StatsPeriodUnit
private let date: Date
private let limit: Int
private var completion: StatsPeriodCompletion
init(service: StatsServiceRemoteV2, for period: StatsPeriodUnit, date: Date, limit: Int = 10, completion: @escaping StatsPeriodCompletion) {
self.service = service
self.period = period
self.date = date
self.limit = limit
self.completion = completion
}
override func main() {
service?.getData(for: period, endingOn: date, limit: limit) { [unowned self] (published: StatsPublishedPostsTimeIntervalData?, error: Error?) in
if self.isCancelled {
self.state = .isFinished
return
}
self.completion(published, error)
}
}
}
final class StatsPostDetailAsyncOperation: AsyncOperation, @unchecked Sendable {
typealias StatsPeriodCompletion = (StatsPostDetails?, Error?) -> Void
private weak var service: StatsServiceRemoteV2?
private let postId: Int
private var completion: StatsPeriodCompletion
init(service: StatsServiceRemoteV2, for postId: Int, completion: @escaping StatsPeriodCompletion) {
self.service = service
self.postId = postId
self.completion = completion
}
override func main() {
service?.getDetails(forPostID: postId) { [unowned self] (details: StatsPostDetails?, error: Error?) in
if self.isCancelled {
self.state = .isFinished
return
}
self.completion(details, error)
}
}
}