Skip to content

Commit

Permalink
refactor: simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Aug 27, 2020
1 parent 734d268 commit 4406a5b
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function createSocky() {
};

return {
async shouldConnect(): Promise<boolean> {
async beginConnecting(): Promise<boolean> {
if (state.connecting) {
let waitedTimes = 0;
while (state.connecting) {
Expand All @@ -54,7 +54,6 @@ function createSocky() {
}
waitedTimes++;
}
// state.connecting === false
}

if (state.disconnecting) {
Expand All @@ -67,27 +66,26 @@ function createSocky() {
}
waitedTimes++;
}
// state.disconnecting === false
}

// the state could've changed while waitinf for `connecting` or `disconnecting`, if it did - wait more...
// the state could've changed while waiting for `connecting` or
// `disconnecting`, if it did - start connecting again
if (state.connecting || state.disconnecting) {
return await this.shouldConnect();
return await this.beginConnecting();
}

// the socket could've connected in the meantime
if (state.connected) {
return false;
}

return !state.connected;
},
connecting() {
state = { connecting: true, connected: false, disconnecting: false };
return true;
},
connected(connectedSocket: WebSocket) {
socket = connectedSocket;
state = { connected: true, connecting: false, disconnecting: false };
},
disconnected() {
state = { connected: false, connecting: false, disconnecting: false };
socket = null;
},
registerMessageListener(
listener: (event: MessageEvent) => void,
): Disposable {
Expand All @@ -112,7 +110,7 @@ function createSocky() {
// TODO-db-200827 decide if accessing missing socket during send is illegal
if (!socket) {
throw new Error(
'Illegal socket access while sending a message. Has Socky been prepared?',
'Illegal socket access while sending a message. Preparation skipped?',
);
}

Expand All @@ -121,7 +119,6 @@ function createSocky() {
}
},
dispose() {
// start dispose/close/disconnect only if its not alredy being performed
if (!state.disconnecting) {
state = { disconnecting: true, connected: false, connecting: false };

Expand Down Expand Up @@ -153,9 +150,7 @@ export function createClient(options: ClientOptions): Client {

const socky = createSocky();
async function prepare(): Promise<void> {
if (await socky.shouldConnect()) {
socky.connecting();

if (await socky.beginConnecting()) {
return new Promise((resolve, reject) => {
let done = false;
const socket = new WebSocket(url, GRAPHQL_TRANSPORT_WS_PROTOCOL);
Expand All @@ -170,8 +165,6 @@ export function createClient(options: ClientOptions): Client {
*/

socket.onclose = (closeEvent) => {
socky.disconnected(); // just calling `onclose` means immediate disconnection

if (closeEvent.code === 1000 || closeEvent.code === 1001) {
// close event `1000: Normal Closure` is ok and so is `1001: Going Away` (maybe the server is restarting)
completeAllSinks();
Expand All @@ -184,6 +177,7 @@ export function createClient(options: ClientOptions): Client {
errorAllSinks(closeEvent);
}

socky.dispose();
if (!done) {
done = true;
reject(closeEvent);
Expand Down Expand Up @@ -219,10 +213,9 @@ export function createClient(options: ClientOptions): Client {
resolve();
}
} catch (err) {
socket.close();
socky.disconnected();

errorAllSinks(err);

socky.dispose();
if (!done) {
done = true;
reject(err);
Expand Down Expand Up @@ -306,7 +299,7 @@ export function createClient(options: ClientOptions): Client {
delete subscribedSinks[uuid];

if (Object.entries(subscribedSinks).length === 0) {
// dispose of socky :(
// dispose of socky if no subscribers are left :(
socky.dispose();
}
};
Expand Down

0 comments on commit 4406a5b

Please sign in to comment.