UNPKG

@zodiac-ui/ng-observable

Version:

Create powerful reactive components with Angular. AoT compatible and Ivy ready.

138 lines (137 loc) 4.28 kB
import { ChangeDetectorRef, OnDestroy } from "@angular/core"; import { BehaviorSubject } from "rxjs"; import { StateChangeStrategy } from "./constants"; /** * State object for bound `valueRef`. State changes mutate the original object in-place and notifies any observers * with a new object reference. * * @see {@link StateFactory} * * @usageNotes * * @publicApi */ export declare class State<T extends object> extends BehaviorSubject<T> { /** * Mutate the state, notify observers and queue change detection. * * @param partialState A plain object containing values to mutate the existing state with. */ readonly next: (partialState?: Partial<T>) => void; /** * * Mutate the state and notify observers without running change detection. * * @param partialState A plain object containing values to mutate the existing state with. */ readonly patchValue: (partialState?: Partial<T>) => void; /** * The value object reference to be mutated. */ private readonly valueRef; /** * Creates a new State `instance`. * * @param valueRef The mutable value object reference to apply state changes to. * @param markForCheck Notifier that schedules change detection to run on the next microtask. */ constructor(valueRef: T, markForCheck: Function); } /** * Reactive state management for Angular components and directives. * * @see {@link State} * * @usageNotes * * Create a `State<T>` instance by passing the component instance as an argument. All mutations to * the internal component state should be handled with this service so that observers can be notified * of changes. * * ```typescript * export interface MyProps { * title: string * } * * export interface MyState extends MyProps { * model: string * } * * @Component({ * viewProviders: [StateFactory, Stream] // or providers * }) * export class MyComponent implements MyState { * @Input() * title: string * model: string * * constructor(@Self() stateFactory: StateFactory<MyState>, @Self() stream: Stream) { * const state = stateFactory.create(this) * * // Imperative usage * state.next({ * model: "app" * }) * * // Reactive usage * stream(state)(of({ model: "app" })) * } * } * ``` * * ### Connecting `@Input()` properties * * Changes received from `@Input()` properties can be mapped onto the state to keep state observers up to date: * * ```ts * @Component() * export class MyComponent extends NgObservable implements MyState { * @Input() * value: any * mappedValue: any * * constructor(@Self() stateFactory: StateFactory<MyState>, @Self() stream: Stream) { * const state = stateFactory.create(this) * * // assuming 1:1 map between inputs and state * stream(state)(mapInputsToState(this)) * * // map inputs to some other value * stream(state)(mapInputsToState(this).pipe( * map((props) => ({ * mappedValue: props.value * })) * ) * } * } * ``` * * @publicApi */ export declare class StateFactory<T extends Object> implements OnDestroy { private readonly stream; private readonly strategy; private readonly cdr?; private readonly checkNoChanges; /** * Creates an instance of `StateFactory` * * @param strategy The {@link StateChangeStrategy} to be used. It will wither detach or reattach * the `ChangeDetectorRef` if one is provided. * * @param cdr The `ChangeDetectorRef` for the current view. * */ constructor(strategy: StateChangeStrategy, cdr?: ChangeDetectorRef); /** * Creates an instance of {@link State} bound to the given `valueRef`. Performs change detection when notified * by `markForCheck`. * * @param valueRef The mutable value object reference to apply state changes to. */ create(valueRef: T): State<T>; /** * Cleanup when the injector is destroyed */ ngOnDestroy(): void; }