Skip to content

Commit

Permalink
fix(core): add a onQueueEmpty callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Mar 10, 2023
1 parent 2c3c5ff commit 78c23e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export class DaemonClient {
reset() {
this.socketMessenger = null;
this.queue = new PromisedBasedQueue();
this.queue.onQueueEmpty(() => {
this.socketMessenger.close();
});
this.currentMessage = null;
this.currentResolve = null;
this.currentReject = null;
Expand Down
21 changes: 17 additions & 4 deletions packages/nx/src/utils/promised-based-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export class PromisedBasedQueue {
private counter = 0;
private promise = Promise.resolve(null);

private emptyCallback: () => void = null;

sendToQueue(fn: () => Promise<any>): Promise<any> {
this.counter++;
let res, rej;
Expand All @@ -14,19 +16,19 @@ export class PromisedBasedQueue {
.then(async () => {
try {
res(await fn());
this.counter--;
this._promiseResolved();
} catch (e) {
rej(e);
this.counter--;
this._promiseResolved();
}
})
.catch(async () => {
try {
res(await fn());
this.counter--;
this._promiseResolved();
} catch (e) {
rej(e);
this.counter--;
this._promiseResolved();
}
});
return r;
Expand All @@ -35,4 +37,15 @@ export class PromisedBasedQueue {
isEmpty() {
return this.counter === 0;
}

onQueueEmpty(callback: () => void): void {
this.emptyCallback = callback;
}

private _promiseResolved() {
this.counter--;
if (this.isEmpty()) {
this.emptyCallback();
}
}
}

0 comments on commit 78c23e3

Please sign in to comment.