<commit_before>import React from 'react';

import toVoid from '../utils/to-void';

const GLOBAL_STACK = {
  __times: {}
};

/**
 * Subscribe to an event.
 * @param {string} event - Event name to subscribe to.
 * @param {function} callback - Function to be called when event of type
 *   `event` is published.
 * @param {number} times - Number of times the callback should called for the
 *   given event. The event listener will automatically be unsubscribed once
 *   the number of calls exceeds `times`.
 * @return {object} Object with the event name and the callback. The object
 *   can be used to unsubscribe.
 */
const subscribe = stack => (event, callback, times = Infinity) => {
  if (!stack[event]) {
    stack[event] = [];
    stack.__times[event] = [];
  }

  stack[event].push(callback);
  stack.__times[event].push(parseInt(times, 10) || Infinity);

  return { event, callback };
};

/**
 * Unsubscribe from event.
 *
 * @param {string|object} event - Event from which to unsubscribe or the return
 *   object provided by `subscribe()`.
 * @param {function} callback - Callback function to be unsubscribed. It is
 *   ignored if `id` is provided.
 */
const unsubscribe = stack => (event, callback) => {
  if (!stack[event]) return;

  if (typeof event === 'object') {
    ({ event, callback } = event); // eslint-disable-line no-param-reassign
  }

  const id = stack[event].indexOf(callback);

  if (!stack[event]) return;
  if (id === -1 || id >= stack[event].length) return;

  stack[event].splice(id, 1);
  stack.__times[event].splice(id, 1);
};

/**
 * Publish an event.
 *
 * @param {string} event - Event type to be published.
 * @param {any} news - The news to be published.
 */
const publish = stack => (event, news) => {
  if (!stack[event]) return;

  const unsubscriber = unsubscribe(stack);

  stack[event].forEach((listener, i) => {
    listener(news);
    if (!(stack.__times[event][i] -= 1)) { // eslint-disable-line no-cond-assign
      unsubscriber(event, listener);
    }
  });
};

const create = (stack = { __times: {} }) => {
  if (!stack.__times) stack.__times = {};
  return {
    publish: publish(stack),
    subscribe: subscribe(stack),
    unsubscribe: unsubscribe(stack),
  };
};

const globalPubSub = create(GLOBAL_STACK);

const { Provider, Consumer } = React.createContext();

// Higher order component
const withPubSub = Component => props => (
  <Consumer>
    {pubSub => <Component {...props} pubSub={pubSub} />}
  </Consumer>
);

const fake = {
  __fake__: true,
  publish: toVoid,<commit_msg>Forward reference to wrapped components<commit_after>import React from 'react';

import toVoid from '../utils/to-void';

const GLOBAL_STACK = {
  __times: {}
};

/**
 * Subscribe to an event.
 * @param {string} event - Event name to subscribe to.
 * @param {function} callback - Function to be called when event of type
 *   `event` is published.
 * @param {number} times - Number of times the callback should called for the
 *   given event. The event listener will automatically be unsubscribed once
 *   the number of calls exceeds `times`.
 * @return {object} Object with the event name and the callback. The object
 *   can be used to unsubscribe.
 */
const subscribe = stack => (event, callback, times = Infinity) => {
  if (!stack[event]) {
    stack[event] = [];
    stack.__times[event] = [];
  }

  stack[event].push(callback);
  stack.__times[event].push(parseInt(times, 10) || Infinity);

  return { event, callback };
};

/**
 * Unsubscribe from event.
 *
 * @param {string|object} event - Event from which to unsubscribe or the return
 *   object provided by `subscribe()`.
 * @param {function} callback - Callback function to be unsubscribed. It is
 *   ignored if `id` is provided.
 */
const unsubscribe = stack => (event, callback) => {
  if (!stack[event]) return;

  if (typeof event === 'object') {
    ({ event, callback } = event); // eslint-disable-line no-param-reassign
  }

  const id = stack[event].indexOf(callback);

  if (!stack[event]) return;
  if (id === -1 || id >= stack[event].length) return;

  stack[event].splice(id, 1);
  stack.__times[event].splice(id, 1);
};

/**
 * Publish an event.
 *
 * @param {string} event - Event type to be published.
 * @param {any} news - The news to be published.
 */
const publish = stack => (event, news) => {
  if (!stack[event]) return;

  const unsubscriber = unsubscribe(stack);

  stack[event].forEach((listener, i) => {
    listener(news);
    if (!(stack.__times[event][i] -= 1)) { // eslint-disable-line no-cond-assign
      unsubscriber(event, listener);
    }
  });
};

const create = (stack = { __times: {} }) => {
  if (!stack.__times) stack.__times = {};
  return {
    publish: publish(stack),
    subscribe: subscribe(stack),
    unsubscribe: unsubscribe(stack),
  };
};

const globalPubSub = create(GLOBAL_STACK);

const { Provider, Consumer } = React.createContext();

// Higher order component
const withPubSub = Component => React.forwardRef((props, ref) => (
  <Consumer>
    {pubSub => <Component ref={ref} {...props} pubSub={pubSub} />}
  </Consumer>
));

const fake = {
  __fake__: true,
  publish: toVoid,
{'fraction_non_alphanumeric': 0.15502820462944952, 'fraction_numerical': 0.002334176230305388, 'mean_word_length': 3.5105263157894737, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, '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': '7296426', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1578, 'n_words': 497}