Skip to content

Commit

Permalink
fixed #125 MaxListenersExceededWarning was triggered by gracefulExit …
Browse files Browse the repository at this point in the history
…handlers added in v3.11.0
  • Loading branch information
AndiDittrich committed May 15, 2022
1 parent 31692f2 commit 33ef015
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Branch 3.x ##

### 3.11.1 ###

* Bugfix: `MaxListenersExceededWarning` was triggered by `gracefulExit` handlers added in `v3.11.0` - thanks to [TychoTheTaco on GitHub](https://github.com/npkgz/cli-progress/pull/125)

### 3.11.0 ###

* Added: `log()` convenience method the multibar to enable custom logging output on top of the progress bars during operation
Expand Down
20 changes: 15 additions & 5 deletions lib/multi-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ module.exports = class MultiBar extends _EventEmitter{
// logging output buffer
this.loggingBuffer = [];

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.options.gracefulExit){
process.once('SIGINT', this.stop.bind(this));
process.once('SIGTERM', this.stop.bind(this));
}
// callback used for gracefulExit
this.sigintCallback = null;
}

// add a new bar to the stack
Expand All @@ -52,6 +49,13 @@ module.exports = class MultiBar extends _EventEmitter{
if (this.options.noTTYOutput === false && this.terminal.isTTY() === false){
return bar;
}

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.sigintCallback === null && this.options.gracefulExit){
this.sigintCallback = this.stop.bind(this);
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
}

// multiprogress already active ?
if (!this.isActive){
Expand Down Expand Up @@ -170,6 +174,12 @@ module.exports = class MultiBar extends _EventEmitter{
clearTimeout(this.timer);
this.timer = null;

// remove sigint listener
if (this.sigintCallback){
process.removeListener('SIGINT', this.sigintCallback);
process.removeListener('SIGTERM', this.sigintCallback);
}

// set flag
this.isActive = false;

Expand Down
20 changes: 15 additions & 5 deletions lib/single-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ module.exports = class SingleBar extends _GenericBar{
// update interval
this.schedulingRate = (this.terminal.isTTY() ? this.options.throttleTime : this.options.notTTYSchedule);

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.options.gracefulExit){
process.once('SIGINT', this.stop.bind(this));
process.once('SIGTERM', this.stop.bind(this));
}
// callback used for gracefulExit
this.sigintCallback = null;
}

// internal render function
Expand Down Expand Up @@ -68,6 +65,13 @@ module.exports = class SingleBar extends _GenericBar{
return;
}

// add handler to restore cursor settings (stop the bar) on SIGINT/SIGTERM ?
if (this.sigintCallback === null && this.options.gracefulExit){
this.sigintCallback = this.stop.bind(this);
process.once('SIGINT', this.sigintCallback);
process.once('SIGTERM', this.sigintCallback);
}

// save current cursor settings
this.terminal.cursorSave();

Expand All @@ -94,6 +98,12 @@ module.exports = class SingleBar extends _GenericBar{
if (!this.timer) {
return;
}

// remove sigint listener
if (this.sigintCallback){
process.removeListener('SIGINT', this.sigintCallback);
process.removeListener('SIGTERM', this.sigintCallback);
}

// trigger final rendering
this.render();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli-progress",
"version": "3.11.0",
"version": "3.11.1",
"description": "easy to use progress-bar for command-line/terminal applications",
"keywords": [
"cli",
Expand Down

0 comments on commit 33ef015

Please sign in to comment.