<commit_before>import { ADDON_STATE_CHANGED, ADDON_STATE_SET } from '@storybook/core-events';

import {
  HooksContext,
  applyHooks,
  useMemo,
  useCallback,
  useRef,
  useState,
  useReducer,
  useEffect,
  useChannel,
  useStoryContext,
  useParameter,
} from '@storybook/addons';

export {
  HooksContext,
  applyHooks,
  useMemo,
  useCallback,
  useRef,
  useState,
  useReducer,
  useEffect,
  useChannel,
  useStoryContext,
  useParameter,
};

export function useAddonState<S>(addonId: string, defaultState?: S): [S, (s: S) => void] {
  const [state, setState] = useState<S>(defaultState);
  const emit = useChannel(
    {
      [`${ADDON_STATE_CHANGED}-manager-${addonId}`]: (s: S) => {
        setState(s);
      },
      [`${ADDON_STATE_SET}-manager-${addonId}`]: (s: S) => {
        setState(s);
      },
    },
    [addonId]
  );

  useEffect(() => {
    // init
    if (defaultState !== undefined) {
      emit(`${ADDON_STATE_SET}-client-${addonId}`, defaultState);
    }
  }, []);

  return [
    state,
    s => {
      setState(s);
      emit(`${ADDON_STATE_CHANGED}-client-${addonId}`, s);
    },
  ];
}<commit_msg>ADD memoization for eventHandlers object & ADD a addonState cache for HMR compat<commit_after>import { ADDON_STATE_CHANGED, ADDON_STATE_SET } from '@storybook/core-events';

import {
  HooksContext,
  applyHooks,
  useMemo,
  useCallback,
  useRef,
  useState,
  useReducer,
  useEffect,
  useChannel,
  useStoryContext,
  useParameter,
} from '@storybook/addons';

export {
  HooksContext,
  applyHooks,
  useMemo,
  useCallback,
  useRef,
  useState,
  useReducer,
  useEffect,
  useChannel,
  useStoryContext,
  useParameter,
};

// We keep this store, because when stories are edited by the user, and HMR, state is lost.
// This allows us to restore instantly.
const addonStateCache: Record<string, any> = {};

export function useAddonState<S>(addonId: string, defaultState?: S): [S, (s: S) => void] {
  const restoredState = addonStateCache[addonId] as S;

  const [state, setState] = useState<S>(
    typeof restoredState === 'undefined' ? defaultState : restoredState
  );

  addonStateCache[addonId] = state;

  const allListeners = useMemo(
    () => ({
      [`${ADDON_STATE_CHANGED}-manager-${addonId}`]: (s: S) => setState(s),
      [`${ADDON_STATE_SET}-manager-${addonId}`]: (s: S) => setState(s),
    }),
    [addonId]
  );

  const emit = useChannel(allListeners, [addonId]);

  useEffect(() => {
    // init
    if (defaultState !== undefined) {
      emit(`${ADDON_STATE_SET}-client-${addonId}`, defaultState);
    }
  }, []);

  return [
    state,
    s => {
      setState(s);
      emit(`${ADDON_STATE_CHANGED}-client-${addonId}`, s);
    },
  ];
}
{'fraction_non_alphanumeric': 0.15532544378698224, 'fraction_numerical': 0.0, 'mean_word_length': 3.478476821192053, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 19, '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': '7120608', 'n_tokens_mistral': 991, 'n_tokens_neox': 989, 'n_words': 194}