Skip to content

Commit

Permalink
refactor: 🧽
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Aug 27, 2020
1 parent a7419df commit 6ff96c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
11 changes: 4 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -135,6 +135,7 @@ export function createClient(options: ClientOptions): Client {
}
};

socket.addEventListener('message', handleMessage);
function handleMessage({ data }: MessageEvent) {
try {
if (!socket) {
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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];
});
Expand Down
14 changes: 7 additions & 7 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
28 changes: 14 additions & 14 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<MessageType.Next>(ctx, {
id: message.id,
Expand Down Expand Up @@ -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<MessageType.Next>(ctx, {
id: message.id,
Expand All @@ -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<MessageType.Next>(ctx, {
id: message.id,
Expand Down

0 comments on commit 6ff96c2

Please sign in to comment.