Code coverage report for ReactFlux/lib/storeActionHandler.js

Statements: 94.87% (37 / 39)      Branches: 96.55% (28 / 29)      Functions: 50% (2 / 4)      Lines: 94.87% (37 / 39)      Ignored: none     

All files » ReactFlux/lib/ » storeActionHandler.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 701   1   1   1 24 1   23 1   22 20 20     22 1     21 21 21 21 21 21 21 21   21     21 21 21 81 81 16   65 1   64 64 48   64   20     1                             1  
var _isString = require('lodash-node/modern/lang/isString');
 
var constantsConfigs = require('./configs').constants.get();
 
var HANDLER_NAMES = ['before', 'after', 'success', 'fail'];
 
function StoreActionHandler(store, constant, configs) {
	if (!store || typeof store.isStore !== 'function' || !store.isStore()) {
		throw new Error('StoreActionHandler expects first parameter to be a store');
	}
	if (!_isString(constant) || !constant.length) {
		throw new Error('StoreActionHandler expects second parameter to be a constant(string)');
	}
	if (typeof configs.getInitialState == 'undefined') {
		configs.getInitialState = function () {
			return {};
		};
	}
	if (typeof configs.getInitialState != 'function') {
		throw new Error('StoreActionHandler expects getInitialState to be a function');
	}
 
	configs = configs || {};
	this.parent = store;
	this.constant = constant;
	this.getInitialState = configs.getInitialState;
	this.before = configs.before || null;
	this.after = configs.after || null;
	this.success = configs.success || null;
	this.fail = configs.fail || null;
 
	this.parent.setActionState(this.constant, this.getInitialState());
 
	//register handlers for this constant
	var handlers = [];
	var len = HANDLER_NAMES.length;
	for (var i = 0; i < len; i++) {
		var handlerName = HANDLER_NAMES[i];
		if (this[handlerName] === null) {
			continue;
		}
		if (typeof this[handlerName] !== 'function') {
			throw new Error('StoreActionHandler expects "' + handlerName + '" to be a function');
		}
		var constantName = this.constant;
		if (handlerName !== 'before') {
			constantName += constantsConfigs.separator + constantsConfigs[handlerName + 'Suffix'];
		}
		handlers.push([constantName, this[handlerName].bind(this)]);
	}
	store._setConstantHandlers(handlers);
}
 
StoreActionHandler.prototype = {
	/**
	 * @param {object} newState
	 */
	setState: function (newState) {
		this.parent.setActionState(this.constant, newState);
	},
	/**
	 * @return {object} state
	 */
	getState: function () {
		return this.parent.getActionState(this.constant);
	}
};
 
module.exports = StoreActionHandler;