Skip to content

Commit

Permalink
fixed bug causing once events to run more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Mar 14, 2024
1 parent f1e4651 commit a1c1110
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### Fixes

* Fixed prompt showing in `non-interactive` environments
* Fixed `once` events running more than `once`

## v3.21.0-beta.9 - [March 10, 2024](https://github.com/lando/core/releases/tag/v3.21.0-beta.9)

Expand Down
22 changes: 7 additions & 15 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ class AsyncEvents extends EventEmitter {
.value();

// Map to array of func
const fns = _.map(evnts, evnt => evnt.fn);

// if (name === 'engine-autostart') {
// console.log(fns);
// }
const fns = _.map(evnts, evnt => ({fn: evnt.fn, id: evnt.id}));

// Log non engine events so we can keep things quiet
if (!_.includes(name, '-engine-')) {
Expand All @@ -116,27 +112,23 @@ class AsyncEvents extends EventEmitter {
}

// Make listener functions to a promise in series.
return Promise.each(fns, fn => {
return Promise.each(fns, listener => {
const {fn, id} = listener;
// Clone function arguments.
const fnArgs = args.slice();
// Add listener function to front of arguments.
fnArgs.unshift(fn);
// If its a onetimer then remove it from listeners
if (fn.name && fn.name.includes('onceWrapper')) {
this._listeners = this._listeners.filter(listener => listener.id !== id);
}
// Apply function that calls the listener function and returns a promise.
return handle.apply(self, fnArgs);
})

// Make sure to wait for all mappings.
.all()

// .then(() => {
// if (name === 'engine-autostart') {
// console.log(_(this._listeners)
// .filter(listener => listener.name === 'engine-autostart')
// .sortBy('priority')
// .value());
// }
// })

// Return true if event had listeners just like the original emit function.
.return(!!fns.length);
};
Expand Down

0 comments on commit a1c1110

Please sign in to comment.