mmlpx
Version:
mobx model layer paradigm
40 lines • 1.46 kB
JavaScript
/**
* @author Kuitos
* @homepage https://github.com/kuitos/
* @since 2017-07-11
*/
import 'reflect-metadata';
import hydrate from '../hydrate';
import instantiate from '../instantiate';
export default (function (InjectedClass) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return function (target, property) {
var symbol = Symbol(property);
if (!InjectedClass) {
InjectedClass = Reflect.getMetadata('design:type', target, property);
/* istanbul ignore next */
if (!InjectedClass) {
throw new SyntaxError('You must pass a Class for injection while you are not using typescript!' + 'Or you may need to add "emitDecoratorMetadata: true" configuration to your tsconfig.json');
}
}
return {
enumerable: true,
configurable: true,
get: function () {
if (!this[symbol]) {
var initializedValue = instantiate.apply(this, [InjectedClass].concat(args));
this[symbol] = initializedValue;
return initializedValue;
} else {
return hydrate.apply(void 0, [this[symbol], InjectedClass].concat(args));
}
},
// @formatter:off
// tslint:disable-next-line
set: function () {}
};
};
});