-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add defer/stream support for subscriptions
- Loading branch information
1 parent
6c5b85c
commit f5ebcbe
Showing
4 changed files
with
355 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { flattenAsyncIterator } from '../flattenAsyncIterator'; | ||
|
||
describe('flattenAsyncIterator', () => { | ||
it('does not modify an already flat async generator', async () => { | ||
async function* source() { | ||
yield await Promise.resolve(1); | ||
yield await Promise.resolve(2); | ||
yield await Promise.resolve(3); | ||
} | ||
|
||
const result = flattenAsyncIterator(source()); | ||
|
||
expect(await result.next()).to.deep.equal({ value: 1, done: false }); | ||
expect(await result.next()).to.deep.equal({ value: 2, done: false }); | ||
expect(await result.next()).to.deep.equal({ value: 3, done: false }); | ||
expect(await result.next()).to.deep.equal({ | ||
value: undefined, | ||
done: true, | ||
}); | ||
}); | ||
|
||
it('does not modify an already flat async iterator', async () => { | ||
const items = [1, 2, 3]; | ||
|
||
const iterator: any = { | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
next() { | ||
return Promise.resolve({ | ||
done: items.length === 0, | ||
value: items.shift(), | ||
}); | ||
}, | ||
}; | ||
|
||
const result = flattenAsyncIterator(iterator); | ||
|
||
expect(await result.next()).to.deep.equal({ value: 1, done: false }); | ||
expect(await result.next()).to.deep.equal({ value: 2, done: false }); | ||
expect(await result.next()).to.deep.equal({ value: 3, done: false }); | ||
expect(await result.next()).to.deep.equal({ | ||
value: undefined, | ||
done: true, | ||
}); | ||
}); | ||
|
||
it('flatten nested async generators', async () => { | ||
async function* source() { | ||
yield await Promise.resolve(1); | ||
yield await Promise.resolve(2); | ||
yield await Promise.resolve( | ||
(async function* nested(): AsyncGenerator<number, void, void> { | ||
yield await Promise.resolve(2.1); | ||
yield await Promise.resolve(2.2); | ||
})(), | ||
); | ||
yield await Promise.resolve(3); | ||
} | ||
|
||
const doubles = flattenAsyncIterator(source()); | ||
|
||
const result = []; | ||
for await (const x of doubles) { | ||
result.push(x); | ||
} | ||
expect(result).to.deep.equal([1, 2, 2.1, 2.2, 3]); | ||
}); | ||
|
||
it('allows returning early from a nested async generator', async () => { | ||
async function* source() { | ||
yield await Promise.resolve(1); | ||
yield await Promise.resolve(2); | ||
yield await Promise.resolve( | ||
(async function* nested(): AsyncGenerator<number, void, void> { | ||
yield await Promise.resolve(2.1); /* c8 ignore start */ | ||
// Not reachable, early return | ||
yield await Promise.resolve(2.2); | ||
})(), | ||
); | ||
// Not reachable, early return | ||
yield await Promise.resolve(3); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
const doubles = flattenAsyncIterator(source()); | ||
|
||
expect(await doubles.next()).to.deep.equal({ value: 1, done: false }); | ||
expect(await doubles.next()).to.deep.equal({ value: 2, done: false }); | ||
expect(await doubles.next()).to.deep.equal({ value: 2.1, done: false }); | ||
|
||
// Early return | ||
expect(await doubles.return()).to.deep.equal({ | ||
value: undefined, | ||
done: true, | ||
}); | ||
|
||
// Subsequent next calls | ||
expect(await doubles.next()).to.deep.equal({ | ||
value: undefined, | ||
done: true, | ||
}); | ||
expect(await doubles.next()).to.deep.equal({ | ||
value: undefined, | ||
done: true, | ||
}); | ||
}); | ||
|
||
it('allows throwing errors from a nested async generator', async () => { | ||
async function* source() { | ||
yield await Promise.resolve(1); | ||
yield await Promise.resolve(2); | ||
yield await Promise.resolve( | ||
(async function* nested(): AsyncGenerator<number, void, void> { | ||
yield await Promise.resolve(2.1); /* c8 ignore start */ | ||
// Not reachable, early return | ||
yield await Promise.resolve(2.2); | ||
})(), | ||
); | ||
// Not reachable, early return | ||
yield await Promise.resolve(3); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
const doubles = flattenAsyncIterator(source()); | ||
|
||
expect(await doubles.next()).to.deep.equal({ value: 1, done: false }); | ||
expect(await doubles.next()).to.deep.equal({ value: 2, done: false }); | ||
expect(await doubles.next()).to.deep.equal({ value: 2.1, done: false }); | ||
|
||
// Throw error | ||
let caughtError; | ||
try { | ||
await doubles.throw('ouch'); /* c8 ignore start */ | ||
// Not reachable, always throws | ||
/* c8 ignore stop */ | ||
} catch (e) { | ||
caughtError = e; | ||
} | ||
expect(caughtError).to.equal('ouch'); | ||
}); | ||
}); |
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
Oops, something went wrong.