<commit_before>import { from, isObservable, of, OperatorFunction, ReplaySubject, Subscription } from 'rxjs';
import { filter, map, skip } from 'rxjs/operators';
import { setAction } from './actions';
import { $$addStore, $$deleteStore } from './dispatchers';
import { getValue } from './getValueByString';
import { isFunction } from './isFunction';
import { isNil } from './isNil';
import { isObject } from './isObject';
import { hasLocalStorage, hasSessionStorage, isNotBrowser } from './root';
import { setValue } from './setValueByString';
import { __stores__ } from './stores';
import { HashMap, MaybeAsync } from './types';

let skipStorageUpdate = false;

const _persistStateInit = new ReplaySubject(1);

export function selectPersistStateInit() {
  return _persistStateInit.asObservable();
}

export function setSkipStorageUpdate(skip: boolean) {
  skipStorageUpdate = skip;
}

export function getSkipStorageUpdate() {
  return skipStorageUpdate;
}

export interface PersistStateStorage {
  getItem(key: string): MaybeAsync;

  setItem(key: string, value: any): MaybeAsync;

  clear(): void;
}

function isPromise(v: any) {
  return v && isFunction(v.then);
}

function observify(asyncOrValue: any) {
  if (isPromise(asyncOrValue) || isObservable(asyncOrValue)) {
    return from(asyncOrValue);
  }

  return of(asyncOrValue);
}

export type PersistStateSelectFn<T = any> = ((store: T) => Partial<T>) & { storeName: string };

export interface PersistStateParams {
  /** The storage key */
  key: string;
  /** Whether to enable persistState in a non-browser environment */
  enableInNonBrowser: boolean;
  /** Storage strategy to use. This defaults to LocalStorage but you can pass SessionStorage or anything that implements the StorageEngine API. */
  storage: PersistStateStorage;
  /** Custom deserializer. Defaults to JSON.parse */
  deserialize: Function;
  /** Custom serializer, defaults to JSON.stringify */
  serialize: Function;
  /** By default the whole state is saved to storage, use this param to include only the stores you need. */
  include: (string | ((storeName: string) => boolean))[];
  /** By default the whole state is saved to storage, use this param to include only the data you need. */
  select: PersistStateSelectFn[];

  preStorageUpdate(storeName: string, state: any): any;

  preStoreUpdate(storeName: string, state: any): any;

  skipStorageUpdate: () => boolean;
  preStorageUpdateOperator: () => OperatorFunction<any, any>;
  /** Whether to persist a dynamic store upon destroy */
  persistOnDestroy: boolean;
}

export interface PersistState {
  destroy(): void;
  clear(): void;
  clearStore(storeName?: string): void;
}

export function persistState(params?: Partial<PersistStateParams>): PersistState {
  const defaults: PersistStateParams = {
    key: 'AkitaStores',
    enableInNonBrowser: false,
    storage: !hasLocalStorage() ? params.storage : localStorage,
    deserialize: JSON.parse,
    serialize: JSON.stringify,
    include: [],
    select: [],
    persistOnDestroy: false,
    preStorageUpdate: function (storeName, state) {<commit_msg>chore(persist-state): deprecate clear function (#532) (#554)<commit_after>import { from, isObservable, of, OperatorFunction, ReplaySubject, Subscription } from 'rxjs';
import { filter, map, skip } from 'rxjs/operators';
import { setAction } from './actions';
import { $$addStore, $$deleteStore } from './dispatchers';
import { getValue } from './getValueByString';
import { isFunction } from './isFunction';
import { isNil } from './isNil';
import { isObject } from './isObject';
import { hasLocalStorage, hasSessionStorage, isNotBrowser } from './root';
import { setValue } from './setValueByString';
import { __stores__ } from './stores';
import { HashMap, MaybeAsync } from './types';

let skipStorageUpdate = false;

const _persistStateInit = new ReplaySubject(1);

export function selectPersistStateInit() {
  return _persistStateInit.asObservable();
}

export function setSkipStorageUpdate(skip: boolean) {
  skipStorageUpdate = skip;
}

export function getSkipStorageUpdate() {
  return skipStorageUpdate;
}

export interface PersistStateStorage {
  getItem(key: string): MaybeAsync;

  setItem(key: string, value: any): MaybeAsync;

  clear(): void;
}

function isPromise(v: any) {
  return v && isFunction(v.then);
}

function observify(asyncOrValue: any) {
  if (isPromise(asyncOrValue) || isObservable(asyncOrValue)) {
    return from(asyncOrValue);
  }

  return of(asyncOrValue);
}

export type PersistStateSelectFn<T = any> = ((store: T) => Partial<T>) & { storeName: string };

export interface PersistStateParams {
  /** The storage key */
  key: string;
  /** Whether to enable persistState in a non-browser environment */
  enableInNonBrowser: boolean;
  /** Storage strategy to use. This defaults to LocalStorage but you can pass SessionStorage or anything that implements the StorageEngine API. */
  storage: PersistStateStorage;
  /** Custom deserializer. Defaults to JSON.parse */
  deserialize: Function;
  /** Custom serializer, defaults to JSON.stringify */
  serialize: Function;
  /** By default the whole state is saved to storage, use this param to include only the stores you need. */
  include: (string | ((storeName: string) => boolean))[];
  /** By default the whole state is saved to storage, use this param to include only the data you need. */
  select: PersistStateSelectFn[];

  preStorageUpdate(storeName: string, state: any): any;

  preStoreUpdate(storeName: string, state: any): any;

  skipStorageUpdate: () => boolean;
  preStorageUpdateOperator: () => OperatorFunction<any, any>;
  /** Whether to persist a dynamic store upon destroy */
  persistOnDestroy: boolean;
}

export interface PersistState {
  destroy(): void;
  /**
   * @deprecated Use clearStore instead.
   */
  clear(): void;
  clearStore(storeName?: string): void;
}

export function persistState(params?: Partial<PersistStateParams>): PersistState {
  const defaults: PersistStateParams = {
    key: 'AkitaStores',
    enableInNonBrowser: false,
    storage: !hasLocalStorage() ? params.storage : localStorage,
    deserialize: JSON.parse,
    serialize: JSON.stringify,
    include: [],
    select: [],
    persistOnDestroy: false,
    preStorageUpdate: function (storeName, state) {
{'fraction_non_alphanumeric': 0.11955484896661367, 'fraction_numerical': 0.0012718600953895071, 'mean_word_length': 4.693212669683258, 'pattern_counts': {'":': 0, '<': 11, '<?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': '650536', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1775, 'n_words': 573}