Skip to content

Commit

Permalink
refactor(client): Stabilize and simplify tests, smaller cleanup (#41)
Browse files Browse the repository at this point in the history
* feat: testing server

* feat: introduce tclient

* refactor: move pong to tserver

* feat: tclient improvements and usage

* fix: expiry timeouts

* feat: pong to ping at key

* fix: emissions on correct key

* fix: wait for closes and correct emitter

* fix: tclient improvements and patches

* refactor: clarify wait

* fix: shift client on done and cleanup

* feat: no wait in tests

* refactor: use tries over retries for better understanding

* chore: graphql-subscriptions is not necessary

* refactor: drop extra complete call

* style: dots for clarity
  • Loading branch information
enisdenjo authored Oct 24, 2020
1 parent 3e49b95 commit 5483a5d
Show file tree
Hide file tree
Showing 6 changed files with 497 additions and 534 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"eslint-config-prettier": "^6.14.0",
"eslint-plugin-prettier": "^3.1.4",
"graphql": "^15.3.0",
"graphql-subscriptions": "^1.1.0",
"jest": "^26.6.1",
"prettier": "^2.1.2",
"semantic-release": "^17.2.1",
Expand Down
19 changes: 11 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export function createClient(options: ClientOptions): Client {
socket: null as WebSocket | null,
acknowledged: false,
locks: 0,
retries: 0,
tries: 0,
};
async function connect(
cancellerRef: CancellerRef,
Expand Down Expand Up @@ -251,7 +251,7 @@ export function createClient(options: ClientOptions): Client {
...state,
acknowledged: false,
socket,
retries: state.retries + 1,
tries: state.tries + 1,
};
emitter.emit('connecting');

Expand Down Expand Up @@ -294,7 +294,7 @@ export function createClient(options: ClientOptions): Client {
}

clearTimeout(tooLong);
state = { ...state, acknowledged: true, socket, retries: 0 };
state = { ...state, acknowledged: true, socket, tries: 0 };
emitter.emit('connected', socket); // connected = socket opened + acknowledged
return resolve();
} catch (err) {
Expand Down Expand Up @@ -376,8 +376,8 @@ export function createClient(options: ClientOptions): Client {
return;
}

// retries expired, close for good
if (state.retries >= retryAttempts) {
// retries are not allowed or we tried to many times, close for good
if (!retryAttempts || state.tries > retryAttempts) {
return;
}

Expand Down Expand Up @@ -418,22 +418,25 @@ export function createClient(options: ClientOptions): Client {
case MessageType.Error: {
if (message.id === id) {
sink.error(message.payload);

// the canceller must be set at this point
// because you cannot receive a message
// if there is no existing connection
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cancellerRef.current!();
// TODO-db-201025 calling canceller will complete the sink, meaning that both the `error` and `complete` will be
// called. neither promises or observables care; once they settle, additional calls to the resolvers will be ignored
}
return;
}
case MessageType.Complete: {
if (message.id === id) {
sink.complete();
// the canceller must be set at this point
// because you cannot receive a message
// if there is no existing connection
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
cancellerRef.current!();
// calling canceller will complete the sink
}
return;
}
Expand Down Expand Up @@ -489,8 +492,8 @@ export function createClient(options: ClientOptions): Client {
return;
}

// retries expired, throw
if (state.retries >= retryAttempts) {
// retries are not allowed or we tried to many times, close for good
if (!retryAttempts || state.tries > retryAttempts) {
throw errOrCloseEvent;
}

Expand Down
Loading

0 comments on commit 5483a5d

Please sign in to comment.