-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathupdate.js
71 lines (64 loc) · 1.86 KB
/
update.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
const tape = require('tape')
const create = require('./helpers/create')
const HypercoreProtocol = require('hypercore-protocol')
tape('get without alwaysUpdate returns null', t => {
const trie1 = create()
var trie2 = null
trie1.ready(() => {
trie2 = create(trie1.key)
trie1.put('a', 'b', () => {
trie2.get('a', (err, node) => {
t.error(err, 'no error')
t.same(node, null)
t.end()
})
})
replicate(trie1, trie2, { live: true })
})
})
tape('get with alwaysUpdate will wait for an update', t => {
const trie1 = create({ alwaysUpdate: true })
var trie2 = null
trie1.ready(() => {
trie2 = create(trie1.key, { alwaysUpdate: true, valueEncoding: 'utf8' })
trie1.put('a', 'b', () => {
trie2.get('a', (err, node) => {
t.error(err, 'no error')
t.same(node.key, 'a')
t.same(node.value, 'b')
t.end()
})
})
replicate(trie1, trie2, { live: true })
})
})
tape('replication with an empty peer is not problematic', t => {
const trie1 = create({ alwaysUpdate: true })
const emptyPeer = {
replicate: (isInitiator, opts) => {
const stream = new HypercoreProtocol(isInitiator, { ...opts, live: true })
stream.on('discovery-key', dkey => {
stream.close(dkey)
})
return stream
}
}
var trie2 = null
trie1.ready(() => {
trie2 = create(trie1.key, { alwaysUpdate: true, valueEncoding: 'utf8' })
trie1.put('a', 'b', () => {
trie2.get('a', (err, node) => {
t.error(err, 'no error')
t.same(node.key, 'a')
t.same(node.value, 'b')
t.end()
})
})
replicate(trie1, trie2, { live: true })
replicate(trie1, emptyPeer, { live: true })
})
})
function replicate (trie1, trie2, opts) {
const stream = trie1.replicate(true, opts)
return stream.pipe(trie2.replicate(false, opts)).pipe(stream)
}