async-iterable-stream
Advanced tools
Comparing version 2.1.0 to 3.0.0
48
index.js
class AsyncIterableStream { | ||
createAsyncIterator() { | ||
throw new TypeError('Method must be overriden by subclass'); | ||
next(timeout) { | ||
return this.createAsyncIterator(timeout).next(); | ||
} | ||
next() { | ||
return this.createAsyncIterator().next(); | ||
} | ||
async _once() { | ||
let result = await this.next(); | ||
async once(timeout) { | ||
let result = await this.next(timeout); | ||
if (result.done) { | ||
@@ -19,20 +15,12 @@ // If stream was ended, this function should never resolve. | ||
async once(timeout) { | ||
if (timeout === undefined) { | ||
return this._once(); | ||
createAsyncIterator() { | ||
throw new TypeError('Method must be overriden by subclass'); | ||
} | ||
createAsyncIterable(timeout) { | ||
return { | ||
[Symbol.asyncIterator]: () => { | ||
return this.createAsyncIterator(timeout); | ||
} | ||
} | ||
let delay = wait(timeout); | ||
return Promise.race([ | ||
(async () => { | ||
await delay.promise; | ||
let error = new Error('The once promise timed out'); | ||
error.name = 'TimeoutError'; | ||
throw error; | ||
})(), | ||
(async () => { | ||
let value = await this._once(); | ||
clearTimeout(delay.id); | ||
return value; | ||
})() | ||
]); | ||
} | ||
@@ -45,12 +33,2 @@ | ||
function wait(timeout) { | ||
let id; | ||
let promise = new Promise((resolve) => { | ||
id = setTimeout(() => { | ||
resolve(); | ||
}, timeout); | ||
}); | ||
return {id, promise}; | ||
} | ||
module.exports = AsyncIterableStream; |
{ | ||
"name": "async-iterable-stream", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "A readable async stream which can be iterated over using a for-await-of loop.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -125,53 +125,3 @@ const AsyncIterableStream = require('../index'); | ||
}); | ||
it('should receive next packet asynchronously when once() method is used and sufficiently long timeout values are provided', async () => { | ||
let nextPacket = await stream.once(30); | ||
assert.equal(nextPacket, 0); | ||
nextPacket = await stream.once(30); | ||
assert.equal(nextPacket, 1); | ||
nextPacket = await stream.once(30); | ||
assert.equal(nextPacket, 2); | ||
}); | ||
it('should throw error if timeout value is specified and it occurs before the next once() value is received', async () => { | ||
let nextPacket; | ||
let error; | ||
try { | ||
// Set the timeout to 10 ms. Next packet is in 20 ms. | ||
nextPacket = await stream.once(10); | ||
} catch (err) { | ||
error = err; | ||
} | ||
assert.equal(nextPacket, null); | ||
assert.notEqual(error, null); | ||
assert.equal(error.name, 'TimeoutError'); | ||
}); | ||
}); | ||
describe('AsyncIterableStream subclass - Inactive stream', () => { | ||
let stream; | ||
beforeEach(async () => { | ||
stream = new AsyncIterableStreamSubclass([new Promise(() => {})]); | ||
}); | ||
afterEach(async () => { | ||
cancelAllPendingWaits(); | ||
}); | ||
it('should throw error if once() does not resolve before a specified timeout', async () => { | ||
let nextPacket; | ||
let error; | ||
try { | ||
nextPacket = await stream.once(100); | ||
} catch (err) { | ||
error = err; | ||
} | ||
assert.equal(nextPacket, null); | ||
assert.notEqual(error, null); | ||
assert.equal(error.name, 'TimeoutError'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7330
132