All files react-observer.js

0% Statements 0/74
0% Branches 0/36
0% Functions 0/20
0% Lines 0/70

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                                                                                                                                                                                                                                                 
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
exports.__esModule = true;
/*
 * @Author: lijianzhang
 * @Date: 2018-03-31 21:40:26
 * @Last Modified by: lijianzhang
 * @Last Modified time: 2018-08-29 23:50:29
 * @flow
 */
var react_1 = require("react");
var observer_1 = require("./observer");
var constant_1 = require("./constant");
var baseRenderKey = Symbol('baseRender');
var isReCollectDepsKey = Symbol('isReCollectDeps');
var connectKey = Symbol('connect');
var $componentWillMount = Symbol('componentWillMount');
function reactiveRender() {
    return this[constant_1.OBSERVER_COMPONENT_KEY].collectDep(this[baseRenderKey]);
}
function initRender() {
    var _this = this;
    this[constant_1.OBSERVER_COMPONENT_KEY] = new observer_1["default"](function () {
        if (!_this[isReCollectDepsKey] && _this[$componentWillMount]) {
            _this[isReCollectDepsKey] = true;
            _this.forceUpdate();
        }
    }, this.constructor.name + ".render()");
    this.render = reactiveRender;
    return reactiveRender.call(this);
}
var reactiveMixin = {
    componentWillMount: function () {
        this[$componentWillMount] = true;
        this[baseRenderKey] = this.render.bind(this);
        this.render = initRender;
    },
    componentWillUnmount: function () {
        this[constant_1.OBSERVER_COMPONENT_KEY].unSubscribe();
        this[constant_1.OBSERVER_COMPONENT_KEY] = null;
    }
};
function patch(target, funcName, runMixinFirst) {
    if (runMixinFirst === void 0) { runMixinFirst = false; }
    var base = target[funcName];
    var mixinFunc = reactiveMixin[funcName];
    if (!base) {
        target[funcName] = mixinFunc;
    }
    else {
        target[funcName] =
            runMixinFirst === true ?
                function funcName() {
                    var args = [];
                    for (var _i = 0; _i < arguments.length; _i++) {
                        args[_i] = arguments[_i];
                    }
                    mixinFunc.apply(this, args);
                    base.apply(this, args);
                } :
                function funcName() {
                    var args = [];
                    for (var _i = 0; _i < arguments.length; _i++) {
                        args[_i] = arguments[_i];
                    }
                    base.apply(this, args);
                    mixinFunc.apply(this, args);
                };
    }
}
function createObserverComponent(component) {
    var _a;
    return _a = /** @class */ (function (_super) {
            __extends(class_1, _super);
            function class_1() {
                return _super !== null && _super.apply(this, arguments) || this;
            }
            class_1.prototype.render = function () {
                return component.call(this, this.props, this.context);
            };
            return class_1;
        }(react_1["default"].PureComponent)),
        _a.displayName = component.displayName || component.name,
        _a.propTypes = component.propTypes,
        _a.contextTypes = component.contextTypes,
        _a.defaultProps = component.defaultProps,
        _a;
}
function ReactObserver(target) {
    if (typeof target === 'function' && !target.prototype)
        return ReactObserver(createObserverComponent(target));
    if (target[connectKey])
        return target;
    target[connectKey] = true;
    patch(target.prototype, 'componentWillMount', true);
    patch(target.prototype, 'componentWillUnmount');
    return target;
}
exports["default"] = ReactObserver;
function ObserverComponent(_a) {
    var children = _a.children, render = _a.render;
    var Component = children || render;
    if (!Component)
        return null;
    Component = ReactObserver(Component);
    return react_1["default"].createElement(Component);
}
exports.ObserverComponent = ObserverComponent;