UNPKG

@ngrx/signals

Version:

Reactive Store and Set of Utilities for Angular Signals

1 lines 5.21 kB
{"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 isSignal,\n Signal,\n untracked,\n} from '@angular/core';\nimport { isObservable, noop, Observable, Subject } from 'rxjs';\n\ndeclare const ngDevMode: unknown;\n\ntype RxMethodRef = {\n destroy: () => void;\n};\n\nexport type RxMethod<Input> = ((\n input: Input | Signal<Input> | Observable<Input>,\n config?: { injector?: Injector }\n) => RxMethodRef) &\n RxMethodRef;\n\nexport function rxMethod<Input>(\n generator: (source$: Observable<Input>) => Observable<unknown>,\n config?: { injector?: Injector }\n): RxMethod<Input> {\n if (!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 | Signal<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 if (\n typeof ngDevMode !== 'undefined' &&\n ngDevMode &&\n config?.injector === undefined &&\n callerInjector === undefined\n ) {\n console.warn(\n '@ngrx/signals/rxjs-interop: The reactive method was called outside',\n 'the injection context with a signal or observable. This may lead to',\n 'a memory leak. Make sure to call it within the 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 (isSignal(input)) {\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 | Signal<T> | Observable<T>): value is T {\n return !isSignal(value) && !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":";;;AAwBM,SAAU,QAAQ,CACtB,SAA8D,EAC9D,MAAgC,EAAA;AAEhC,IAAA,IAAI,CAAC,MAAM,EAAE,QAAQ,EAAE;QACrB,wBAAwB,CAAC,QAAQ,CAAC;IACpC;IAEA,MAAM,cAAc,GAAG,MAAM,EAAE,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC;AAC3D,IAAA,MAAM,OAAO,GAAG,IAAI,OAAO,EAAS;IACpC,MAAM,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE;AAChD,IAAA,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,SAAS,CAAC,MAAM,SAAS,CAAC,WAAW,EAAE,CAAC;AAEvE,IAAA,MAAM,UAAU,GAAG,CACjB,KAAgD,EAChD,MAAgC,KACjB;AACf,QAAA,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE;AACnB,YAAA,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;AACnB,YAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE;QAC1B;AAEA,QAAA,MAAM,cAAc,GAAG,iBAA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