Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split async iterator behavior to different file
- Loading branch information
1 parent
c951511
commit d9b617a
Showing
7 changed files
with
98 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
const assertEventName = require('./util'); | ||
const EmitteryClass = require('./index'); | ||
|
||
async function * iterator(emitter, eventName) { | ||
const queue = []; | ||
const off = emitter.on(eventName, data => { | ||
queue.push(data); | ||
}); | ||
|
||
try { | ||
/* eslint-disable no-constant-condition */ | ||
/* eslint-disable no-await-in-loop */ | ||
while (true) { | ||
if (queue.length > 0) { | ||
yield queue.shift(); | ||
} else { | ||
await emitter.once(eventName); | ||
} | ||
} | ||
/* eslint-enable no-constant-condition */ | ||
/* eslint-enable no-await-in-loop */ | ||
} finally { | ||
off(); | ||
} | ||
} | ||
|
||
module.exports = class IterableEmittery extends EmitteryClass { | ||
on(eventName, listener) { | ||
assertEventName(eventName); | ||
if (typeof listener === 'function') { | ||
this._getListeners(eventName).add(listener); | ||
return this.off.bind(this, eventName, listener); | ||
} | ||
return iterator(this, eventName); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import test from 'ava'; | ||
|
||
let Emittery; | ||
try { | ||
Emittery = require('../iterator'); | ||
} catch (err) { | ||
test('does not work due to syntax errors', t => { | ||
t.is(err.name, 'SyntaxError'); | ||
}); | ||
} | ||
|
||
if (Emittery) { | ||
require('./_run')(Emittery); | ||
/* eslint-disable ava/no-async-fn-without-await */ | ||
test('on() - async iterator (for await)', async t => { | ||
t.plan(3); | ||
const fixture = 'π'; | ||
const emitter = new Emittery(); | ||
setInterval(() => { | ||
emitter.emit('π¦', fixture); | ||
}, 50); | ||
let count = 0; | ||
for await (const data of emitter.on('π¦')) { | ||
count++; | ||
if (count >= 3) { | ||
break; | ||
} | ||
t.deepEqual(data, fixture); | ||
} | ||
}); | ||
/* eslint-enable ava/no-async-fn-without-await */ | ||
|
||
test('on() - async iterator', async t => { | ||
const fixture = 'π'; | ||
const emitter = new Emittery(); | ||
const iterator = emitter.on('π¦'); | ||
emitter.emit('π¦', fixture); | ||
const {value, done} = await iterator.next(); | ||
t.deepEqual(done, false); | ||
t.deepEqual(value, fixture); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
module.exports = function (eventName) { | ||
if (typeof eventName !== 'string') { | ||
throw new TypeError('eventName must be a string'); | ||
} | ||
}; |