Skip to content

Commit

Permalink
refactor(client): releaser current ref is unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Mar 25, 2021
1 parent 84462c8 commit 1b6bf3f
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,11 @@ export function createClient(options: ClientOptions): Client {
subscribe(payload, sink) {
const id = generateID();

let completed = false;
const releaserRef = {
current: () => {
let completed = false,
releaser = () => {
// for handling completions before connect
completed = true;
},
};
};

(async () => {
for (;;) {
Expand All @@ -532,7 +530,7 @@ export function createClient(options: ClientOptions): Client {
if (message.id === id) {
completed = true;
sink.error(message.payload);
releaserRef.current();
releaser();
// TODO-db-201025 calling releaser 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
}
Expand All @@ -541,7 +539,7 @@ export function createClient(options: ClientOptions): Client {
case MessageType.Complete: {
if (message.id === id) {
completed = true;
releaserRef.current(); // release completes the sink
releaser(); // release completes the sink
}
return;
}
Expand All @@ -556,7 +554,7 @@ export function createClient(options: ClientOptions): Client {
}),
);

releaserRef.current = () => {
releaser = () => {
if (!completed && socket.readyState === WebSocketImpl.OPEN)
// if not completed already and socket is open, send complete message to server on release
socket.send(
Expand All @@ -582,7 +580,7 @@ export function createClient(options: ClientOptions): Client {
.catch(sink.error)
.then(sink.complete); // resolves on release or normal closure

return () => releaserRef.current();
return () => releaser();
},
async dispose() {
disposed = true;
Expand Down

0 comments on commit 1b6bf3f

Please sign in to comment.