<commit_before>/**
 * @name Morearty
 * @namespace
 * @classdesc Morearty main module. Exposes [createContext]{@link Morearty.createContext} function.
 */
var Imm      = require('immutable');
var Util     = require('./Util');
var Binding  = require('./Binding');
var History  = require('./History');
var Callback = require('./util/Callback');

var MERGE_STRATEGY = Object.freeze({
  OVERWRITE: 'overwrite',
  OVERWRITE_EMPTY: 'overwrite-empty',
  MERGE_PRESERVE: 'merge-preserve',
  MERGE_REPLACE: 'merge-replace'
});

var getBinding, bindingStateChanged, stateChanged;

getBinding = function (props, key) {
  var binding = props.binding;
  return key ? binding[key] : binding;
};

bindingStateChanged = function (context, currentBinding, previousState, previousMetaState) {
  return (context._stateChanged && previousState !== currentBinding.get()) ||
    (context._metaChanged && context._metaBinding.sub(currentBinding.getPath()).isChanged(previousMetaState));
};

stateChanged = function (self, currentBinding, previousBinding, previousState, previousMetaState) {
  if (!currentBinding) return false;
  else {
    var context = self.getMoreartyContext();

    if (currentBinding instanceof Binding) {
      return currentBinding !== previousBinding || bindingStateChanged(context, currentBinding, previousState, previousMetaState);
    } else {
      if (context._stateChanged || context._metaChanged) {
        var keys = Object.keys(currentBinding);
        return !!Util.find(keys, function (key) {
          var binding = currentBinding[key];
          return binding &&
            (binding !== previousBinding[key] || bindingStateChanged(context, binding, previousState[key], previousMetaState));
        });
      } else {
        return false;
      }
    }
  }
};

var propChanged, observedPropsChanged;

propChanged = function (prop, currentProps, previousProps) {
  return currentProps[prop] !== previousProps[prop];
};

observedPropsChanged = function (self, currentProps) {
  if (self.observedProps) {
    var effectiveCurrentProps = currentProps || {}, effectivePreviousProps = self.props || {};
    return Util.find(self.observedProps, function (prop) {
      return propChanged(prop, effectiveCurrentProps, effectivePreviousProps);
    });
  } else {
    return false;
  }
};

var merge = function (mergeStrategy, defaultState, stateBinding) {
  var tx = stateBinding.atomically();

  if (typeof mergeStrategy === 'function') {
    tx = tx.update(function (currentState) {
      return mergeStrategy(currentState, defaultState);
    });
  } else {
    switch (mergeStrategy) {
      case MERGE_STRATEGY.OVERWRITE:
        tx = tx.set(defaultState);
        break;
      case MERGE_STRATEGY.OVERWRITE_EMPTY:
        tx = tx.update(function (currentState) {
          var empty = Util.undefinedOrNull(currentState) ||
            (currentState instanceof Imm.Iterable && currentState.isEmpty());
          return empty ? defaultState : currentState;
        });
        break;
      case MERGE_STRATEGY.MERGE_PRESERVE:
        tx = tx.merge(true, defaultState);
        break;
      case MERGE_STRATEGY.MERGE_REPLACE:
        tx = tx.merge(false, defaultState);
        break;
      default:
        throw new Error('Invalid merge strategy: ' + mergeStrategy);
    }
  }

  tx.commit({ notify: false });
};

var getRenderRoutine = function (self) {
  var requestAnimationFrame = (typeof window !== 'undefined') && window.requestAnimationFrame;
  var fallback = function (f) { setTimeout(f, 1000 / 60); };

  if (self._options.requestAnimationFrameEnabled) {
    if (requestAnimationFrame) return requestAnimationFrame;
    else {
      console.warn('Morearty: requestAnimationFrame is not available, will render using setTimeout');
      return fallback;
    }
  } else {
    return fallback;
  }
};

var initState, initDefaultState, initDefaultMetaState, savePreviousState;

initState = function (self, getStateMethodName, f) {
  if (typeof self[getStateMethodName] === 'function') {
    var defaultStateValue = self[getStateMethodName]();
    if (defaultStateValue) {
      var binding = getBinding(self.props);
      var mergeStrategy =
        typeof self.getMergeStrategy === 'function' ? self.getMergeStrategy() : MERGE_STRATEGY.MERGE_PRESERVE;

      var immutableInstance = defaultStateValue instanceof Imm.Iterable;

      if (binding instanceof Binding) {
        var effectiveDefaultStateValue = immutableInstance ? defaultStateValue : defaultStateValue['default'];
        merge(mergeStrategy, effectiveDefaultStateValue, f(binding));
      } else {
        var keys = Object.keys(binding);
        var defaultKey = keys.length === 1 ? keys[0] : 'default';
        var effectiveMergeStrategy = typeof mergeStrategy === 'string' ? mergeStrategy : mergeStrategy[defaultKey];

        if (immutableInstance) {
          merge(effectiveMergeStrategy, defaultStateValue, f(binding[defaultKey]));
        } else {
          keys.forEach(function (key) {
            if (defaultStateValue[key]) {
              merge(effectiveMergeStrategy, defaultStateValue[key], f(binding[key]));
            }
          });
        }
      }
    }
  }
};

initDefaultState = function (self) {
  initState(self, 'getDefaultState', Util.identity);
};

initDefaultMetaState = function (self) {
  initState(self, 'getDefaultMetaState', function (b) { return b.meta(); });
};

savePreviousState = function (self) {
  var binding = self.props.binding;
  if (binding) {
    var ctx = self.getMoreartyContext();
    self._previousMetaState = ctx && ctx.getCurrentMeta();
    if (binding instanceof Binding) {
      self._previousState = binding.get();
    } else {
      self._previousState = {};
      Object.keys(self.props.binding)
        .forEach(function (key) {
          self._previousState[key] = self.props.binding[key] && self.props.binding[key].get();
        });
    }
  } else {
    self._previousState = null;
    self._previousMetaState = null;
  }
};

var addComponentToRenderQueue, removeComponentFromRenderQueue, getUniqueComponentQueueId;

addComponentToRenderQueue = function (self, component, renderFirst) {
  if (renderFirst) {
    self._renderFirstComponentQueue[component.componentQueueId] = component;
  } else {
    self._componentQueue[component.componentQueueId] = component;
  }
};

removeComponentFromRenderQueue = function (self, component) {
  delete self._componentQueue[component.componentQueueId];
};

getUniqueComponentQueueId = function (self) {
  return self ? ++self._lastComponentQueueId : 0;
};

var resolveObSpecBinding, resolveObSpecRenderFirst, ensureObSpec, setupObservedBindingListener;

resolveObSpecBinding = function (obSpec) {
  return obSpec instanceof Binding ? obSpec : obSpec.binding;
};

resolveObSpecRenderFirst = function (obSpec) {
  return obSpec instanceof Binding ? false : obSpec.renderFirst;
};

ensureObSpec = function (obSpec) {
  return obSpec instanceof Binding ? { binding: obSpec.binding, renderFirst: false } : obSpec;
};

setupObservedBindingListener = function (self, obSpec) {
  if (!self._observedListenerRemovers) {<commit_msg>Remove render-first components from queue.<commit_after>/**
 * @name Morearty
 * @namespace
 * @classdesc Morearty main module. Exposes [createContext]{@link Morearty.createContext} function.
 */
var Imm      = require('immutable');
var Util     = require('./Util');
var Binding  = require('./Binding');
var History  = require('./History');
var Callback = require('./util/Callback');

var MERGE_STRATEGY = Object.freeze({
  OVERWRITE: 'overwrite',
  OVERWRITE_EMPTY: 'overwrite-empty',
  MERGE_PRESERVE: 'merge-preserve',
  MERGE_REPLACE: 'merge-replace'
});

var getBinding, bindingStateChanged, stateChanged;

getBinding = function (props, key) {
  var binding = props.binding;
  return key ? binding[key] : binding;
};

bindingStateChanged = function (context, currentBinding, previousState, previousMetaState) {
  return (context._stateChanged && previousState !== currentBinding.get()) ||
    (context._metaChanged && context._metaBinding.sub(currentBinding.getPath()).isChanged(previousMetaState));
};

stateChanged = function (self, currentBinding, previousBinding, previousState, previousMetaState) {
  if (!currentBinding) return false;
  else {
    var context = self.getMoreartyContext();

    if (currentBinding instanceof Binding) {
      return currentBinding !== previousBinding || bindingStateChanged(context, currentBinding, previousState, previousMetaState);
    } else {
      if (context._stateChanged || context._metaChanged) {
        var keys = Object.keys(currentBinding);
        return !!Util.find(keys, function (key) {
          var binding = currentBinding[key];
          return binding &&
            (binding !== previousBinding[key] || bindingStateChanged(context, binding, previousState[key], previousMetaState));
        });
      } else {
        return false;
      }
    }
  }
};

var propChanged, observedPropsChanged;

propChanged = function (prop, currentProps, previousProps) {
  return currentProps[prop] !== previousProps[prop];
};

observedPropsChanged = function (self, currentProps) {
  if (self.observedProps) {
    var effectiveCurrentProps = currentProps || {}, effectivePreviousProps = self.props || {};
    return Util.find(self.observedProps, function (prop) {
      return propChanged(prop, effectiveCurrentProps, effectivePreviousProps);
    });
  } else {
    return false;
  }
};

var merge = function (mergeStrategy, defaultState, stateBinding) {
  var tx = stateBinding.atomically();

  if (typeof mergeStrategy === 'function') {
    tx = tx.update(function (currentState) {
      return mergeStrategy(currentState, defaultState);
    });
  } else {
    switch (mergeStrategy) {
      case MERGE_STRATEGY.OVERWRITE:
        tx = tx.set(defaultState);
        break;
      case MERGE_STRATEGY.OVERWRITE_EMPTY:
        tx = tx.update(function (currentState) {
          var empty = Util.undefinedOrNull(currentState) ||
            (currentState instanceof Imm.Iterable && currentState.isEmpty());
          return empty ? defaultState : currentState;
        });
        break;
      case MERGE_STRATEGY.MERGE_PRESERVE:
        tx = tx.merge(true, defaultState);
        break;
      case MERGE_STRATEGY.MERGE_REPLACE:
        tx = tx.merge(false, defaultState);
        break;
      default:
        throw new Error('Invalid merge strategy: ' + mergeStrategy);
    }
  }

  tx.commit({ notify: false });
};

var getRenderRoutine = function (self) {
  var requestAnimationFrame = (typeof window !== 'undefined') && window.requestAnimationFrame;
  var fallback = function (f) { setTimeout(f, 1000 / 60); };

  if (self._options.requestAnimationFrameEnabled) {
    if (requestAnimationFrame) return requestAnimationFrame;
    else {
      console.warn('Morearty: requestAnimationFrame is not available, will render using setTimeout');
      return fallback;
    }
  } else {
    return fallback;
  }
};

var initState, initDefaultState, initDefaultMetaState, savePreviousState;

initState = function (self, getStateMethodName, f) {
  if (typeof self[getStateMethodName] === 'function') {
    var defaultStateValue = self[getStateMethodName]();
    if (defaultStateValue) {
      var binding = getBinding(self.props);
      var mergeStrategy =
        typeof self.getMergeStrategy === 'function' ? self.getMergeStrategy() : MERGE_STRATEGY.MERGE_PRESERVE;

      var immutableInstance = defaultStateValue instanceof Imm.Iterable;

      if (binding instanceof Binding) {
        var effectiveDefaultStateValue = immutableInstance ? defaultStateValue : defaultStateValue['default'];
        merge(mergeStrategy, effectiveDefaultStateValue, f(binding));
      } else {
        var keys = Object.keys(binding);
        var defaultKey = keys.length === 1 ? keys[0] : 'default';
        var effectiveMergeStrategy = typeof mergeStrategy === 'string' ? mergeStrategy : mergeStrategy[defaultKey];

        if (immutableInstance) {
          merge(effectiveMergeStrategy, defaultStateValue, f(binding[defaultKey]));
        } else {
          keys.forEach(function (key) {
            if (defaultStateValue[key]) {
              merge(effectiveMergeStrategy, defaultStateValue[key], f(binding[key]));
            }
          });
        }
      }
    }
  }
};

initDefaultState = function (self) {
  initState(self, 'getDefaultState', Util.identity);
};

initDefaultMetaState = function (self) {
  initState(self, 'getDefaultMetaState', function (b) { return b.meta(); });
};

savePreviousState = function (self) {
  var binding = self.props.binding;
  if (binding) {
    var ctx = self.getMoreartyContext();
    self._previousMetaState = ctx && ctx.getCurrentMeta();
    if (binding instanceof Binding) {
      self._previousState = binding.get();
    } else {
      self._previousState = {};
      Object.keys(self.props.binding)
        .forEach(function (key) {
          self._previousState[key] = self.props.binding[key] && self.props.binding[key].get();
        });
    }
  } else {
    self._previousState = null;
    self._previousMetaState = null;
  }
};

var addComponentToRenderQueue, removeComponentFromRenderQueue, getUniqueComponentQueueId;

addComponentToRenderQueue = function (self, component, renderFirst) {
  if (renderFirst) {
    self._renderFirstComponentQueue[component.componentQueueId] = component;
  } else {
    self._componentQueue[component.componentQueueId] = component;
  }
};

removeComponentFromRenderQueue = function (self, component) {
  delete self._renderFirstComponentQueue[component.componentQueueId];
  delete self._componentQueue[component.componentQueueId];
};

getUniqueComponentQueueId = function (self) {
  return self ? ++self._lastComponentQueueId : 0;
};

var resolveObSpecBinding, resolveObSpecRenderFirst, ensureObSpec, setupObservedBindingListener;

resolveObSpecBinding = function (obSpec) {
  return obSpec instanceof Binding ? obSpec : obSpec.binding;
};

resolveObSpecRenderFirst = function (obSpec) {
  return obSpec instanceof Binding ? false : obSpec.renderFirst;
};

ensureObSpec = function (obSpec) {
  return obSpec instanceof Binding ? { binding: obSpec.binding, renderFirst: false } : obSpec;
};

setupObservedBindingListener = function (self, obSpec) {
  if (!self._observedListenerRemovers) {
{'fraction_non_alphanumeric': 0.11496261094416102, 'fraction_numerical': 0.0012579495422461388, 'mean_word_length': 3.8198046480296397, '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': '2920258', 'n_tokens_mistral': 3995, 'n_tokens_neox': 3902, 'n_words': 900}