{"version":3,"file":"subscriber.cjs","names":["#equals","equal","#subscriber","#eventsMap","#actorsMap","#state","#reduceFn","isFunction","_any","toEventsMap","nothing","#cannotPerform"],"sources":["../../src/interpreters/subscriber.ts"],"sourcesContent":["import _any from '#bemedev/features/common/castings/any';\nimport type { PrimitiveObject } from '#bemedev/globals/types';\nimport type {\n  ActorsConfigMap,\n  EventObject,\n  EventsMap,\n  ToEventObject,\n  ToEvents,\n} from '#events';\nimport type { State } from '#states';\nimport { nothing, toEventsMap } from '#utils';\nimport type { TimerState } from '@bemedev/interval2';\nimport equal from 'fast-deep-equal';\nimport { nanoid } from 'nanoid';\nimport { FnMapR, isFunction } from '../types/primitives';\n\n/**\n * Subscriber class that manages the subscription state and provides methods\n * to handle state changes and unsubscribe.\n *\n * @template : {@linkcode EventsMap} [E] - Type of the events map\n * @template : {@linkcode ActorsConfigMap} [A] - Type of the actors map\n * @template : {@linkcode PrimitiveObject} [Tc] - Type of the context\n * @template : [R] - Type of the return value\n *\n */\nclass SubscriberClass<\n  E extends EventsMap = EventsMap,\n  A extends ActorsConfigMap = ActorsConfigMap,\n  Tc extends PrimitiveObject = PrimitiveObject,\n  T extends string = string,\n  Eo extends ToEventObject<ToEvents<E, A>> = ToEventObject<ToEvents<E, A>>,\n  St extends State<Eo, Tc, T> = State<Eo, Tc, T>,\n> {\n  #subscriber: FnMapR<Eo, Tc, T, void>;\n  #eventsMap: E;\n  #actorsMap: A;\n\n  #state: TimerState = 'idle';\n\n  /**\n   * Function to compare two {@linkcode State}s for equality.\n   * @param previous of type {@linkcode State} - First state to compare\n   * @param next of type {@linkcode State} - Second state to compare\n   */\n  #equals: (previous: St, next: St) => boolean;\n\n  get id() {\n    return this._id;\n  }\n\n  get equals() {\n    return this.#equals;\n  }\n\n  /**\n   * Creates an instance of SubscriberMapClass.\n   * @param eventsMap - {@linkcode EventsMap} [E] - The events map.\n   * @param promiseesMap - {@linkcode PromiseeMap} [P] - The promisees map.\n   * @param subscriber - The {@linkcode FnSubReduced} subscriber function or object.\n   * @param equals - Function to compare two {@linkcode State}s for equality (optional).\n   * @param _id - Unique identifier for the subscriber (optional).\n   */\n  constructor(\n    eventsMap: E,\n    actorsMap: A,\n    subscriber: FnMapR<Eo, Tc, T, void>,\n    equals: (a: St, b: St) => boolean = equal,\n    private _id = nanoid(),\n  ) {\n    this.#subscriber = subscriber;\n    this.#eventsMap = eventsMap;\n    this.#actorsMap = actorsMap;\n    this.#equals = equals;\n\n    this.#state = 'active';\n  }\n\n  /**\n   * Function that returns a reduced function based on the subscriber's logic.\n   * @returns A function that reduces the state based on the subscriber's logic.\n   *\n   * @see {@linkcode isFunction} to check if the subscriber is a function.\n   * @see {@linkcode toEventsMap} to convert the events and promisees maps\n   * to a unified map.\n   * @see {@linkcode nothing} to provide a default action if no event matches.\n   * @see {@linkcode t} to ensure type safety in the returned function.\n   */\n  get #reduceFn() {\n    const sub = this.#subscriber;\n    const check1 = isFunction(sub);\n    if (check1) return _any(sub);\n\n    const map = toEventsMap(this.#eventsMap, this.#actorsMap);\n    const keys = Object.keys(map);\n\n    return ({ event, ...rest }: St) => {\n      const _else = sub.else ?? nothing;\n      const { type, payload } = event;\n\n      for (const key of keys) {\n        const check2 = type === key;\n        const func = _any(sub)[key];\n        const check3 = !!func;\n\n        const check4 = check2 && check3;\n        if (check4) return func({ payload, ...rest });\n      }\n\n      return _any(_else({ event, ...rest }));\n    };\n  }\n\n  get #cannotPerform() {\n    return !(this.#state === 'active');\n  }\n\n  /**\n   * Function to handle state changes.\n   * @param previous of type {@linkcode State} - Previous state\n   * @param next of type {@linkcode State} - Next state\n   *\n   * @remarks\n   * This function checks if the subscriber can perform its action,\n   * compares the previous and next states using the provided equality function,\n   * and if they are not equal, it calls the subscriber with the next state.\n   * If the states are equal or if the subscriber cannot perform its action,\n   */\n  fn = (previous: St, next: St) => {\n    if (this.#cannotPerform) return;\n\n    const _equals = this.#equals(previous, next);\n    if (_equals) return;\n\n    return this.#reduceFn(next);\n  };\n\n  /* v8 ignore next 3*/\n  get state() {\n    return this.#state;\n  }\n\n  close = () => {\n    if (this.state !== 'disposed') this.#state = 'paused';\n  };\n\n  open = () => {\n    if (this.state !== 'disposed') this.#state = 'active';\n  };\n\n  unsubscribe = () => {\n    this.close();\n    this.#state = 'disposed';\n  };\n}\n\nexport type { SubscriberClass };\n\nexport type SubscriberOptions<\n  E extends EventObject = EventObject,\n  Tc extends PrimitiveObject = PrimitiveObject,\n  T extends string = string,\n> = {\n  id?: string;\n  equals?: (a: State<E, Tc, T>, b: State<E, Tc, T>) => boolean;\n};\n\ntype CreateSubscriber_F = <\n  E extends EventsMap = EventsMap,\n  const A extends ActorsConfigMap = ActorsConfigMap,\n  Tc extends PrimitiveObject = PrimitiveObject,\n  T extends string = string,\n  const Eo extends ToEventObject<ToEvents<E, A>> = ToEventObject<\n    ToEvents<E, A>\n  >,\n>(\n  eventsMap: E,\n  actorsMap: A,\n  subscriber: FnMapR<Eo, Tc, T, void>,\n  options?: SubscriberOptions<Eo, Tc, T>,\n) => SubscriberClass<E, A, Tc, T, Eo>;\n\n/**\n * Creates a new instance of SubscriberMapClass.\n *\n * @param eventsMap : {@linkcode EventsMap} [E] - The events map.\n * @param actorsMap : {@linkcode ActorsConfigMap} [A] - The actors map.\n * @param subscriber - The subscriber function that will be called with the {@linkcode State}.\n * @param options - Optional parameters for the subscriber, including equality function and ID.\n * @returns A new instance of {@linkcode SubscriberClass} that manages the subscription state and provides methods to handle state changes and unsubscribe.\n *\n * @remarks\n * This function maps the provided events and actors.\n *\n * This allows for efficient subscription management and state handling depending on the events and actors.\n */\nexport const createSubscriber: CreateSubscriber_F = (\n  eventsMap,\n  actorsMap,\n  subscriber,\n  options,\n) => {\n  return new SubscriberClass(\n    eventsMap,\n    actorsMap,\n    subscriber,\n    options?.equals,\n    options?.id,\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA0BA,IAAM,kBAAN,MAOE;CACA;CACA;CACA;CAEA,SAAqB;;;;;;CAOrB;C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