import { Observable, Subject, ObservableInput, PartialObserver, Subscription } from 'rxjs'; import * as i0 from '@angular/core'; import { Injector } from '@angular/core'; /** * Interface to declare name and value of the OnDestroy lifecycle hook */ interface DestroyProp { destroy: true; } /** * Interface to specify an Observable channel for lifecycle hooks. * It is types as Partial of possible lifecycle names and values */ interface DestroyChannel$ { readonly _hooks$: Subject; } /** * Interface to specify the channel for the `onDestroy` hook as Observable */ interface OnDestroy$ extends DestroyChannel$ { onDestroy$: Observable; } /** * @deprecated - use rxEffects instead * * Reduces subscription boilerplate for performing observable-based side-effects in components. * * Before: * ```ts * @Component({ * // ... * }) * export class FooComponent implements OnDestroy { * private readonly destroy$ = new Subject(); * * constructor() { * obs$.pipe(takeUntil(this.destroy$)).subscribe(doSideEffect); * } * * ngOnDestroy(): void { * this.destroy$.next(); * this.destroy$.complete(); * } * } * ``` * * After: * ```ts * @Component({ * // ... * providers: [RxEffects], * }) * export class FooComponent { * constructor(effects: RxEffects) { * effects.register(obs$, doSideEffect); * // OR * effects.register(obs$.pipe(tap(doSideEffect))); * // OR * effects.register(obs$.subscribe(doSideEffect)); * } * } * ``` * * NOTE: Avoid calling register/unregister/subscribe inside the side-effect function. */ declare class RxEffects$1 implements OnDestroy$ { private static nextId; private readonly destroyRef; private readonly errorHandler; readonly _hooks$: Subject; private readonly observables$; private readonly effects$; private readonly subscription; onDestroy$: Observable; private readonly destroyers; constructor(); /** * Performs a side-effect whenever a source observable emits, and handles its subscription. * * @example * effects.register( * colorMode$, * mode => localStorage.setItem('colorMode', mode) * ); * * @param sourceObs Source observable input * @param sideEffectFn Function with side-effect * @returns Effect ID (can be used to unregister imperatively) */ register(sourceObs: ObservableInput, sideEffectFn: (value: T) => void): number; /** * Subscribe to source observable using an observer object. * * @example * effects.register( * colorMode$, * { * next: mode => localStorage.setItem('colorMode', mode), * error: err => { * console.error('Color mode error: ', err); * localStorage.removeItem('colorMode'); * } * } * ); * * @param sourceObs Source observable input * @param observer Observer object */ register(sourceObs: ObservableInput, observer: PartialObserver): number; /** * Handles subscription for an observable with a side-effect. * * @example * effects.register( * colorMode$.pipe( * tap(mode => localStorage.setItem('colorMode', mode)) * ) * ); * * @param sideEffectObs Observable input with side-effect * @returns Effect ID (can be used to unregister imperatively) */ register(sideEffectObs: ObservableInput): number; /** * Handles subscription to an observable with a side-effect. * * @example * effects.register( * colorMode$.subscribe(mode => localStorage.setItem('colorMode', mode)) * ); * * @param subscription Subscription to observable with side-effect */ register(subscription: Subscription): void; /** * Imperatively cancel a side-effect while the component is still running. * * Note that all effects are automatically cancelled when a component is destroyed, * so you most often won't need to call this method. * @param effectId Effect ID (returned by register method) */ unregister(effectId: number): void; /** * Fires a sideEffect when the instances `OnDestroy` hook is fired. * * @example * effects.registerOnDestroy(mode => localStorage.setItem('colorMode', mode)); * * @param sideEffect */ registerOnDestroy(sideEffect: (value: boolean) => void): number | void; /** * Operator that unsubscribes based on emission of an registered effect. * * @NOTICE * This operator has to be placed always at the end of the operator chain (before the subscription). * Otherwise we may leak as a subsequent operator could instantiate new ongoing Observables which will not get unsubscribed. * * @example * const effectId1 = effects.register( * colorMode$.subscribe(mode => localStorage.setItem('colorMode', mode)) * ); * * someValue$.pipe( * effect.untilEffect(effectId1) * ) * */ untilEffect(effectId: number): (source: Observable) => Observable; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } type SideEffectObservable = ObservableInput; type SideEffectFnOrObserver = PartialObserver | ((value: T) => void); interface RxEffects { register(observable: SideEffectObservable, sideEffectOrObserver?: SideEffectFnOrObserver): Fn; onDestroy: (fn: Fn) => Fn; } type Fn = () => void; type RxEffectsSetupFn = (cfg: Pick) => void; type RxEffectsOptions = { injector?: Injector; }; /** * @description * Functional way to setup observable based side effects with RxEffects. * It's a creation function for RxEffects that destroys itself when the provided * `DestroyRef` is destroyed. * * @example * ```ts * import { rxEffects } from '@rx-angular/state/effects'; * * \@Component({}) * export class FooComponent { * const readonly util = inject(Util); * readonly effects = rxEffects(({ register }) => { * register(this.util.windowResize$, () => { * console.log('window was resized'); * }) * }); * * ngOnInit() { * this.effects.register(this.util.rotationChanged$, () => { * console.log('viewport rotation changed'); * }); * } * } * ``` * * @param {RxEffectsSetupFn} setupFn * @returns RxEffects * * @docsCategory RxEffects * @docsPage RxEffects * */ declare function rxEffects(): RxEffects; declare function rxEffects(setupFn: RxEffectsSetupFn): RxEffects; declare function rxEffects(options: RxEffectsOptions): RxEffects; declare function rxEffects(setupFn: RxEffectsSetupFn, options: RxEffectsOptions): RxEffects; export { RxEffects$1 as RxEffects, rxEffects }; //# sourceMappingURL=state-effects.d.ts.map