forked from headzoo/surf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
79 lines (67 loc) · 1.57 KB
/
history.go
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
package jar
import (
"github.com/PuerkitoBio/goquery"
"net/http"
)
// State represents a point in time.
type State struct {
Request *http.Request
Response *http.Response
Dom *goquery.Document
}
// NewHistoryState creates and returns a new *State type.
func NewHistoryState(req *http.Request, resp *http.Response, dom *goquery.Document) *State {
return &State{
Request: req,
Response: resp,
Dom: dom,
}
}
// History is a type that records browser state.
type History interface {
Len() int
Push(p *State) int
Pop() *State
Top() *State
}
// Node holds stack values and points to the next element.
type Node struct {
Value *State
Next *Node
}
// MemoryHistory is an in-memory implementation of the History interface.
type MemoryHistory struct {
top *Node
size int
}
// NewMemoryHistory creates and returns a new *StateHistory type.
func NewMemoryHistory() *MemoryHistory {
return &MemoryHistory{}
}
// Len returns the number of states in the history.
func (his *MemoryHistory) Len() int {
return his.size
}
// Push adds a new State at the front of the history.
func (his *MemoryHistory) Push(p *State) int {
his.top = &Node{p, his.top}
his.size++
return his.size
}
// Pop removes and returns the State at the front of the history.
func (his *MemoryHistory) Pop() *State {
if his.size > 0 {
value := his.top.Value
his.top = his.top.Next
his.size--
return value
}
return nil
}
// Top returns the State at the front of the history without removing it.
func (his *MemoryHistory) Top() *State {
if his.size == 0 {
return nil
}
return his.top.Value
}