Browse Source

fix: replace ex.js with example from README

Lars-Magnus Skog 3 years ago
parent
commit
2eb54f8523
4 changed files with 42 additions and 13 deletions
  1. 2
    0
      .gitignore
  2. 1
    1
      README.md
  3. 0
    12
      ex.js
  4. 39
    0
      example.js

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+db/
2
+node_modules/

+ 1
- 1
README.md View File

@@ -25,7 +25,7 @@ multi.writer('local', function (err, w) {
25 25
 
26 26
   // write data to any writeable feed, just like with hypercore
27 27
   w.append('foo', function () {
28
-    var m2 = multifeed(ram, { valueEncoding: 'json' })
28
+    var m2 = multifeed(hypercore, ram, { valueEncoding: 'json' })
29 29
     m2.writer('local', function (err, w2) {
30 30
       w2.append('bar', function () {
31 31
         replicate(multi, m2, function () {

+ 0
- 12
ex.js View File

@@ -1,12 +0,0 @@
1
-var multicore = require('.')
2
-var hypercore = require('hypercore')
3
-
4
-var ram = require('random-access-memory')
5
-
6
-var multi = multicore(hypercore, './db', { valueEncoding: 'json' })
7
-
8
-multi.writer(function (err, w) {
9
-  console.log(w.key.toString('hex'))
10
-  w.append('foo', console.log)
11
-})
12
-

+ 39
- 0
example.js View File

@@ -0,0 +1,39 @@
1
+var multifeed = require('.')
2
+var hypercore = require('hypercore')
3
+var ram = require('random-access-memory')
4
+
5
+var multi = multifeed(hypercore, './db', { valueEncoding: 'json' })
6
+
7
+// a multifeed starts off empty
8
+console.log(multi.feeds().length)             // => 0
9
+
10
+// create as many writeable feeds as you want; returns hypercores
11
+multi.writer('local', function (err, w) {
12
+  console.log(w.key, w.writeable, w.readable)   // => Buffer <0x..> true true
13
+  console.log(multi.feeds().length)             // => 1
14
+
15
+  // write data to any writeable feed, just like with hypercore
16
+  w.append('foo', function () {
17
+    var m2 = multifeed(hypercore, ram, { valueEncoding: 'json' })
18
+    m2.writer('local', function (err, w2) {
19
+      w2.append('bar', function () {
20
+        replicate(multi, m2, function () {
21
+          console.log(m2.feeds().length)        // => 2
22
+          m2.feeds()[1].get(0, function (_, data) {
23
+            console.log(data)                   // => foo
24
+          })
25
+          multi.feeds()[1].get(0, function (_, data) {
26
+            console.log(data)                   // => bar
27
+          })
28
+        })
29
+      })
30
+    })
31
+  })
32
+})
33
+
34
+function replicate (a, b, cb) {
35
+  var r = a.replicate()
36
+  r.pipe(b.replicate()).pipe(r)
37
+    .once('end', cb)
38
+    .once('error', cb)
39
+}