@turbox3d/renderer-core
Version:
Large-scale declarative graphic ui core renderer
53 lines • 1.66 kB
JavaScript
import { fail } from '@turbox3d/shared';
import { store, depCollector, UNSUBSCRIBE_HANDLER } from '@turbox3d/reactivity';
import { IdCustomType } from './common';
export var TURBOX_PREFIX = '@@TURBOX__';
var IS_INNER = "".concat(TURBOX_PREFIX, "isInner");
export 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 {
depCollector.start(this);
result = baseRender.call(this);
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 (!store) {
fail('store is not ready, please init first.');
}
this[UNSUBSCRIBE_HANDLER] = store.subscribe(function (isInner) {
_this[IS_INNER] = isInner;
_this.forceUpdate();
}, this, IdCustomType);
};
target.componentWillUnmount = function () {
if (this[UNSUBSCRIBE_HANDLER] !== void 0) {
this[UNSUBSCRIBE_HANDLER]();
}
depCollector.clear(this);
baseComponentWillUnmount && baseComponentWillUnmount.call(this);
};
return target.constructor;
};
if (arg === void 0) {
// @reactive()
return decorator;
}
// @reactive
return decorator.call(null, arg);
}