Skip to content

Commit

Permalink
merged TimelineStore and PersistentTimelineStore #41
Browse files Browse the repository at this point in the history
  • Loading branch information
sobri909 committed Sep 11, 2018
1 parent ef22654 commit e126c3d
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 402 deletions.
79 changes: 79 additions & 0 deletions LocoKit/Timelines/ItemsObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// ItemsObserver.swift
// LocoKit
//
// Created by Matt Greenfield on 11/9/18.
//

import os.log
import GRDB

class ItemsObserver: TransactionObserver {

var store: TimelineStore
var changedRowIds: Set<Int64> = []

init(store: TimelineStore) {
self.store = store
}

// observe updates to next/prev item links
func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool {
switch eventKind {
case .update(let tableName, let columnNames):
guard tableName == "TimelineItem" else { return false }
let itemEdges: Set<String> = ["previousItemId", "nextItemId"]
return itemEdges.intersection(columnNames).count > 0
default: return false
}
}

func databaseDidChange(with event: DatabaseEvent) {
changedRowIds.insert(event.rowID)
}

func databaseDidCommit(_ db: Database) {
let rowIds: Set<Int64> = store.mutex.sync {
let rowIds = changedRowIds
changedRowIds = []
return rowIds
}

if rowIds.isEmpty { return }

/** maintain the timeline items linked list locally, for changes made outside the managed environment **/

do {
let marks = repeatElement("?", count: rowIds.count).joined(separator: ",")
let query = "SELECT itemId, previousItemId, nextItemId FROM TimelineItem WHERE rowId IN (\(marks))"
let rows = try Row.fetchCursor(db, query, arguments: StatementArguments(rowIds))

while let row = try rows.next() {
let previousItemIdString = row["previousItemId"] as String?
let nextItemIdString = row["nextItemId"] as String?

guard let uuidString = row["itemId"] as String?, let itemId = UUID(uuidString: uuidString) else { continue }
guard let item = store.object(for: itemId) as? TimelineItem else { continue }

if let uuidString = previousItemIdString, item.previousItemId?.uuidString != uuidString {
item.previousItemId = UUID(uuidString: uuidString)

} else if previousItemIdString == nil && item.previousItemId != nil {
item.previousItemId = nil
}

if let uuidString = nextItemIdString, item.nextItemId?.uuidString != uuidString {
item.nextItemId = UUID(uuidString: uuidString)

} else if nextItemIdString == nil && item.nextItemId != nil {
item.nextItemId = nil
}
}

} catch {
os_log("SQL Exception: %@", error.localizedDescription)
}
}

func databaseDidRollback(_ db: Database) {}
}
Loading

0 comments on commit e126c3d

Please sign in to comment.