Browse Source

fix to get test passing

ameba23 2 years ago
parent
commit
072b1f6065
3 changed files with 12 additions and 9 deletions
  1. 3
    2
      README.md
  2. 2
    0
      index.js
  3. 7
    7
      test/index.test.js

+ 3
- 2
README.md View File

@@ -11,9 +11,10 @@ Broadcast a message regularly.
11 11
   - `opts.interval` interval between packets in ms, default 1000
12 12
   - `opts.port` port to broadcast on.
13 13
 
14
-### `bc.receive(callback [opts])`
14
+### `const destroy = bc.receive(callback [opts])`
15 15
 
16
-Listen for messages
16
+Listen for messages.
17
+- `destroy` is a function which you can call with no arguments to stop listening.
17 18
 - `callback` is called whenever a ***unique*** message is found.  That is, whenever the message or address changes. The message is passed in the callback.
18 19
 - `opts` may include:
19 20
   - `opts.port` port to listen to

+ 2
- 0
index.js View File

@@ -41,4 +41,6 @@ function receive (callback, opts = {}) {
41 41
       callback(msg)
42 42
     }
43 43
   })
44
+
45
+  return () => { stream.end() }
44 46
 }

+ 7
- 7
test/index.test.js View File

@@ -1,18 +1,18 @@
1 1
 const { describe } = require('tape-plus')
2 2
 const { broadcast, receive } = require('..')
3
+const message = Buffer.from('boop')
3 4
 
4 5
 describe('basic', (context) => {
5 6
   context('broadcast and receive', (assert, next) => {
6
-    const destroy = broadcast(null, { interval: 1000 })
7
-
7
+    const destroyBroadcast = broadcast(message, { interval: 1000 })
8
+    const destroyReceive = receive((msg) => {
9
+      assert.same(msg, message, 'received the message that was broadcast')
10
+    })
8 11
 
9
-    console.log(destroy)
10 12
     setTimeout(() => {
11
-      destroy()
13
+      destroyBroadcast()
14
+      destroyReceive()
12 15
       next()
13 16
     }, 2000)
14
-    receive((msg) => {
15
-      console.log(msg)
16
-    })
17 17
   })
18 18
 })