All files AsyncParallelBailHook.ts

94.74% Statements 54/57
87.5% Branches 7/8
75% Functions 6/8
94.74% Lines 54/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 941x 1x       4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   8x 8x     8x 8x 8x 8x 8x     8x 8x     8x 8x 8x 8x 8x     8x 8x 4x 4x 4x   8x 8x 4x   8x       4x       1x         4x   4x 4x 4x   4x       4x       4x           1x  
import CodeFactory from './CodeFactory'
import Hook from './Hook'
 
class AsyncParallelBailHookCodeFactory extends CodeFactory {
    content({ onError, onResult, onDone }: CodeFactoryContent) {
        let code = ''
        Iif (!this.options || !onResult) {
            return code
        }
        code += `var _results = new Array(${this.options.taps.length});\n`
        code += 'var _checkDone = function() {\n'
        code += 'for(var i = 0; i < _results.length; i++) {\n'
        code += 'var item = _results[i];\n'
        code += 'if(item === undefined) return false;\n'
        code += 'if(item.result !== undefined) {\n'
        code += onResult('item.result')
        code += 'return true;\n'
        code += '}\n'
        code += 'if(item.error) {\n'
        code += onError('item.error')
        code += 'return true;\n'
        code += '}\n'
        code += '}\n'
        code += 'return false;\n'
        code += '}\n'
        code += this.callTapsParallel({
            onError: (i, err, done, doneBreak) => {
                let _code = ''
                _code += `if(${i} < _results.length && ((_results.length = ${
                    i + 1
                }), (_results[${i}] = { error: ${err} }), _checkDone())) {\n`
                _code += doneBreak(true)
                _code += '} else {\n'
                _code += done()
                _code += '}\n'
                return _code
            },
            onResult: (i, result, done, doneBreak) => {
                let _code = ''
                _code += `if(${i} < _results.length && (${result} !== undefined && (_results.length = ${
                    i + 1
                }), (_results[${i}] = { result: ${result} }), _checkDone())) {\n`
                _code += doneBreak(true)
                _code += '} else {\n'
                _code += done()
                _code += '}\n'
                return _code
            },
            onTap: (i, run, done, _doneBreak) => {
                let _code = ''
                if (i > 0) {
                    _code += `if(${i} >= _results.length) {\n`
                    _code += done()
                    _code += '} else {\n'
                }
                _code += run()
                if (i > 0) {
                    _code += '}\n'
                }
                return _code
            },
            onDone
        })
        return code
    }
}
 
const codeFactory = new AsyncParallelBailHookCodeFactory()
 
class AsyncParallelBailHook extends Hook {
    private fns: any[] | undefined
    constructor(args: string[], name?: string) {
        super(args, name)
 
        this.compile = function (options: HookCompileOptions) {
            this.fns = []
            codeFactory.setup(this.fns, options)
 
            return codeFactory.create(options)
        }
 
        // async hook did not support call
        this.call = () => {
            throw new Error('call is not supported on a AsyncParallelBailHook')
        }
 
        this._call = () => {
            throw new Error('call is not supported on a AsyncParallelBailHook')
        }
    }
}
 
export default AsyncParallelBailHook