mmlpx
Version:
mobx model layer paradigm
61 lines (55 loc) • 2.34 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = namedModelDecorator;
var _mobx = require('mobx');
var _configure = require('../api/configure');
var _meta = require('../core/dependency-inject/meta');
var _types = require('../utils/types');
/**
* @author Kuitos
* @homepage https://github.com/kuitos/
* @since 2017-12-19
*/
function namedModelDecorator(name, type) {
return function (target) {
target[_meta.modelNameSymbol] = name;
target[_meta.modelTypeSymbol] = type;
if (_configure.isStrict) {
var methodNames = Object.getOwnPropertyNames(target.prototype);
methodNames.forEach(function (methodName) {
var method = target.prototype[methodName];
// when enable the strict mode, the action should not return anything
if ((0, _mobx.isAction)(method)) {
target.prototype[methodName] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var returnedValue = method.apply(this, args);
if (returnedValue) {
var throwStrictError_1 = function () {
throw new SyntaxError('you should not return any values from actions when the strict mode enabled!');
};
if ((0, _types.isPromiseLike)(returnedValue)) {
var promise = returnedValue.then(function (resolved) {
if (resolved !== void 0) {
throwStrictError_1();
}
});
// for testing convenience
if (process.env.NODE_ENV === 'test') {
return promise;
}
} else {
throwStrictError_1();
}
}
};
}
});
}
return target;
};
}
;