-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement live chat bidi stream endpoint
- Loading branch information
1 parent
d8f6b3a
commit 6f0f3f8
Showing
8 changed files
with
84 additions
and
158 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
dist | ||
db.json | ||
chat-*.txt |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fs from 'fs'; | ||
import inquirer from 'inquirer'; | ||
import client from './client'; | ||
import { Comment } from '../proto/songs_pb'; | ||
|
||
async function liveChat(): Promise<void> { | ||
const { name, songId } = await inquirer.prompt([ | ||
{ | ||
name: 'name', | ||
message: 'What is your name?', | ||
}, | ||
{ | ||
name: 'songId', | ||
message: 'Which song do you want to discuss?', | ||
}, | ||
]); | ||
const stream = client.liveChat(); | ||
return new Promise(async (resolve, reject) => { | ||
stream.on('data', (comment: Comment) => { | ||
fs.writeFileSync( | ||
`chat-${name}-${comment.getSongId()}.txt`, | ||
`(${comment.getUsername()}) ${comment.getBody()}\n`, | ||
{ | ||
flag: 'a', | ||
}, | ||
); | ||
}); | ||
stream.on('end', resolve); | ||
stream.on('error', reject); | ||
|
||
while (true) { | ||
const answer = await inquirer.prompt([ | ||
{ | ||
name: 'message', | ||
message: 'Type message:', | ||
}, | ||
]); | ||
const comment = new Comment(); | ||
comment.setUsername(name); | ||
comment.setBody(answer.message); | ||
comment.setSongId(songId); | ||
stream.write(comment); | ||
} | ||
}); | ||
} | ||
|
||
export default { | ||
command: 'chat', | ||
describe: 'Chat about a song', | ||
builder: {}, | ||
handler: async (): Promise<void> => { | ||
await liveChat(); | ||
}, | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Comment } from '../proto/songs_pb'; | ||
import db from './db'; | ||
|
||
type ListenerFn = (c: Comment) => void; | ||
|
||
const listeners: ListenerFn[] = []; | ||
|
||
export function registerListener(fn: ListenerFn): void { | ||
listeners.push(fn); | ||
} | ||
|
||
export function addComment(comment: Comment): void { | ||
// Use of `any` required due to bug in @types/lowdb | ||
// SEE: https://github.com/typicode/lowdb/issues/349 | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const dbComments = db.get('comments') as any; | ||
dbComments.push(comment.toObject()).write(); | ||
listeners.map(listener => listener(comment)); | ||
} |