{"version":3,"file":"angular-kit-effects.mjs","sources":["../../../../libs/effects/src/lib/assert-injector.ts","../../../../libs/effects/src/lib/effects.ts","../../../../libs/effects/src/angular-kit-effects.ts"],"sourcesContent":["import {\n  Injector,\n  assertInInjectionContext,\n  inject,\n  runInInjectionContext,\n} from '@angular/core';\n\n/**\n * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`\n * After assertion, `assertInjector` runs the `runner` function with the guaranteed `Injector`\n * whether it is the default `Injector` within the current **Injection Context**\n * or the custom `Injector` that was passed in.\n *\n * @template {() => any} Runner - Runner is a function that can return anything\n * @param {Function} fn - the Function to pass in `assertInInjectionContext`\n * @param {Injector | undefined | null} injector - the optional \"custom\" Injector\n * @param {Runner} runner - the runner fn\n * @returns {ReturnType<Runner>} result - returns the result of the Runner\n *\n * @example\n * ```ts\n * function injectValue(injector?: Injector) {\n *  return assertInjector(injectValue, injector, () => 'value');\n * }\n *\n * injectValue(); // string\n * ```\n */\nexport function assertInjector<Runner extends () => any>(\n  fn: Function,\n  injector: Injector | undefined | null,\n  runner: Runner,\n): ReturnType<Runner>;\n/**\n * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`\n * After assertion, `assertInjector` returns a guaranteed `Injector` whether it is the default `Injector`\n * within the current **Injection Context** or the custom `Injector` that was passed in.\n *\n * @param {Function} fn - the Function to pass in `assertInInjectionContext`\n * @param {Injector | undefined | null} injector - the optional \"custom\" Injector\n * @returns Injector\n *\n * @example\n * ```ts\n * function injectDestroy(injector?: Injector) {\n *  injector = assertInjector(injectDestroy, injector);\n *\n *  return runInInjectionContext(injector, () => {\n *    // code\n *  })\n * }\n * ```\n */\nexport function assertInjector(\n  fn: Function,\n  injector: Injector | undefined | null,\n): Injector;\nexport function assertInjector(\n  fn: Function,\n  injector: Injector | undefined | null,\n  runner?: () => any,\n) {\n  !injector && assertInInjectionContext(fn);\n  const assertedInjector = injector ?? inject(Injector);\n\n  if (!runner) return assertedInjector;\n  return runInInjectionContext(assertedInjector, runner);\n}\n","import {\n  DestroyRef,\n  ErrorHandler,\n  inject,\n  Injectable,\n  Injector,\n  Optional,\n  runInInjectionContext,\n} from '@angular/core';\nimport {\n  catchError,\n  EMPTY,\n  Observable,\n  pipe,\n  ReplaySubject,\n  Subscription,\n  tap,\n} from 'rxjs';\nimport { assertInjector } from './assert-injector';\n\nexport type EffectCleanUpRef = {\n  cleanUp: () => void;\n};\n\ntype RunOptions = {\n  onCleanUp?: () => void;\n};\n\nexport function isRunOptionsGuard(obj: any): obj is RunOptions {\n  return (\n    typeof obj === 'object' &&\n    obj?.onCleanUp !== undefined &&\n    typeof obj.onCleanUp === 'function'\n  );\n}\n\n/**\n * @internal\n *\n * @description\n * Keeps track of your subscriptions and unsubscribes them automatically when destroyed.\n *\n */\n@Injectable()\nexport class Effects {\n  private static nextId = 0;\n  private readonly sub = new Subscription();\n  private readonly idSubMap = new Map<number, Subscription>();\n  private readonly destroyHook$$ = new ReplaySubject<void>(1);\n\n  constructor(@Optional() private readonly errorHandler: ErrorHandler | null) {}\n\n  /**\n   * @description\n   * Manage the subscription of an observable and execute the side effect.\n   *\n   * Unsubscribes automatically when the provided {@link DestroyRef} is destroyed.\n   *\n   * Manually unsubscribe by calling {@link EffectCleanUpRef.cleanUp}.\n   *\n   * @example\n   * ```typescript\n   * ef = effects(({run})=> run(source$.subscribe(console.log)))\n   * ```\n   * @param sub\n   * @param options\n   */\n  run(sub: Subscription, options?: RunOptions): EffectCleanUpRef;\n  /**\n   * @description\n   * Subscribe to the passed observable and execute the side effect.\n   *\n   * Unsubscribes automatically when the provided {@link DestroyRef} is destroyed.\n   *\n   * Manually unsubscribe by calling {@link EffectCleanUpRef.cleanUp}.\n   *\n   * @example\n   * ```typescript\n   * ef = effects(({run})=> run(source$.pipe(tap(console.log))))\n   * ```\n   *\n   * @param o$\n   * @param options\n   */\n  run<T>(o$: Observable<T>, options?: RunOptions): EffectCleanUpRef;\n  /**\n   * @description\n   * Subscribe to the passed observable and execute the side effect.\n   *\n   * Unsubscribes automatically when the provided {@link DestroyRef} is destroyed.\n   *\n   * Manually unsubscribe by calling {@link EffectCleanUpRef.cleanUp}.\n   *\n   * @example\n   * ```typescript\n   * const trigger$ = of(1);\n   * const effect = console.log;\n   * ef = effects(({run})=> run(trigger$, effect))\n   * ```\n   *\n   * @param o$\n   * @param sideEffectFn\n   * @param options\n   */\n  run<T>(\n    o$: Observable<T>,\n    sideEffectFn: (arg: T) => void,\n    options?: RunOptions\n  ): EffectCleanUpRef;\n  /**\n   * @internal\n   */\n  run<T>(\n    obsOrSub$: Observable<T> | Subscription,\n    sideEffectFn?: ((arg: T) => void) | RunOptions,\n    options?: RunOptions\n  ): EffectCleanUpRef {\n    const effectId = Effects.nextId++;\n\n    if (obsOrSub$ instanceof Subscription) {\n      this.sub.add(obsOrSub$);\n      this.idSubMap.set(effectId, obsOrSub$);\n      let runOnInstanceDestroySub: undefined | EffectCleanUpRef = undefined;\n      if (sideEffectFn ?? isRunOptionsGuard(sideEffectFn)) {\n        runOnInstanceDestroySub = this.runOnInstanceDestroy(() =>\n          (sideEffectFn as RunOptions).onCleanUp?.()\n        );\n      }\n\n      return {\n        cleanUp: () => {\n          if (sideEffectFn ?? isRunOptionsGuard(sideEffectFn)) {\n            (sideEffectFn as RunOptions).onCleanUp?.();\n            runOnInstanceDestroySub?.cleanUp();\n          }\n          this.unregister(effectId);\n        },\n      };\n    }\n\n    const subscription = obsOrSub$\n      .pipe(\n        // execute operation/ side effect\n        sideEffectFn && typeof sideEffectFn === 'function'\n          ? tap(sideEffectFn)\n          : pipe(),\n        catchError((err) => {\n          this.errorHandler?.handleError(err);\n          return EMPTY;\n        })\n      )\n      .subscribe();\n    this.sub.add(subscription);\n    this.idSubMap.set(effectId, subscription);\n\n    // cases sideEffectFn is a CleanUpRef OR options given\n    let runOnInstanceDestroySub: undefined | EffectCleanUpRef = undefined;\n    if (options && options.onCleanUp) {\n      runOnInstanceDestroySub = this.runOnInstanceDestroy(() =>\n        options.onCleanUp?.()\n      );\n    }\n    if (sideEffectFn && isRunOptionsGuard(sideEffectFn)) {\n      runOnInstanceDestroySub = this.runOnInstanceDestroy(() =>\n        (sideEffectFn as RunOptions).onCleanUp?.()\n      );\n    }\n\n    return {\n      cleanUp: () => {\n        if (options && options.onCleanUp) {\n          options.onCleanUp?.();\n          runOnInstanceDestroySub?.cleanUp();\n        }\n        if (sideEffectFn && isRunOptionsGuard(sideEffectFn)) {\n          (sideEffectFn as RunOptions).onCleanUp?.();\n          runOnInstanceDestroySub?.cleanUp();\n        }\n        this.unregister(effectId);\n      },\n    };\n  }\n\n  /**\n   * Execute a sideEffect when the rxEffect instance OnDestroy hook is executed\n   * @param sideEffectFn\n   */\n  runOnInstanceDestroy(sideEffectFn: () => void) {\n    return this.run(this.destroyHook$$.pipe(tap(sideEffectFn)).subscribe());\n  }\n\n  /**\n   * Cancel a registered side effect\n   * @param effectId\n   */\n  private unregister(effectId: number): void {\n    const sub = this.idSubMap.get(effectId);\n    if (sub) {\n      sub.unsubscribe();\n    }\n  }\n\n  cleanUp() {\n    this.destroyHook$$.next(void 0);\n    this.sub.unsubscribe();\n  }\n}\n\ntype EffectsSetupFn = (\n  rxEffect: Pick<Effects, 'run' | 'runOnInstanceDestroy'>\n) => void;\n\n/**\n * @description\n * Functional way to setup observable based side effects.\n *\n * It will destroy itself when the provided {@link DestroyRef} is destroyed.\n *\n * @param setupFn\n * @param options\n *\n * @example\n *\n * const ef = effects(({run, runOnInstanceDestroy}) => {\n *     run(source$, console.log)\n *\n *     run(source$.subscribe(console.log))\n *\n *     run(source$.pipe(tap(console.log)))\n *\n *     runOnInstanceDestroy(() => {\n *       // any teardown logic, e.g unsubscribe from any source, clear timers etc.\n *     })\n * })\n */\nexport function rxEffect(\n  setupFn?: EffectsSetupFn,\n  options?: {\n    injector?: Injector;\n    destroyRef?: DestroyRef;\n  }\n): Effects {\n  const injector = assertInjector(rxEffect, options?.injector);\n  return runInInjectionContext(injector, () => {\n    const errorHandler = inject(ErrorHandler, { optional: true });\n    const destroyRef = options?.destroyRef ?? inject(DestroyRef);\n    const effects = new Effects(errorHandler);\n\n    const teardownFn = setupFn?.({\n      run: effects.run.bind(effects),\n      runOnInstanceDestroy: effects.runOnInstanceDestroy.bind(effects),\n    });\n\n    destroyRef.onDestroy(() => {\n      effects.cleanUp();\n    });\n\n    return effects;\n  });\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;SAyDgB,cAAc,CAC5B,EAAY,EACZ,QAAqC,EACrC,MAAkB,EAAA;AAElB,IAAA,CAAC,QAAQ,IAAI,wBAAwB,CAAC,EAAE,CAAC,CAAC;IAC1C,MAAM,gBAAgB,GAAG,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC;AAEtD,IAAA,IAAI,CAAC,MAAM;AAAE,QAAA,OAAO,gBAAgB,CAAC;AACrC,IAAA,OAAO,qBAAqB,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;AACzD;;ACvCM,SAAU,iBAAiB,CAAC,GAAQ,EAAA;AACxC,IAAA,QACE,OAAO,GAAG,KAAK,QAAQ;QACvB,GAAG,EAAE,SAAS,KAAK,SAAS;AAC5B,QAAA,OAAO,GAAG,CAAC,SAAS,KAAK,UAAU,EACnC;AACJ,CAAC;AAED;;;;;;AAMG;MAEU,OAAO,CAAA;aACH,IAAM,CAAA,MAAA,GAAG,CAAH,CAAK,EAAA;AAK1B,IAAA,WAAA,CAAyC,YAAiC,EAAA;QAAjC,IAAY,CAAA,YAAA,GAAZ,YAAY,CAAqB;AAJzD,QAAA,IAAA,CAAA,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;AACzB,QAAA,IAAA,CAAA,QAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;AAC3C,QAAA,IAAA,CAAA,aAAa,GAAG,IAAI,aAAa,CAAO,CAAC,CAAC,CAAC;KAEkB;AA2D9E;;AAEG;AACH,IAAA,GAAG,CACD,SAAuC,EACvC,YAA8C,EAC9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