<commit_before>import { nextId } from '../utils';
import procOf from '../logic/procOf';
import Command from '../../classes/Command';
import ensureCommand from './ensureCommand';

/**
 * Creates a Command factory function, which makes a command
 * binded to a model from given context.
 * @param  {string} name
 * @param  {function} func
 * @param  {object} ctx
 * @param  {?object} opts
 * @return {function}
 */
export function createCommandFactory(name, logic, nonhandlable, func) {
  const id = func.__cmdId || nextId();

  const creator = function commandFactory(...args) {
    const cmd = new Command(func, args, name, nonhandlable);
    return logic ? cmd.bind(logic) : cmd;
  };

  func.__cmdId = id;
  creator.id = id;
  creator.func = func;
  creator.logic = logic;
  return creator;
}

/**
 * Function should be used as a decorator to create
 * a Command factory function for makeing a command to
 * execute decorated function by Process.
 * @param  {object}    obj
 * @param  {string} methodName
 * @param  {object} descr
 * @return {object}
 */
export function cmd(obj, methodName, descr, nonhandlable, cmdName) {
  // Creates a descriptor with lazy factory creation and autobinding
  // to the instance of the logic, but only when some model exists
  // and command executed in scope of top-level prototype in prototypes
  // chain (to support calling "super" commands in extended logic)
  return {
    __func: descr.value,
    configurable: true,
    enumerable: true,
    get() {
      const fromInstance = Object.getPrototypeOf(this) === obj;
      const factory = createCommandFactory(
        cmdName || methodName,
        fromInstance ? this : null,
        nonhandlable,
        descr.value
      );
      if (this && this.model && fromInstance) {
        Object.defineProperty(this, methodName, {
          configurable: false,
          enumerable: true,
          value: factory
        });
      }
      return factory;
    }
  };
}

export default cmd;
<commit_msg>fix(core): set logic in factory only if model presented in this<commit_after>import { nextId } from '../utils';
import procOf from '../logic/procOf';
import Command from '../../classes/Command';
import ensureCommand from './ensureCommand';

/**
 * Creates a Command factory function, which makes a command
 * binded to a model from given context.
 * @param  {string} name
 * @param  {function} func
 * @param  {object} ctx
 * @param  {?object} opts
 * @return {function}
 */
export function createCommandFactory(name, logic, nonhandlable, func) {
  const id = func.__cmdId || nextId();

  const creator = function commandFactory(...args) {
    const cmd = new Command(func, args, name, nonhandlable);
    return logic ? cmd.bind(logic) : cmd;
  };

  func.__cmdId = id;
  creator.id = id;
  creator.func = func;
  creator.logic = logic;
  return creator;
}

/**
 * Function should be used as a decorator to create
 * a Command factory function for makeing a command to
 * execute decorated function by Process.
 * @param  {object}    obj
 * @param  {string} methodName
 * @param  {object} descr
 * @return {object}
 */
export function cmd(obj, methodName, descr, nonhandlable, cmdName) {
  // Creates a descriptor with lazy factory creation and autobinding
  // to the instance of the logic, but only when some model exists
  // and command executed in scope of top-level prototype in prototypes
  // chain (to support calling "super" commands in extended logic)
  return {
    __func: descr.value,
    configurable: true,
    enumerable: true,
    get() {
      const haveModel = this && !!this.model && !!this.meta;
      const fromInstance = Object.getPrototypeOf(this) === obj;
      const factory = createCommandFactory(
        cmdName || methodName,
        haveModel ? this : null,
        nonhandlable,
        descr.value
      );
      if (haveModel && fromInstance) {
        Object.defineProperty(this, methodName, {
          configurable: false,
          enumerable: true,
          value: factory
        });
      }
      return factory;
    }
  };
}

export default cmd;

{'fraction_non_alphanumeric': 0.10928693947561872, 'fraction_numerical': 0.0, 'mean_word_length': 2.955426356589147, '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': '7548370', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1160, 'n_words': 416}