forked from github/turbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_adapter.ts
134 lines (108 loc) · 3.33 KB
/
browser_adapter.ts
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 { Adapter } from "./adapter"
import { ProgressBar } from "../drive/progress_bar"
import { SystemStatusCode, Visit, VisitOptions } from "../drive/visit"
import { FormSubmission } from "../drive/form_submission"
import { Session } from "../session"
import { uuid, dispatch } from "../../util"
export type ReloadReason = StructuredReason | undefined
interface StructuredReason {
reason: string
context?: { [key: string]: any }
}
export class BrowserAdapter implements Adapter {
readonly session: Session
readonly progressBar = new ProgressBar()
visitProgressBarTimeout?: number
formProgressBarTimeout?: number
location?: URL
constructor(session: Session) {
this.session = session
}
visitProposedToLocation(location: URL, options?: Partial<VisitOptions>) {
this.navigator.startVisit(location, options?.restorationIdentifier || uuid(), options)
}
visitStarted(visit: Visit) {
this.location = visit.location
visit.loadCachedSnapshot()
visit.issueRequest()
visit.goToSamePageAnchor()
}
visitRequestStarted(visit: Visit) {
this.progressBar.setValue(0)
if (visit.hasCachedSnapshot() || visit.action != "restore") {
this.showVisitProgressBarAfterDelay()
} else {
this.showProgressBar()
}
}
visitRequestCompleted(visit: Visit) {
visit.loadResponse()
}
visitRequestFailedWithStatusCode(visit: Visit, statusCode: number) {
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: Visit) {
this.progressBar.setValue(1)
this.hideVisitProgressBar()
}
visitCompleted(_visit: Visit) {}
pageInvalidated(reason: ReloadReason) {
this.reload(reason)
}
visitFailed(_visit: Visit) {}
visitRendered(_visit: Visit) {}
formSubmissionStarted(_formSubmission: FormSubmission) {
this.progressBar.setValue(0)
this.showFormProgressBarAfterDelay()
}
formSubmissionFinished(_formSubmission: 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: ReloadReason) {
dispatch("turbo:reload", { detail: reason })
if (!this.location) return
window.location.href = this.location.toString()
}
get navigator() {
return this.session.navigator
}
}