Created
May 11, 2019 06:36
-
-
Save carsonfarmer/90e5bfd7ac8fa39af4126fe065c87411 to your computer and use it in GitHub Desktop.
Flushing out the subscription step
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/index.js b/index.js | |
index 598b1d1..963d96f 100644 | |
--- a/index.js | |
+++ b/index.js | |
@@ -31,7 +31,31 @@ cli.command('', 'Starts an interactive chat session in a thread.') | |
// Only subscribe to text events on the specified thread | |
textile.subscribe.stream(['TEXT'], opts.thread) | |
.then((stream) => { | |
- log('do something') | |
+ // All js-http-client stream endpoints return a ReadableSream | |
+ const reader = stream.getReader() | |
+ const read = (result) => { | |
+ if (result.done) { | |
+ return | |
+ } | |
+ // Extract the text update and display it nicely... | |
+ try { | |
+ const item = result.value.payload | |
+ const name = item.user.name || item.user.address.slice(7) | |
+ if (item.user.address !== peer.address) { | |
+ readline.clearLine(process.stdout, 0) | |
+ readline.cursorTo(process.stdout, 0) | |
+ log(name + '\t' + item.body) | |
+ rl.prompt() | |
+ } | |
+ } catch (err) { | |
+ reader.cancel(undefined) | |
+ return | |
+ } | |
+ // Keep reading from the stream | |
+ reader.read().then(read) | |
+ } | |
+ // Start reading from the stream | |
+ reader.read().then(read) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment