Skip to content

Commit

Permalink
Stale content (#184)
Browse files Browse the repository at this point in the history
* Let Session know it needs to reload

It is helpful to let other Session instances know that data on the
server changed and what they are rendering might be out of date. This is
mostly useful when there are tabs.

* Public functions to clear snapshot cache or reload
  • Loading branch information
joemasilotti authored Feb 28, 2024
1 parent bad1d55 commit c96149f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Session: NSObject {
private lazy var bridge = WebViewBridge(webView: webView)
private var initialized = false
private var refreshing = false
private var isShowingStaleContent = false
private var isSnapshotCacheStale = false

/// Automatically creates a web view with the passed-in configuration
public convenience init(webViewConfiguration: WKWebViewConfiguration? = nil) {
Expand Down Expand Up @@ -91,6 +93,18 @@ public class Session: NSObject {
bridge.clearSnapshotCache()
}

// MARK: Caching

/// Clear the snapshot cache the next time the visitable view appears.
public func markSnapshotCacheAsStale() {
isSnapshotCacheStale = true
}

/// Reload the `Session` the next time the visitable view appears.
public func markContentAsStale() {
isShowingStaleContent = true
}

// MARK: Visitable activation

private var activatedVisitable: Visitable?
Expand Down Expand Up @@ -228,6 +242,12 @@ extension Session: VisitableDelegate {
} else if visitable !== topmostVisit.visitable {
// Navigating backward
visit(visitable, action: .restore)
} else if isShowingStaleContent {
reload()
isShowingStaleContent = false
} else if isSnapshotCacheStale {
clearSnapshotCache()
isSnapshotCacheStale = false
}
}

Expand Down

0 comments on commit c96149f

Please sign in to comment.