Skip to content

Commit

Permalink
Persist visit options if a response is present
Browse files Browse the repository at this point in the history
  • Loading branch information
olivaresf committed Nov 6, 2023
1 parent c476bac commit ded644e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Source/Session/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class Session: NSObject {
private lazy var bridge = WebViewBridge(webView: webView)
private var initialized = false
private var refreshing = false

/// Options behave differently if a response is provided.
private let visitOptionsHandler = VisitOptionsHandler()

/// Automatically creates a web view with the passed-in configuration
public convenience init(webViewConfiguration: WKWebViewConfiguration? = nil) {
Expand Down Expand Up @@ -61,7 +64,8 @@ public class Session: NSObject {
initialized = false
}

let visit = makeVisit(for: visitable, options: options ?? VisitOptions())
let processedOptions = visitOptionsHandler.process(options)
let visit = makeVisit(for: visitable, options: processedOptions)
currentVisit?.cancel()
currentVisit = visit

Expand Down
24 changes: 24 additions & 0 deletions Source/Session/VisitOptionsHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation

class VisitOptionsHandler {

private var unhandledVisitOptions: VisitOptions?

/// If a form submission provides a response HTML, save the options and pass them to the next visit proposal.
func process(_ options: VisitOptions?) -> VisitOptions {

if let options, options.response != nil {
/// Options are provided for the next visit.
unhandledVisitOptions = options
return options
} else if let unhandledVisitOptions {
/// Next visit is happening. Use the previous visit options.
self.unhandledVisitOptions = nil
return unhandledVisitOptions
} else {
/// No options are unhandled.
return options ?? VisitOptions()
}
}

}

0 comments on commit ded644e

Please sign in to comment.