Refactoring

This commit is contained in:
Justin Mazzocchi 2020-09-02 02:39:44 -07:00
parent 825eb8bf39
commit 66e7e38496
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
3 changed files with 20 additions and 26 deletions

View File

@ -28,20 +28,22 @@ struct ContentDatabase {
}
extension ContentDatabase {
func insert(statuses: [Status], timeline: Timeline? = nil) -> AnyPublisher<Never, Error> {
func insert(status: Status) -> AnyPublisher<Never, Error> {
databaseQueue.writePublisher(updates: status.save)
.ignoreOutput()
.eraseToAnyPublisher()
}
func insert(statuses: [Status], timeline: Timeline) -> AnyPublisher<Never, Error> {
databaseQueue.writePublisher {
try timeline?.save($0)
try timeline.save($0)
for status in statuses {
for component in status.storedComponents() {
try component.save($0)
}
try status.save($0)
if let timeline = timeline {
try TimelineStatusJoin(timelineId: timeline.id, statusId: status.id).save($0)
}
}
}
.ignoreOutput()
.eraseToAnyPublisher()
}
@ -49,9 +51,7 @@ extension ContentDatabase {
func insert(context: Context, parentID: String) -> AnyPublisher<Never, Error> {
databaseQueue.writePublisher {
for status in context.ancestors + context.descendants {
for component in status.storedComponents() {
try component.save($0)
}
try status.save($0)
}
for (section, statuses) in [(StatusContextJoin.Section.ancestors, context.ancestors),
@ -502,17 +502,15 @@ struct StatusResult: Codable, Hashable, FetchableRecord {
}
private extension Status {
func storedComponents() -> [PersistableRecord] {
var components: [PersistableRecord] = [account]
func save(_ db: Database) throws {
try account.save(db)
if let reblog = reblog {
components.append(reblog.account)
components.append(StoredStatus(status: reblog))
try reblog.account.save(db)
try StoredStatus(status: reblog).save(db)
}
components.append(StoredStatus(status: self))
return components
try StoredStatus(status: self).save(db)
}
convenience init(statusResult: StatusResult) {

View File

@ -33,8 +33,7 @@ extension StatusListService {
networkClient: networkClient,
contentDatabase: contentDatabase) { maxID, minID in
networkClient.request(Paged(timeline.endpoint, maxID: maxID, minID: minID))
.map { ($0, timeline) }
.flatMap(contentDatabase.insert(statuses:timeline:))
.flatMap { contentDatabase.insert(statuses: $0, timeline: timeline) }
.eraseToAnyPublisher()
}
}
@ -62,12 +61,10 @@ public extension StatusListService {
contentDatabase: contentDatabase) { _, _ in
Publishers.Merge(
networkClient.request(StatusEndpoint.status(id: statusID))
.map { ([$0], nil) }
.flatMap(contentDatabase.insert(statuses:timeline:))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher(),
networkClient.request(ContextEndpoint.context(id: statusID))
.map { ($0, statusID) }
.flatMap(contentDatabase.insert(context:parentID:))
.flatMap { contentDatabase.insert(context: $0, parentID: statusID) }
.eraseToAnyPublisher())
.eraseToAnyPublisher()
}

View File

@ -21,8 +21,7 @@ public extension StatusService {
networkClient.request(status.displayStatus.favourited
? StatusEndpoint.unfavourite(id: status.displayStatus.id)
: StatusEndpoint.favourite(id: status.displayStatus.id))
.map { ([$0], nil) }
.flatMap(contentDatabase.insert(statuses:timeline:))
.flatMap(contentDatabase.insert(status:))
.eraseToAnyPublisher()
}
}