-
Notifications
You must be signed in to change notification settings - Fork 433
/
browser_adapter.js
134 lines (108 loc) · 3.1 KB
/
browser_adapter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { ProgressBar } from "../drive/progress_bar"
import { SystemStatusCode } from "../drive/visit"
import { uuid, dispatch } from "../../util"
import { locationIsVisitable } from "../url"
export class BrowserAdapter {
progressBar = new ProgressBar()
constructor(session) {
this.session = session
}
visitProposedToLocation(location, options) {
if (locationIsVisitable(location, this.navigator.rootLocation)) {
this.navigator.startVisit(location, options?.restorationIdentifier || uuid(), options)
} else {
window.location.href = location.toString()
}
}
visitStarted(visit) {
this.location = visit.location
visit.loadCachedSnapshot()
visit.issueRequest()
visit.goToSamePageAnchor()
}
visitRequestStarted(visit) {
this.progressBar.setValue(0)
if (visit.hasCachedSnapshot() || visit.action != "restore") {
this.showVisitProgressBarAfterDelay()
} else {
this.showProgressBar()
}
}
visitRequestCompleted(visit) {
visit.loadResponse()
}
visitRequestFailedWithStatusCode(visit, statusCode) {
switch (statusCode) {
case SystemStatusCode.networkFailure:
case SystemStatusCode.timeoutFailure:
case SystemStatusCode.contentTypeMismatch:
return this.reload({
reason: "request_failed",
context: {
statusCode
}
})
default:
return visit.loadResponse()
}
}
visitRequestFinished(_visit) {}
visitCompleted(_visit) {
this.progressBar.setValue(1)
this.hideVisitProgressBar()
}
pageInvalidated(reason) {
this.reload(reason)
}
visitFailed(_visit) {
this.progressBar.setValue(1)
this.hideVisitProgressBar()
}
visitRendered(_visit) {}
// Link prefetching
linkPrefetchingIsEnabledForLocation(location) {
return true
}
// Form Submission Delegate
formSubmissionStarted(_formSubmission) {
this.progressBar.setValue(0)
this.showFormProgressBarAfterDelay()
}
formSubmissionFinished(_formSubmission) {
this.progressBar.setValue(1)
this.hideFormProgressBar()
}
// Private
showVisitProgressBarAfterDelay() {
this.visitProgressBarTimeout = window.setTimeout(this.showProgressBar, this.session.progressBarDelay)
}
hideVisitProgressBar() {
this.progressBar.hide()
if (this.visitProgressBarTimeout != null) {
window.clearTimeout(this.visitProgressBarTimeout)
delete this.visitProgressBarTimeout
}
}
showFormProgressBarAfterDelay() {
if (this.formProgressBarTimeout == null) {
this.formProgressBarTimeout = window.setTimeout(this.showProgressBar, this.session.progressBarDelay)
}
}
hideFormProgressBar() {
this.progressBar.hide()
if (this.formProgressBarTimeout != null) {
window.clearTimeout(this.formProgressBarTimeout)
delete this.formProgressBarTimeout
}
}
showProgressBar = () => {
this.progressBar.show()
}
reload(reason) {
dispatch("turbo:reload", { detail: reason })
window.location.href = this.location?.toString() || window.location.href
}
get navigator() {
return this.session.navigator
}
}