<commit_before>var _         = require('underscore');
var debug     = require('debug')('feathers-react-redux:FeathersMixin');
var pluralize = require('pluralize');

var DEFAULT_OPTIONS = { server_load: true, client_load: false, realtime: false };

var actions;
var app;

module.exports = {
	setFeathersApp: function(new_app) {
		app = new_app;
	},
	setFeathersActions: function(new_actions) {
		actions = new_actions;
	},
	feathers: function(resource, options) {
		options = _.chain(options || {})
			.defaults(DEFAULT_OPTIONS)
			.each(function(value, name) {
				if (!value) {
					return;
				}
				this[name] = _.union(this[name], [resource]);
			}.bind(this))
			.value();

		if (!process.browser && options.server_load) {
			var resources = pluralize(resource);
			var Resources = resources.charAt(0).toUpperCase() + resources.slice(1);

			this.props.dispatch({
				type:    'SERVER_ACTION',
				payload: {
					type: 'load' + Resources
				}
			});
		}
	},
	// There's pretty much no reason for anything to be in componentWillMount, since feathers() can only be called AFTER it. Anything that would be there should be in feathers().
	componentDidMount: function() {
		_.each(this.client_load, function(resource) {
			var resources = pluralize(resource);
			var Resources = resources.charAt(0).toUpperCase() + resources.slice(1);

			debug('client load for ' + resources);
			this.props.dispatch(actions['load' + Resources]());
		}.bind(this));

		_.each(this.realtime, function(resource) {
			var Resource  = resource.charAt(0).toUpperCase() + resource.slice(1);
			var resources = pluralize(resource);

			this.cleanups = _.chain(['created', 'updated', 'patched', 'removed'])
				.map(function(action) {
					debug('listening for ' + action + ' events for ' + resources);
					var dispatchAction = function(object) {
						debug(action + ' ' + resource, object);
						this.props.dispatch(actions[action + Resource](object));
					}.bind(this);
					app.service('/api/' + resources).on(action, dispatchAction);
					return function() {
						debug('stop listening for ' + action + ' events for ' + resources);
						app.service('/api/' + resources).off(action, dispatchAction);
					};
				}.bind(this))
				.union(this.cleanups)
				.value();
		}.bind(this));
	},
	componentWillUnmount: function() {
		_.each(this.cleanups, _.partial);
	}
};
<commit_msg>clear cleanups after doing them<commit_after>var _         = require('underscore');
var debug     = require('debug')('feathers-react-redux:FeathersMixin');
var pluralize = require('pluralize');

var DEFAULT_OPTIONS = { server_load: true, client_load: false, realtime: false };

var actions;
var app;

module.exports = {
	setFeathersApp: function(new_app) {
		app = new_app;
	},
	setFeathersActions: function(new_actions) {
		actions = new_actions;
	},
	feathers: function(resource, options) {
		options = _.chain(options || {})
			.defaults(DEFAULT_OPTIONS)
			.each(function(value, name) {
				if (!value) {
					return;
				}
				this[name] = _.union(this[name], [resource]);
			}.bind(this))
			.value();

		if (!process.browser && options.server_load) {
			var resources = pluralize(resource);
			var Resources = resources.charAt(0).toUpperCase() + resources.slice(1);

			this.props.dispatch({
				type:    'SERVER_ACTION',
				payload: {
					type: 'load' + Resources
				}
			});
		}
	},
	// There's pretty much no reason for anything to be in componentWillMount, since feathers() can only be called AFTER it. Anything that would be there should be in feathers().
	componentDidMount: function() {
		_.each(this.client_load, function(resource) {
			var resources = pluralize(resource);
			var Resources = resources.charAt(0).toUpperCase() + resources.slice(1);

			debug('client load for ' + resources);
			this.props.dispatch(actions['load' + Resources]());
		}.bind(this));

		_.each(this.realtime, function(resource) {
			var Resource  = resource.charAt(0).toUpperCase() + resource.slice(1);
			var resources = pluralize(resource);

			this.cleanups = _.chain(['created', 'updated', 'patched', 'removed'])
				.map(function(action) {
					debug('listening for ' + action + ' events for ' + resources);
					var dispatchAction = function(object) {
						debug(action + ' ' + resource, object);
						this.props.dispatch(actions[action + Resource](object));
					}.bind(this);
					app.service('/api/' + resources).on(action, dispatchAction);
					return function() {
						debug('stop listening for ' + action + ' events for ' + resources);
						app.service('/api/' + resources).off(action, dispatchAction);
					};
				}.bind(this))
				.union(this.cleanups)
				.value();
		}.bind(this));
	},
	componentWillUnmount: function() {
		_.each(this.cleanups, _.partial);
		this.cleanups = [];
	}
};

{'fraction_non_alphanumeric': 0.15708892617449666, 'fraction_numerical': 0.0025167785234899327, 'mean_word_length': 3.7547357926221334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2048160', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1471, 'n_words': 327}