<commit_before>// @flow
/**
 * ComponentsMachine: manages all of the component controls for withAnimatronics.
 *
 * @module internal/machines/components-machine
 */

import Debug from 'debug'

import type { Styles, StyleUpdater, DOMNode, ComponentsMachine } from '../flow-types'

const debug = Debug('animatronics:controls');

export default (): ComponentsMachine => {
  const _nodes: { [string]: DOMNode } = {};
  const _styleUpdaters: { [string]: StyleUpdater } = {};

  const registerComponent = (
    componentName: string,
    node: DOMNode,
    styleUpdater: StyleUpdater,
  ) => {
    debug('registering component "%s"', componentName);
    _nodes[componentName] = node;
    _styleUpdaters[componentName] = styleUpdater;
  };

  const unregisterComponent = (componentName: string) => {
    delete _nodes[componentName];
    delete _styleUpdaters[componentName];
  };

  const updateStyles = (componentName: string, styles: Styles) => {
    // Can assume that the correct style updater is available since we validate
    // the animation declarations in animation-machine.
    _styleUpdaters[componentName](styles);
  };

  const getNodes = () => _nodes;

  const machine: ComponentsMachine = {
    registerComponent,
    unregisterComponent,
    updateStyles,
    getNodes,
  };
  return machine;
}<commit_msg>:bug: [components-machine] Oops, need to check if styleUpdaters exist because race conditions on unmount<commit_after>// @flow
/**
 * ComponentsMachine: manages all of the component controls for withAnimatronics.
 *
 * @module internal/machines/components-machine
 */

import Debug from 'debug'

import type { Styles, StyleUpdater, DOMNode, ComponentsMachine } from '../flow-types'

const debug = Debug('animatronics:controls');

export default (): ComponentsMachine => {
  const _nodes: { [string]: DOMNode } = {};
  const _styleUpdaters: { [string]: StyleUpdater } = {};

  const registerComponent = (
    componentName: string,
    node: DOMNode,
    styleUpdater: StyleUpdater,
  ) => {
    debug('registering component "%s"', componentName);
    _nodes[componentName] = node;
    _styleUpdaters[componentName] = styleUpdater;
  };

  const unregisterComponent = (componentName: string) => {
    delete _nodes[componentName];
    delete _styleUpdaters[componentName];
  };

  const updateStyles = (componentName: string, styles: Styles) => {
    if (_styleUpdaters[componentName]) {
      _styleUpdaters[componentName](styles);
    }
  };

  const getNodes = () => _nodes;

  const machine: ComponentsMachine = {
    registerComponent,
    unregisterComponent,
    updateStyles,
    getNodes,
  };
  return machine;
}
{'fraction_non_alphanumeric': 0.11675511751326763, 'fraction_numerical': 0.0, 'mean_word_length': 4.134241245136187, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7048722', 'n_tokens_mistral': 775, 'n_tokens_neox': 772, 'n_words': 196}