Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 12, 2019
1 parent aea0ca8 commit cc2f580
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 90 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
50 changes: 30 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ const normalizeEvents = event => Array.isArray(event) ? event : [event];
const multiple = (emitter, event, options) => {
let cancel;
const ret = new Promise((resolve, reject) => {
options = Object.assign({
options = {
rejectionEvents: ['error'],
multiArgs: false,
resolveImmediately: false
}, options);
resolveImmediately: false,
...options
};

if (!(options.count >= 0 && (options.count === Infinity || Number.isInteger(options.count)))) {
throw new TypeError('The `count` option should be at least 0 or more');
Expand Down Expand Up @@ -97,14 +98,14 @@ module.exports = (emitter, event, options) => {
options = {filter: options};
}

options = Object.assign({}, options, {
options = {
...options,
count: 1,
resolveImmediately: false
});
};

const arrayPromise = multiple(emitter, event, options);

const promise = arrayPromise.then(array => array[0]);
const promise = arrayPromise.then(array => array[0]); // eslint-disable-line promise/prefer-await-to-then
promise.cancel = arrayPromise.cancel;

return promise;
Expand All @@ -120,12 +121,13 @@ module.exports.iterator = (emitter, event, options) => {
// Allow multiple events
const events = normalizeEvents(event);

options = Object.assign({
options = {
rejectionEvents: ['error'],
resolutionEvents: [],
limit: Infinity,
multiArgs: false
}, options);
multiArgs: false,
...options
};

const {limit} = options;
const isValidLimit = limit >= 0 && (limit === Infinity || Number.isInteger(limit));
Expand All @@ -139,14 +141,15 @@ module.exports.iterator = (emitter, event, options) => {
[Symbol.asyncIterator]() {
return this;
},
next() {
return Promise.resolve({done: true, value: undefined});
async next() {
return {
done: true,
value: undefined
};
}
};
}

let isLimitReached = false;

const {addListener, removeListener} = normalizeEmitter(emitter);

let done = false;
Expand All @@ -155,6 +158,7 @@ module.exports.iterator = (emitter, event, options) => {
const nextQueue = [];
const valueQueue = [];
let eventCount = 0;
let isLimitReached = false;

const valueHandler = (...args) => {
eventCount++;
Expand Down Expand Up @@ -247,26 +251,32 @@ module.exports.iterator = (emitter, event, options) => {
[symbolAsyncIterator]() {
return this;
},
next() {
async next() {
if (valueQueue.length > 0) {
const value = valueQueue.shift();
return Promise.resolve({done: done && valueQueue.length === 0 && !isLimitReached, value});
return {
done: done && valueQueue.length === 0 && !isLimitReached,
value
};
}

if (hasPendingError) {
hasPendingError = false;
return Promise.reject(error);
throw error;
}

if (done) {
return Promise.resolve({done: true, value: undefined});
return {
done: true,
value: undefined
};
}

return new Promise((resolve, reject) => nextQueue.push({resolve, reject}));
},
return(value) {
async return(value) {
cancel();
return Promise.resolve({done, value});
return {done, value};
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand Down
Loading

0 comments on commit cc2f580

Please sign in to comment.