mmlpx
Version:
mobx model layer paradigm
50 lines • 2.22 kB
JavaScript
/**
* @author Kuitos
* @homepage https://github.com/kuitos/
* @since 2017-12-19
*/
import { isAction } from 'mobx';
import { isStrict } from '../api/configure';
import { modelNameSymbol, modelTypeSymbol } from '../core/dependency-inject/meta';
import { isPromiseLike } from '../utils/types';
export default function namedModelDecorator(name, type) {
return function (target) {
target[modelNameSymbol] = name;
target[modelTypeSymbol] = type;
if (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 (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 (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;
};
}