Code coverage report for ReactFlux/lib/actions.js

Statements: 97.37% (37 / 38)      Branches: 100% (15 / 15)      Functions: 81.82% (9 / 11)      Lines: 97.37% (37 / 38)      Ignored: none     

All files » ReactFlux/lib/ » actions.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 851 1 1 1   1   1   23 23       1           23 58 1   57 57 57 1     56 1   56                   56 13 13 13 13 13 2     2 2     13 11 11   2 2                           39 26   39         1  
var _merge = require('lodash-node/modern/object/merge');
var _forEach = require('lodash-node/modern/collection/forEach');
var _isArray = require('lodash-node/modern/lang/isArray');
var Promise = Promise || require('promise');
 
var constantsConfigs = require('./configs').constants.get();
 
var Actions = function (dispather, actions) {
 
	this._dispatcher = dispather;
	this._registerActions(actions);
 
};
 
Actions.prototype = _merge(Actions.prototype, {
 
	/**
	 *@param {object} actions
	 */
	_registerActions: function (actions) {
		_forEach(actions, function (options, actionName) {
			if (!_isArray(options)) {
				throw new Error('ReactFlux.Actions: Action must be an array {login: [CONSTANT, callback]}');
			}
			var constant = options[0];
			var callback = options[1];
			if (typeof callback === "undefined") {
				callback = function () {
				};
			}
			else if (typeof callback != 'function') {
				throw new Error('ReactFlux.Actions: you did not provide a valid callback for action: ' + actionName);
			}
			this[actionName] = this._createAction(actionName, constant, callback);
		}.bind(this));
	},
 
	/**
	 *@param {string} name
	 *@param {string} constant
	 *@param {string} callback
	 */
	_createAction: function (name, constant, callback) {
		return function () {
			this._dispatch(constant, null, arguments);
			var resp = null;
			try {
				resp = callback.apply(this, arguments);
				if (!!resp && typeof resp == 'object' && Object.prototype.toString.call(resp) == '[object Error]') {
					throw resp;
				}
			} catch (e) {
				resp = new Promise(function (_, reject) {
					reject(e);
				});
			}
			Promise.resolve(resp).then(function (payload) {
				this._dispatch(constant, 'successSuffix', payload);
				this._dispatch(constant, 'afterSuffix', payload);
			}.bind(this), function (payload) {
				this._dispatch(constant, 'failSuffix', payload);
				this._dispatch(constant, 'afterSuffix', payload);
			}.bind(this))
				.catch(function (e) {
					console.error(e.toString(), e.stack);
				});
		}.bind(this);
	},
 
	/**
	 *@param {string} constant
	 *@param {string} suffixName
	 *@param {mixed} payload
	 */
	_dispatch: function (constant, suffixName, payload) {
		if (!!suffixName) {
			constant += constantsConfigs.separator + constantsConfigs[suffixName];
		}
		this._dispatcher.dispatch(constant, payload);
	}
 
});
 
module.exports = Actions;