From cce867ae824cf42485a04ee8eff1c11f7ebe1175 Mon Sep 17 00:00:00 2001 From: Sean Johnson Date: Fri, 10 Sep 2021 13:42:02 -0700 Subject: [PATCH] Changed SongServer to use interface types. --- src/server/index.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/server/index.ts b/src/server/index.ts index e5e2388..3912a64 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,5 +1,4 @@ import * as grpc from '@grpc/grpc-js'; -import { sendUnaryData } from '@grpc/grpc-js/build/src/server-call'; import { Empty } from 'google-protobuf/google/protobuf/empty_pb'; import { Song, Comment } from '../proto/songs_pb'; import { ISongsServer, SongsService } from '../proto/songs_grpc_pb'; @@ -8,19 +7,19 @@ import addSong from './add-song'; import getChat from './get-chat'; import { addComment, registerListener } from './live-chat'; -class SongsServer implements ISongsServer { - getSong(call: grpc.ServerUnaryCall, callback: sendUnaryData): void { +const createSongsServer = (): ISongsServer => ({ + getSong(_call, callback): void { console.log(`${new Date().toISOString()} getSong`); callback(null, getSong()); - } - addSongs(call: grpc.ServerReadableStream, callback: sendUnaryData): void { + }, + addSongs(call, callback): void { console.log(`${new Date().toISOString()} addSongs`); call.on('data', (song: Song) => { addSong(song); }); call.on('end', () => callback(null, new Empty())); - } - async getChat(call: grpc.ServerWritableStream): Promise { + }, + async getChat(call): Promise { console.log(`${new Date().toISOString()} getChat`); const song = call.request as Song; const comments = await getChat(song.getId()); @@ -28,8 +27,8 @@ class SongsServer implements ISongsServer { call.write(comment); } call.end(); - } - liveChat(call: grpc.ServerDuplexStream): void { + }, + liveChat(call): void { console.log(`${new Date().toISOString()} liveChat`); registerListener((comment) => call.write(comment)); call.on('data', (comment: Comment) => { @@ -37,12 +36,12 @@ class SongsServer implements ISongsServer { }); call.on('end', () => call.end()); } -} +}); function serve(): void { const server = new grpc.Server(); // @ts-ignore - server.addService(SongsService, new SongsServer()); + server.addService(SongsService, createSongsServer()); server.bindAsync(`localhost:${process.env.PORT}`, grpc.ServerCredentials.createInsecure(), (err, port) => { if (err) { throw err;