-
Notifications
You must be signed in to change notification settings - Fork 27
/
example.js
38 lines (33 loc) · 1.19 KB
/
example.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
var multifeed = require('.')
var ram = require('random-access-memory')
var multi = multifeed('./db', { valueEncoding: 'json' })
// a multifeed starts off empty
console.log(multi.feeds().length) // => 0
// create as many writeable feeds as you want; returns hypercores
multi.writer('local', function (err, w) {
console.log(w.key, w.writable, w.readable) // => Buffer <0x..> true true
console.log(multi.feeds().length) // => 1
// write data to any writeable feed, just like with hypercore
w.append('foo', function () {
var m2 = multifeed(ram, { valueEncoding: 'json' })
m2.writer('local', function (err, w2) {
w2.append('bar', function () {
replicate(multi, m2, function () {
console.log(m2.feeds().length) // => 2
m2.feeds()[1].get(0, function (_, data) {
console.log(data) // => foo
})
multi.feeds()[1].get(0, function (_, data) {
console.log(data) // => bar
})
})
})
})
})
})
function replicate (a, b, cb) {
var r = a.replicate(true)
r.pipe(b.replicate(false)).pipe(r)
.once('end', cb)
.once('error', cb)
}