All files action.ts

95.65% Statements 22/23
100% Branches 2/2
100% Functions 5/5
95.45% Lines 21/22

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  6x               6x 6x   22x   22x 22x           22x 22x 16x     22x   6x   7x 7x 10x 10x 10x 10x   7x   6x   3x   6x
/*
 * @Author: lijianzhang
 * @Date: 2018-03-31 21:31:41
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-08-29 23:53:44
 * @flow
 */
import store from './store';
import event from './event';
 
 
export function runAction(this: any, fn: Function, ...args: any[]) {
    store.stack += 1;
    let value;
    try {
        value = fn.call(this, ...args);
    } catch (error) {
        store.onError(error);
    } finally {
        store.stack -= 1;
        if (store.stack === 0) {
            store.runQueue();
        }
    }
    return value;
}
 
 
 
export function decorativeAction(target: any, key: string, descriptor: PropertyDescriptor){
    const { value } = descriptor;
    descriptor.value = function wrapAction(...args) {
        event.emit('action', `${target.constructor.name}.${key}`);
        const res = runAction.call(this, value, ...args);
        event.emit('endAction', `${target.constructor.name}.${key}`);
        return res;
    };
    return descriptor;
}
 
export function WrapperAction(fn: Function) {
    return (...args: Array<any>) => runAction(fn, ...args)
}