Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

The New NGRX Signal Store for Angular: 3+n Flavors

The New NGRX Signal Store for Angular: 3+n Flavors

Manfred Steyer

February 14, 2024
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer flights = signal<Flight[]>([]); const flights = await this.flightService.findAsPromise(from, to);

    this.flights.set(flights); <div *ngFor="let f of flights()"> <flight-card [item]="f" /> </div>
  2. @ManfredSteyer from = signal('Paris'); to = signal('London'); flightRoute = computed(()

    => this.from() + ' to ' + this.to()); constructor() { effect(() => { console.log('from', this.from()); console.log('to', this.to()); }); }
  3. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { private

    state = signalState({ from: 'Paris', to: 'London', flights: [] as Flight[], basket: {} as Record<number, boolean>, }); readonly flights = this.state.flights; readonly from = this.state.from; […] }
  4. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    readonly selected = computed( () => this.flights().filter((f) => this.basket()[f.id]) ); […] }
  5. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    updateCriteria(from: string, to: string): void { patchState(this.state, { from, to }) } […] }
  6. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    updateCriteria(from: string, to: string): void { patchState(this.state, (state) => ({ from: state.to, to: state.from })); } […] }
  7. @ManfredSteyer @Injectable({ providedIn: 'root' }) export class FlightBookingFacade { […]

    updateCriteria(from: string, to: string): void { patchState(this.state, updateRoute(from, to)); } […] } function updateRoute<T>(from: string, to: string) { return (state: T) => ({ from: to, to: from }) }
  8. @ManfredSteyer export const FlightBookingStore = signalStore( { providedIn: 'root' },

    withState({ from: 'Paris', to: 'London', […] }), […] );
  9. @ManfredSteyer export const FlightBookingStore = signalStore( { providedIn: 'root' },

    withState({ from: 'Paris', to: 'London', […] }), withComputed(([…]) => ({ […] })), withMethods(([…]) => ({ })), withHooks({ […] }) );
  10. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { connectCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  11. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { connectCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  12. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { connectCriteria: rxMethod<Criteria>((c$) => c$.pipe( filter(c => c.from.length >= 3 && c.to.length >= 3), debounceTime(300), switchMap((c) => flightService.find(c.from, c.to)), tap(flights => patchState(state, { flights })) )) }; }), […] );
  13. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

    […] return { connectCriteria: rxMethod<Criteria>((c$) => c$.pipe( filter(c => c.from.length >= 3 && c.to.length >= 3), debounceTime(300), switchMap((c) => flightService.find(c.from, c.to)), tap(flights => patchState(state, { flights })) )) }; }), […] );
  14. @ManfredSteyer export const FlightBookingStore = signalStore( […] withHooks({ onInit({ connectCriteria,

    criteria }) { connectCriteria(criteria); }, }), ); takes: Signal<T>, Observable<T>, T
  15. @ManfredSteyer Signals: Future of CD Take Care of Data Flow

    Signal Store: Lightweight Different Flavors Highly Extensible Typed