@turbox3d/renderer-core
Version:
Large-scale declarative graphic ui core renderer
60 lines (59 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Reactive = Reactive;
exports.TURBOX_PREFIX = void 0;
var _shared = require("@turbox3d/shared");
var _reactivity = require("@turbox3d/reactivity");
var _common = require("./common");
var TURBOX_PREFIX = exports.TURBOX_PREFIX = '@@TURBOX__';
var IS_INNER = "".concat(TURBOX_PREFIX, "isInner");
function Reactive(arg) {
var decorator = function decorator(Target) {
// class component
var target = Target.prototype;
var baseRender = target.render;
var baseComponentDidMount = target.componentDidMount;
var baseComponentWillUnmount = target.componentWillUnmount;
target.render = function () {
var result;
try {
_reactivity.depCollector.start(this);
result = baseRender.call(this);
_reactivity.depCollector.end();
} catch (error) {
if (!this[IS_INNER]) {
throw error;
}
return null;
}
return result;
};
target.componentDidMount = function () {
var _this = this;
baseComponentDidMount && baseComponentDidMount.call(this);
if (!_reactivity.store) {
(0, _shared.fail)('store is not ready, please init first.');
}
this[_reactivity.UNSUBSCRIBE_HANDLER] = _reactivity.store.subscribe(function (isInner) {
_this[IS_INNER] = isInner;
_this.forceUpdate();
}, this, _common.IdCustomType);
};
target.componentWillUnmount = function () {
if (this[_reactivity.UNSUBSCRIBE_HANDLER] !== void 0) {
this[_reactivity.UNSUBSCRIBE_HANDLER]();
}
_reactivity.depCollector.clear(this);
baseComponentWillUnmount && baseComponentWillUnmount.call(this);
};
return target.constructor;
};
if (arg === void 0) {
// @reactive()
return decorator;
}
// @reactive
return decorator.call(null, arg);
}