Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern State Management in Angular: 3+n Flavors...

Modern State Management in Angular: 3+n Flavors of the Signal Store @ijs London 2024

Manfred Steyer

April 10, 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 export const FlightBookingStore = signalStore( { providedIn: 'root' },

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

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

    […] return { connectCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  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( 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 })) )) }; }), […] );
  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( […] withHooks({ onInit({ connectCriteria,

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

    Stores are the Missing Link Signal Store Lightweight Highly Extensible