From 6ff96c224305079cb1a24c8fe27574356c1a61fa Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Fri, 28 Aug 2020 00:04:02 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=A7=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client.ts | 11 ++++------- src/message.ts | 14 +++++++------- src/server.ts | 28 ++++++++++++++-------------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/client.ts b/src/client.ts index d6c7f3ea..0ffb5951 100644 --- a/src/client.ts +++ b/src/client.ts @@ -75,7 +75,7 @@ export function createClient(options: ClientOptions): Client { connected = false; connecting = true; return new Promise((resolve, reject) => { - let done = false; // used to avoid resolving/rejecting the promise multiple times + let done = false; socket = new WebSocket(url, GRAPHQL_TRANSPORT_WS_PROTOCOL); /** @@ -102,9 +102,9 @@ export function createClient(options: ClientOptions): Client { if (!done) { done = true; connecting = false; - connected = false; // the connection is lost + connected = false; socket = null; - reject(closeEvent); // we reject here bacause the close is not supposed to be called during the connect phase + reject(closeEvent); } }; socket.onopen = () => { @@ -135,6 +135,7 @@ export function createClient(options: ClientOptions): Client { } }; + socket.addEventListener('message', handleMessage); function handleMessage({ data }: MessageEvent) { try { if (!socket) { @@ -166,12 +167,10 @@ export function createClient(options: ClientOptions): Client { } } finally { if (socket) { - // this listener is not necessary anymore socket.removeEventListener('message', handleMessage); } } } - socket.addEventListener('message', handleMessage); }); } @@ -259,11 +258,9 @@ export function createClient(options: ClientOptions): Client { }; }, dispose: async () => { - // complete all sinks // TODO-db-200817 complete or error? the sinks should be completed BEFORE the client gets disposed completeAllSinks(); - // delete all sinks Object.keys(subscribedSinks).forEach((uuid) => { delete subscribedSinks[uuid]; }); diff --git a/src/message.ts b/src/message.ts index 44c106bc..10f99120 100644 --- a/src/message.ts +++ b/src/message.ts @@ -134,14 +134,14 @@ export function parseMessage(data: unknown): Message { if (isMessage(data)) { return data; } - if (typeof data === 'string') { - const message = JSON.parse(data); - if (!isMessage(message)) { - throw new Error('Invalid message'); - } - return message; + if (typeof data !== 'string') { + throw new Error('Message not parsable'); + } + const message = JSON.parse(data); + if (!isMessage(message)) { + throw new Error('Invalid message'); } - throw new Error('Message not parsable'); + return message; } /** diff --git a/src/server.ts b/src/server.ts index 35e8a7f1..2e2be61b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -339,13 +339,13 @@ export function createServer( variableValues: operation.variables, }; - let executionResultFormatter: ExecutionResultFormatter | undefined; - + let onSubscribeFormatter: ExecutionResultFormatter | undefined; if (onSubscribe) { - [ + [execArgsMaybeSchema, onSubscribeFormatter] = await onSubscribe( + ctx, + message, execArgsMaybeSchema, - executionResultFormatter, - ] = await onSubscribe(ctx, message, execArgsMaybeSchema); + ); } if (!execArgsMaybeSchema.schema) { // not providing a schema is a fatal server error @@ -391,9 +391,9 @@ export function createServer( if (formatExecutionResult) { result = await formatExecutionResult(ctx, result); } - // use the subscription specific formatter - if (executionResultFormatter) { - result = await executionResultFormatter(ctx, result); + // then use the subscription specific formatter + if (onSubscribeFormatter) { + result = await onSubscribeFormatter(ctx, result); } await sendMessage(ctx, { id: message.id, @@ -431,9 +431,9 @@ export function createServer( if (formatExecutionResult) { result = await formatExecutionResult(ctx, result); } - // use the subscription specific formatter - if (executionResultFormatter) { - result = await executionResultFormatter(ctx, result); + // then use the subscription specific formatter + if (onSubscribeFormatter) { + result = await onSubscribeFormatter(ctx, result); } await sendMessage(ctx, { id: message.id, @@ -458,9 +458,9 @@ export function createServer( if (formatExecutionResult) { result = await formatExecutionResult(ctx, result); } - // use the subscription specific formatter - if (executionResultFormatter) { - result = await executionResultFormatter(ctx, result); + // then use the subscription specific formatter + if (onSubscribeFormatter) { + result = await onSubscribeFormatter(ctx, result); } await sendMessage(ctx, { id: message.id,