Skip to content

Commit

Permalink
Merge pull request #189 from hotwired/fix-visitable-snapshots-and-dea…
Browse files Browse the repository at this point in the history
…ctivation-feedback

Snapshot and deactivate Visitable during it's lifecycle feedback
  • Loading branch information
jayohms authored Mar 22, 2024
2 parents 979b1a9 + fc05e38 commit 3bcea29
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Session: NSObject {

private var currentVisit: Visit?
private var topmostVisit: Visit?
private var previousVisit: Visit?

/// The topmost visitable is the visitable that has most recently completed a visit
public var topmostVisitable: Visitable? {
Expand Down Expand Up @@ -227,6 +228,11 @@ extension Session: VisitDelegate {

extension Session: VisitableDelegate {
public func visitableViewWillAppear(_ visitable: Visitable) {
defer {
/// Nilling out the previous visit here prevents `double-snapshotting` for web -> web visits.
previousVisit = nil
}

guard let topmostVisit = self.topmostVisit, let currentVisit = self.currentVisit else { return }

if isSnapshotCacheStale {
Expand All @@ -248,7 +254,10 @@ extension Session: VisitableDelegate {
// Navigating forward - complete navigation early
completeNavigationForCurrentVisit()
} else if visitable !== topmostVisit.visitable {
// Navigating backward
// Navigating backward from a web view screen to a web view screen.
visit(visitable, action: .restore)
} else if visitable === previousVisit?.visitable {
// Navigating backward from a native to a web view screen.
visit(visitable, action: .restore)
}
}
Expand All @@ -266,6 +275,15 @@ extension Session: VisitableDelegate {
}
}

public func visitableViewWillDisappear(_ visitable: Visitable) {
previousVisit = topmostVisit
}

public func visitableViewDidDisappear(_ visitable: Visitable) {
previousVisit?.cacheSnapshot()
deactivateVisitable(visitable)
}

public func visitableDidRequestReload(_ visitable: Visitable) {
guard visitable === topmostVisitable else { return }
reload()
Expand Down
4 changes: 4 additions & 0 deletions Source/Visit/Visit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Visit: NSObject {
delegate?.visitDidFinish(self)
}

func cacheSnapshot() {
bridge.cacheSnapshot()
}

func startVisit() {}
func cancelVisit() {}
func completeVisit() {}
Expand Down
2 changes: 2 additions & 0 deletions Source/Visitable/Visitable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import WebKit
public protocol VisitableDelegate: AnyObject {
func visitableViewWillAppear(_ visitable: Visitable)
func visitableViewDidAppear(_ visitable: Visitable)
func visitableViewWillDisappear(_ visitable: Visitable)
func visitableViewDidDisappear(_ visitable: Visitable)
func visitableDidRequestReload(_ visitable: Visitable)
func visitableDidRequestRefresh(_ visitable: Visitable)
}
Expand Down
10 changes: 10 additions & 0 deletions Source/Visitable/VisitableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ open class VisitableViewController: UIViewController, Visitable {
visitableDelegate?.visitableViewDidAppear(self)
}

open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
visitableDelegate?.visitableViewWillDisappear(self)
}

open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
visitableDelegate?.visitableViewDidDisappear(self)
}

// MARK: Visitable

open func visitableDidRender() {
Expand Down
4 changes: 4 additions & 0 deletions Source/WebView/WebViewBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ final class WebViewBridge {
callJavaScript(function: "window.turboNative.clearSnapshotCache")
}

func cacheSnapshot() {
callJavaScript(function: "window.turboNative.cacheSnapshot")
}

func cancelVisit(withIdentifier identifier: String) {
callJavaScript(function: "window.turboNative.cancelVisitWithIdentifier", arguments: [identifier])
}
Expand Down
6 changes: 6 additions & 0 deletions Source/WebView/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
}
}

cacheSnapshot() {
if (window.Turbo) {
Turbo.session.view.cacheSnapshot()
}
}

// Current visit

issueRequestForVisitWithIdentifier(identifier) {
Expand Down

0 comments on commit 3bcea29

Please sign in to comment.