/
ComicDataSource.swift
66 lines (50 loc) · 2.18 KB
/
ComicDataSource.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
//
// ComicDataSource.swift
// xkcd
//
// Created by Thanh Huy Nguyen on 5/10/22.
//
import Foundation
import Combine
protocol FetchBookmark {}
/// Params for fetching next comics
struct BatchFetchParams {
/// The bookmark for this batch
let bookmark: FetchBookmark
/// How many comics to fetch?
let batchSize: Int
}
/// The result of a batch fetch
struct BatchFetchResult {
/// The comics
let comics: [Comic]
/// The bookmark to be used to fetch the next batch
/// No next batch available if nil
let nextFetchBookmark: FetchBookmark?
}
/// A immutable data source for comics, can be remote or local
protocol ImmutableComicDataSource {
/// Tells the repository to prime itself because it'll be asked for some comics very shortly.
/// For examples, it can tell its data source(s) to warm up HTTP connection(s) to backend API or local file/DB.
func prewarm()
/// Provides a publisher that can fetch the first batch of comics
/// - Parameter size: The size of this first batch
/// - Returns: A publisher that delivers the first batch, as well as the bookmark for the next one.
func firstComics(size: Int) -> AnyPublisher<BatchFetchResult, Error>
/// Provides a publisher that can fetch multiple comics at once
/// - Parameter params: Parameters for the fetch.
/// - Returns: A publisher that delivers a batch of comics, as well as the bookmark for the next one.
func comics(withParams params: BatchFetchParams) -> AnyPublisher<BatchFetchResult, Error>
/// Provides a publisher that can answer whether the data source contains a given comic
/// - Parameter comicWithId: id of the comic to check
/// - Returns: A publisher that delivers the answer
func contains(comicWithId id: Int) -> AnyPublisher<Bool, Never>
/// Tells the data source to refresh its data.
func refresh()
}
protocol MutableComicDataSource: ImmutableComicDataSource {
/// Adds the comics to the end of the data source
/// - Parameter comics: The comics to append
/// - Returns: A publisher that delivers the result of this operation
func append(comics: [Comic]) -> AnyPublisher<Bool, Error>
}