{"version":3,"file":"ngrx-signals-rxjs-interop.mjs","sources":["../../../../modules/signals/rxjs-interop/src/rx-method.ts","../../../../modules/signals/rxjs-interop/ngrx-signals-rxjs-interop.ts"],"sourcesContent":["import {\n  assertInInjectionContext,\n  DestroyRef,\n  effect,\n  inject,\n  Injector,\n  untracked,\n} from '@angular/core';\nimport { isObservable, noop, Observable, Subject } from 'rxjs';\n\ntype RxMethodRef = {\n  destroy: () => void;\n};\n\nexport type RxMethod<Input> = ((\n  input: Input | (() => Input) | Observable<Input>,\n  config?: { injector?: Injector }\n) => RxMethodRef) &\n  RxMethodRef;\n\n/**\n * @description\n *\n * Creates a reactive method for managing side effects by utilizing RxJS APIs.\n * The method accepts an observable, a signal, a computation function, or\n * a static value.\n *\n * @usageNotes\n *\n * ```ts\n * import { Component, inject, signal } from '@angular/core';\n * import { switchMap } from 'rxjs';\n * import { rxMethod } from '@ngrx/signals/rxjs-interop';\n * import { tapResponse } from '@ngrx/operators';\n *\n * \\@Component(...)\n * export class TodoList {\n *   readonly #todosService = inject(TodosService);\n *   readonly userId = signal(1);\n *   readonly todos = signal<Todo[]>([]);\n *\n *   readonly loadTodos = rxMethod<number>(\n *     switchMap((id) =>\n *       this.#todosService.getByUserId(id).pipe(\n *         tapResponse({\n *           next: (todos) => this.todos.set(todos),\n *           error: console.error,\n *         })\n *       )\n *     )\n *   );\n *\n *   constructor() {\n *     // 👇 Load todos on `userId` changes.\n *     this.loadTodos(this.userId);\n *   }\n * }\n * ```\n */\nexport function rxMethod<Input>(\n  generator: (source$: Observable<Input>) => Observable<unknown>,\n  config?: { injector?: Injector }\n): RxMethod<Input> {\n  if (typeof ngDevMode !== 'undefined' && ngDevMode && !config?.injector) {\n    assertInInjectionContext(rxMethod);\n  }\n\n  const sourceInjector = config?.injector ?? inject(Injector);\n  const source$ = new Subject<Input>();\n  const sourceSub = generator(source$).subscribe();\n  sourceInjector.get(DestroyRef).onDestroy(() => sourceSub.unsubscribe());\n\n  const rxMethodFn = (\n    input: Input | (() => Input) | Observable<Input>,\n    config?: { injector?: Injector }\n  ): RxMethodRef => {\n    if (isStatic(input)) {\n      source$.next(input);\n      return { destroy: noop };\n    }\n\n    const callerInjector = getCallerInjector();\n\n    if (\n      typeof ngDevMode !== 'undefined' &&\n      ngDevMode &&\n      config?.injector === undefined &&\n      callerInjector === undefined\n    ) {\n      console.warn(\n        '@ngrx/signals/rxjs-interop: Calling a reactive method outside of',\n        'an injection context with a signal or observable is deprecated.',\n        'In a future version, this will throw an error.',\n        'Either call it within an injection context',\n        '(e.g. in a constructor or field initializer) or pass an injector',\n        'explicitly via the config parameter.\\n\\nFor more information, see:',\n        'https://ngrx.io/guide/signals/rxjs-integration#reactive-methods-and-injector-hierarchies'\n      );\n    }\n\n    const instanceInjector =\n      config?.injector ?? callerInjector ?? sourceInjector;\n\n    if (typeof input === 'function') {\n      const watcher = effect(\n        () => {\n          const value = input();\n          untracked(() => source$.next(value));\n        },\n        { injector: instanceInjector }\n      );\n      sourceSub.add({ unsubscribe: () => watcher.destroy() });\n\n      return watcher;\n    }\n\n    const instanceSub = input.subscribe((value) => source$.next(value));\n    sourceSub.add(instanceSub);\n\n    if (instanceInjector !== sourceInjector) {\n      instanceInjector\n        .get(DestroyRef)\n        .onDestroy(() => instanceSub.unsubscribe());\n    }\n\n    return { destroy: () => instanceSub.unsubscribe() };\n  };\n  rxMethodFn.destroy = sourceSub.unsubscribe.bind(sourceSub);\n\n  return rxMethodFn;\n}\n\nfunction isStatic<T>(value: T | (() => T) | Observable<T>): value is T {\n  return typeof value !== 'function' && !isObservable(value);\n}\n\nfunction getCallerInjector(): Injector | undefined {\n  try {\n    return inject(Injector);\n  } catch {\n    return undefined;\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCG;AACG,SAAU,QAAQ,CACtB,SAA8D,EAC9D,MAAgC,EAAA;AAEh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