All files / lib/src getDataFromTree.js

96.23% Statements 102/106
86.9% Branches 73/84
100% Functions 22/22
96.88% Lines 93/96

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168    133x     305x     133x     105x     313x 6x 2x   311x 6x   305x 261x 133x 133x 133x 133x 133x 105x 105x     105x 105x 105x 3x 1x   3x   105x 1x 1x 1x     104x 1x   103x 2x   105x 19x   105x 27x   78x     28x     28x   104x 100x 10x     95x       128x 8x     8x 8x 2x 2x     6x   8x 8x 4x     6x         120x     120x 72x 124x 122x           44x 44x       95x     30x     46x 46x 46x 201x 30x 30x 27x 27x       44x     46x 46x 44x 24x   20x 27x 27x 26x 3x   20x     20x   18x   1x   1x 1x 1x       20x 20x 20x 20x 20x      
import * as React from 'react';
function getProps(element) {
    return element.props || element.attributes;
}
function isReactElement(element) {
    return !!element.type;
}
function isComponentClass(Comp) {
    return Comp.prototype && (Comp.prototype.render || Comp.prototype.isReactComponent);
}
function providesChildContext(instance) {
    return !!instance.getChildContext;
}
export function walkTree(element, context, visitor) {
    if (Array.isArray(element)) {
        element.forEach(function (item) { return walkTree(item, context, visitor); });
        return;
    }
    if (!element) {
        return;
    }
    if (isReactElement(element)) {
        if (typeof element.type === 'function') {
            var Comp = element.type;
            var props = Object.assign({}, Comp.defaultProps, getProps(element));
            var childContext_1 = context;
            var child = void 0;
            if (isComponentClass(Comp)) {
                var instance_1 = new Comp(props, context);
                Object.defineProperty(instance_1, 'props', {
                    value: instance_1.props || props,
                });
                instance_1.context = instance_1.context || context;
                instance_1.state = instance_1.state || null;
                instance_1.setState = function (newState) {
                    if (typeof newState === 'function') {
                        newState = newState(instance_1.state, instance_1.props, instance_1.context);
                    }
                    instance_1.state = Object.assign({}, instance_1.state, newState);
                };
                if (Comp.getDerivedStateFromProps) {
                    var result = Comp.getDerivedStateFromProps(instance_1.props, instance_1.state);
                    Eif (result !== null) {
                        instance_1.state = Object.assign({}, instance_1.state, result);
                    }
                }
                else if (instance_1.UNSAFE_componentWillMount) {
                    instance_1.UNSAFE_componentWillMount();
                }
                else if (instance_1.componentWillMount) {
                    instance_1.componentWillMount();
                }
                if (providesChildContext(instance_1)) {
                    childContext_1 = Object.assign({}, context, instance_1.getChildContext());
                }
                if (visitor(element, instance_1, context, childContext_1) === false) {
                    return;
                }
                child = instance_1.render();
            }
            else {
                Iif (visitor(element, null, context) === false) {
                    return;
                }
                child = Comp(props, context);
            }
            if (child) {
                if (Array.isArray(child)) {
                    child.forEach(function (item) { return walkTree(item, childContext_1, visitor); });
                }
                else {
                    walkTree(child, childContext_1, visitor);
                }
            }
        }
        else if (element.type._context || element.type.Consumer) {
            Iif (visitor(element, null, context) === false) {
                return;
            }
            var child = void 0;
            if (element.type._context) {
                element.type._context._currentValue = element.props.value;
                child = element.props.children;
            }
            else {
                child = element.props.children(element.type._currentValue);
            }
            Eif (child) {
                if (Array.isArray(child)) {
                    child.forEach(function (item) { return walkTree(item, context, visitor); });
                }
                else {
                    walkTree(child, context, visitor);
                }
            }
        }
        else {
            Iif (visitor(element, null, context) === false) {
                return;
            }
            if (element.props && element.props.children) {
                React.Children.forEach(element.props.children, function (child) {
                    if (child) {
                        walkTree(child, context, visitor);
                    }
                });
            }
        }
    }
    else Eif (typeof element === 'string' || typeof element === 'number') {
        visitor(element, null, context);
    }
}
function hasFetchDataFunction(instance) {
    return typeof instance.fetchData === 'function';
}
function isPromise(promise) {
    return typeof promise.then === 'function';
}
function getPromisesFromTree(_a) {
    var rootElement = _a.rootElement, _b = _a.rootContext, rootContext = _b === void 0 ? {} : _b;
    var promises = [];
    walkTree(rootElement, rootContext, function (_, instance, context, childContext) {
        if (instance && hasFetchDataFunction(instance)) {
            var promise = instance.fetchData();
            if (isPromise(promise)) {
                promises.push({ promise: promise, context: childContext || context, instance: instance });
                return false;
            }
        }
    });
    return promises;
}
function getDataAndErrorsFromTree(rootElement, rootContext, storeError) {
    Iif (rootContext === void 0) { rootContext = {}; }
    var promises = getPromisesFromTree({ rootElement: rootElement, rootContext: rootContext });
    if (!promises.length) {
        return Promise.resolve();
    }
    var mappedPromises = promises.map(function (_a) {
        var promise = _a.promise, context = _a.context, instance = _a.instance;
        return promise
            .then(function (_) { return getDataAndErrorsFromTree(instance.render(), context, storeError); })
            .catch(function (e) { return storeError(e); });
    });
    return Promise.all(mappedPromises);
}
function processErrors(errors) {
    switch (errors.length) {
        case 0:
            break;
        case 1:
            throw errors.pop();
        default:
            var wrapperError = new Error(errors.length + " errors were thrown when executing your fetchData functions.");
            wrapperError.queryErrors = errors;
            throw wrapperError;
    }
}
export default function getDataFromTree(rootElement, rootContext) {
    Eif (rootContext === void 0) { rootContext = {}; }
    var errors = [];
    var storeError = function (error) { return errors.push(error); };
    return getDataAndErrorsFromTree(rootElement, rootContext, storeError).then(function (_) {
        return processErrors(errors);
    });
}
//# sourceMappingURL=getDataFromTree.js.map