import { PartialStateUpdater, Prettify, SignalStoreFeatureResult, StateSignals, WritableStateSource, SignalStoreFeature, EmptyFeatureResult } from '@ngrx/signals'; import * as i0 from '@angular/core'; import { Type, Provider, Injector } from '@angular/core'; import { OperatorFunction, Observable } from 'rxjs'; type EventInstance = { type: Type; payload: Payload; }; type EventCreator = ((payload: Payload) => EventInstance) & { type: Type; }; declare function event(type: Type): EventCreator; declare function event(type: Type, payload: Payload): EventCreator; type CaseReducerResult[]> = { reducer: CaseReducer; events: EventCreators; }; type CaseReducer[]> = (event: { [K in keyof EventCreators]: ReturnType; }[number], state: State) => Partial | PartialStateUpdater | Array | PartialStateUpdater>; /** * @description * * Creates a case reducer that can be used with the `withReducer` feature. */ declare function on[]>(...args: [ ...events: [...EventCreators], reducer: CaseReducer, NoInfer> ]): CaseReducerResult; type EventScope = 'self' | 'parent' | 'global'; type EventScopeConfig = { scope: EventScope; }; /** * @description * * Marks a single event to be dispatched in the specified scope. * Used in a tuple alongside an event to indicate its dispatch scope. * * @usageNotes * * ```ts * import { signalStore, type } from '@ngrx/signals'; * import { event, Events, withEffects } from '@ngrx/signals/events'; * import { mapResponse } from '@ngrx/operators'; * * const opened = event('[Users Page] Opened'); * const loadedSuccess = event('[Users API] Loaded Success', type()); * const loadedFailure = event('[Users API] Loaded Failure', type()); * * const UsersStore = signalStore( * withEffects(( * _, * events = inject(Events), * usersService = inject(UsersService) * ) => ({ * loadUsers$: events.on(opened).pipe( * exhaustMap(() => * usersService.getAll().pipe( * mapResponse({ * next: (users) => loadedSuccess(users), * error: (error: { message: string }) => [ * loadedFailure(error.message), * toScope('global'), * ], * }), * ), * ), * ), * })), * ); * ``` */ declare function toScope(scope: EventScope): EventScopeConfig; /** * @description * * RxJS operator that maps all emitted events in the stream to be dispatched * in the specified scope. * * @usageNotes * * ```ts * import { signalStore, type } from '@ngrx/signals'; * import { event, Events, withEffects } from '@ngrx/signals/events'; * import { mapResponse } from '@ngrx/operators'; * * const opened = event('[Users Page] Opened'); * const loadedSuccess = event('[Users API] Loaded Success', type()); * const loadedFailure = event('[Users API] Loaded Failure', type()); * * const UsersStore = signalStore( * withEffects(( * _, * events = inject(Events), * usersService = inject(UsersService) * ) => ({ * loadUsers$: events.on(opened).pipe( * exhaustMap(() => * usersService.getAll().pipe( * mapResponse({ * next: (users) => loadedSuccess(users), * error: (error: { message: string }) => * loadedFailure(error.message), * }), * mapToScope('parent'), * ), * ), * ), * })), * ); * ``` */ declare function mapToScope>(scope: EventScope): OperatorFunction; declare abstract class BaseEvents { protected readonly events$: Observable>; protected constructor(parentEventsToken: Type); on(): Observable>; on[]>(...events: [...EventCreators]): Observable<{ [K in keyof EventCreators]: ReturnType; }[number]>; } /** * @description * * Globally provided service for listening to dispatched events. * * @usageNotes * * ```ts * import { event, Events } from '@ngrx/signals/events'; * * const increment = event('[Counter Page] Increment'); * * \@Component(...) * class Counter { * readonly #events = inject(Events); * * constructor() { * this.#events * .on(increment) * .pipe(takeUntilDestroyed()) * .subscribe(() => /* handle increment event *\/); * } * } * ``` */ declare class Events extends BaseEvents { constructor(); static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * @description * * Globally provided service for listening to dispatched events. * Receives events before the `Events` service and is primarily used for * handling state transitions. */ declare class ReducerEvents extends BaseEvents { constructor(); static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * @description * * Globally provided service for dispatching events. * * @usageNotes * * ```ts * import { Dispatcher, event } from '@ngrx/signals/events'; * * const increment = event('[Counter Page] Increment'); * * \@Component(...) * class Counter { * readonly #dispatcher = inject(Dispatcher); * * increment(): void { * this.#dispatcher.dispatch(increment()); * } * } * ``` */ declare class Dispatcher { protected readonly reducerEvents: ReducerEvents; protected readonly events: Events; protected readonly parentDispatcher: Dispatcher | null; dispatch(event: EventInstance, config?: EventScopeConfig): void; static ɵfac: i0.ɵɵFactoryDeclaration; static ɵprov: i0.ɵɵInjectableDeclaration; } /** * @description * * Provides scoped instances of Dispatcher and Events services. * Enables event dispatching within a specific component or feature scope. * * @usageNotes * * ```ts * import { Dispatcher, event } from '@ngrx/signals/events'; * * const increment = event('[Counter Page] Increment'); * * \@Component({ * // ... * providers: [provideDispatcher()], * }) * class Counter { * readonly #dispatcher = inject(Dispatcher); * * increment(): void { * // Dispatching an event to the local Dispatcher. * this.#dispatcher.dispatch(increment()); * * // Dispatching an event to the parent Dispatcher. * this.#dispatcher.dispatch(increment(), { scope: 'parent' }); * * // Dispatching an event to the global Dispatcher. * this.#dispatcher.dispatch(increment(), { scope: 'global' }); * } * } * ``` */ declare function provideDispatcher(): Provider[]; type EventType = `[${Source}] ${EventName}`; type EventCreatorGroup> = { readonly [EventName in keyof Events]: EventName extends string ? EventCreator, Events[EventName]> : never; }; /** * @description * * Creates a group of event creators. * * @usageNotes * * ```ts * import { type } from '@ngrx/signals'; * import { eventGroup } from '@ngrx/signals/events'; * * const counterPageEvents = eventGroup({ * source: 'Counter Page', * events: { * increment: type(), * decrement: type(), * set: type(), * }, * }); * ``` */ declare function eventGroup>(config: { source: Source; events: Events; }): Prettify>; type SelfDispatchingEvents>> = { readonly [EventName in keyof EventGroup]: Parameters extends [infer Payload] ? (payload: Payload) => void : () => void; }; /** * @description * * Creates self-dispatching events for a given event group. * * @usageNotes * * ```ts * import { type } from '@ngrx/signals'; * import { eventGroup, injectDispatch } from '@ngrx/signals/events'; * * const counterPageEvents = eventGroup({ * source: 'Counter Page', * events: { * increment: type(), * decrement: type(), * }, * }); * * \@Component(...) * class Counter { * readonly dispatch = injectDispatch(counterPageEvents); * * increment(): void { * this.dispatch.increment(); * } * * decrement(): void { * this.dispatch.decrement(); * } * } * ``` */ declare function injectDispatch>>(events: EventGroup, config?: { injector?: Injector; }): ((config: EventScopeConfig) => Prettify>) & Prettify>; /** * @description * * SignalStore feature for defining event handlers. * * @usageNotes * * ```ts * import { signalStore, withState } from '@ngrx/signals'; * import { event, Events, withEventHandlers } from '@ngrx/signals/events'; * * const increment = event('[Counter Page] Increment'); * const decrement = event('[Counter Page] Decrement'); * * const CounterStore = signalStore( * withState({ count: 0 }), * withEventHandlers(({ count }, events = inject(Events)) => ({ * logCount$: events.on(increment, decrement).pipe( * tap(({ type }) => console.log(type, count())), * ), * })), * ); * ``` */ declare function withEventHandlers(handlersFactory: (store: Prettify & Input['props'] & Input['methods'] & WritableStateSource>>) => Record> | Observable[]): SignalStoreFeature; /** * @description * * SignalStore feature for defining state transitions based on dispatched events. * * @usageNotes * * ```ts * import { signalStore, type, withState } from '@ngrx/signals'; * import { event, on, withReducer } from '@ngrx/signals/events'; * * const set = event('[Counter Page] Set', type()); * * const CounterStore = signalStore( * withState({ count: 0 }), * withReducer( * on(set, ({ payload }) => ({ count: payload })), * ), * ); * ``` */ declare function withReducer(...caseReducers: CaseReducerResult[]>[]): SignalStoreFeature<{ state: State; props: {}; methods: {}; }, EmptyFeatureResult>; export { Dispatcher, Events, ReducerEvents, event, eventGroup, injectDispatch, mapToScope, on, provideDispatcher, toScope, withEventHandlers, withReducer }; export type { EventCreator, EventInstance, EventScope, EventScopeConfig };