From c96149ff1951fdf89d35002ac45904c2e1e8fa24 Mon Sep 17 00:00:00 2001 From: Joe Masilotti Date: Wed, 28 Feb 2024 13:37:47 -0800 Subject: [PATCH] Stale content (#184) * 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 --- Source/Session/Session.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Source/Session/Session.swift b/Source/Session/Session.swift index bcfdab3..f9ef2fc 100644 --- a/Source/Session/Session.swift +++ b/Source/Session/Session.swift @@ -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) { @@ -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? @@ -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 } }