<commit_before>import { getEntity } from './getEntity';
import { isEmpty } from './isEmpty';
import { SetEntities, setEntities } from './setEntities';
import { Store } from './store';
import {
  Constructor,
  EntityState,
  EntityUICreateFn,
  IDS,
  OrArray,
  StateWithActive,
  UpdateEntityPredicate,
  UpdateStateCallback,
  getEntityType,
  getIDType,
  CreateStateCallback,
  UpsertStateCallback,
} from './types';
import { getActiveEntities, SetActiveOptions } from './getActiveEntities';
import { addEntities, AddEntitiesOptions } from './addEntities';
import { coerceArray } from './coerceArray';
import { removeEntities } from './removeEntities';
import { getInitialEntitiesState } from './getInitialEntitiesState';
import { isDefined } from './isDefined';
import { updateEntities } from './updateEntities';
import { transaction } from './transaction';
import { isNil } from './isNil';
import { isFunction } from './isFunction';
import { isUndefined } from './isUndefined';
import { StoreConfigOptions } from './storeConfig';
import { logAction, setAction } from './actions';
import { isDev } from './env';
import { hasEntity } from './hasEntity';
import { Observable, Subject } from 'rxjs';
import { EntityAction, EntityActions } from './entityActions';
import { DEFAULT_ID_KEY } from './defaultIDKey';

/**
 *
 * Store for managing a collection of entities
 *
 * @example
 *
 * export interface WidgetsState extends EntityState<Widget> { }
 *
 * @StoreConfig({ name: 'widgets' })
 *  export class WidgetsStore extends EntityStore<WidgetsState> {
 *   constructor() {
 *     super();
 *   }
 * }
 *
 *
 */
export class EntityStore<S extends EntityState = any, EntityType = getEntityType<S>, IDType = getIDType<S>> extends Store<S> {
  ui: EntityUIStore<any, EntityType>;
  private entityActions = new Subject<EntityAction<IDType>>();

  constructor(initialState: Partial<S> = {}, protected options: Partial<StoreConfigOptions> = {}) {
    super({ ...getInitialEntitiesState(), ...initialState }, options);
  }

  // @internal
  get selectEntityAction$(): Observable<EntityAction<IDType>> {
    return this.entityActions.asObservable();
  }

  // @internal
  get idKey() {
    return (this.config as StoreConfigOptions).idKey || this.options.idKey || DEFAULT_ID_KEY;
  }

  /**
   *
   * Replace current collection with provided collection
   *
   * @example
   *
   * this.store.set([Entity, Entity])
   * this.store.set({ids: [], entities: {}})
   * this.store.set({ 1: {}, 2: {}})
   *
   */
  set(entities: SetEntities<EntityType>, options: { activeId?: IDType | null } = {}) {
    if (isNil(entities)) return;

    isDev() && setAction('Set Entity');

    const isNativePreAdd = this.akitaPreAddEntity === EntityStore.prototype.akitaPreAddEntity;

    this._setState((state) => {
      const newState = setEntities({
        state,
        entities,
        idKey: this.idKey,
        preAddEntity: this.akitaPreAddEntity,
        isNativePreAdd,
      });

      if (isUndefined(options.activeId) === false) {
        (newState as any).active = options.activeId;
      }

      return newState;
    });

    this.setHasCache(true, { restartTTL: true });

    if (this.hasInitialUIState()) {
      this.handleUICreation();
    }

    this.entityActions.next({ type: EntityActions.Set, ids: this.ids });
  }

  /**
   * Add entities
   *
   * @example
   *
   * this.store.add([Entity, Entity])
   * this.store.add(Entity)
   * this.store.add(Entity, { prepend: true })
   *
   * this.store.add(Entity, { loading: false })
   */
  add(entities: OrArray<EntityType>, options: AddEntitiesOptions = { loading: false }) {<commit_msg>fix(entity-store): change set cache location<commit_after>import { Observable, Subject } from 'rxjs';
import { logAction, setAction } from './actions';
import { addEntities, AddEntitiesOptions } from './addEntities';
import { coerceArray } from './coerceArray';
import { DEFAULT_ID_KEY } from './defaultIDKey';
import { EntityAction, EntityActions } from './entityActions';
import { isDev } from './env';
import { getActiveEntities, SetActiveOptions } from './getActiveEntities';
import { getInitialEntitiesState } from './getInitialEntitiesState';
import { hasEntity } from './hasEntity';
import { isDefined } from './isDefined';
import { isEmpty } from './isEmpty';
import { isFunction } from './isFunction';
import { isNil } from './isNil';
import { isUndefined } from './isUndefined';
import { removeEntities } from './removeEntities';
import { SetEntities, setEntities } from './setEntities';
import { Store } from './store';
import { StoreConfigOptions } from './storeConfig';
import { transaction } from './transaction';
import {
  Constructor,
  CreateStateCallback,
  EntityState,
  EntityUICreateFn,
  getEntityType,
  getIDType,
  IDS,
  OrArray,
  StateWithActive,
  UpdateEntityPredicate,
  UpdateStateCallback,
  UpsertStateCallback,
} from './types';
import { updateEntities } from './updateEntities';

/**
 *
 * Store for managing a collection of entities
 *
 * @example
 *
 * export interface WidgetsState extends EntityState<Widget> { }
 *
 * @StoreConfig({ name: 'widgets' })
 *  export class WidgetsStore extends EntityStore<WidgetsState> {
 *   constructor() {
 *     super();
 *   }
 * }
 *
 *
 */
export class EntityStore<S extends EntityState = any, EntityType = getEntityType<S>, IDType = getIDType<S>> extends Store<S> {
  ui: EntityUIStore<any, EntityType>;
  private entityActions = new Subject<EntityAction<IDType>>();

  constructor(initialState: Partial<S> = {}, protected options: Partial<StoreConfigOptions> = {}) {
    super({ ...getInitialEntitiesState(), ...initialState }, options);
  }

  // @internal
  get selectEntityAction$(): Observable<EntityAction<IDType>> {
    return this.entityActions.asObservable();
  }

  // @internal
  get idKey() {
    return (this.config as StoreConfigOptions).idKey || this.options.idKey || DEFAULT_ID_KEY;
  }

  /**
   *
   * Replace current collection with provided collection
   *
   * @example
   *
   * this.store.set([Entity, Entity])
   * this.store.set({ids: [], entities: {}})
   * this.store.set({ 1: {}, 2: {}})
   *
   */
  set(entities: SetEntities<EntityType>, options: { activeId?: IDType | null } = {}) {
    if (isNil(entities)) return;

    isDev() && setAction('Set Entity');

    const isNativePreAdd = this.akitaPreAddEntity === EntityStore.prototype.akitaPreAddEntity;
    this.setHasCache(true, { restartTTL: true });

    this._setState((state) => {
      const newState = setEntities({
        state,
        entities,
        idKey: this.idKey,
        preAddEntity: this.akitaPreAddEntity,
        isNativePreAdd,
      });

      if (isUndefined(options.activeId) === false) {
        (newState as any).active = options.activeId;
      }

      return newState;
    });

    if (this.hasInitialUIState()) {
      this.handleUICreation();
    }

    this.entityActions.next({ type: EntityActions.Set, ids: this.ids });
  }

  /**
   * Add entities
   *
   * @example
   *
   * this.store.add([Entity, Entity])
   * this.store.add(Entity)
   * this.store.add(Entity, { prepend: true })
   *
   * this.store.add(Entity, { loading: false })
   */
  add(entities: OrArray<EntityType>, options: AddEntitiesOptions = { loading: false }) {
{'fraction_non_alphanumeric': 0.14607667995052906, 'fraction_numerical': 0.0005496770647244744, 'mean_word_length': 3.6623959000640616, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, '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': '6698559', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2188, 'n_words': 508}