Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Sources/History/HistoryCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ import os.log

public typealias BrowsingHistory = [HistoryEntry]

public protocol HistoryCoordinating: AnyObject {
/**
* This protocol allows for debugging History.
*/
public protocol HistoryCoordinatingDebuggingSupport {
/**
* Adds visit at an arbitrary time, rather than current timestamp.
*
* > This function shouldn't be used in production code. Instead, `addVisit(of: URL)` should be used.
*/
@discardableResult func addVisit(of url: URL, at date: Date) -> Visit?
}

public protocol HistoryCoordinating: AnyObject, HistoryCoordinatingDebuggingSupport {

func loadHistory(onCleanFinished: @escaping () -> Void)

Expand All @@ -47,6 +59,12 @@ public protocol HistoryCoordinating: AnyObject {
func removeUrlEntry(_ url: URL, completion: ((Error?) -> Void)?)
}

extension HistoryCoordinating {
public func addVisit(of url: URL) -> Visit? {
addVisit(of: url, at: Date())
}
}

/// Coordinates access to History. Uses its own queue with high qos for all operations.
final public class HistoryCoordinator: HistoryCoordinating {

Expand Down Expand Up @@ -86,14 +104,14 @@ final public class HistoryCoordinator: HistoryCoordinating {

private var cancellables = Set<AnyCancellable>()

@discardableResult public func addVisit(of url: URL) -> Visit? {
@discardableResult public func addVisit(of url: URL, at date: Date) -> Visit? {
guard let historyDictionary = historyDictionary else {
Logger.history.debug("Visit of \(url.absoluteString) ignored")
return nil
}

let entry = historyDictionary[url] ?? HistoryEntry(url: url)
let visit = entry.addVisit()
let visit = entry.addVisit(at: date)
entry.failedToLoad = false

self.historyDictionary?[url] = entry
Expand Down
6 changes: 3 additions & 3 deletions Sources/History/HistoryEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ final public class HistoryEntry {

public var visits: Set<Visit>

func addVisit() -> Visit {
let visit = Visit(date: Date(), historyEntry: self)
func addVisit(at date: Date = Date()) -> Visit {
let visit = Visit(date: date, historyEntry: self)
visits.insert(visit)

lastVisit = numberOfTotalVisits == 0 ? date : max(lastVisit, date)
numberOfTotalVisits += 1
lastVisit = Date()

return visit
}
Expand Down
Loading