fix: stack overflow on large number of templates, #513

This commit is contained in:
Harttle
2022-07-08 01:46:51 +08:00
parent 2f87708989
commit 3dc4290b56
16 changed files with 172 additions and 180 deletions
+19 -21
View File
@@ -1,4 +1,4 @@
import { toThenable, toPromise, toValue } from '../../../src/util/async'
import { toPromise, toValue } from '../../../src/util/async'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
@@ -13,47 +13,45 @@ describe('utils/async', () => {
const result = await toPromise(foo())
expect(result).to.equal('foo')
})
})
describe('#toThenable()', function () {
it('should support iterable with single return statement', async () => {
function * foo () {
return 'foo'
}
const result = await toThenable(foo())
const result = await toPromise(foo())
expect(result).to.equal('foo')
})
it('should support promise', async () => {
function foo () {
return Promise.resolve('foo')
}
const result = await toThenable(foo())
const result = await toPromise(foo())
expect(result).to.equal('foo')
})
it('should resolve dependency', async () => {
function * foo () {
function * foo (): Generator<Generator<string>> {
return yield bar()
}
function * bar () {
function * bar (): Generator<string> {
return 'bar'
}
const result = await toThenable(foo())
const result = await toPromise(foo())
expect(result).to.equal('bar')
})
it('should support promise dependency', async () => {
function * foo () {
function * foo (): Generator<Promise<string>> {
return yield Promise.resolve('foo')
}
const result = await toThenable(foo())
const result = await toPromise(foo())
expect(result).to.equal('foo')
})
it('should reject Promise if dependency throws syncly', done => {
function * foo () {
function * foo (): Generator<Generator<never>> {
return yield bar()
}
function * bar (): IterableIterator<any> {
function * bar (): Generator<never> {
throw new Error('bar')
}
toThenable(foo()).catch(err => {
toPromise(foo()).catch(err => {
expect(err.message).to.equal('bar')
done()
return 0 as any
@@ -70,43 +68,43 @@ describe('utils/async', () => {
ret += 'foo'
return ret
}
function * bar (): IterableIterator<any> {
function * bar (): Generator<never> {
throw new Error('bar')
}
const ret = await toThenable(foo())
const ret = await toPromise(foo())
expect(ret).to.equal('barfoo')
})
})
describe('#toValue()', function () {
it('should throw Error if dependency throws syncly', () => {
function * foo () {
function * foo (): Generator<Generator<never>> {
return yield bar()
}
function * bar (): IterableIterator<any> {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(() => toValue(foo())).to.throw('bar')
})
it('should resume yield after catch', () => {
function * foo () {
function * foo (): Generator<unknown, never, never> {
try {
yield bar()
} catch (e) {}
return yield 'foo'
}
function * bar (): IterableIterator<any> {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValue(foo())).to.equal('foo')
})
it('should resume return after catch', () => {
function * foo () {
function * foo (): Generator<Generator<never>, string> {
try {
yield bar()
} catch (e) {}
return 'foo'
}
function * bar (): IterableIterator<any> {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValue(foo())).to.equal('foo')