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

The New NGRX Signal Store for Angular: 3+n Flav...

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

Manfred Steyer

April 23, 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 { loadByCriteria: rxMethod<Criteria>((c$) => c$.pipe([…])) }; }), […] );
  10. @ManfredSteyer export const FlightBookingStore = signalStore( […] withMethods((state) => {

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

    […] return { loadByCriteria: 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 { loadByCriteria: 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({ loadByCriteria,

    criteria }) { loadByCriteria(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