fluid-func
Version:
A way to write your code with functional programming in mind.
116 lines (98 loc) • 4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPlugins = exports.getLogMonitor = undefined;
exports.getChain = getChain;
exports.getChainDataById = getChainDataById;
exports.getChainContext = getChainContext;
exports.setGetChainContextPlugin = setGetChainContextPlugin;
exports.setGetChainPlugin = setGetChainPlugin;
var _constants = require('./constants');
var _Util = require('../Util');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var GET_CHAIN_METHOD = 'GET_CHAIN_PLUGIN';
var GET_CHAIN_CONTEXT_METHOD = 'GET_CHAIN_CONTEXT_PLUGIN';
var GET_CHAIN_CONFIG_LOG_MONITOR = 'logMonitor';
var GET_CHAIN_CONFIG_PLUGINS = 'plugins';
/**
* Gets the chain instance from storage
* @param storage
* @param name
*/
function getChain(storage, name) {
if (storage[GET_CHAIN_METHOD]) {
return storage[GET_CHAIN_METHOD](name);
}
var chain = Object.assign({}, storage[name]);
chain['$chainId'] = (0, _Util.generateUUID)();
return Object.assign({}, chain);
}
/**
* Gets the chain data from storage by chain id
* @param storage
* @param chainId
*/
function getChainDataById(storage, chainId) {
if (storage[GET_CHAIN_METHOD]) {
return storage[GET_CHAIN_METHOD](chainId);
}
return storage[chainId];
}
/**
* Gets the chain context value from storage
* @param storage
* @param chainId
* @param field
* @returns {*}
*/
function getChainContext(storage, chainId, field) {
if (storage[GET_CHAIN_CONTEXT_METHOD]) {
return storage[GET_CHAIN_CONTEXT_METHOD](chainId, field);
}
if (storage[chainId]) {
return storage[chainId][field];
}
}
/**
* Overrides getChainContext method
* @param storage
* @param plugin
*/
function setGetChainContextPlugin(storage, plugin) {
if (!(plugin instanceof Function)) {
throw new GetChainTypeException();
} else {
storage[GET_CHAIN_CONTEXT_METHOD] = plugin;
}
}
/**
* Overrides getChain method
* @param storage
* @param plugin
*/
function setGetChainPlugin(storage, plugin) {
if (!(plugin instanceof Function)) {
throw new GetChainTypeException();
} else {
storage[GET_CHAIN_METHOD] = plugin;
}
}
var GetChainTypeException = function (_Error) {
_inherits(GetChainTypeException, _Error);
function GetChainTypeException() {
_classCallCheck(this, GetChainTypeException);
return _possibleConstructorReturn(this, (GetChainTypeException.__proto__ || Object.getPrototypeOf(GetChainTypeException)).call(this, 'Get chain plugin must be a function.'));
}
return GetChainTypeException;
}(Error);
var getLogMonitor = exports.getLogMonitor = function getLogMonitor(chainConfig, storage) {
return storage[chainConfig] && storage[chainConfig][GET_CHAIN_CONFIG_LOG_MONITOR] ? storage[chainConfig][GET_CHAIN_CONFIG_LOG_MONITOR] : function () {
return false;
};
};
var getPlugins = exports.getPlugins = function getPlugins(chainConfig, storage) {
return storage[chainConfig] && storage[chainConfig][GET_CHAIN_CONFIG_PLUGINS] ? storage[chainConfig][GET_CHAIN_CONFIG_PLUGINS] : undefined;
};