-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhyperhistory.js
More file actions
176 lines (143 loc) · 3.76 KB
/
hyperhistory.js
File metadata and controls
176 lines (143 loc) · 3.76 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import ReadyResource from 'ready-resource'
import b4a from 'b4a'
export default class History extends ReadyResource {
/** @type {Hypercore} */
#history = null
/** @type {Buffer|string} */
#key = null
/** @type {any} */
#currentState = null
/** @type {number} */
#currentIndex = -1
#sessionMinIndex = 0
constructor(store, key = null) {
super()
this.#key = key ? Buffer.isBuffer(key) ? key : b4a.from(key, 'hex') : null
this.#history = store.get({ name: this.#key ? undefined : "history", key: this.#key, valueEncoding: 'json' })
}
/**
* Get the length of the history.
*
* @returns {number}
*/
get length() {
return this.#history.length
}
/**
* Get the current position in the history.
*
* @returns {number}
*/
get position() {
return this.#currentIndex
}
/**
* Get the current state.
*
* @returns {any}
*/
get state() {
return this.#currentState
}
/**
* Go to the previous state.
*
* @returns {Promise<any>}
*/
back() {
return this.go(-1)
}
/**
* Go to the next state.
*
* @returns {Promise<any>}
*/
forward() {
return this.go(1)
}
/**
* Check if the history has a state at the given index.
* This is useful to check if the history has a state before calling go.
*
* @param {number} delta
* @returns {boolean}
*/
peek(delta = 0) {
// index = 5(-1)
// 5-2
// 5-3
// 0 1 2 3 4
const index = this.#currentIndex + delta
// Trying to go back to initial state
if (index === this.#sessionMinIndex) {
return true
}
return index >= this.#sessionMinIndex && index < this.#history.length
}
/**
* Go to the given index.
*
* @param {number} delta
* @returns {Promise<any>}
*/
async go(delta = 0) {
if (!this.peek(delta)) {
return this.#currentState
}
const index = this.#currentIndex + delta
if (index === this.#sessionMinIndex) {
this.#currentState = null
} else {
this.#currentState = await this.#history.get(index)
}
this.#currentIndex = index
this.emit('popState', this.#currentState)
return this.#currentState
}
/**
* Push a new state to the history.
* Will truncate the history if not at the tip.
*
* @param {any} state
* @returns {Promise<number>}
*/
async pushState(state) {
// If we're not at the tip, truncate the history
if (this.#currentIndex !== this.#history.length - 1) {
await this.#history.truncate(this.#currentIndex + 1)
}
const { length } = await this.#history.append(state)
this.#currentState = state
this.#currentIndex = length - 1
this.emit('popState', state)
}
async list() {
const stream = this.#history.createReadStream()
const states = []
for await (const state of stream) {
states.push(state)
}
return states
}
/**
* Clear all history entries.
*
* @returns {Promise<void>}
*/
async clear() {
await this.#history.truncate(0)
this.#currentState = null
this.#currentIndex = -1
this.#sessionMinIndex = 0
this.emit('popState', null)
}
async _open() {
await this.#history.ready()
// TODO: fix going back in time
this.#sessionMinIndex = this.#history.length - 1
this.#currentIndex = this.#history.length - 1
}
async _close() {
await this.#history.close()
}
}