<commit_before>'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = socketMiddleware;

var _deepmerge = require('deepmerge');

var _deepmerge2 = _interopRequireDefault(_deepmerge);

var _constants = require('./constants');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var defaultOptions = {
  actions: []
};

function socketMiddleware() {
  var options = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];

  var socket = undefined;
  options = (0, _deepmerge2.default)(defaultOptions, options);

  // Determines wether or not an action should be emitted to the socket server.
  var shouldEmit = function shouldEmit(action) {
    return socket // We need a socket server
     && !action.emitted // If the action was emitted before, we will not emit it again.
     && options.actions.includes(action.type); // The action type needs to be included
  };

  return function (store) {
    var setupSocket = function setupSocket(url) {
      socket = options.resolveSocket(url);

      socket.on(_constants.SOCKET_RECEIVE_ACTION, function (action) {
        store.dispatch(_extends({}, action, { emitted: true }));
      });

      socket.on('disconnect', function () {
        store.dispatch({
          type: _constants.SOCKET_DISCONNECT
        });
      });

      socket.on('reconnect', function () {
        store.dispatch({
          type: _constants.SOCKET_CONNECT
        });
      });

      return socket;
    };

    return function (next) {
      return function (action) {
        var result = undefined;
        if (_lodash2.default.isFunction(action)) {
          result = next(action(store.dispatch));
        } else {
          // We first execute the action locally
          result = next(action);
        }

        // We want to intercept a couple of actions related to sockets.
        switch (action.type) {
          case _constants.SOCKET_CONNECT:
            if (!socket) {
              socket = setupSocket(action.url);
            }
            break;
        }

        // If the action should be emitted we do so.
        if (shouldEmit(action)) {
          socket.emit(_constants.SOCKET_DISPATCH, action);
        }

        // Return the result to the next middleware.
        return result;
      };
    };<commit_msg>Check if th action is a function, and if so, execute it<commit_after>'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = socketMiddleware;

var _deepmerge = require('deepmerge');

var _deepmerge2 = _interopRequireDefault(_deepmerge);

var _constants = require('./constants');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var defaultOptions = {
  actions: []
};

function socketMiddleware() {
  var options = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];

  var socket = undefined;
  options = (0, _deepmerge2.default)(defaultOptions, options);

  // Determines wether or not an action should be emitted to the socket server.
  var shouldEmit = function shouldEmit(action) {
    return socket // We need a socket server
     && !action.emitted // If the action was emitted before, we will not emit it again.
     && options.actions.includes(action.type); // The action type needs to be included
  };

  return function (store) {
    var setupSocket = function setupSocket(url) {
      socket = options.resolveSocket(url);

      socket.on(_constants.SOCKET_RECEIVE_ACTION, function (action) {
        store.dispatch(_extends({}, action, { emitted: true }));
      });

      socket.on('disconnect', function () {
        store.dispatch({
          type: _constants.SOCKET_DISCONNECT
        });
      });

      socket.on('reconnect', function () {
        store.dispatch({
          type: _constants.SOCKET_CONNECT
        });
      });

      return socket;
    };

    return function (next) {
      return function (action) {
        var result = undefined;

        // If the action is a function, execute it
        if (_lodash2.default.isFunction(action)) {
          result = action(store.dispatch);
        } else {
          // We first execute the action locally
          result = next(action);
        }

        // We want to intercept a couple of actions related to sockets.
        switch (action.type) {
          case _constants.SOCKET_CONNECT:
            if (!socket) {
              socket = setupSocket(action.url);
            }
            break;
        }

        // If the action should be emitted we do so.
        if (shouldEmit(action)) {
          socket.emit(_constants.SOCKET_DISPATCH, action);
        }

        // Return the result to the next middleware.
        return result;
      };
    };
{'fraction_non_alphanumeric': 0.11735985533453888, 'fraction_numerical': 0.0032549728752260397, 'mean_word_length': 2.6388157894736843, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2177283', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1527, 'n_words': 449}