-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (56 loc) · 1.47 KB
/
index.js
File metadata and controls
63 lines (56 loc) · 1.47 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
module.exports = class HypercoreSession {
constructor (feed, opts) {
this.feed = feed
this.checkout = (opts && opts.checkout) || 0
this.defaults = opts || null
this.gets = new Set()
this.downloads = new Set()
}
get length () {
return this.checkout || this.feed.length
}
clone () {
return new HypercoreSession(this.feed, this.defaults)
}
ready () {
return new Promise((resolve, reject) => {
this.feed.ready(function (err) {
if (err) reject(err)
else resolve()
})
})
}
download (range) {
return new Promise((resolve, reject) => {
const download = this.feed.download(range, function (err) {
this.downloads.delete(download)
if (err) reject(err)
else resolve()
})
this.downloads.add(download)
})
}
update (opts = this.defaults) {
return new Promise((resolve, reject) => {
// TODO: updates should be cancellable
this.feed.update(opts, function (err) {
if (err) reject(err)
else resolve()
})
})
}
get (seq, opts = this.defaults) {
return new Promise((resolve, reject) => {
const get = this.feed.get(seq, opts, function (err, block) {
this.gets.delete(get)
if (err) reject(err)
else resolve(block)
})
this.gets.add(get)
})
}
cancel () {
for (const get of this.gets) this.feed.cancel(get)
for (const download of this.downloads) this.feed.undownload(download)
}
}